Skip to content

Instantly share code, notes, and snippets.

@tomara-x
Created September 5, 2024 11:09
Show Gist options
  • Save tomara-x/1f9c7bc04c4a1843c420c06312643196 to your computer and use it in GitHub Desktop.
Save tomara-x/1f9c7bc04c4a1843c420c06312643196 to your computer and use it in GitHub Desktop.
/// phasor (ramp from 0..1)
/// - input 0: frequency
/// - output 0: ramp output
#[derive(Default, Clone)]
pub struct Phasor {
val: f32,
sr: f32,
}
impl Phasor {
pub fn new() -> Self {
Phasor { val: 0., sr: DEFAULT_SR as f32 }
}
}
impl AudioNode for Phasor {
const ID: u64 = 1116;
type Inputs = U1;
type Outputs = U1;
#[inline]
fn tick(&mut self, input: &Frame<f32, Self::Inputs>) -> Frame<f32, Self::Outputs> {
let buffer = [self.val];
self.val += input[0] / self.sr;
if self.val >= 1. {
self.val -= 1.;
} else if self.val < 0. {
self.val += 1.;
}
buffer.into()
}
fn reset(&mut self) {
self.val = 0.;
}
fn set_sample_rate(&mut self, sample_rate: f64) {
self.sr = sample_rate as f32;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment