37 lines
771 B
Rust
37 lines
771 B
Rust
|
|
#[derive(Clone, Debug)]
|
|
pub enum Waveform {
|
|
Triangle,
|
|
Sawtooth,
|
|
Square,
|
|
Sine,
|
|
}
|
|
|
|
impl Waveform {
|
|
pub fn get_doub(&self, x: f32) -> f32 {
|
|
self.get_norm(x) * 2.0 - 1.0
|
|
}
|
|
|
|
pub fn get_norm(&self, x: f32) -> f32 {
|
|
match self {
|
|
Waveform::Sine => 0.5 + 0.5*(x * 2.0*3.1415926).sin(),
|
|
Waveform::Square =>
|
|
if x % 1.0 < 0.5 {
|
|
0.0
|
|
} else {
|
|
1.0
|
|
}
|
|
Waveform::Sawtooth => {
|
|
x % 1.0
|
|
}
|
|
Waveform::Triangle => {
|
|
if (x%1.0) > 0.5 {
|
|
1.0-(x%1.0)
|
|
} else {
|
|
x%1.0
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|