From 44cb217d48e65f3a2484b865a2f3b0b94ea45333 Mon Sep 17 00:00:00 2001 From: Jamie Hardt Date: Sun, 29 Sep 2024 16:45:29 -0700 Subject: [PATCH] Code review changes --- CHANGELOG.md | 6 +++--- examples/signal_generator.rs | 16 ++++++++-------- src/source/sine.rs | 8 ++++---- src/source/test_waveform.rs | 10 +++++----- 4 files changed, 20 insertions(+), 20 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 390bfa03..b1567d48 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -12,10 +12,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - New test signal generator sources: - `TestSignal` source generates a sine, triangle, square wave or sawtooth of a given frequency and sample rate. - - `Chirp` source generates a sine wave with linearly period over a given - frequency range and duration. + - `Chirp` source generates a sine wave with a linearly-increasing + frequency over a given frequency range and duration. - `white` and `pink` generate white or pink noise, respectively. These - sources depend on the `rand` crate and are guarded with the "noise" + sources depend on the `rand` crate and are guarded with the "noise" feature. - Documentation for the "noise" feature has been added to `lib.rs`. - New Fade and Crossfade sources: diff --git a/examples/signal_generator.rs b/examples/signal_generator.rs index ca761937..d17f1675 100644 --- a/examples/signal_generator.rs +++ b/examples/signal_generator.rs @@ -1,4 +1,4 @@ -//! Noise generator example. Use the "noise" feature to enable the noise generator sources. +//! Test signal generator example. fn main() { use rodio::source::{chirp, Function, Source, TestWaveform}; @@ -7,7 +7,7 @@ fn main() { let (_stream, stream_handle) = rodio::OutputStream::try_default().unwrap(); - let noise_duration = Duration::from_millis(1000); + let test_signal_duration = Duration::from_millis(1000); let interval_duration = Duration::from_millis(1500); println!("Playing 1000 Hz tone"); @@ -15,7 +15,7 @@ fn main() { .play_raw( TestWaveform::new(cpal::SampleRate(48000), 1000.0, Function::Sine) .amplify(0.1) - .take_duration(noise_duration), + .take_duration(test_signal_duration), ) .unwrap(); @@ -26,7 +26,7 @@ fn main() { .play_raw( TestWaveform::new(cpal::SampleRate(48000), 10000.0, Function::Sine) .amplify(0.1) - .take_duration(noise_duration), + .take_duration(test_signal_duration), ) .unwrap(); @@ -37,7 +37,7 @@ fn main() { .play_raw( TestWaveform::new(cpal::SampleRate(48000), 440.0, Function::Triangle) .amplify(0.1) - .take_duration(noise_duration), + .take_duration(test_signal_duration), ) .unwrap(); @@ -48,7 +48,7 @@ fn main() { .play_raw( TestWaveform::new(cpal::SampleRate(48000), 440.0, Function::Sawtooth) .amplify(0.1) - .take_duration(noise_duration), + .take_duration(test_signal_duration), ) .unwrap(); @@ -59,7 +59,7 @@ fn main() { .play_raw( TestWaveform::new(cpal::SampleRate(48000), 440.0, Function::Square) .amplify(0.1) - .take_duration(noise_duration), + .take_duration(test_signal_duration), ) .unwrap(); @@ -75,7 +75,7 @@ fn main() { Duration::from_secs(1), ) .amplify(0.1) - .take_duration(noise_duration), + .take_duration(test_signal_duration), ) .unwrap(); diff --git a/src/source/sine.rs b/src/source/sine.rs index 73db5eba..b9b97caa 100644 --- a/src/source/sine.rs +++ b/src/source/sine.rs @@ -5,8 +5,6 @@ use crate::Source; use super::SeekError; -const SAMPLE_RATE: u32 = 48000; - /// An infinite source that produces a sine. /// /// Always has a rate of 48kHz and one channel. @@ -16,10 +14,12 @@ pub struct SineWave { } impl SineWave { + const SAMPLE_RATE: u32 = 48000; + /// The frequency of the sine. #[inline] pub fn new(freq: f32) -> SineWave { - let sr = cpal::SampleRate(SAMPLE_RATE); + let sr = cpal::SampleRate(Self::SAMPLE_RATE); SineWave { test_sine: TestWaveform::new(sr, freq, Function::Sine), } @@ -48,7 +48,7 @@ impl Source for SineWave { #[inline] fn sample_rate(&self) -> u32 { - SAMPLE_RATE + Self::SAMPLE_RATE } #[inline] diff --git a/src/source/test_waveform.rs b/src/source/test_waveform.rs index ec8db6c2..bcd4799c 100644 --- a/src/source/test_waveform.rs +++ b/src/source/test_waveform.rs @@ -34,19 +34,19 @@ impl Function { /// Create a single sample for the given waveform #[inline] fn render(&self, i: u64, period: f32) -> f32 { - let i_div_p: f32 = i as f32 / period; + let cycle_pos: f32 = i as f32 / period; match self { - Self::Sine => (TAU * i_div_p).sin(), - Self::Triangle => 4.0f32 * (i_div_p - (i_div_p + 0.5f32).floor()).abs() - 1f32, + Self::Sine => (TAU * cycle_pos).sin(), + Self::Triangle => 4.0f32 * (cycle_pos - (cycle_pos + 0.5f32).floor()).abs() - 1f32, Self::Square => { - if i_div_p % 1.0f32 < 0.5f32 { + if cycle_pos % 1.0f32 < 0.5f32 { 1.0f32 } else { -1.0f32 } } - Self::Sawtooth => 2.0f32 * (i_div_p - (i_div_p + 0.5f32).floor()), + Self::Sawtooth => 2.0f32 * (cycle_pos - (cycle_pos + 0.5f32).floor()), } } }