From 6941c17f5261173a2f7d09771cde8625bf41257e Mon Sep 17 00:00:00 2001 From: annieversary Date: Sat, 11 Sep 2021 12:44:46 +0200 Subject: [PATCH] [velociter] create and implement crate --- README.md | 11 +++++ crates/velociter/Cargo.toml | 14 +++++++ crates/velociter/src/lib.rs | 84 +++++++++++++++++++++++++++++++++++++ 3 files changed, 109 insertions(+) create mode 100644 crates/velociter/Cargo.toml create mode 100644 crates/velociter/src/lib.rs diff --git a/README.md b/README.md index 435e2c5..b7a3ff8 100644 --- a/README.md +++ b/README.md @@ -65,6 +65,7 @@ the following is the current list of plugins - `transmute_pitch`: pitch to midi converter - `reverter`: play sound backwards - `panera`: pan individual notes differently +- `velociter`: random velocity setter there's a bit of an explanation of each of the plugins below, but it's not a thorough documentation or a manual, it's just a bunch of notes i've written and a short description of the parameters @@ -287,6 +288,16 @@ there's three panning modes: the peak detector is still a bit fiddly, so you'll have to tweak the params a bit until it works for the kind of sound you're giving it. the defaults have worked great for me, so i recommend you start from there and change as you see fit +### velociter + +velociter sets random velocities to midi notes + +params: +- `min_vel`: minimum velocity +- `max_vel`: maximum velocity + +it'll set the velocity of every incoming midi note to be in the range `[min_vel, max_vel)` + ## contributing issues and prs are welcome, but please open an issue before making any big pr, i don't wanna have to reject a pr where you have put a lot of effort on. if you are fine with that, ig go ahead i'm not your mum diff --git a/crates/velociter/Cargo.toml b/crates/velociter/Cargo.toml new file mode 100644 index 0000000..63be850 --- /dev/null +++ b/crates/velociter/Cargo.toml @@ -0,0 +1,14 @@ +[package] +name = "velociter" +version = "0.1.0" +edition = "2018" + +[lib] +crate-type = ["cdylib"] + +[dependencies] +baseplug = { git = "https://github.com/wrl/baseplug.git", rev = "9cec68f31cca9c0c7a1448379f75d92bbbc782a8" } +rand = "0.8.4" +serde = "1.0.126" + +# utils = { path = "../utils" } diff --git a/crates/velociter/src/lib.rs b/crates/velociter/src/lib.rs new file mode 100644 index 0000000..6d63f2f --- /dev/null +++ b/crates/velociter/src/lib.rs @@ -0,0 +1,84 @@ +#![allow(incomplete_features)] +#![feature(generic_associated_types)] + +use baseplug::{event::Data, Event, MidiReceiver, Plugin, ProcessContext}; +use serde::{Deserialize, Serialize}; + +baseplug::model! { + #[derive(Debug, Serialize, Deserialize)] + struct VelociterModel { + #[model(min = 0.0, max = 127.9)] + #[parameter(name = "min_vel")] + min_vel: f32, + #[model(min = 0.0, max = 127.9)] + #[parameter(name = "max_vel")] + max_vel: f32, + } +} + +impl Default for VelociterModel { + fn default() -> Self { + Self { + min_vel: 0.0, + max_vel: 127.9, + } + } +} + +struct Velociter { + notes: Vec<[u8; 3]>, +} + +impl Plugin for Velociter { + const NAME: &'static str = "velociter"; + const PRODUCT: &'static str = "velociter"; + const VENDOR: &'static str = "unnieversal"; + + const INPUT_CHANNELS: usize = 2; + const OUTPUT_CHANNELS: usize = 2; + + type Model = VelociterModel; + + #[inline] + fn new(_sample_rate: f32, _model: &VelociterModel) -> Self { + Self { + notes: Vec::with_capacity(300), + } + } + + #[inline] + fn process(&mut self, model: &VelociterModelProcess, ctx: &mut ProcessContext) { + let input = &ctx.inputs[0].buffers; + let output = &mut ctx.outputs[0].buffers; + let enqueue_midi = &mut ctx.enqueue_event; + + use rand::{thread_rng, Rng}; + let mut rng = thread_rng(); + + for i in 0..ctx.nframes { + output[0][i] = input[0][i]; + output[1][i] = input[1][i]; + + let min = model.min_vel[i]; + let max = model.max_vel[i]; + // make sure they're right + let (min, max) = (min.min(max), max.max(min)); + + for mut note in self.notes.drain(0..) { + note[2] = rng.gen_range(min..max).trunc() as u8; + let note = Event:: { + frame: 0, + data: Data::Midi(note), + }; + enqueue_midi(note); + } + } + } +} +impl MidiReceiver for Velociter { + fn midi_input(&mut self, _model: &VelociterModelProcess, data: [u8; 3]) { + self.notes.push(data); + } +} + +baseplug::vst2!(Velociter, b"tAnE");