mirror of
https://github.com/project-slippi/cpal.git
synced 2025-10-06 00:02:40 +02:00
Improve synth tones example (#784)
This commit is contained in:
@@ -13,54 +13,100 @@ use cpal::{
|
||||
use cpal::{FromSample, Sample};
|
||||
|
||||
fn main() -> anyhow::Result<()> {
|
||||
let stream = stream_setup_for(sample_next)?;
|
||||
let stream = stream_setup_for()?;
|
||||
stream.play()?;
|
||||
std::thread::sleep(std::time::Duration::from_millis(3000));
|
||||
std::thread::sleep(std::time::Duration::from_millis(4000));
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn sample_next(o: &mut SampleRequestOptions) -> f32 {
|
||||
o.tick();
|
||||
o.tone(440.) * 0.1 + o.tone(880.) * 0.1
|
||||
// combination of several tones
|
||||
pub enum Waveform {
|
||||
Sine,
|
||||
Square,
|
||||
Saw,
|
||||
Triangle,
|
||||
}
|
||||
|
||||
pub struct SampleRequestOptions {
|
||||
pub struct Oscillator {
|
||||
pub sample_rate: f32,
|
||||
pub sample_clock: f32,
|
||||
pub nchannels: usize,
|
||||
pub waveform: Waveform,
|
||||
pub current_sample_index: f32,
|
||||
pub frequency_hz: f32,
|
||||
}
|
||||
|
||||
impl SampleRequestOptions {
|
||||
fn tone(&self, freq: f32) -> f32 {
|
||||
(self.sample_clock * freq * 2.0 * std::f32::consts::PI / self.sample_rate).sin()
|
||||
impl Oscillator {
|
||||
fn advance_sample(&mut self) {
|
||||
self.current_sample_index = (self.current_sample_index + 1.0) % self.sample_rate;
|
||||
}
|
||||
fn tick(&mut self) {
|
||||
self.sample_clock = (self.sample_clock + 1.0) % self.sample_rate;
|
||||
|
||||
fn set_waveform(&mut self, waveform: Waveform) {
|
||||
self.waveform = waveform;
|
||||
}
|
||||
|
||||
fn calculate_sine_output_from_freq(&self, freq: f32) -> f32 {
|
||||
let two_pi = 2.0 * std::f32::consts::PI;
|
||||
(self.current_sample_index * freq * two_pi / self.sample_rate).sin()
|
||||
}
|
||||
|
||||
fn is_multiple_of_freq_above_nyquist(&self, multiple: f32) -> bool {
|
||||
self.frequency_hz * multiple > self.sample_rate / 2.0
|
||||
}
|
||||
|
||||
fn sine_wave(&mut self) -> f32 {
|
||||
self.advance_sample();
|
||||
self.calculate_sine_output_from_freq(self.frequency_hz)
|
||||
}
|
||||
|
||||
fn generative_waveform(&mut self, harmonic_index_increment: i32, gain_exponent: f32) -> f32 {
|
||||
self.advance_sample();
|
||||
let mut output = 0.0;
|
||||
let mut i = 1;
|
||||
while !self.is_multiple_of_freq_above_nyquist(i as f32) {
|
||||
let gain = 1.0 / (i as f32).powf(gain_exponent);
|
||||
output += gain * self.calculate_sine_output_from_freq(self.frequency_hz * i as f32);
|
||||
i += harmonic_index_increment;
|
||||
}
|
||||
output
|
||||
}
|
||||
|
||||
fn square_wave(&mut self) -> f32 {
|
||||
self.generative_waveform(2, 1.0)
|
||||
}
|
||||
|
||||
fn saw_wave(&mut self) -> f32 {
|
||||
self.generative_waveform(1, 1.0)
|
||||
}
|
||||
|
||||
fn triangle_wave(&mut self) -> f32 {
|
||||
self.generative_waveform(2, 2.0)
|
||||
}
|
||||
|
||||
fn tick(&mut self) -> f32 {
|
||||
self.advance_sample();
|
||||
match self.waveform {
|
||||
Waveform::Sine => self.sine_wave(),
|
||||
Waveform::Square => self.square_wave(),
|
||||
Waveform::Saw => self.saw_wave(),
|
||||
Waveform::Triangle => self.triangle_wave(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub fn stream_setup_for<F>(on_sample: F) -> Result<cpal::Stream, anyhow::Error>
|
||||
pub fn stream_setup_for() -> Result<cpal::Stream, anyhow::Error>
|
||||
where
|
||||
F: FnMut(&mut SampleRequestOptions) -> f32 + std::marker::Send + 'static + Copy,
|
||||
{
|
||||
let (_host, device, config) = host_device_setup()?;
|
||||
|
||||
match config.sample_format() {
|
||||
cpal::SampleFormat::I8 => stream_make::<i8, _>(&device, &config.into(), on_sample),
|
||||
cpal::SampleFormat::I16 => stream_make::<i16, _>(&device, &config.into(), on_sample),
|
||||
// cpal::SampleFormat::I24 => stream_make::<I24, _>(&device, &config.into(), on_sample),
|
||||
cpal::SampleFormat::I32 => stream_make::<i32, _>(&device, &config.into(), on_sample),
|
||||
// cpal::SampleFormat::I48 => stream_make::<I48, _>(&device, &config.into(), on_sample),
|
||||
cpal::SampleFormat::I64 => stream_make::<i64, _>(&device, &config.into(), on_sample),
|
||||
cpal::SampleFormat::U8 => stream_make::<u8, _>(&device, &config.into(), on_sample),
|
||||
cpal::SampleFormat::U16 => stream_make::<u16, _>(&device, &config.into(), on_sample),
|
||||
// cpal::SampleFormat::U24 => stream_make::<U24, _>(&device, &config.into(), on_sample),
|
||||
cpal::SampleFormat::U32 => stream_make::<u32, _>(&device, &config.into(), on_sample),
|
||||
// cpal::SampleFormat::U48 => stream_make::<U48, _>(&device, &config.into(), on_sample),
|
||||
cpal::SampleFormat::U64 => stream_make::<u64, _>(&device, &config.into(), on_sample),
|
||||
cpal::SampleFormat::F32 => stream_make::<f32, _>(&device, &config.into(), on_sample),
|
||||
cpal::SampleFormat::F64 => stream_make::<f64, _>(&device, &config.into(), on_sample),
|
||||
cpal::SampleFormat::I8 => make_stream::<i8>(&device, &config.into()),
|
||||
cpal::SampleFormat::I16 => make_stream::<i16>(&device, &config.into()),
|
||||
cpal::SampleFormat::I32 => make_stream::<i32>(&device, &config.into()),
|
||||
cpal::SampleFormat::I64 => make_stream::<i64>(&device, &config.into()),
|
||||
cpal::SampleFormat::U8 => make_stream::<u8>(&device, &config.into()),
|
||||
cpal::SampleFormat::U16 => make_stream::<u16>(&device, &config.into()),
|
||||
cpal::SampleFormat::U32 => make_stream::<u32>(&device, &config.into()),
|
||||
cpal::SampleFormat::U64 => make_stream::<u64>(&device, &config.into()),
|
||||
cpal::SampleFormat::F32 => make_stream::<f32>(&device, &config.into()),
|
||||
cpal::SampleFormat::F64 => make_stream::<f64>(&device, &config.into()),
|
||||
sample_format => Err(anyhow::Error::msg(format!(
|
||||
"Unsupported sample format '{sample_format}'"
|
||||
))),
|
||||
@@ -82,29 +128,44 @@ pub fn host_device_setup(
|
||||
Ok((host, device, config))
|
||||
}
|
||||
|
||||
pub fn stream_make<T, F>(
|
||||
pub fn make_stream<T>(
|
||||
device: &cpal::Device,
|
||||
config: &cpal::StreamConfig,
|
||||
on_sample: F,
|
||||
) -> Result<cpal::Stream, anyhow::Error>
|
||||
where
|
||||
T: SizedSample + FromSample<f32>,
|
||||
F: FnMut(&mut SampleRequestOptions) -> f32 + std::marker::Send + 'static + Copy,
|
||||
{
|
||||
let sample_rate = config.sample_rate.0 as f32;
|
||||
let sample_clock = 0f32;
|
||||
let nchannels = config.channels as usize;
|
||||
let mut request = SampleRequestOptions {
|
||||
sample_rate,
|
||||
sample_clock,
|
||||
nchannels,
|
||||
let num_channels = config.channels as usize;
|
||||
let mut oscillator = Oscillator {
|
||||
waveform: Waveform::Sine,
|
||||
sample_rate: config.sample_rate.0 as f32,
|
||||
current_sample_index: 0.0,
|
||||
frequency_hz: 440.0,
|
||||
};
|
||||
let err_fn = |err| eprintln!("Error building output sound stream: {}", err);
|
||||
|
||||
let time_at_start = std::time::Instant::now();
|
||||
println!("Time at start: {:?}", time_at_start);
|
||||
|
||||
let stream = device.build_output_stream(
|
||||
config,
|
||||
move |output: &mut [T], _: &cpal::OutputCallbackInfo| {
|
||||
on_window(output, &mut request, on_sample)
|
||||
// for 0-1s play sine, 1-2s play square, 2-3s play saw, 3-4s play triangle_wave
|
||||
let time_since_start = std::time::Instant::now()
|
||||
.duration_since(time_at_start)
|
||||
.as_secs_f32();
|
||||
if time_since_start < 1.0 {
|
||||
oscillator.set_waveform(Waveform::Sine);
|
||||
} else if time_since_start < 2.0 {
|
||||
oscillator.set_waveform(Waveform::Triangle);
|
||||
} else if time_since_start < 3.0 {
|
||||
oscillator.set_waveform(Waveform::Square);
|
||||
} else if time_since_start < 4.0 {
|
||||
oscillator.set_waveform(Waveform::Saw);
|
||||
} else {
|
||||
oscillator.set_waveform(Waveform::Sine);
|
||||
}
|
||||
process_frame(output, &mut oscillator, num_channels)
|
||||
},
|
||||
err_fn,
|
||||
None,
|
||||
@@ -113,13 +174,17 @@ where
|
||||
Ok(stream)
|
||||
}
|
||||
|
||||
fn on_window<T, F>(output: &mut [T], request: &mut SampleRequestOptions, mut on_sample: F)
|
||||
where
|
||||
T: Sample + FromSample<f32>,
|
||||
F: FnMut(&mut SampleRequestOptions) -> f32 + std::marker::Send + 'static,
|
||||
fn process_frame<SampleType>(
|
||||
output: &mut [SampleType],
|
||||
oscillator: &mut Oscillator,
|
||||
num_channels: usize,
|
||||
) where
|
||||
SampleType: Sample + FromSample<f32>,
|
||||
{
|
||||
for frame in output.chunks_mut(request.nchannels) {
|
||||
let value: T = T::from_sample(on_sample(request));
|
||||
for frame in output.chunks_mut(num_channels) {
|
||||
let value: SampleType = SampleType::from_sample(oscillator.tick());
|
||||
|
||||
// copy the same value to all channels
|
||||
for sample in frame.iter_mut() {
|
||||
*sample = value;
|
||||
}
|
||||
|
Reference in New Issue
Block a user