1
0
Fork 0

Control sine level through velocity and aftertouch

This commit is contained in:
Robbert van der Helm 2022-04-08 17:44:05 +02:00
parent 2a4a61947f
commit 25dd0d9bef

View file

@ -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);
}
_ => (),
},