From efaef15a4be2028189d88239430d42be486cb416 Mon Sep 17 00:00:00 2001 From: plule <630159+plule@users.noreply.github.com> Date: Mon, 6 Nov 2023 12:53:49 +0100 Subject: [PATCH] Clippy, update readme --- README.md | 6 +- theremotion/src/thread_dsp.rs | 107 ++++++++++++++++++---------------- theremotion/src/thread_ui.rs | 2 +- 3 files changed, 63 insertions(+), 52 deletions(-) diff --git a/README.md b/README.md index b057437..59a7ee3 100644 --- a/README.md +++ b/README.md @@ -71,7 +71,8 @@ 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 @@ -79,7 +80,8 @@ 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 diff --git a/theremotion/src/thread_dsp.rs b/theremotion/src/thread_dsp.rs index f19dcfb..5dfe7af 100644 --- a/theremotion/src/thread_dsp.rs +++ b/theremotion/src/thread_dsp.rs @@ -67,59 +67,68 @@ where let mut inputs: Vec> = vec![vec![0_f32; buffer_size]; num_inputs]; let mut outputs: Vec> = 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> 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> 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> 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> 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") } diff --git a/theremotion/src/thread_ui.rs b/theremotion/src/thread_ui.rs index 7c72f3c..d6782d6 100644 --- a/theremotion/src/thread_ui.rs +++ b/theremotion/src/thread_ui.rs @@ -104,7 +104,7 @@ pub fn run( let tx = tx.clone(); move || { tx.send(CM::Exit).unwrap(); - return slint::CloseRequestResponse::HideWindow; + slint::CloseRequestResponse::HideWindow } });