Skip to content

Commit

Permalink
Clippy, update readme
Browse files Browse the repository at this point in the history
  • Loading branch information
plule committed Nov 6, 2023
1 parent 193f032 commit efaef15
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 52 deletions.
6 changes: 4 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -71,15 +71,17 @@ Theremotion is run by 4 main threads:
- `conductor` receives the updates from all the other threads and transmits
them. It contains all the movement definitions and holds the settings
- `leap` provides the hand tracking messages
- `dsp` produces the sounds based on the input parameter messages
- `dsp_controller` manages reads the parameter messages and manages the DSP state
- `dsp` produces the sounds based on the DSP state
- `ui` is the main thread and provides the user interface

```mermaid
flowchart TD
leap -->|hand tracking| conductor
ui -->|user input| conductor
conductor -->|ui update| ui
conductor -->|parameter update| dsp
conductor -->|parameter update| dsp_controller
dsp_controller --> |dsp state| dsp
```

## License
Expand Down
107 changes: 58 additions & 49 deletions theremotion/src/thread_dsp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,59 +67,68 @@ where
let mut inputs: Vec<Vec<f32>> = vec![vec![0_f32; buffer_size]; num_inputs];
let mut outputs: Vec<Vec<f32>> = vec![vec![0_f32; buffer_size]; num_outputs];

thread::spawn(move || {
let stream = device
.build_output_stream(
&config,
move |data: &mut [f32], _: &cpal::OutputCallbackInfo| {
// Ensure the exchange buffers are large enough
let len = data.len();
if len > buffer_size {
for input in &mut inputs {
input.resize(len, 0.0);
thread::Builder::new()
.name("dsp_controller".to_string())
.spawn(move || {
let stream = device
.build_output_stream(
&config,
move |data: &mut [f32], _: &cpal::OutputCallbackInfo| {
// Ensure the exchange buffers are large enough
let len = data.len();
if len > buffer_size {
for input in &mut inputs {
input.resize(len, 0.0);
}
for output in &mut outputs {
output.resize(len, 0.0);
}
buffer_size = len;
}
for output in &mut outputs {
output.resize(len, 0.0);
}
buffer_size = len;
}

// Compute the DSP
// Map our Vec<Vec<f32>> to a Vec<&f[32]> to create a buffer for the faust lib
let buffer_input: Vec<&[f32]> = inputs
.iter()
.map(|input| unsafe { slice::from_raw_parts(input.as_ptr(), buffer_size) })
.collect();
// Map our Vec<Vec<f32>> to a Vec<&f[32]> to create a buffer for the faust lib
let mut buffer_output: Vec<&mut [f32]> = outputs
.iter_mut()
.map(|output| unsafe {
slice::from_raw_parts_mut(output.as_mut_ptr(), buffer_size)
})
.collect();
dsp.update_and_compute(len as i32, &buffer_input[..], &mut buffer_output[..]);
// Send to audio buffer
for (out, dsp_sample) in data.iter_mut().zip(&outputs[0]) {
*out = *dsp_sample;
}
},
|err| log::error!("an error occurred on the output audio stream: {err}"),
None,
)
.unwrap();
stream.play().expect("Failed to play stream");
// Compute the DSP
// Map our Vec<Vec<f32>> to a Vec<&f[32]> to create a buffer for the faust lib
let buffer_input: Vec<&[f32]> = inputs
.iter()
.map(|input| unsafe {
slice::from_raw_parts(input.as_ptr(), buffer_size)
})
.collect();
// Map our Vec<Vec<f32>> to a Vec<&f[32]> to create a buffer for the faust lib
let mut buffer_output: Vec<&mut [f32]> = outputs
.iter_mut()
.map(|output| unsafe {
slice::from_raw_parts_mut(output.as_mut_ptr(), buffer_size)
})
.collect();
dsp.update_and_compute(
len as i32,
&buffer_input[..],
&mut buffer_output[..],
);
// Send to audio buffer
for (out, dsp_sample) in data.iter_mut().zip(&outputs[0]) {
*out = *dsp_sample;
}
},
|err| log::error!("an error occurred on the output audio stream: {err}"),
None,
)
.unwrap();
stream.play().expect("Failed to play stream");

loop {
// Retrieve the parameter updates
for msg in rx.try_iter() {
match msg {
Msg::Exit => return,
Msg::ParameterUpdate(parameter) => {
state.set_param(parameter.idx, parameter.value)
loop {
// Retrieve the parameter updates
for msg in rx.try_iter() {
match msg {
Msg::Exit => return,
Msg::ParameterUpdate(parameter) => {
state.set_param(parameter.idx, parameter.value)
}
}
}
state.send();
}
state.send();
}
})
})
.expect("Failed to spawn the DSP controller")
}
2 changes: 1 addition & 1 deletion theremotion/src/thread_ui.rs
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ pub fn run(
let tx = tx.clone();
move || {
tx.send(CM::Exit).unwrap();
return slint::CloseRequestResponse::HideWindow;
slint::CloseRequestResponse::HideWindow
}
});

Expand Down

0 comments on commit efaef15

Please sign in to comment.