Function run_output_device
pub fn run_output_device<C>(
params: OutputDeviceParameters,
data_callback: C,
) -> Result<OutputDevice, Box<dyn Error>> ⓘ
Available on crate features
dep_tinyaudio
and alloc
only.Expand description
Creates a new output device that uses default audio output device of your operating system to play the
samples produced by the specified data_callback
. The callback will be called periodically to generate
another portion of samples.
§Examples
The following examples plays a 440 Hz sine wave for 5 seconds.
let params = OutputDeviceParameters {
channels_count: 2,
sample_rate: 44100,
channel_sample_count: 4410,
};
let _device = run_output_device(params, {
let mut clock = 0f32;
move |data| {
for samples in data.chunks_mut(params.channels_count) {
clock = (clock + 1.0) % params.sample_rate as f32;
let value =
(clock * 440.0 * 2.0 * std::f32::consts::PI / params.sample_rate as f32).sin();
for sample in samples {
*sample = value;
}
}
}
})
.unwrap();
std::thread::sleep(std::time::Duration::from_secs(5));