[utils, sosten] move delay to utils lib
parent
fdbb43895f
commit
66772aca19
|
@ -9,3 +9,5 @@ crate-type = ["cdylib"]
|
||||||
[dependencies]
|
[dependencies]
|
||||||
baseplug = { git = "https://github.com/wrl/baseplug.git", rev = "9cec68f31cca9c0c7a1448379f75d92bbbc782a8" }
|
baseplug = { git = "https://github.com/wrl/baseplug.git", rev = "9cec68f31cca9c0c7a1448379f75d92bbbc782a8" }
|
||||||
serde = "1.0.126"
|
serde = "1.0.126"
|
||||||
|
|
||||||
|
utils = { path = "../utils" }
|
||||||
|
|
|
@ -1,30 +0,0 @@
|
||||||
pub struct DelayLine<const LEN: usize> {
|
|
||||||
buffer: [f32; LEN],
|
|
||||||
index: usize,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl<const LEN: usize> DelayLine<LEN> {
|
|
||||||
pub fn new() -> Self {
|
|
||||||
Self {
|
|
||||||
buffer: [0.0; LEN],
|
|
||||||
index: 0,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn read_slice(&self, slice: &mut [f32]) {
|
|
||||||
// Copy values in order
|
|
||||||
for i in 0..LEN {
|
|
||||||
slice[i] = self.buffer[(self.index + i - LEN) % LEN];
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn write_and_advance(&mut self, value: f32) {
|
|
||||||
self.buffer[self.index] = value;
|
|
||||||
|
|
||||||
if self.index == LEN - 1 {
|
|
||||||
self.index = 0;
|
|
||||||
} else {
|
|
||||||
self.index += 1;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -4,8 +4,7 @@
|
||||||
use baseplug::{Plugin, ProcessContext};
|
use baseplug::{Plugin, ProcessContext};
|
||||||
use serde::{Deserialize, Serialize};
|
use serde::{Deserialize, Serialize};
|
||||||
|
|
||||||
mod delay;
|
use utils::delay::*;
|
||||||
use delay::DelayLine;
|
|
||||||
|
|
||||||
// If you change this remember to change the max on the model
|
// If you change this remember to change the max on the model
|
||||||
const LEN: usize = 48000;
|
const LEN: usize = 48000;
|
||||||
|
|
|
@ -0,0 +1,78 @@
|
||||||
|
pub struct DelayLine<const LEN: usize> {
|
||||||
|
buffer: [f32; LEN],
|
||||||
|
index: usize,
|
||||||
|
}
|
||||||
|
impl<const LEN: usize> DelayLine<LEN> {
|
||||||
|
pub fn new() -> Self {
|
||||||
|
Self {
|
||||||
|
buffer: [0.0; LEN],
|
||||||
|
index: 0,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn read_slice(&self, slice: &mut [f32]) {
|
||||||
|
// Copy values in order
|
||||||
|
for i in 0..LEN {
|
||||||
|
slice[i] = self.wrapped_index(self.index + i);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn write_and_advance(&mut self, value: f32) {
|
||||||
|
self.buffer[self.index] = value;
|
||||||
|
|
||||||
|
if self.index == LEN - 1 {
|
||||||
|
self.index = 0;
|
||||||
|
} else {
|
||||||
|
self.index += 1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Returns the sample at idx after taking modulo LEN
|
||||||
|
pub fn wrapped_index(&self, idx: usize) -> f32 {
|
||||||
|
self.buffer[idx % LEN]
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Indexes the buffer but interpolates between the current and the next sample
|
||||||
|
pub fn floating_index(&self, val: f32) -> f32 {
|
||||||
|
let idx = val.trunc() as usize;
|
||||||
|
let a = val.fract();
|
||||||
|
|
||||||
|
let one = self.wrapped_index(idx);
|
||||||
|
let two = self.wrapped_index(idx + 1);
|
||||||
|
|
||||||
|
(1.0 - a) * one + a * two
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Get a reference to the delay line's index.
|
||||||
|
pub fn idx(&self) -> &usize {
|
||||||
|
&self.index
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Get a reference to the delay line's buffer.
|
||||||
|
pub fn buffer(&self) -> &[f32; LEN] {
|
||||||
|
&self.buffer
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub struct DelayLines<const LEN: usize> {
|
||||||
|
pub l: DelayLine<LEN>,
|
||||||
|
pub r: DelayLine<LEN>,
|
||||||
|
}
|
||||||
|
impl<const LEN: usize> DelayLines<LEN> {
|
||||||
|
pub fn new() -> Self {
|
||||||
|
Self {
|
||||||
|
l: DelayLine::<LEN>::new(),
|
||||||
|
r: DelayLine::<LEN>::new(),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn read_slices(&self, l: &mut [f32], r: &mut [f32]) {
|
||||||
|
self.l.read_slice(l);
|
||||||
|
self.r.read_slice(r);
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn write_and_advance(&mut self, l: f32, r: f32) {
|
||||||
|
self.l.write_and_advance(l);
|
||||||
|
self.r.write_and_advance(r);
|
||||||
|
}
|
||||||
|
}
|
|
@ -1,4 +1,5 @@
|
||||||
pub mod buffers;
|
pub mod buffers;
|
||||||
|
pub mod delay;
|
||||||
pub mod logs;
|
pub mod logs;
|
||||||
pub mod pitch;
|
pub mod pitch;
|
||||||
pub mod threeband;
|
pub mod threeband;
|
||||||
|
|
Loading…
Reference in New Issue