135 lines
4.5 KiB
Rust
135 lines
4.5 KiB
Rust
#![allow(incomplete_features)]
|
|
#![feature(generic_associated_types)]
|
|
|
|
use baseplug::{event::Data, Event, Plugin, ProcessContext};
|
|
use serde::{Deserialize, Serialize};
|
|
|
|
use utils::pitch::*;
|
|
|
|
const BUFFER_LEN: usize = 2 << 9;
|
|
|
|
baseplug::model! {
|
|
#[derive(Debug, Serialize, Deserialize)]
|
|
struct TransmutePitchModel {
|
|
#[model(min = 0.0, max = 1.0)]
|
|
#[parameter(name = "passthrough")]
|
|
passthrough: f32,
|
|
#[model(min = 0.0, max = 2.0)]
|
|
#[parameter(name = "min note length")]
|
|
min_note_len: f32,
|
|
}
|
|
}
|
|
|
|
impl Default for TransmutePitchModel {
|
|
fn default() -> Self {
|
|
Self {
|
|
passthrough: 0.0,
|
|
min_note_len: 0.0,
|
|
}
|
|
}
|
|
}
|
|
|
|
struct TransmutePitch {
|
|
detector_thread: pitch_detection::PitchDetectorThread<BUFFER_LEN>,
|
|
last_note: Option<u8>,
|
|
last_note_time: Option<usize>,
|
|
counter: usize,
|
|
}
|
|
|
|
impl Plugin for TransmutePitch {
|
|
const NAME: &'static str = "transmute pitch";
|
|
const PRODUCT: &'static str = "transmute pitch";
|
|
const VENDOR: &'static str = "unnieversal";
|
|
|
|
const INPUT_CHANNELS: usize = 1;
|
|
const OUTPUT_CHANNELS: usize = 1;
|
|
|
|
type Model = TransmutePitchModel;
|
|
|
|
#[inline]
|
|
fn new(_sample_rate: f32, _model: &TransmutePitchModel) -> Self {
|
|
let detector_thread = pitch_detection::PitchDetectorThread::<BUFFER_LEN>::new();
|
|
Self {
|
|
detector_thread,
|
|
last_note: None,
|
|
last_note_time: None,
|
|
counter: 0,
|
|
}
|
|
}
|
|
|
|
#[inline]
|
|
fn process(&mut self, model: &TransmutePitchModelProcess, ctx: &mut ProcessContext<Self>) {
|
|
let input = &ctx.inputs[0].buffers;
|
|
let output = &mut ctx.outputs[0].buffers;
|
|
let enqueue_midi = &mut ctx.enqueue_event;
|
|
|
|
let curr_bpm = ctx.musical_time.bpm;
|
|
let beat_in_seconds = 60.0 / curr_bpm;
|
|
let samples_in_beat = beat_in_seconds * ctx.sample_rate as f64;
|
|
|
|
for i in 0..ctx.nframes {
|
|
self.counter += 1;
|
|
|
|
output[0][i] = model.passthrough[i] * input[0][i];
|
|
output[1][i] = model.passthrough[i] * input[1][i];
|
|
|
|
// pass input to pitch detector
|
|
self.detector_thread
|
|
.write(input[0][i], 0.0, ctx.sample_rate as u32);
|
|
|
|
let min_len = model.min_note_len[i] as f64 * samples_in_beat;
|
|
|
|
// Try to get a processed buffer from the processor thread
|
|
match self.detector_thread.try_get_pitch() {
|
|
Some((Some(pitch), _)) => {
|
|
let note = pitch_to_midi_note(pitch);
|
|
|
|
// If note changed
|
|
if self.last_note != Some(note)
|
|
&& min_len < (self.counter - self.last_note_time.unwrap_or(0)) as f64
|
|
{
|
|
// Send note off for last note
|
|
if let Some(last_note) = self.last_note {
|
|
let note_off = Event::<TransmutePitch> {
|
|
frame: i,
|
|
data: Data::Midi([0x80, last_note, 0]),
|
|
};
|
|
enqueue_midi(note_off);
|
|
}
|
|
|
|
// Send note on for the new one
|
|
let note_on = Event::<TransmutePitch> {
|
|
frame: i,
|
|
data: Data::Midi([0x90, note, 64]),
|
|
};
|
|
enqueue_midi(note_on);
|
|
|
|
// Update note
|
|
self.last_note = Some(note);
|
|
self.last_note_time = Some(self.counter);
|
|
}
|
|
}
|
|
Some((None, _)) => {
|
|
if min_len < (self.counter - self.last_note_time.unwrap_or(0)) as f64 {
|
|
if let Some(last_note) = self.last_note {
|
|
let note_off = Event::<TransmutePitch> {
|
|
frame: i,
|
|
data: Data::Midi([0x80, last_note, 0]),
|
|
};
|
|
enqueue_midi(note_off);
|
|
}
|
|
|
|
self.last_note = None;
|
|
self.last_note_time = None;
|
|
// restart the counter so we don't go super far away ig
|
|
self.counter = min_len as usize;
|
|
}
|
|
}
|
|
_ => {}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
baseplug::vst2!(TransmutePitch, b"trpi");
|