1
0
Fork 0

Add a basic amp envelope to Buffr Glitch

This commit is contained in:
Robbert van der Helm 2023-01-17 01:53:38 +01:00
parent 22b3b9527b
commit bc98463b28
3 changed files with 139 additions and 11 deletions

View file

@ -12,6 +12,8 @@ Versioning](https://semver.org/spec/v2.0.0.html).
- Added an option to crossfade the recorded buffer to avoid clicks when looping - Added an option to crossfade the recorded buffer to avoid clicks when looping
around. around.
- Added attack and release time controls to avoid clicks when pressing or
releasing a key.
- Added an optional velocity sensitive mode. - Added an optional velocity sensitive mode.
### Removed ### Removed

View file

@ -0,0 +1,85 @@
// Buffr Glitch: a MIDI-controlled buffer repeater
// Copyright (C) 2022-2023 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 <https://www.gnu.org/licenses/>.
/// The most barebones envelope generator you can imagine using a bog standard first order IIR
/// filter. We don't need anything fancy right now.
#[derive(Debug, Default)]
pub struct AREnvelope {
/// The internal filter state.
state: f32,
/// For each sample, the output becomes `(state * t) + (target * (1.0 - t))`. This is `t` during
/// the attack portion of the envelope generator.
attack_retain_t: f32,
/// `attack_retain_t`, but for the release portion.
release_retain_t: f32,
/// The value the envelope follower should try to achieve when not in the release stage.
target_value: f32,
/// Whether the envelope follower is currently in its release stage.
releasing: bool,
}
impl AREnvelope {
pub fn set_attack_time(&mut self, sample_rate: f32, time_ms: f32) {
self.attack_retain_t = (-1.0 / (time_ms / 1000.0 * sample_rate)).exp();
}
pub fn set_release_time(&mut self, sample_rate: f32, time_ms: f32) {
self.release_retain_t = (-1.0 / (time_ms / 1000.0 * sample_rate)).exp();
}
/// Completely reset the envelope follower.
pub fn reset(&mut self) {
self.state = 0.0;
self.releasing = false;
}
/// Only reset the release state, but don't reset the internal filter state.
pub fn soft_reset(&mut self) {
self.releasing = false;
}
/// Set the maximum value the envelope follower should achieve.
pub fn set_target(&mut self, target: f32) {
self.target_value = target;
}
/// Get the next value from the envelope follower.
pub fn next(&mut self) -> f32 {
let (target, t) = if self.releasing {
(0.0, self.release_retain_t)
} else {
(self.target_value, self.attack_retain_t)
};
let new = (self.state * t) + (target * (1.0 - t));
self.state = new;
new
}
/// Start the release segment of the envelope generator.
pub fn start_release(&mut self) {
self.releasing = true;
}
/// Whether the envelope generator is still in its release stage and the value hasn't dropped
/// down to 0.0 yet.
pub fn is_releasing(&self) -> bool {
self.releasing && self.state >= 0.001
}
}

View file

@ -18,6 +18,7 @@ use nih_plug::prelude::*;
use std::sync::Arc; use std::sync::Arc;
mod buffer; mod buffer;
mod envelope;
/// The maximum number of octaves the sample can be pitched down. This is used in calculating the /// The maximum number of octaves the sample can be pitched down. This is used in calculating the
/// recording buffer's size. /// recording buffer's size.
@ -37,8 +38,9 @@ struct BuffrGlitch {
/// The gain scaling from the velocity. If velocity sensitive mode is enabled, then this is the `[0, 1]` velocity /// The gain scaling from the velocity. If velocity sensitive mode is enabled, then this is the `[0, 1]` velocity
/// devided by `100/127` such that MIDI velocity 100 corresponds to 1.0 gain. /// devided by `100/127` such that MIDI velocity 100 corresponds to 1.0 gain.
velocity_gain: f32, velocity_gain: f32,
/// The gain from the gain note expression. /// The envelope genrator used during playback. This handles both gain smoothing as well as fade
gain_expression_gain: f32, /// ins and outs to prevent clicks.
amp_envelope: envelope::AREnvelope,
} }
#[derive(Params)] #[derive(Params)]
@ -55,6 +57,14 @@ struct BuffrGlitchParams {
#[id = "octave_shift"] #[id = "octave_shift"]
octave_shift: IntParam, octave_shift: IntParam,
/// The attack time in milliseconds. Useful to avoid clicks. Or to introduce them if that's
/// aesthetically pleasing.
#[id = "attack_ms"]
attack_ms: FloatParam,
/// The attack time in milliseconds. Useful to avoid clicks. Or to introduce them if that's
/// aesthetically pleasing.
#[id = "release_ms"]
release_ms: FloatParam,
/// The length of the loop crossfade to use, in milliseconds. This will cause the start of the /// The length of the loop crossfade to use, in milliseconds. This will cause the start of the
/// loop to be faded into the last `(crossfade_ms/2)` ms of the loop region, and the part after /// loop to be faded into the last `(crossfade_ms/2)` ms of the loop region, and the part after
/// the end to be faded into the first `(crossfade_ms/2)` ms of the loop after the first /// the end to be faded into the first `(crossfade_ms/2)` ms of the loop after the first
@ -73,7 +83,7 @@ impl Default for BuffrGlitch {
midi_note_id: None, midi_note_id: None,
velocity_gain: 1.0, velocity_gain: 1.0,
gain_expression_gain: 1.0, amp_envelope: envelope::AREnvelope::default(),
} }
} }
} }
@ -103,9 +113,32 @@ impl Default for BuffrGlitchParams {
max: MAX_OCTAVE_SHIFT as i32, max: MAX_OCTAVE_SHIFT as i32,
}, },
), ),
attack_ms: FloatParam::new(
"Attack",
2.0,
FloatRange::Skewed {
min: 0.0,
max: 50.0,
factor: FloatRange::skew_factor(-2.5),
},
)
.with_unit(" ms")
.with_step_size(0.001),
release_ms: FloatParam::new(
"Release",
2.0,
FloatRange::Skewed {
min: 0.0,
max: 50.0,
factor: FloatRange::skew_factor(-2.5),
},
)
.with_unit(" ms")
.with_step_size(0.001),
crossfade_ms: FloatParam::new( crossfade_ms: FloatParam::new(
"Crossfade", "Crossfade",
0.0, 2.0,
FloatRange::Skewed { FloatRange::Skewed {
min: 0.0, min: 0.0,
max: 50.0, max: 50.0,
@ -160,6 +193,7 @@ impl Plugin for BuffrGlitch {
fn reset(&mut self) { fn reset(&mut self) {
self.buffer.reset(); self.buffer.reset();
self.midi_note_id = None; self.midi_note_id = None;
self.amp_envelope.reset();
} }
fn process( fn process(
@ -190,7 +224,9 @@ impl Plugin for BuffrGlitch {
} else { } else {
1.0 1.0
}; };
self.gain_expression_gain = 1.0;
self.amp_envelope.soft_reset();
self.amp_envelope.set_target(self.velocity_gain);
// We'll copy audio to the playback buffer to match the pitch of the note // We'll copy audio to the playback buffer to match the pitch of the note
// that was just played. The octave shift parameter makes it possible to get // that was just played. The octave shift parameter makes it possible to get
@ -201,11 +237,12 @@ impl Plugin for BuffrGlitch {
.prepare_playback(note_frequency, self.params.crossfade_ms.value()); .prepare_playback(note_frequency, self.params.crossfade_ms.value());
} }
NoteEvent::NoteOff { note, .. } if self.midi_note_id == Some(note) => { NoteEvent::NoteOff { note, .. } if self.midi_note_id == Some(note) => {
// A NoteOff for the currently playing note immediately ends playback // Playback still continues until the release is done.
self.amp_envelope.start_release();
self.midi_note_id = None; self.midi_note_id = None;
} }
NoteEvent::PolyVolume { note, gain, .. } if self.midi_note_id == Some(note) => { NoteEvent::PolyVolume { note, gain, .. } if self.midi_note_id == Some(note) => {
self.gain_expression_gain = gain; self.amp_envelope.set_target(self.velocity_gain * gain);
} }
_ => (), _ => (),
} }
@ -216,10 +253,14 @@ impl Plugin for BuffrGlitch {
// When a note is being held, we'll replace the input audio with the looping contents of // When a note is being held, we'll replace the input audio with the looping contents of
// the playback buffer // the playback buffer
// TODO: At some point also handle polyphony here // TODO: At some point also handle polyphony here
if self.midi_note_id.is_some() { if self.midi_note_id.is_some() || self.amp_envelope.is_releasing() {
// TOOD: This needs to be smoothed, but we this should be part of a proper gain self.amp_envelope
// envelope .set_attack_time(self.sample_rate, self.params.attack_ms.value());
let gain = self.velocity_gain * self.gain_expression_gain; self.amp_envelope
.set_release_time(self.sample_rate, self.params.release_ms.value());
// FIXME: This should fade in and out from the dry buffer
let gain = self.amp_envelope.next();
for (channel_idx, sample) in channel_samples.into_iter().enumerate() { for (channel_idx, sample) in channel_samples.into_iter().enumerate() {
// This will start recording on the first iteration, and then loop the recorded // This will start recording on the first iteration, and then loop the recorded
// buffer afterwards // buffer afterwards