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.
|
||||
#[derive(Resource)]
|
||||
struct AudioCaptureStream {
|
||||
|
|
@ -50,8 +41,7 @@ pub struct AudioCapturePlugin;
|
|||
|
||||
impl Plugin for AudioCapturePlugin {
|
||||
fn build(&self, app: &mut App) {
|
||||
app.add_event::<AudioCaptureData>()
|
||||
.init_resource::<AudioFft>()
|
||||
app.init_resource::<AudioFft>()
|
||||
.add_systems(Startup, init_audio_input_stream)
|
||||
.add_systems(Update, dispatch_audio_events);
|
||||
}
|
||||
|
|
@ -132,25 +122,15 @@ where
|
|||
)
|
||||
}
|
||||
|
||||
fn dispatch_audio_events(
|
||||
mic: Res<AudioCaptureStream>,
|
||||
mut writer: EventWriter<AudioCaptureData>,
|
||||
mut fft_res: ResMut<AudioFft>,
|
||||
) {
|
||||
fn dispatch_audio_events(stream: Res<AudioCaptureStream>, mut fft_res: ResMut<AudioFft>) {
|
||||
// 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<f32> = if mic.channels > 1 {
|
||||
let mut mono: Vec<f32> = 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;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue