rename
This commit is contained in:
parent
518c9835e8
commit
55a94f0a75
2 changed files with 12 additions and 12 deletions
|
|
@ -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<f32>,
|
||||
pub sample_rate: u32,
|
||||
pub channels: u16,
|
||||
}
|
||||
|
||||
pub fn handle_mic_events(mut reader: EventReader<MicData>) {
|
||||
pub fn handle_audio_capture_events(mut reader: EventReader<AudioCaptureData>) {
|
||||
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<MicData>) {
|
|||
|
||||
/// Holds the CPAL stream alive and a channel for transferring audio to Bevy world.
|
||||
#[derive(Resource)]
|
||||
struct MicStream {
|
||||
struct AudioCaptureStream {
|
||||
rx: Receiver<Vec<f32>>,
|
||||
#[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::<MicData>()
|
||||
app.add_event::<AudioCaptureData>()
|
||||
.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<MicStream>, mut writer: EventWriter<MicData>) {
|
||||
fn dispatch_audio_events(mic: Res<AudioCaptureStream>, mut writer: EventWriter<AudioCaptureData>) {
|
||||
// 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,
|
||||
|
|
|
|||
|
|
@ -12,14 +12,14 @@ fn main() {
|
|||
.add_plugins((
|
||||
DefaultPlugins,
|
||||
Material2dPlugin::<CustomMaterial>::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)),
|
||||
|
|
|
|||
Loading…
Reference in a new issue