diff --git a/src/audio_input.rs b/src/audio_input.rs index 5f1733f..0dd0339 100644 --- a/src/audio_input.rs +++ b/src/audio_input.rs @@ -27,15 +27,6 @@ impl Default for AudioFft { } } -/// Event carrying interleaved mic samples as f32 in the device's native -/// sample rate and channel layout. -#[derive(Event, Debug, Clone)] -pub struct AudioCaptureData { - pub samples: Vec, - pub sample_rate: u32, - pub channels: u16, -} - /// Holds the CPAL stream alive and a channel for transferring audio to Bevy world. #[derive(Resource)] struct AudioCaptureStream { @@ -50,8 +41,7 @@ pub struct AudioCapturePlugin; impl Plugin for AudioCapturePlugin { fn build(&self, app: &mut App) { - app.add_event::() - .init_resource::() + app.init_resource::() .add_systems(Startup, init_audio_input_stream) .add_systems(Update, dispatch_audio_events); } @@ -132,25 +122,15 @@ where ) } -fn dispatch_audio_events( - mic: Res, - mut writer: EventWriter, - mut fft_res: ResMut, -) { +fn dispatch_audio_events(stream: Res, mut fft_res: ResMut) { // FFT parameters const FFT_SIZE: usize = 1024; // Drain any available audio buffers without blocking the frame. - while let Ok(samples) = mic.rx.try_recv() { - writer.write(AudioCaptureData { - samples: samples.clone(), - sample_rate: mic.sample_rate, - channels: mic.channels, - }); - + while let Ok(samples) = stream.rx.try_recv() { // Compute FFT on the first channel (mono mix) - let mut mono: Vec = if mic.channels > 1 { + let mut mono: Vec = if stream.channels > 1 { samples - .chunks(mic.channels as usize) + .chunks(stream.channels as usize) .map(|frame| frame[0]) .collect() } else { @@ -179,7 +159,7 @@ fn dispatch_audio_events( // Update AudioFft resource fft_res.spectrum = spectrum; - fft_res.sample_rate = mic.sample_rate; - fft_res.channels = mic.channels; + fft_res.sample_rate = stream.sample_rate; + fft_res.channels = stream.channels; } }