1
0
Fork 0

Add MIDI support to the sine example

This commit is contained in:
Robbert van der Helm 2022-02-04 02:37:40 +01:00
parent fdfd1b01c2
commit 366deda83c
3 changed files with 80 additions and 11 deletions

View file

@ -21,7 +21,7 @@ use nih_plug::{
formatters, util, Buffer, BufferConfig, BusConfig, Plugin, ProcessContext, ProcessStatus, formatters, util, Buffer, BufferConfig, BusConfig, Plugin, ProcessContext, ProcessStatus,
Vst3Plugin, Vst3Plugin,
}; };
use nih_plug::{FloatParam, Param, Params, Range, Smoother, SmoothingStyle}; use nih_plug::{BoolParam, FloatParam, Param, Params, Range, Smoother, SmoothingStyle};
use std::f32::consts; use std::f32::consts;
use std::pin::Pin; use std::pin::Pin;
@ -32,7 +32,10 @@ struct Sine {
params: Pin<Box<SineParams>>, params: Pin<Box<SineParams>>,
sample_rate: f32, sample_rate: f32,
/// The current phase of the sine wave, always kept between in `[0, 1]`.
phase: f32, phase: f32,
/// The active frequency, if triggered by MIDI.
active_note_freq: Option<f32>,
} }
#[derive(Params)] #[derive(Params)]
@ -42,6 +45,9 @@ struct SineParams {
#[id = "freq"] #[id = "freq"]
pub frequency: FloatParam, pub frequency: FloatParam,
#[id = "usemid"]
pub use_midi: BoolParam,
} }
impl Default for Sine { impl Default for Sine {
@ -51,6 +57,7 @@ impl Default for Sine {
sample_rate: 1.0, sample_rate: 1.0,
phase: 0.0, phase: 0.0,
active_note_freq: None,
} }
} }
} }
@ -83,10 +90,29 @@ impl Default for SineParams {
value_to_string: formatters::f32_rounded(0), value_to_string: formatters::f32_rounded(0),
..Default::default() ..Default::default()
}, },
use_midi: BoolParam {
value: false,
name: "Use MIDI",
..Default::default()
},
} }
} }
} }
impl Sine {
fn calculate_sine(&mut self, frequency: f32) -> f32 {
let phase_delta = frequency / self.sample_rate;
let sine = (self.phase * consts::TAU).sin();
self.phase += phase_delta;
if self.phase >= 1.0 {
self.phase -= 1.0;
}
sine
}
}
impl Plugin for Sine { impl Plugin for Sine {
const NAME: &'static str = "Sine Test Tone"; const NAME: &'static str = "Sine Test Tone";
const VENDOR: &'static str = "Moist Plugins GmbH"; const VENDOR: &'static str = "Moist Plugins GmbH";
@ -120,20 +146,47 @@ impl Plugin for Sine {
true true
} }
fn process(&mut self, buffer: &mut Buffer, _context: &dyn ProcessContext) -> ProcessStatus { fn process(&mut self, buffer: &mut Buffer, context: &dyn ProcessContext) -> ProcessStatus {
for samples in buffer.iter_mut() { let mut next_event = context.next_midi_event();
for (sample_id, samples) in buffer.iter_mut().enumerate() {
// Smoothing is optionally built into the parameters themselves // Smoothing is optionally built into the parameters themselves
let gain = self.params.gain.smoothed.next(); let gain = self.params.gain.smoothed.next();
let frequency = self.params.frequency.smoothed.next();
let phase_delta = frequency / self.sample_rate; // This plugin can be either triggered by MIDI or controleld by a parameter
let sine = (self.phase * consts::TAU).sin(); let sine = if self.params.use_midi.value {
// Act on the next MIDI event
self.phase += phase_delta; 'midi_events: loop {
if self.phase >= 1.0 { match next_event {
self.phase -= 1.0; Some(event) if event.timing() == sample_id as u32 => match event {
nih_plug::NoteEvent::NoteOn { note, .. } => {
// Reset the phase if this is a new note
if self.active_note_freq.is_none() {
self.phase = 0.0;
} }
self.active_note_freq = Some(util::midi_note_to_freq(note));
}
nih_plug::NoteEvent::NoteOff { note, .. } => {
if self.active_note_freq == Some(util::midi_note_to_freq(note)) {
self.active_note_freq = None;
}
}
},
_ => break 'midi_events,
}
next_event = context.next_midi_event();
}
match self.active_note_freq {
Some(frequency) => self.calculate_sine(frequency),
None => 0.0,
}
} else {
let frequency = self.params.frequency.smoothed.next();
self.calculate_sine(frequency)
};
for sample in samples { for sample in samples {
*sample = sine * util::db_to_gain(gain); *sample = sine * util::db_to_gain(gain);
} }

View file

@ -155,7 +155,7 @@ pub enum ProcessStatus {
} }
/// Event for (incoming) notes. Right now this only supports a very small subset of the MIDI /// Event for (incoming) notes. Right now this only supports a very small subset of the MIDI
/// specification. /// specification. See the util module for convenient conversion functions.
/// ///
/// All of the timings are sample offsets withing the current buffer. /// All of the timings are sample offsets withing the current buffer.
/// ///
@ -175,3 +175,13 @@ pub enum NoteEvent {
velocity: u8, velocity: u8,
}, },
} }
impl NoteEvent {
/// Return the sample within the current buffer this event belongs to.
pub fn timing(&self) -> u32 {
match &self {
NoteEvent::NoteOn { timing, .. } => *timing,
NoteEvent::NoteOff { timing, .. } => *timing,
}
}
}

View file

@ -35,6 +35,12 @@ pub fn gain_to_db(gain: f32) -> f32 {
} }
} }
/// Convert a MIDI note ID to a frequency at A4 = 440 Hz equal temperament and middle C = note 60 =
/// C4.
pub fn midi_note_to_freq(pitch: u8) -> f32 {
2.0f32.powf((pitch as f32 - 69.0) / 12.0) * 440.0
}
/// A version of [std::thread::Builder::spawn_unchecked] that works on the stable compiler. /// A version of [std::thread::Builder::spawn_unchecked] that works on the stable compiler.
/// Implementation courtesy of Yandros on the Rust Discord. /// Implementation courtesy of Yandros on the Rust Discord.
pub(crate) trait ThreadSpawnUnchecked { pub(crate) trait ThreadSpawnUnchecked {