summaryrefslogtreecommitdiff
path: root/src/synth/sine.rs
blob: 3ee4857c9c95467527435d13a1689168f885e7f8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
use std::f32::consts::PI;
use super::*;

const TAU: f32 = 2f32 * PI;

pub struct Sine {
    freq: GenBox,
    phase: f32,
    buf: SampleBuffer,
}

impl Generator for Sine {
    fn eval<'a>(&'a mut self, params: &Parameters) -> &'a SampleBuffer {
        self.buf.rate = Rate::Control;

        let pvel = TAU * self.freq.eval(params).first() / params.env.sample_rate;
        for i in 0..self.buf.len() {
            self.buf[i] = (self.phase + pvel * (i as f32)).sin()
        }

        self.phase = (self.phase + pvel * (self.buf.len() as f32)) % TAU;
        &self.buf
    }
}