Improve synth tones example (#784)
This commit is contained in:
parent
d4ba28df73
commit
19991d3607
1 changed files with 112 additions and 47 deletions
|
|
@ -13,54 +13,100 @@ use cpal::{
|
||||||
use cpal::{FromSample, Sample};
|
use cpal::{FromSample, Sample};
|
||||||
|
|
||||||
fn main() -> anyhow::Result<()> {
|
fn main() -> anyhow::Result<()> {
|
||||||
let stream = stream_setup_for(sample_next)?;
|
let stream = stream_setup_for()?;
|
||||||
stream.play()?;
|
stream.play()?;
|
||||||
std::thread::sleep(std::time::Duration::from_millis(3000));
|
std::thread::sleep(std::time::Duration::from_millis(4000));
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
fn sample_next(o: &mut SampleRequestOptions) -> f32 {
|
pub enum Waveform {
|
||||||
o.tick();
|
Sine,
|
||||||
o.tone(440.) * 0.1 + o.tone(880.) * 0.1
|
Square,
|
||||||
// combination of several tones
|
Saw,
|
||||||
|
Triangle,
|
||||||
}
|
}
|
||||||
|
|
||||||
pub struct SampleRequestOptions {
|
pub struct Oscillator {
|
||||||
pub sample_rate: f32,
|
pub sample_rate: f32,
|
||||||
pub sample_clock: f32,
|
pub waveform: Waveform,
|
||||||
pub nchannels: usize,
|
pub current_sample_index: f32,
|
||||||
|
pub frequency_hz: f32,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl SampleRequestOptions {
|
impl Oscillator {
|
||||||
fn tone(&self, freq: f32) -> f32 {
|
fn advance_sample(&mut self) {
|
||||||
(self.sample_clock * freq * 2.0 * std::f32::consts::PI / self.sample_rate).sin()
|
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
|
where
|
||||||
F: FnMut(&mut SampleRequestOptions) -> f32 + std::marker::Send + 'static + Copy,
|
|
||||||
{
|
{
|
||||||
let (_host, device, config) = host_device_setup()?;
|
let (_host, device, config) = host_device_setup()?;
|
||||||
|
|
||||||
match config.sample_format() {
|
match config.sample_format() {
|
||||||
cpal::SampleFormat::I8 => stream_make::<i8, _>(&device, &config.into(), on_sample),
|
cpal::SampleFormat::I8 => make_stream::<i8>(&device, &config.into()),
|
||||||
cpal::SampleFormat::I16 => stream_make::<i16, _>(&device, &config.into(), on_sample),
|
cpal::SampleFormat::I16 => make_stream::<i16>(&device, &config.into()),
|
||||||
// cpal::SampleFormat::I24 => stream_make::<I24, _>(&device, &config.into(), on_sample),
|
cpal::SampleFormat::I32 => make_stream::<i32>(&device, &config.into()),
|
||||||
cpal::SampleFormat::I32 => stream_make::<i32, _>(&device, &config.into(), on_sample),
|
cpal::SampleFormat::I64 => make_stream::<i64>(&device, &config.into()),
|
||||||
// cpal::SampleFormat::I48 => stream_make::<I48, _>(&device, &config.into(), on_sample),
|
cpal::SampleFormat::U8 => make_stream::<u8>(&device, &config.into()),
|
||||||
cpal::SampleFormat::I64 => stream_make::<i64, _>(&device, &config.into(), on_sample),
|
cpal::SampleFormat::U16 => make_stream::<u16>(&device, &config.into()),
|
||||||
cpal::SampleFormat::U8 => stream_make::<u8, _>(&device, &config.into(), on_sample),
|
cpal::SampleFormat::U32 => make_stream::<u32>(&device, &config.into()),
|
||||||
cpal::SampleFormat::U16 => stream_make::<u16, _>(&device, &config.into(), on_sample),
|
cpal::SampleFormat::U64 => make_stream::<u64>(&device, &config.into()),
|
||||||
// cpal::SampleFormat::U24 => stream_make::<U24, _>(&device, &config.into(), on_sample),
|
cpal::SampleFormat::F32 => make_stream::<f32>(&device, &config.into()),
|
||||||
cpal::SampleFormat::U32 => stream_make::<u32, _>(&device, &config.into(), on_sample),
|
cpal::SampleFormat::F64 => make_stream::<f64>(&device, &config.into()),
|
||||||
// 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),
|
|
||||||
sample_format => Err(anyhow::Error::msg(format!(
|
sample_format => Err(anyhow::Error::msg(format!(
|
||||||
"Unsupported sample format '{sample_format}'"
|
"Unsupported sample format '{sample_format}'"
|
||||||
))),
|
))),
|
||||||
|
|
@ -82,29 +128,44 @@ pub fn host_device_setup(
|
||||||
Ok((host, device, config))
|
Ok((host, device, config))
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn stream_make<T, F>(
|
pub fn make_stream<T>(
|
||||||
device: &cpal::Device,
|
device: &cpal::Device,
|
||||||
config: &cpal::StreamConfig,
|
config: &cpal::StreamConfig,
|
||||||
on_sample: F,
|
|
||||||
) -> Result<cpal::Stream, anyhow::Error>
|
) -> Result<cpal::Stream, anyhow::Error>
|
||||||
where
|
where
|
||||||
T: SizedSample + FromSample<f32>,
|
T: SizedSample + FromSample<f32>,
|
||||||
F: FnMut(&mut SampleRequestOptions) -> f32 + std::marker::Send + 'static + Copy,
|
|
||||||
{
|
{
|
||||||
let sample_rate = config.sample_rate.0 as f32;
|
let num_channels = config.channels as usize;
|
||||||
let sample_clock = 0f32;
|
let mut oscillator = Oscillator {
|
||||||
let nchannels = config.channels as usize;
|
waveform: Waveform::Sine,
|
||||||
let mut request = SampleRequestOptions {
|
sample_rate: config.sample_rate.0 as f32,
|
||||||
sample_rate,
|
current_sample_index: 0.0,
|
||||||
sample_clock,
|
frequency_hz: 440.0,
|
||||||
nchannels,
|
|
||||||
};
|
};
|
||||||
let err_fn = |err| eprintln!("Error building output sound stream: {}", err);
|
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(
|
let stream = device.build_output_stream(
|
||||||
config,
|
config,
|
||||||
move |output: &mut [T], _: &cpal::OutputCallbackInfo| {
|
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,
|
err_fn,
|
||||||
None,
|
None,
|
||||||
|
|
@ -113,13 +174,17 @@ where
|
||||||
Ok(stream)
|
Ok(stream)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn on_window<T, F>(output: &mut [T], request: &mut SampleRequestOptions, mut on_sample: F)
|
fn process_frame<SampleType>(
|
||||||
where
|
output: &mut [SampleType],
|
||||||
T: Sample + FromSample<f32>,
|
oscillator: &mut Oscillator,
|
||||||
F: FnMut(&mut SampleRequestOptions) -> f32 + std::marker::Send + 'static,
|
num_channels: usize,
|
||||||
|
) where
|
||||||
|
SampleType: Sample + FromSample<f32>,
|
||||||
{
|
{
|
||||||
for frame in output.chunks_mut(request.nchannels) {
|
for frame in output.chunks_mut(num_channels) {
|
||||||
let value: T = T::from_sample(on_sample(request));
|
let value: SampleType = SampleType::from_sample(oscillator.tick());
|
||||||
|
|
||||||
|
// copy the same value to all channels
|
||||||
for sample in frame.iter_mut() {
|
for sample in frame.iter_mut() {
|
||||||
*sample = value;
|
*sample = value;
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue