Skip to content

Commit 5ed5ad3

Browse files
committed
apply delay only for notes above duration threshold
1 parent 452713b commit 5ed5ad3

File tree

3 files changed

+38
-23
lines changed

3 files changed

+38
-23
lines changed

note_off_delay/src/lib.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ use util::delayed_message_consumer::{process_scheduled_events, MessageReason};
2020
use util::messages::format_event;
2121
use util::midi_message_type::MidiMessageType;
2222
use util::parameters::ParameterConversion;
23+
use crate::parameters::PARAMETER_COUNT;
2324

2425
plugin_main!(NoteOffDelayPlugin);
2526

@@ -90,7 +91,7 @@ impl Plugin for NoteOffDelayPlugin {
9091
name: "Note Off Delay".to_string(),
9192
vendor: "DJ Crontab".to_string(),
9293
unique_id: 234213173,
93-
parameters: 4,
94+
parameters: PARAMETER_COUNT as i32,
9495
category: Category::Effect,
9596
initial_delay: 0,
9697
version: 1,
@@ -188,7 +189,7 @@ impl Plugin for NoteOffDelayPlugin {
188189
None => {}
189190
Some(note_on) => {
190191
let duration = note_off_play_time - note_on.play_time_in_samples;
191-
match self.parameters.get_delay().apply(duration, self.sample_rate) {
192+
match delay.apply(duration, self.sample_rate) {
192193
None => panic!("delay is supposed to be active"),
193194
Some(new_duration) => {
194195
// send two times the note off, the live one will be only used to mark the note on as delayed

note_off_delay/src/parameters.rs

Lines changed: 27 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,12 @@ use vst::util::ParameterTransfer;
55

66
use util::parameter_value_conversion::{f32_to_bool, f32_to_byte};
77
use util::parameters::{ParameterConversion, get_exponential_scale_value};
8-
use util::{HostCallbackLock, DelayOffset};
8+
use util::{HostCallbackLock, Duration};
99
use util::delayed_message_consumer::MaxNotesParameter;
1010
use std::fmt::{Display, Formatter};
1111
use std::fmt;
1212

13-
const PARAMETER_COUNT: usize = 4;
13+
pub const PARAMETER_COUNT: usize = 5;
1414

1515
pub struct NoteOffDelayPluginParameters {
1616
pub host_mutex: Mutex<HostCallbackLock>,
@@ -23,6 +23,7 @@ pub enum Parameter {
2323
MaxNotes,
2424
MaxNotesAppliesToDelayedNotesOnly,
2525
MultiplyLength,
26+
Threshold
2627
}
2728

2829
impl From<i32> for Parameter {
@@ -32,6 +33,7 @@ impl From<i32> for Parameter {
3233
1 => Parameter::MaxNotes,
3334
2 => Parameter::MaxNotesAppliesToDelayedNotesOnly,
3435
3 => Parameter::MultiplyLength,
36+
4 => Parameter::Threshold,
3537
_ => panic!("no such parameter {}", i),
3638
}
3739
}
@@ -71,8 +73,9 @@ impl NoteOffDelayPluginParameters {
7173

7274
pub fn get_delay(&self) -> Delay {
7375
Delay {
74-
offset: DelayOffset::from(self.get_parameter(Parameter::DelayOffset.into())),
75-
multiplier: DelayMultiplier::from(self.get_parameter(Parameter::MultiplyLength.into()))
76+
offset: Duration::from(self.get_parameter(Parameter::DelayOffset.into())),
77+
multiplier: DelayMultiplier::from(self.get_parameter(Parameter::MultiplyLength.into())),
78+
threshold: Duration::from(self.get_parameter(Parameter::Threshold.into())),
7679
}
7780
}
7881
}
@@ -88,8 +91,9 @@ impl Default for NoteOffDelayPluginParameters {
8891

8992

9093
pub struct Delay {
91-
pub offset: DelayOffset,
94+
pub offset: Duration,
9295
pub multiplier: DelayMultiplier,
96+
pub threshold: Duration
9397
}
9498

9599

@@ -100,19 +104,26 @@ pub enum DelayMultiplier {
100104

101105
impl Delay {
102106
pub fn is_active(&self) -> bool {
103-
!matches!((&self.offset, &self.multiplier), (DelayOffset::Off, DelayMultiplier::Off))
107+
!matches!((&self.offset, &self.multiplier), (Duration::Off, DelayMultiplier::Off))
104108
}
105109

106110
pub fn apply(&self, duration_in_samples: usize, sample_rate: f32) -> Option<usize> {
107111
if self.is_active() {
112+
if let Duration::Duration(threshold) = self.threshold {
113+
let threshold_in_samples = (threshold * sample_rate) as usize;
114+
if duration_in_samples < threshold_in_samples {
115+
return Some(duration_in_samples)
116+
}
117+
}
118+
108119
let duration_in_samples = match self.multiplier {
109120
DelayMultiplier::Off => duration_in_samples as f32,
110121
DelayMultiplier::Multiplier(x) => x * duration_in_samples as f32
111122
};
112123

113124
Some(match self.offset {
114-
DelayOffset::Off => duration_in_samples as usize,
115-
DelayOffset::Duration(x) => (duration_in_samples + x * sample_rate) as usize
125+
Duration::Off => duration_in_samples as usize,
126+
Duration::Duration(x) => (duration_in_samples + x * sample_rate) as usize
116127
})
117128
} else {
118129
None
@@ -145,13 +156,14 @@ impl From<f32> for DelayMultiplier {
145156

146157
impl vst::plugin::PluginParameters for NoteOffDelayPluginParameters {
147158
fn get_parameter_text(&self, index: i32) -> String {
159+
let value = self.get_parameter(index);
148160
match index.into() {
149-
Parameter::DelayOffset => {
150-
DelayOffset::from(self.get_parameter(Parameter::DelayOffset as i32)).to_string()
161+
Parameter::DelayOffset | Parameter::Threshold => {
162+
Duration::from(value).to_string()
151163
}
152164

153165
Parameter::MaxNotes => {
154-
if self.get_parameter(Parameter::MaxNotes as i32) == 0.0 {
166+
if value == 0.0 {
155167
"Off".to_string()
156168
} else {
157169
format!("{}", self.get_max_notes())
@@ -166,7 +178,7 @@ impl vst::plugin::PluginParameters for NoteOffDelayPluginParameters {
166178
}.to_string()
167179
}
168180
Parameter::MultiplyLength => {
169-
DelayMultiplier::from(self.get_parameter(Parameter::MultiplyLength as i32)).to_string()
181+
DelayMultiplier::from(value).to_string()
170182
}
171183
}
172184
}
@@ -176,7 +188,8 @@ impl vst::plugin::PluginParameters for NoteOffDelayPluginParameters {
176188
Parameter::DelayOffset => "Delay",
177189
Parameter::MaxNotes => "Max Notes",
178190
Parameter::MaxNotesAppliesToDelayedNotesOnly => "Apply max notes to delayed notes only",
179-
Parameter::MultiplyLength => "Length multiplier"
191+
Parameter::MultiplyLength => "Length multiplier",
192+
Parameter::Threshold => "Threshold"
180193
}
181194
.to_string()
182195
}
@@ -187,7 +200,7 @@ impl vst::plugin::PluginParameters for NoteOffDelayPluginParameters {
187200

188201
fn set_parameter(&self, index: i32, value: f32) {
189202
match index.into() {
190-
Parameter::DelayOffset | Parameter::MultiplyLength => {
203+
Parameter::DelayOffset | Parameter::MultiplyLength | Parameter::Threshold => {
191204
let old_value = self.get_parameter(index);
192205
if (value - old_value).abs() > 0.0001 || (value == 0.0 && old_value != 0.0) {
193206
self.transfer.set_parameter(index as usize, value)

util/src/lib.rs

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -51,27 +51,28 @@ pub fn duration_display(seconds: f32) -> String {
5151
out
5252
}
5353

54-
impl Display for DelayOffset {
54+
impl Display for Duration {
5555
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
5656
match self {
57-
DelayOffset::Off => "off".to_string(),
58-
DelayOffset::Duration(seconds) => {
57+
Duration::Off => "off".to_string(),
58+
Duration::Duration(seconds) => {
5959
duration_display(*seconds)
6060
}
6161
}.fmt(f)
6262
}
6363
}
6464

65-
pub enum DelayOffset {
65+
pub enum Duration {
6666
Off,
6767
Duration(f32),
6868
}
6969

70-
impl From<f32> for DelayOffset {
70+
71+
impl From<f32> for Duration {
7172
fn from(parameter_value: f32) -> Self {
7273
match get_exponential_scale_value(parameter_value, 10., 20.) {
73-
x if x == 0.0 => DelayOffset::Off,
74-
value => DelayOffset::Duration(value)
74+
x if x == 0.0 => Duration::Off,
75+
value => Duration::Duration(value)
7576
}
7677
}
7778
}

0 commit comments

Comments
 (0)