-
Notifications
You must be signed in to change notification settings - Fork 235
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #602 from iluvcapra/synth-waveforms
Synthesizer Waveforms
- Loading branch information
Showing
10 changed files
with
586 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
//! Noise generator example. Use the "noise" feature to enable the noise generator sources. | ||
|
||
#[cfg(feature = "noise")] | ||
fn main() { | ||
use rodio::source::{pink, white, Source}; | ||
use std::thread; | ||
use std::time::Duration; | ||
|
||
let (_stream, stream_handle) = rodio::OutputStream::try_default().unwrap(); | ||
|
||
let noise_duration = Duration::from_millis(1000); | ||
let interval_duration = Duration::from_millis(1500); | ||
|
||
stream_handle | ||
.play_raw( | ||
white(cpal::SampleRate(48000)) | ||
.amplify(0.1) | ||
.take_duration(noise_duration), | ||
) | ||
.unwrap(); | ||
println!("Playing white noise"); | ||
|
||
thread::sleep(interval_duration); | ||
|
||
stream_handle | ||
.play_raw( | ||
pink(cpal::SampleRate(48000)) | ||
.amplify(0.1) | ||
.take_duration(noise_duration), | ||
) | ||
.unwrap(); | ||
println!("Playing pink noise"); | ||
|
||
thread::sleep(interval_duration); | ||
} | ||
|
||
#[cfg(not(feature = "noise"))] | ||
fn main() { | ||
println!("rodio has not been compiled with noise sources, use `--features noise` to enable this feature."); | ||
println!("Exiting..."); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
//! Test signal generator example. | ||
|
||
fn main() { | ||
use rodio::source::{chirp, Function, SignalGenerator, Source}; | ||
use std::thread; | ||
use std::time::Duration; | ||
|
||
let (_stream, stream_handle) = rodio::OutputStream::try_default().unwrap(); | ||
|
||
let test_signal_duration = Duration::from_millis(1000); | ||
let interval_duration = Duration::from_millis(1500); | ||
|
||
println!("Playing 1000 Hz tone"); | ||
stream_handle | ||
.play_raw( | ||
SignalGenerator::new(cpal::SampleRate(48000), 1000.0, Function::Sine) | ||
.amplify(0.1) | ||
.take_duration(test_signal_duration), | ||
) | ||
.unwrap(); | ||
|
||
thread::sleep(interval_duration); | ||
|
||
println!("Playing 10,000 Hz tone"); | ||
stream_handle | ||
.play_raw( | ||
SignalGenerator::new(cpal::SampleRate(48000), 10000.0, Function::Sine) | ||
.amplify(0.1) | ||
.take_duration(test_signal_duration), | ||
) | ||
.unwrap(); | ||
|
||
thread::sleep(interval_duration); | ||
|
||
println!("Playing 440 Hz Triangle Wave"); | ||
stream_handle | ||
.play_raw( | ||
SignalGenerator::new(cpal::SampleRate(48000), 440.0, Function::Triangle) | ||
.amplify(0.1) | ||
.take_duration(test_signal_duration), | ||
) | ||
.unwrap(); | ||
|
||
thread::sleep(interval_duration); | ||
|
||
println!("Playing 440 Hz Sawtooth Wave"); | ||
stream_handle | ||
.play_raw( | ||
SignalGenerator::new(cpal::SampleRate(48000), 440.0, Function::Sawtooth) | ||
.amplify(0.1) | ||
.take_duration(test_signal_duration), | ||
) | ||
.unwrap(); | ||
|
||
thread::sleep(interval_duration); | ||
|
||
println!("Playing 440 Hz Square Wave"); | ||
stream_handle | ||
.play_raw( | ||
SignalGenerator::new(cpal::SampleRate(48000), 440.0, Function::Square) | ||
.amplify(0.1) | ||
.take_duration(test_signal_duration), | ||
) | ||
.unwrap(); | ||
|
||
thread::sleep(interval_duration); | ||
|
||
println!("Playing 20-10000 Hz Sweep"); | ||
stream_handle | ||
.play_raw( | ||
chirp( | ||
cpal::SampleRate(48000), | ||
20.0, | ||
10000.0, | ||
Duration::from_secs(1), | ||
) | ||
.amplify(0.1) | ||
.take_duration(test_signal_duration), | ||
) | ||
.unwrap(); | ||
|
||
thread::sleep(interval_duration); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
//! Chirp/sweep source. | ||
|
||
use std::{f32::consts::TAU, time::Duration}; | ||
|
||
use crate::Source; | ||
|
||
/// Convenience function to create a new `Chirp` source. | ||
#[inline] | ||
pub fn chirp( | ||
sample_rate: cpal::SampleRate, | ||
start_frequency: f32, | ||
end_frequency: f32, | ||
duration: Duration, | ||
) -> Chirp { | ||
Chirp::new(sample_rate, start_frequency, end_frequency, duration) | ||
} | ||
|
||
/// Generate a sine wave with an instantaneous frequency that changes/sweeps linearly over time. | ||
/// At the end of the chirp, once the `end_frequency` is reached, the source is exhausted. | ||
#[derive(Clone, Debug)] | ||
pub struct Chirp { | ||
start_frequency: f32, | ||
end_frequency: f32, | ||
sample_rate: cpal::SampleRate, | ||
total_samples: u64, | ||
elapsed_samples: u64, | ||
} | ||
|
||
impl Chirp { | ||
fn new( | ||
sample_rate: cpal::SampleRate, | ||
start_frequency: f32, | ||
end_frequency: f32, | ||
duration: Duration, | ||
) -> Self { | ||
Self { | ||
sample_rate, | ||
start_frequency, | ||
end_frequency, | ||
total_samples: (duration.as_secs_f64() * (sample_rate.0 as f64)) as u64, | ||
elapsed_samples: 0, | ||
} | ||
} | ||
} | ||
|
||
impl Iterator for Chirp { | ||
type Item = f32; | ||
|
||
fn next(&mut self) -> Option<Self::Item> { | ||
let i = self.elapsed_samples; | ||
let ratio = self.elapsed_samples as f32 / self.total_samples as f32; | ||
self.elapsed_samples += 1; | ||
let freq = self.start_frequency * (1.0 - ratio) + self.end_frequency * ratio; | ||
let t = (i as f32 / self.sample_rate() as f32) * TAU * freq; | ||
Some(t.sin()) | ||
} | ||
} | ||
|
||
impl Source for Chirp { | ||
fn current_frame_len(&self) -> Option<usize> { | ||
None | ||
} | ||
|
||
fn channels(&self) -> u16 { | ||
1 | ||
} | ||
|
||
fn sample_rate(&self) -> u32 { | ||
self.sample_rate.0 | ||
} | ||
|
||
fn total_duration(&self) -> Option<Duration> { | ||
let secs: f64 = self.total_samples as f64 / self.sample_rate.0 as f64; | ||
Some(Duration::new(1, 0).mul_f64(secs)) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.