diff --git a/plugins/buffr_glitch/src/buffer.rs b/plugins/buffr_glitch/src/buffer.rs new file mode 100644 index 00000000..65455fbe --- /dev/null +++ b/plugins/buffr_glitch/src/buffer.rs @@ -0,0 +1,54 @@ +// Buffr Glitch: a MIDI-controlled buffer repeater +// Copyright (C) 2022 Robbert van der Helm +// +// This program is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program. If not, see . + +use nih_plug::prelude::util; + +/// A super simple ring buffer abstraction to store the last received audio. This needs to be able +/// to store at least the number of samples that correspond to the period size of MIDI note 0. +#[derive(Debug, Default)] +pub struct RingBuffer { + /// Sample buffers indexed by channel and sample index. + buffers: Vec>, + /// The positions within the sample buffers the next sample should be written to. Since all + /// channels will be written to in lockstep we only need a single value here. It's incremented + /// when writing a sample for the last channel. + next_write_pos: usize, +} + +impl RingBuffer { + /// Initialize or resize the buffers to fit a certain number of channels and samples. The inner + /// buffer capacity is determined by the number of samples it takes to represent the period of + /// MIDI note 0 at the specified sample rate, rounded up to a power of two. Make sure to call + /// [`reset()`][Self::reset()] after this. + pub fn resize(&mut self, num_channels: usize, sample_rate: f32) { + let note_frequency = util::midi_note_to_freq(0); + let note_period_samples = (note_frequency.recip() * sample_rate).ceil() as usize; + let buffer_len = note_period_samples.next_power_of_two(); + + self.buffers.resize_with(num_channels, Vec::new); + for buffer in self.buffers.iter_mut() { + buffer.resize(buffer_len, 0.0); + } + } + + /// Zero out the buffers. + pub fn reset(&mut self) { + for buffer in self.buffers.iter_mut() { + buffer.fill(0.0); + } + self.next_write_pos = 0; + } +} diff --git a/plugins/buffr_glitch/src/lib.rs b/plugins/buffr_glitch/src/lib.rs index c083239f..9e151a4c 100644 --- a/plugins/buffr_glitch/src/lib.rs +++ b/plugins/buffr_glitch/src/lib.rs @@ -17,8 +17,15 @@ use nih_plug::prelude::*; use std::sync::Arc; +mod buffer; + struct BuffrGlitch { params: Arc, + + sample_rate: f32, + /// The ring buffer we'll write samples to. When a key is held down, we'll stop writing samples + /// and instead keep reading from this buffer until the key is released. + buffer: buffer::RingBuffer, } #[derive(Params)] @@ -28,6 +35,9 @@ impl Default for BuffrGlitch { fn default() -> Self { Self { params: Arc::new(BuffrGlitchParams::default()), + + sample_rate: 1.0, + buffer: buffer::RingBuffer::default(), } } } @@ -59,6 +69,25 @@ impl Plugin for BuffrGlitch { config.num_input_channels == config.num_output_channels && config.num_input_channels > 0 } + fn initialize( + &mut self, + bus_config: &BusConfig, + buffer_config: &BufferConfig, + _context: &mut impl InitContext, + ) -> bool { + self.sample_rate = buffer_config.sample_rate; + self.buffer.resize( + bus_config.num_input_channels as usize, + buffer_config.sample_rate, + ); + + true + } + + fn reset(&mut self) { + self.buffer.reset(); + } + fn process( &mut self, _buffer: &mut Buffer,