2022-03-04 09:23:51 +11:00
|
|
|
use nih_plug::prelude::*;
|
2022-02-02 05:56:28 +11:00
|
|
|
use std::f32::consts;
|
2022-04-07 23:31:46 +10:00
|
|
|
use std::sync::Arc;
|
2022-02-02 05:56:28 +11:00
|
|
|
|
2022-02-04 12:58:36 +11:00
|
|
|
/// A test tone generator that can either generate a sine wave based on the plugin's parameters or
|
|
|
|
/// based on the current MIDI input.
|
2022-02-02 05:56:28 +11:00
|
|
|
struct Sine {
|
2022-04-07 23:31:46 +10:00
|
|
|
params: Arc<SineParams>,
|
2022-02-02 05:56:28 +11:00
|
|
|
sample_rate: f32,
|
|
|
|
|
2022-02-04 12:37:40 +11:00
|
|
|
/// The current phase of the sine wave, always kept between in `[0, 1]`.
|
2022-02-02 05:56:28 +11:00
|
|
|
phase: f32,
|
2022-02-05 01:26:37 +11:00
|
|
|
|
2022-04-09 01:44:05 +10:00
|
|
|
/// The MIDI note ID of the active note, if triggered by MIDI.
|
|
|
|
midi_note_id: u8,
|
2022-02-05 01:26:37 +11:00
|
|
|
/// The frequency if the active note, if triggered by MIDI.
|
|
|
|
midi_note_freq: f32,
|
2022-04-09 01:44:05 +10:00
|
|
|
/// A simple attack and release envelope to avoid clicks. Controlled through velocity and
|
|
|
|
/// aftertouch.
|
2022-02-05 01:26:37 +11:00
|
|
|
///
|
|
|
|
/// Smoothing is built into the parameters, but you can also use them manually if you need to
|
|
|
|
/// smooth soemthing that isn't a parameter.
|
|
|
|
midi_note_gain: Smoother<f32>,
|
2022-02-02 05:56:28 +11:00
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Params)]
|
|
|
|
struct SineParams {
|
|
|
|
#[id = "gain"]
|
|
|
|
pub gain: FloatParam,
|
|
|
|
|
2022-02-04 03:01:20 +11:00
|
|
|
#[id = "freq"]
|
2022-02-02 05:56:28 +11:00
|
|
|
pub frequency: FloatParam,
|
2022-02-04 12:37:40 +11:00
|
|
|
|
|
|
|
#[id = "usemid"]
|
|
|
|
pub use_midi: BoolParam,
|
2022-02-02 05:56:28 +11:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Default for Sine {
|
|
|
|
fn default() -> Self {
|
|
|
|
Self {
|
2022-04-07 23:31:46 +10:00
|
|
|
params: Arc::new(SineParams::default()),
|
2022-02-02 05:56:28 +11:00
|
|
|
sample_rate: 1.0,
|
|
|
|
|
|
|
|
phase: 0.0,
|
2022-04-09 01:44:05 +10:00
|
|
|
|
|
|
|
midi_note_id: 0,
|
2022-02-05 01:26:37 +11:00
|
|
|
midi_note_freq: 1.0,
|
|
|
|
midi_note_gain: Smoother::new(SmoothingStyle::Linear(5.0)),
|
2022-02-02 05:56:28 +11:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Default for SineParams {
|
|
|
|
fn default() -> Self {
|
|
|
|
Self {
|
2022-02-13 03:19:52 +11:00
|
|
|
gain: FloatParam::new(
|
|
|
|
"Gain",
|
|
|
|
-10.0,
|
2022-03-04 05:24:40 +11:00
|
|
|
FloatRange::Linear {
|
2022-02-02 05:56:28 +11:00
|
|
|
min: -30.0,
|
|
|
|
max: 0.0,
|
|
|
|
},
|
2022-02-13 03:19:52 +11:00
|
|
|
)
|
2022-02-13 05:56:37 +11:00
|
|
|
.with_smoother(SmoothingStyle::Linear(3.0))
|
2022-02-14 02:43:11 +11:00
|
|
|
.with_step_size(0.01)
|
|
|
|
.with_unit(" dB"),
|
2022-02-13 03:19:52 +11:00
|
|
|
frequency: FloatParam::new(
|
|
|
|
"Frequency",
|
|
|
|
420.0,
|
2022-03-04 05:24:40 +11:00
|
|
|
FloatRange::Skewed {
|
2022-02-02 05:56:28 +11:00
|
|
|
min: 1.0,
|
|
|
|
max: 20_000.0,
|
2022-03-04 05:24:40 +11:00
|
|
|
factor: FloatRange::skew_factor(-2.0),
|
2022-02-02 05:56:28 +11:00
|
|
|
},
|
2022-02-13 03:19:52 +11:00
|
|
|
)
|
2022-02-13 05:56:37 +11:00
|
|
|
.with_smoother(SmoothingStyle::Linear(10.0))
|
2022-02-14 02:43:11 +11:00
|
|
|
// We purposely don't specify a step size here, but the parameter should still be
|
2022-03-22 00:44:26 +11:00
|
|
|
// displayed as if it were rounded. This formatter also includes the unit.
|
2022-03-27 12:43:39 +11:00
|
|
|
.with_value_to_string(formatters::v2s_f32_hz_then_khz(0))
|
|
|
|
.with_string_to_value(formatters::s2v_f32_hz_then_khz()),
|
2022-02-13 03:19:52 +11:00
|
|
|
use_midi: BoolParam::new("Use MIDI", false),
|
2022-02-02 05:56:28 +11:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-02-04 12:37:40 +11:00
|
|
|
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
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-02-02 05:56:28 +11:00
|
|
|
impl Plugin for Sine {
|
|
|
|
const NAME: &'static str = "Sine Test Tone";
|
|
|
|
const VENDOR: &'static str = "Moist Plugins GmbH";
|
|
|
|
const URL: &'static str = "https://youtu.be/dQw4w9WgXcQ";
|
|
|
|
const EMAIL: &'static str = "info@example.com";
|
|
|
|
|
2022-11-10 01:50:21 +11:00
|
|
|
const VERSION: &'static str = env!("CARGO_PKG_VERSION");
|
2022-02-02 05:56:28 +11:00
|
|
|
|
2022-08-19 22:34:21 +10:00
|
|
|
const DEFAULT_INPUT_CHANNELS: u32 = 0;
|
|
|
|
const DEFAULT_OUTPUT_CHANNELS: u32 = 2;
|
2022-02-02 05:56:28 +11:00
|
|
|
|
2022-04-08 04:27:37 +10:00
|
|
|
const MIDI_INPUT: MidiConfig = MidiConfig::Basic;
|
2022-03-11 04:57:17 +11:00
|
|
|
const SAMPLE_ACCURATE_AUTOMATION: bool = true;
|
2022-02-04 11:09:09 +11:00
|
|
|
|
2022-10-22 10:59:29 +11:00
|
|
|
type BackgroundTask = ();
|
2022-10-22 08:52:46 +11:00
|
|
|
|
2022-04-07 23:31:46 +10:00
|
|
|
fn params(&self) -> Arc<dyn Params> {
|
2022-05-25 07:55:48 +10:00
|
|
|
self.params.clone()
|
2022-02-02 05:56:28 +11:00
|
|
|
}
|
|
|
|
|
|
|
|
fn accepts_bus_config(&self, config: &BusConfig) -> bool {
|
|
|
|
// This can output to any number of channels, but it doesn't take any audio inputs
|
2022-06-15 08:08:07 +10:00
|
|
|
config.num_input_channels == 0 && config.num_output_channels > 0
|
2022-02-02 05:56:28 +11:00
|
|
|
}
|
|
|
|
|
|
|
|
fn initialize(
|
|
|
|
&mut self,
|
|
|
|
_bus_config: &BusConfig,
|
|
|
|
buffer_config: &BufferConfig,
|
2022-10-22 09:21:08 +11:00
|
|
|
_context: &mut impl InitContext<Self>,
|
2022-02-02 05:56:28 +11:00
|
|
|
) -> bool {
|
|
|
|
self.sample_rate = buffer_config.sample_rate;
|
|
|
|
|
|
|
|
true
|
|
|
|
}
|
|
|
|
|
2022-03-08 10:42:58 +11:00
|
|
|
fn reset(&mut self) {
|
|
|
|
self.phase = 0.0;
|
2022-04-09 01:44:05 +10:00
|
|
|
self.midi_note_id = 0;
|
2022-03-08 10:42:58 +11:00
|
|
|
self.midi_note_freq = 1.0;
|
|
|
|
self.midi_note_gain.reset(0.0);
|
|
|
|
}
|
|
|
|
|
2022-05-27 10:30:57 +10:00
|
|
|
fn process(
|
|
|
|
&mut self,
|
|
|
|
buffer: &mut Buffer,
|
|
|
|
_aux: &mut AuxiliaryBuffers,
|
2022-10-22 10:15:16 +11:00
|
|
|
context: &mut impl ProcessContext<Self>,
|
2022-05-27 10:30:57 +10:00
|
|
|
) -> ProcessStatus {
|
2022-04-12 02:14:25 +10:00
|
|
|
let mut next_event = context.next_event();
|
2022-03-10 06:11:37 +11:00
|
|
|
for (sample_id, channel_samples) in buffer.iter_samples().enumerate() {
|
2022-02-03 07:08:23 +11:00
|
|
|
// Smoothing is optionally built into the parameters themselves
|
|
|
|
let gain = self.params.gain.smoothed.next();
|
|
|
|
|
2022-02-04 12:37:40 +11:00
|
|
|
// This plugin can be either triggered by MIDI or controleld by a parameter
|
2022-09-07 05:55:14 +10:00
|
|
|
let sine = if self.params.use_midi.value() {
|
2022-02-04 12:37:40 +11:00
|
|
|
// Act on the next MIDI event
|
2022-04-12 02:24:07 +10:00
|
|
|
while let Some(event) = next_event {
|
2022-07-14 20:50:16 +10:00
|
|
|
if event.timing() > sample_id as u32 {
|
2022-04-12 02:24:07 +10:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
match event {
|
|
|
|
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, velocity);
|
|
|
|
}
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
_ => (),
|
2022-02-04 12:37:40 +11:00
|
|
|
}
|
|
|
|
|
2022-04-12 02:14:25 +10:00
|
|
|
next_event = context.next_event();
|
2022-02-04 12:37:40 +11:00
|
|
|
}
|
|
|
|
|
2022-02-05 01:26:37 +11:00
|
|
|
// This gain envelope prevents clicks with new notes and with released notes
|
|
|
|
self.calculate_sine(self.midi_note_freq) * self.midi_note_gain.next()
|
2022-02-04 12:37:40 +11:00
|
|
|
} else {
|
|
|
|
let frequency = self.params.frequency.smoothed.next();
|
2022-02-15 13:45:39 +11:00
|
|
|
self.calculate_sine(frequency)
|
2022-02-04 12:37:40 +11:00
|
|
|
};
|
2022-02-02 05:56:28 +11:00
|
|
|
|
2022-03-04 07:38:50 +11:00
|
|
|
for sample in channel_samples {
|
2023-01-05 03:05:05 +11:00
|
|
|
*sample = sine * util::db_to_gain_fast(gain);
|
2022-02-02 05:56:28 +11:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-05-24 21:17:40 +10:00
|
|
|
ProcessStatus::KeepAlive
|
2022-02-02 05:56:28 +11:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-03-01 03:04:39 +11:00
|
|
|
impl ClapPlugin for Sine {
|
|
|
|
const CLAP_ID: &'static str = "com.moist-plugins-gmbh.sine";
|
2022-07-04 20:44:30 +10:00
|
|
|
const CLAP_DESCRIPTION: Option<&'static str> =
|
|
|
|
Some("An optionally MIDI controlled sine test tone");
|
|
|
|
const CLAP_MANUAL_URL: Option<&'static str> = Some(Self::URL);
|
|
|
|
const CLAP_SUPPORT_URL: Option<&'static str> = None;
|
2022-06-02 09:16:30 +10:00
|
|
|
const CLAP_FEATURES: &'static [ClapFeature] = &[
|
2023-01-29 09:23:35 +11:00
|
|
|
ClapFeature::Instrument,
|
2022-06-29 08:54:26 +10:00
|
|
|
ClapFeature::Synthesizer,
|
2022-06-02 09:16:30 +10:00
|
|
|
ClapFeature::Stereo,
|
|
|
|
ClapFeature::Mono,
|
|
|
|
ClapFeature::Utility,
|
|
|
|
];
|
2022-03-01 03:04:39 +11:00
|
|
|
}
|
2022-03-01 00:45:07 +11:00
|
|
|
|
2022-02-02 05:56:28 +11:00
|
|
|
impl Vst3Plugin for Sine {
|
|
|
|
const VST3_CLASS_ID: [u8; 16] = *b"SineMoistestPlug";
|
|
|
|
const VST3_CATEGORIES: &'static str = "Instrument|Synth|Tools";
|
|
|
|
}
|
|
|
|
|
2022-03-01 00:45:31 +11:00
|
|
|
nih_export_clap!(Sine);
|
2022-02-02 05:56:28 +11:00
|
|
|
nih_export_vst3!(Sine);
|