-
Notifications
You must be signed in to change notification settings - Fork 0
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Use fundsp network to play up to 20 samples at once #35
Closed
Closed
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
a31a14c
Add a simple example of using fundsp
bschwind afb7df7
Rename c to dsp_graph
bschwind 21239e2
Play around with more dsp functions
bschwind 44dd826
Split the DSP graph into a frontend and backend
bschwind c5552cd
Add MacOS .DS_Store files to .gitignore
bschwind a252b47
Use fundsp network to play up to 20 samples at once
goodhoko File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -10,3 +10,4 @@ Cargo.lock | |
**/*.rs.bk | ||
|
||
.vscode | ||
.DS_Store |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,107 @@ | ||
use cpal::{ | ||
traits::{DeviceTrait, HostTrait, StreamTrait}, | ||
BufferSize, SampleRate, StreamConfig, | ||
}; | ||
use eyre::Result; | ||
use fundsp::hacker::*; | ||
use std::sync::Arc; | ||
|
||
const NUM_SLOTS: usize = 20; | ||
|
||
pub struct SoundController { | ||
_stream: cpal::Stream, | ||
frontend_net: Net64, | ||
slots: Vec<NodeId>, | ||
slot_index: usize, | ||
click: Arc<Wave64>, | ||
} | ||
|
||
impl SoundController { | ||
pub fn new() -> Result<Self> { | ||
let click = Wave64::load("src/sound_samples/click.wav")?; | ||
|
||
let host = cpal::default_host(); | ||
|
||
let device = host.default_output_device().expect("Failed to find a default output device"); | ||
|
||
// TODO(bschwind) - Hardcode this for now, but let's extract these param | ||
// from device.default_output_config later. | ||
let stream_config = StreamConfig { | ||
channels: 1, | ||
sample_rate: SampleRate(48_000), | ||
buffer_size: BufferSize::Default, | ||
}; | ||
|
||
let err_fn = |err| eprintln!("an error occurred on stream: {}", err); | ||
|
||
let (frontend_net, mut backend, slots) = Self::build_dsp_graph(); | ||
|
||
let stream = device.build_output_stream( | ||
&stream_config, | ||
move |data: &mut [f32], _: &cpal::OutputCallbackInfo| { | ||
for sample in data { | ||
*sample = backend.get_mono() as f32; | ||
} | ||
}, | ||
err_fn, | ||
None, | ||
)?; | ||
|
||
stream.play()?; | ||
|
||
Ok(Self { _stream: stream, frontend_net, slots, slot_index: 0, click: Arc::new(click) }) | ||
} | ||
|
||
fn build_dsp_graph() -> (Net64, Net64Backend, Vec<NodeId>) { | ||
let mut net = Net64::new(0, 1); | ||
|
||
// Create a node with 20 inputs that are mixed into one output | ||
let mixer = pass() | ||
+ pass() | ||
+ pass() | ||
+ pass() | ||
+ pass() | ||
+ pass() | ||
+ pass() | ||
+ pass() | ||
+ pass() | ||
+ pass() | ||
+ pass() | ||
+ pass() | ||
+ pass() | ||
+ pass() | ||
+ pass() | ||
+ pass() | ||
+ pass() | ||
+ pass() | ||
+ pass() | ||
+ pass(); | ||
|
||
// add the mixer to the network and connect its output to the network's output | ||
let mixer_id = net.push(Box::new(mixer)); | ||
net.connect_output(mixer_id, 0, 0); | ||
|
||
// Add 20 silent nodes to the network and connect each to one of the inputs of the mixer. | ||
let slots = (0..NUM_SLOTS) | ||
.map(|i| { | ||
let node_id = net.push(Box::new(zero())); | ||
net.connect(node_id, 0, mixer_id, i); | ||
node_id | ||
}) | ||
.collect::<Vec<_>>(); | ||
|
||
let backend = net.backend(); | ||
|
||
(net, backend, slots) | ||
} | ||
|
||
pub fn play_click(&mut self) { | ||
let player = Wave64Player::new(&self.click, 0, 0, self.click.length(), None); | ||
let node_id = self.slots.get(self.slot_index).expect("programmer made a mistake"); | ||
|
||
self.frontend_net.replace(*node_id, Box::new(An(player))); | ||
self.frontend_net.commit(); | ||
|
||
self.slot_index = if self.slot_index == NUM_SLOTS - 1 { 0 } else { self.slot_index + 1 }; | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Heh, I guess this cannot use a for loop because this uses some kind of "incremental" generic type? Might be a candidate for a (declarative) macro if we choose to go this way and polish the code.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, this definitely would benefit from a macro. Not sure it's possible to do with declarative macros alone, though (as in; not sure it's possible to expand a number into a variable number of lines, though I may be forgetting how! the closest I've done is matrix expansion).
Pulling proc macros into this seems too heavyweight, but then I start to wonder if a crate that lets you write code as crazy as this is a good idea 🤔.