Skip to content

Commit

Permalink
Code review changes
Browse files Browse the repository at this point in the history
  • Loading branch information
iluvcapra committed Sep 29, 2024
1 parent 9d20ea2 commit 44cb217
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 20 deletions.
6 changes: 3 additions & 3 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
16 changes: 8 additions & 8 deletions examples/signal_generator.rs
Original file line number Diff line number Diff line change
@@ -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};
Expand All @@ -7,15 +7,15 @@ 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");
stream_handle
.play_raw(
TestWaveform::new(cpal::SampleRate(48000), 1000.0, Function::Sine)
.amplify(0.1)
.take_duration(noise_duration),
.take_duration(test_signal_duration),
)
.unwrap();

Expand All @@ -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();

Expand All @@ -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();

Expand All @@ -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();

Expand All @@ -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();

Expand All @@ -75,7 +75,7 @@ fn main() {
Duration::from_secs(1),
)
.amplify(0.1)
.take_duration(noise_duration),
.take_duration(test_signal_duration),
)
.unwrap();

Expand Down
8 changes: 4 additions & 4 deletions src/source/sine.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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),
}
Expand Down Expand Up @@ -48,7 +48,7 @@ impl Source for SineWave {

#[inline]
fn sample_rate(&self) -> u32 {
SAMPLE_RATE
Self::SAMPLE_RATE
}

#[inline]
Expand Down
10 changes: 5 additions & 5 deletions src/source/test_waveform.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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()),
}
}
}
Expand Down

0 comments on commit 44cb217

Please sign in to comment.