Skip to content

Commit

Permalink
samp_delay()
Browse files Browse the repository at this point in the history
  • Loading branch information
tomara-x committed Jul 8, 2024
1 parent 2d6bb15 commit 2eda517
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 0 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -771,6 +771,7 @@ other
- `reverb_stereo(float, [float], [float])` (room size, reverberation time, damping) when damping isn't provided it defaults to 1, time defaults to 5
- `tap(float, float)` (min delay time, max delay time)
- `tap_linear(float, float)` (same)
- `samp_delay(float)` argument is max delay time (in samples), node takes 2 inputs (signal, delay time in samples)

math
- `add(float, [float], [float], ...)` (up to 8 params)
Expand Down
5 changes: 5 additions & 0 deletions src/functions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -641,6 +641,11 @@ pub fn str_to_net(op: &str) -> Net {
return Net::wrap(Box::new(tap_linear(min(p0, p1), max(p0, p1))));
}
}
"samp_delay" => {
if let Some(p) = p.first() {
return Net::wrap(Box::new(An(SampDelay::new(*p as usize))));
}
}
// thanks to the pdhalf csound opcode
// https://github.com/csound/csound/blob/master/Opcodes/shape.c#L299
"pdhalf_bi" => {
Expand Down
39 changes: 39 additions & 0 deletions src/nodes.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use crossbeam_channel::Receiver;
use fundsp::fft::*;
use fundsp::hacker32::*;
use std::collections::VecDeque;

/// switch between nets based on index
/// - input 0: index
Expand Down Expand Up @@ -637,3 +638,41 @@ impl AudioNode for Ifft {
self.output.fill(Complex32::ZERO);
}
}

/// variable delay with input time in samples
/// - input 0: signal
/// - input 1: delay time in samples
/// - output 0: delayed signal
#[derive(Clone)]
pub struct SampDelay {
buffer: VecDeque<f32>,
max: usize,
}

impl SampDelay {
pub fn new(max: usize) -> Self {
let mut buffer = VecDeque::new();
buffer.resize(max, 0.);
SampDelay { buffer, max }
}
}

impl AudioNode for SampDelay {
const ID: u64 = 1122;
type Inputs = U2;
type Outputs = U1;

#[inline]
fn tick(&mut self, input: &Frame<f32, Self::Inputs>) -> Frame<f32, Self::Outputs> {
self.buffer.push_front(input[0]);
let _ = self.buffer.pop_back();
let out = self.buffer.get(input[1] as usize).unwrap_or(&0.);
[*out].into()
}

fn reset(&mut self) {
let mut new = VecDeque::new();
new.resize(self.max, 0.);
self.buffer = new;
}
}

0 comments on commit 2eda517

Please sign in to comment.