refactor: run Lathe, Temper and Knurl as tracked modules
Add a Tracked trait and a Lookahead harness to common-dsp. Each engine is now its tracker plus a module that follows note events aligned with its input, so the same module can run behind a shared tracker. Lathe and Temper output is bit-exact against the old engines. Knurl's interpolation now reads one newer sample inside the module, so its latency is one sample longer; the output matches within float rounding. Each editor exposes body() and toggles() besides show(), so another editor can draw a module's page.
This commit is contained in:
1 parent
4b1a89626e
commit
340376eee2
8 files changed
+418
-163
No files matched your search
@@ -397,3 +397,55 @@ impl PitchFollower {
|
||||
self.hz
|
||||
}
|
||||
}
|
||||
|
||||
/// A processor driven by note events that are aligned with its input audio. Lathe, Temper and
|
||||
/// Knurl are built from one each, so the same code runs standalone (after its own tracker) and
|
||||
/// inside a strip that shares one tracker between several of them.
|
||||
pub trait Tracked {
|
||||
/// Delay of the output behind the input, in samples.
|
||||
fn latency(&self) -> usize;
|
||||
/// The undelayed input, one tracker lookahead ahead of the audio passed to `process`.
|
||||
/// Called before the sample's events.
|
||||
fn ahead(&mut self, _mono: f32) {}
|
||||
/// A note event due at the next `process` sample.
|
||||
fn event(&mut self, e: NoteEvent);
|
||||
/// One sample of audio; `sounding` is the tracker's note state at this sample.
|
||||
fn process(&mut self, x: [f32; 2], sounding: bool) -> [f32; 2];
|
||||
}
|
||||
|
||||
/// A tracker and the lookahead delay in front of one [`Tracked`] module: the standalone form.
|
||||
pub struct Lookahead {
|
||||
pub tracker: Tracker,
|
||||
delay: Vec<[f32; 2]>,
|
||||
at: usize,
|
||||
}
|
||||
impl Lookahead {
|
||||
pub fn new(rate: f32, cfg: TrackerConfig) -> Self {
|
||||
let tracker = Tracker::new(rate, cfg);
|
||||
let len = tracker.latency() + 1;
|
||||
Self {
|
||||
tracker,
|
||||
delay: vec![[0.; 2]; len],
|
||||
at: 0,
|
||||
}
|
||||
}
|
||||
/// The tracker's lookahead, in samples.
|
||||
pub fn latency(&self) -> usize {
|
||||
self.tracker.latency()
|
||||
}
|
||||
pub fn reset(&mut self) {
|
||||
self.tracker.reset();
|
||||
self.delay.fill([0.; 2]);
|
||||
}
|
||||
/// Feeds one input sample (`mono` is its channel average) and returns `module`'s output.
|
||||
#[inline]
|
||||
pub fn process(&mut self, x: [f32; 2], mono: f32, module: &mut impl Tracked) -> [f32; 2] {
|
||||
module.ahead(mono);
|
||||
self.tracker.process(mono, |e| module.event(e));
|
||||
let len = self.delay.len();
|
||||
self.delay[self.at] = x;
|
||||
self.at = (self.at + 1) % len;
|
||||
let y = self.delay[self.at];
|
||||
module.process(y, self.tracker.sounding())
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user