From 25dd0d9bef461b5a7bf759a82fee249dd4572d1e Mon Sep 17 00:00:00 2001 From: Robbert van der Helm Date: Fri, 8 Apr 2022 17:44:05 +0200 Subject: [PATCH] Control sine level through velocity and aftertouch --- plugins/examples/sine/src/lib.rs | 24 +++++++++++++++++------- 1 file changed, 17 insertions(+), 7 deletions(-) diff --git a/plugins/examples/sine/src/lib.rs b/plugins/examples/sine/src/lib.rs index a96a329f..7d7a8b1e 100644 --- a/plugins/examples/sine/src/lib.rs +++ b/plugins/examples/sine/src/lib.rs @@ -11,9 +11,12 @@ struct Sine { /// The current phase of the sine wave, always kept between in `[0, 1]`. phase: f32, + /// The MIDI note ID of the active note, if triggered by MIDI. + midi_note_id: u8, /// The frequency if the active note, if triggered by MIDI. midi_note_freq: f32, - /// A simple attack and release envelope to avoid clicks. + /// A simple attack and release envelope to avoid clicks. Controlled through velocity and + /// aftertouch. /// /// Smoothing is built into the parameters, but you can also use them manually if you need to /// smooth soemthing that isn't a parameter. @@ -39,6 +42,8 @@ impl Default for Sine { sample_rate: 1.0, phase: 0.0, + + midi_note_id: 0, midi_note_freq: 1.0, midi_note_gain: Smoother::new(SmoothingStyle::Linear(5.0)), } @@ -128,6 +133,7 @@ impl Plugin for Sine { fn reset(&mut self) { self.phase = 0.0; + self.midi_note_id = 0; self.midi_note_freq = 1.0; self.midi_note_gain.reset(0.0); } @@ -144,14 +150,18 @@ impl Plugin for Sine { 'midi_events: loop { match next_event { Some(event) if event.timing() == sample_id as u32 => match event { - NoteEvent::NoteOn { note, .. } => { + NoteEvent::NoteOn { note, velocity, .. } => { + self.midi_note_id = note; self.midi_note_freq = util::midi_note_to_freq(note); - self.midi_note_gain.set_target(self.sample_rate, 1.0); + self.midi_note_gain.set_target(self.sample_rate, velocity); } - NoteEvent::NoteOff { note, .. } => { - if self.midi_note_freq == util::midi_note_to_freq(note) { - self.midi_note_gain.set_target(self.sample_rate, 0.0); - } + NoteEvent::NoteOff { note, .. } if note == self.midi_note_id => { + self.midi_note_gain.set_target(self.sample_rate, 0.0); + } + NoteEvent::PolyPressure { note, pressure, .. } + if note == self.midi_note_id => + { + self.midi_note_gain.set_target(self.sample_rate, pressure); } _ => (), },