remove useless stuff
This commit is contained in:
parent
8389e88a80
commit
a61ebda78b
1 changed files with 7 additions and 27 deletions
|
|
@ -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<f32>,
|
|
||||||
pub sample_rate: u32,
|
|
||||||
pub channels: u16,
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Holds the CPAL stream alive and a channel for transferring audio to Bevy world.
|
/// Holds the CPAL stream alive and a channel for transferring audio to Bevy world.
|
||||||
#[derive(Resource)]
|
#[derive(Resource)]
|
||||||
struct AudioCaptureStream {
|
struct AudioCaptureStream {
|
||||||
|
|
@ -50,8 +41,7 @@ pub struct AudioCapturePlugin;
|
||||||
|
|
||||||
impl Plugin for AudioCapturePlugin {
|
impl Plugin for AudioCapturePlugin {
|
||||||
fn build(&self, app: &mut App) {
|
fn build(&self, app: &mut App) {
|
||||||
app.add_event::<AudioCaptureData>()
|
app.init_resource::<AudioFft>()
|
||||||
.init_resource::<AudioFft>()
|
|
||||||
.add_systems(Startup, init_audio_input_stream)
|
.add_systems(Startup, init_audio_input_stream)
|
||||||
.add_systems(Update, dispatch_audio_events);
|
.add_systems(Update, dispatch_audio_events);
|
||||||
}
|
}
|
||||||
|
|
@ -132,25 +122,15 @@ where
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn dispatch_audio_events(
|
fn dispatch_audio_events(stream: Res<AudioCaptureStream>, mut fft_res: ResMut<AudioFft>) {
|
||||||
mic: Res<AudioCaptureStream>,
|
|
||||||
mut writer: EventWriter<AudioCaptureData>,
|
|
||||||
mut fft_res: ResMut<AudioFft>,
|
|
||||||
) {
|
|
||||||
// FFT parameters
|
// FFT parameters
|
||||||
const FFT_SIZE: usize = 1024;
|
const FFT_SIZE: usize = 1024;
|
||||||
// Drain any available audio buffers without blocking the frame.
|
// Drain any available audio buffers without blocking the frame.
|
||||||
while let Ok(samples) = mic.rx.try_recv() {
|
while let Ok(samples) = stream.rx.try_recv() {
|
||||||
writer.write(AudioCaptureData {
|
|
||||||
samples: samples.clone(),
|
|
||||||
sample_rate: mic.sample_rate,
|
|
||||||
channels: mic.channels,
|
|
||||||
});
|
|
||||||
|
|
||||||
// Compute FFT on the first channel (mono mix)
|
// Compute FFT on the first channel (mono mix)
|
||||||
let mut mono: Vec<f32> = if mic.channels > 1 {
|
let mut mono: Vec<f32> = if stream.channels > 1 {
|
||||||
samples
|
samples
|
||||||
.chunks(mic.channels as usize)
|
.chunks(stream.channels as usize)
|
||||||
.map(|frame| frame[0])
|
.map(|frame| frame[0])
|
||||||
.collect()
|
.collect()
|
||||||
} else {
|
} else {
|
||||||
|
|
@ -179,7 +159,7 @@ fn dispatch_audio_events(
|
||||||
|
|
||||||
// Update AudioFft resource
|
// Update AudioFft resource
|
||||||
fft_res.spectrum = spectrum;
|
fft_res.spectrum = spectrum;
|
||||||
fft_res.sample_rate = mic.sample_rate;
|
fft_res.sample_rate = stream.sample_rate;
|
||||||
fft_res.channels = mic.channels;
|
fft_res.channels = stream.channels;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue