Trait pix_engine::audio::AudioCallback
source · pub trait AudioCallback: Sendwhere
Self::Channel: AudioFormatNum + 'static,{
type Channel;
// Required method
fn callback(&mut self, buffer: &mut [Self::Channel]);
}
Expand description
Trait for allowing Engine
to request audio samples from your application.
Please see the module-level documentation for more examples.
Required Associated Types§
Required Methods§
sourcefn callback(&mut self, buffer: &mut [Self::Channel])
fn callback(&mut self, buffer: &mut [Self::Channel])
Called when the audio playback device needs samples to play or the capture device has
samples available. buffer
is a pre-allocated buffer you can iterate over and update to
provide audio samples, or consume to record audio samples.
Example
use pix_engine::prelude::*;
struct SquareWave {
phase_inc: f32,
phase: f32,
volume: f32,
}
impl AudioCallback for SquareWave {
type Channel = f32;
fn callback(&mut self, out: &mut [Self::Channel]) {
// Generate a square wave
for x in out.iter_mut() {
*x = if self.phase <= 0.5 {
self.volume
} else {
-self.volume
};
self.phase = (self.phase + self.phase_inc) % 1.0;
}
}
}