Skip to content

Commit

Permalink
Use AudioFormat::default() in more places.
Browse files Browse the repository at this point in the history
The ::new() methods should be easier to use this way.

Also speeds up doc tests by not sleeping in README.md example.
  • Loading branch information
thoren-d committed Aug 14, 2021
1 parent 059a563 commit e0128a9
Show file tree
Hide file tree
Showing 9 changed files with 34 additions and 23 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
speaker.set_source(mixer.into_shared());
speaker.resume();

std::thread::sleep(Duration::from_secs_f32(10.0));
// std::thread::sleep(Duration::from_secs_f32(120.0));
Ok(())
}
```
Expand Down
2 changes: 1 addition & 1 deletion benches/generators.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ fn bench_sinewave(c: &mut Criterion) {
sample_rate: SAMPLE_RATE as u32,
};

let mut sin_wave = SineWave::new(format, 1.0, 440.0);
let mut sin_wave = SineWave::with_format(format, 1.0, 440.0);

b.iter(|| {
sin_wave.read(&mut samples);
Expand Down
5 changes: 1 addition & 4 deletions src/drivers/sdl2_input.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,10 +75,7 @@ impl Sdl2Input {
pub fn new(subsystem: &sdl2::AudioSubsystem) -> Result<Self, Error> {
Sdl2Input::with_format(
subsystem,
AudioFormat {
channels: 2,
sample_rate: 44100,
},
AudioFormat::default(),
)
}

Expand Down
5 changes: 1 addition & 4 deletions src/drivers/sdl2_output.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,10 +83,7 @@ impl Sdl2Output {
pub fn new(subsystem: &sdl2::AudioSubsystem) -> Result<Self, Error> {
Sdl2Output::with_format(
subsystem,
AudioFormat {
channels: 2,
sample_rate: 44100,
},
AudioFormat::default(),
)
}

Expand Down
10 changes: 5 additions & 5 deletions src/effects/basic_mixer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@ use tracing::instrument;
/// # Examples
/// ```
/// # fn main() -> Result<(), Box<dyn std::error::Error>> {
/// # use timbre::{AudioFormat, generators::SineWave, effects::BasicMixer, IntoShared};
/// let sin1 = SineWave::new(AudioFormat::MONO_CD, 0.5, 440.0);
/// let sin2 = SineWave::new(AudioFormat::MONO_CD, 0.5, 220.0);
/// # use timbre::{generators::SineWave, effects::BasicMixer, IntoShared};
/// let sin1 = SineWave::new(0.5, 440.0);
/// let sin2 = SineWave::new(0.5, 220.0);
///
/// let mut mixer = BasicMixer::new();
/// let sin1 = mixer.add_source(sin1.into_shared());
Expand Down Expand Up @@ -82,8 +82,8 @@ impl BasicMixer {
///
/// # Examples
/// ```
/// # use timbre::{AudioFormat, effects::BasicMixer, generators::SineWave, IntoShared};
/// let sin = SineWave::new(AudioFormat::MONO_CD, 1.0, 440.0);
/// # use timbre::{effects::BasicMixer, generators::SineWave, IntoShared};
/// let sin = SineWave::new(1.0, 440.0);
/// let mut mixer = BasicMixer::new();
/// let sin = mixer.add_source(sin.into_shared());
/// mixer.remove_source(sin);
Expand Down
4 changes: 2 additions & 2 deletions src/effects/echo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ use tracing::instrument;
///
/// # Examples
/// ```
/// # use timbre::{AudioFormat, generators::SineWave, effects::Echo, IntoShared};
/// # use timbre::{generators::SineWave, effects::Echo, IntoShared};
/// # use std::time::Duration;
/// let sin = SineWave::new(AudioFormat::MONO_CD, 1.0, 440.0);
/// let sin = SineWave::new(1.0, 440.0);
/// let echo = Echo::new(sin, Duration::from_secs_f32(0.5), 0.8);
/// ```
pub struct Echo<S: AudioSource> {
Expand Down
4 changes: 2 additions & 2 deletions src/effects/high_pass.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@ use tracing::instrument;
///
/// # Examples
/// ```
/// # use timbre::{AudioFormat, generators::SineWave, effects::HighPass, IntoShared};
/// # use timbre::{generators::SineWave, effects::HighPass, IntoShared};
/// # use std::time::Duration;
/// let sin = SineWave::new(AudioFormat::STEREO_CD, 1.0, 440.0);
/// let sin = SineWave::new(1.0, 440.0);
/// let high_pass = HighPass::new(sin, 4000.0);
/// ```
pub struct HighPass<S: AudioSource> {
Expand Down
4 changes: 2 additions & 2 deletions src/effects/low_pass.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@ use tracing::instrument;
///
/// # Examples
/// ```
/// # use timbre::{AudioFormat, generators::SineWave, effects::LowPass, IntoShared};
/// # use timbre::{generators::SineWave, effects::LowPass, IntoShared};
/// # use std::time::Duration;
/// let sin = SineWave::new(AudioFormat::MONO_CD, 1.0, 440.0);
/// let sin = SineWave::new(1.0, 440.0);
/// let low_pass = LowPass::new(sin, 200.0);
/// ```
pub struct LowPass<S: AudioSource> {
Expand Down
21 changes: 19 additions & 2 deletions src/generators/tone.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use tracing::instrument;
/// # Examples
/// ```
/// # use timbre::{AudioFormat, generators::SineWave};
/// let sin = SineWave::new(AudioFormat::STEREO_DVD, 1.0, 440.0);
/// let sin = SineWave::new(1.0, 440.0);
/// ```
#[derive(Clone)]
pub struct SineWave {
Expand All @@ -19,12 +19,29 @@ pub struct SineWave {
impl SineWave {
/// Construct a new sine wave generator with the given amplitude and frequency.
///
/// Uses [`AudioFormat::default()`](crate::AudioFormat::default) as the format.
///
/// # Arguments
///
/// * `amplitude` -- The peak value of samples generated by the generator.
/// * `frequency` -- The frequency of the wave generated, in Hz.
pub fn new(amplitude: f32, frequency: f32) -> Self {
SineWave {
amplitude,
format: AudioFormat::default(),
phase: 0.0,
frequency,
}
}

/// Construct a new sine wave generator with the given format, amplitude, and frequency.
///
/// # Arguments
///
/// * `format` -- The format for the generated stream.
/// * `amplitude` -- The peak value of samples generated by the generator.
/// * `frequency` -- The frequency of the wave generated, in Hz.
pub fn new(format: AudioFormat, amplitude: f32, frequency: f32) -> Self {
pub fn with_format(format: AudioFormat, amplitude: f32, frequency: f32) -> Self {
SineWave {
amplitude,
format,
Expand Down

0 comments on commit e0128a9

Please sign in to comment.