78 lines
2.1 KiB
Rust
78 lines
2.1 KiB
Rust
#![allow(incomplete_features)]
|
|
#![feature(generic_associated_types)]
|
|
|
|
use baseplug::{Plugin, ProcessContext};
|
|
use serde::{Deserialize, Serialize};
|
|
|
|
use utils::buffers::*;
|
|
|
|
// If you change this remember to change the max on the model
|
|
const LEN: usize = 48000;
|
|
|
|
baseplug::model! {
|
|
#[derive(Debug, Serialize, Deserialize)]
|
|
struct ReverterModel {
|
|
#[model(min = 10.0, max = 48000.0)]
|
|
#[parameter(name = "length")]
|
|
length: f32,
|
|
}
|
|
}
|
|
|
|
impl Default for ReverterModel {
|
|
fn default() -> Self {
|
|
Self { length: 1.0 }
|
|
}
|
|
}
|
|
|
|
struct Reverter {
|
|
recording: Buffers<LEN>,
|
|
playing: Buffers<LEN>,
|
|
}
|
|
|
|
impl Plugin for Reverter {
|
|
const NAME: &'static str = "reverter";
|
|
const PRODUCT: &'static str = "reverter";
|
|
const VENDOR: &'static str = "unnieversal";
|
|
|
|
const INPUT_CHANNELS: usize = 2;
|
|
const OUTPUT_CHANNELS: usize = 2;
|
|
|
|
type Model = ReverterModel;
|
|
|
|
#[inline]
|
|
fn new(_sample_rate: f32, _model: &ReverterModel) -> Self {
|
|
Self {
|
|
recording: Buffers::<LEN>::new(),
|
|
playing: Buffers::<LEN>::new(),
|
|
}
|
|
}
|
|
|
|
#[inline]
|
|
fn process(&mut self, model: &ReverterModelProcess, ctx: &mut ProcessContext<Self>) {
|
|
let input = &ctx.inputs[0].buffers;
|
|
let output = &mut ctx.outputs[0].buffers;
|
|
|
|
for i in 0..ctx.nframes {
|
|
let len = model.length[i].floor() as usize;
|
|
|
|
// add to recording buffer
|
|
let finished = self.recording.write_advance(input[0][i], input[1][i]);
|
|
|
|
// we read from the playing buffer at the inverse of the recording buffer
|
|
// this ensures the two don't go out of sync
|
|
let (l, r) = self.playing.read_at(len - self.recording.idx);
|
|
|
|
// if we're done, swap the two buffers and reset the index
|
|
if finished || self.recording.idx >= len {
|
|
self.recording.reset();
|
|
std::mem::swap(&mut self.recording, &mut self.playing);
|
|
}
|
|
|
|
output[0][i] = l;
|
|
output[1][i] = r;
|
|
}
|
|
}
|
|
}
|
|
|
|
baseplug::vst2!(Reverter, b"reve");
|