diff --git a/src/audio_input.rs b/src/audio_input.rs index 73c1ee9..b119ac0 100644 --- a/src/audio_input.rs +++ b/src/audio_input.rs @@ -10,13 +10,13 @@ use { /// Event carrying interleaved mic samples as f32 in the device's native /// sample rate and channel layout. #[derive(Event, Debug, Clone)] -pub struct MicData { +pub struct AudioCaptureData { pub samples: Vec, pub sample_rate: u32, pub channels: u16, } -pub fn handle_mic_events(mut reader: EventReader) { +pub fn handle_audio_capture_events(mut reader: EventReader) { for event in reader.read() { // Process mic data here. // For example, print the number of samples received. @@ -31,7 +31,7 @@ pub fn handle_mic_events(mut reader: EventReader) { /// Holds the CPAL stream alive and a channel for transferring audio to Bevy world. #[derive(Resource)] -struct MicStream { +struct AudioCaptureStream { rx: Receiver>, #[allow(dead_code)] stream: cpal::Stream, // kept to prevent drop @@ -39,13 +39,13 @@ struct MicStream { channels: u16, } -pub struct AudioInputPlugin; +pub struct AudioCapturePlugin; -impl Plugin for AudioInputPlugin { +impl Plugin for AudioCapturePlugin { fn build(&self, app: &mut App) { - app.add_event::() + app.add_event::() .add_systems(Startup, init_audio_input_stream) - .add_systems(Update, pump_mic_events); + .add_systems(Update, dispatch_audio_events); } } @@ -81,7 +81,7 @@ fn init_audio_input_stream(mut commands: Commands) { channels, sample_rate ); - commands.insert_resource(MicStream { + commands.insert_resource(AudioCaptureStream { rx, stream, sample_rate, @@ -124,10 +124,10 @@ where ) } -fn pump_mic_events(mic: Res, mut writer: EventWriter) { +fn dispatch_audio_events(mic: Res, mut writer: EventWriter) { // Drain any available audio buffers without blocking the frame. while let Ok(samples) = mic.rx.try_recv() { - writer.write(MicData { + writer.write(AudioCaptureData { samples, sample_rate: mic.sample_rate, channels: mic.channels, diff --git a/src/main.rs b/src/main.rs index 7517bd7..a15db58 100644 --- a/src/main.rs +++ b/src/main.rs @@ -12,14 +12,14 @@ fn main() { .add_plugins(( DefaultPlugins, Material2dPlugin::::default(), - audio_input::AudioInputPlugin, + audio_input::AudioCapturePlugin, )) .add_systems(Startup, systems::setup) .add_systems( Update, ( material::update_material_time, - audio_input::handle_mic_events, + audio_input::handle_audio_capture_events, systems::screen_resized, systems::mouse_moved, systems::exit_app.run_if(input_just_pressed(KeyCode::Escape)),