Skip to content

Commit

Permalink
Fix Sine distortion caused by f32 roundoff errors
Browse files Browse the repository at this point in the history
  • Loading branch information
mlindner committed Nov 16, 2018
1 parent a55cac7 commit f1a1c5b
Showing 1 changed file with 9 additions and 6 deletions.
15 changes: 9 additions & 6 deletions src/source/sine.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
use std::time::Duration;
use Source;

const PI_2: f32 = std::f32::consts::PI * 2.0f32;

/// An infinite source that produces a sine.
///
/// Always has a rate of 48kHz and one channel.
#[derive(Clone, Debug)]
pub struct SineWave {
freq: f32,
num_sample: usize,
cur_val: f32,
}

impl SineWave {
Expand All @@ -16,7 +18,7 @@ impl SineWave {
pub fn new(freq: u32) -> SineWave {
SineWave {
freq: freq as f32,
num_sample: 0,
cur_val: 0.0,
}
}
}
Expand All @@ -26,10 +28,11 @@ impl Iterator for SineWave {

#[inline]
fn next(&mut self) -> Option<f32> {
self.num_sample = self.num_sample.wrapping_add(1);

let value = 2.0 * 3.14159265 * self.freq * self.num_sample as f32 / 48000.0;
Some(value.sin())
self.cur_val += PI_2 * self.freq / 48000.0;
if self.cur_val > PI_2 {
self.cur_val -= PI_2;
}
Some(self.cur_val.sin())
}
}

Expand Down

0 comments on commit f1a1c5b

Please sign in to comment.