Skip to content

Commit

Permalink
sample and hold node
Browse files Browse the repository at this point in the history
  • Loading branch information
tomara-x committed Aug 9, 2024
1 parent 318218a commit f925233
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 0 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -769,6 +769,7 @@ envelopes (all subsampled at ~2 ms)
other
- `tick()` one sample delay
- `shift_reg()` 2 ins (input signal, trigger signal), 8 outs (outputs of the shift register)
- `snh()` sample and hold node. 2 ins (input signal, trigger signal), 1 out (held signal)
- `meter(peak/rms, float)` e.g. `meter(rms, 0.5)` `rms(peak, 2)`
- `chorus(float, float, float, float)` (seed, separation, variation, mod frequency)
- `declick([float])` e.g. `declick()` 10ms fade in, `declick(2)` 2 second fade in
Expand Down
1 change: 1 addition & 0 deletions src/functions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -575,6 +575,7 @@ pub fn str_to_net(op: &str) -> Net {
// -------------------- other --------------------
"tick" => return Net::wrap(Box::new(tick())),
"shift_reg" => return Net::wrap(Box::new(An(ShiftReg::new()))),
"snh" => return Net::wrap(Box::new(An(SnH::new()))),
"meter" => {
if let (Some(arg), Some(p)) = (args.get(1), p.first()) {
if arg.starts_with("peak") {
Expand Down
33 changes: 33 additions & 0 deletions src/nodes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -776,3 +776,36 @@ impl AudioNode for BuffOut {
[self.r.try_recv().unwrap_or(0.)].into()
}
}

/// sample and hold
/// - input 0: input signal
/// - input 1: trigger
/// - output 0: held signal
#[derive(Clone)]
pub struct SnH {
val: f32,
}

impl SnH {
pub fn new() -> Self {
SnH { val: 0. }
}
}

impl AudioNode for SnH {
const ID: u64 = 1125;
type Inputs = U2;
type Outputs = U1;

#[inline]
fn tick(&mut self, input: &Frame<f32, Self::Inputs>) -> Frame<f32, Self::Outputs> {
if input[1] != 0. {
self.val = input[0];
}
[self.val].into()
}

fn reset(&mut self) {
self.val = 0.;
}
}

0 comments on commit f925233

Please sign in to comment.