Add a sine test tone generator
This commit is contained in:
parent
15e0f6f21a
commit
59b70eecae
7
Cargo.lock
generated
7
Cargo.lock
generated
|
@ -253,6 +253,13 @@ dependencies = [
|
|||
"serde",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "sine"
|
||||
version = "0.1.0"
|
||||
dependencies = [
|
||||
"nih_plug",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "smallvec"
|
||||
version = "1.8.0"
|
||||
|
|
|
@ -11,6 +11,7 @@ members = [
|
|||
"xtask",
|
||||
|
||||
"plugins/gain",
|
||||
"plugins/sine",
|
||||
]
|
||||
|
||||
[dependencies]
|
||||
|
|
11
plugins/sine/Cargo.toml
Normal file
11
plugins/sine/Cargo.toml
Normal file
|
@ -0,0 +1,11 @@
|
|||
[package]
|
||||
name = "sine"
|
||||
version = "0.1.0"
|
||||
edition = "2021"
|
||||
license = "GPL-3.0-or-later"
|
||||
|
||||
[lib]
|
||||
crate-type = ["cdylib"]
|
||||
|
||||
[dependencies]
|
||||
nih_plug = { path = "../../" }
|
170
plugins/sine/src/lib.rs
Normal file
170
plugins/sine/src/lib.rs
Normal file
|
@ -0,0 +1,170 @@
|
|||
// nih-plug: plugins, but rewritten in Rust
|
||||
// Copyright (C) 2022 Robbert van der Helm
|
||||
//
|
||||
// This program is free software: you can redistribute it and/or modify
|
||||
// it under the terms of the GNU General Public License as published by
|
||||
// the Free Software Foundation, either version 3 of the License, or
|
||||
// (at your option) any later version.
|
||||
//
|
||||
// This program is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
// GNU General Public License for more details.
|
||||
//
|
||||
// You should have received a copy of the GNU General Public License
|
||||
// along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
|
||||
#[macro_use]
|
||||
extern crate nih_plug;
|
||||
|
||||
use nih_plug::{
|
||||
context::ProcessContext,
|
||||
formatters,
|
||||
params::{FloatParam, Param, Params, Range},
|
||||
plugin::{BufferConfig, BusConfig, Plugin, ProcessStatus, Vst3Plugin},
|
||||
util,
|
||||
};
|
||||
use std::f32::consts;
|
||||
use std::pin::Pin;
|
||||
|
||||
/// A test tone generator.
|
||||
///
|
||||
/// TODO: Add MIDI support, this seems like a nice minimal example for that.
|
||||
struct Sine {
|
||||
params: Pin<Box<SineParams>>,
|
||||
sample_rate: f32,
|
||||
|
||||
phase: f32,
|
||||
}
|
||||
|
||||
#[derive(Params)]
|
||||
struct SineParams {
|
||||
#[id = "gain"]
|
||||
pub gain: FloatParam,
|
||||
|
||||
#[id = "frequency"]
|
||||
pub frequency: FloatParam,
|
||||
}
|
||||
|
||||
impl Default for Sine {
|
||||
fn default() -> Self {
|
||||
Self {
|
||||
params: Box::pin(SineParams::default()),
|
||||
sample_rate: 1.0,
|
||||
|
||||
phase: 0.0,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Default for SineParams {
|
||||
fn default() -> Self {
|
||||
Self {
|
||||
gain: FloatParam {
|
||||
value: -10.0,
|
||||
range: Range::Linear {
|
||||
min: -30.0,
|
||||
max: 0.0,
|
||||
},
|
||||
name: "Gain",
|
||||
unit: " dB",
|
||||
value_to_string: formatters::f32_rounded(2),
|
||||
..Default::default()
|
||||
},
|
||||
frequency: FloatParam {
|
||||
value: 420.0,
|
||||
// TODO: Add logarithmic ranges
|
||||
range: Range::Linear {
|
||||
min: 1.0,
|
||||
max: 20_000.0,
|
||||
},
|
||||
name: "Frequency",
|
||||
unit: " Hz",
|
||||
value_to_string: formatters::f32_rounded(0),
|
||||
..Default::default()
|
||||
},
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
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";
|
||||
|
||||
const VERSION: &'static str = "0.0.1";
|
||||
|
||||
const DEFAULT_NUM_INPUTS: u32 = 0;
|
||||
const DEFAULT_NUM_OUTPUTS: u32 = 2;
|
||||
|
||||
fn params(&self) -> Pin<&dyn Params> {
|
||||
self.params.as_ref()
|
||||
}
|
||||
|
||||
fn accepts_bus_config(&self, config: &BusConfig) -> bool {
|
||||
// This can output to any number of channels, but it doesn't take any audio inputs
|
||||
config.num_input_channels == 0 && config.num_input_channels > 0
|
||||
}
|
||||
|
||||
fn initialize(
|
||||
&mut self,
|
||||
_bus_config: &BusConfig,
|
||||
buffer_config: &BufferConfig,
|
||||
_context: &dyn ProcessContext,
|
||||
) -> bool {
|
||||
self.sample_rate = buffer_config.sample_rate;
|
||||
|
||||
true
|
||||
}
|
||||
|
||||
fn process(
|
||||
&mut self,
|
||||
samples: &mut [&mut [f32]],
|
||||
_context: &dyn ProcessContext,
|
||||
) -> ProcessStatus {
|
||||
if samples.is_empty() {
|
||||
return ProcessStatus::Error("Empty buffers");
|
||||
}
|
||||
|
||||
// TODO: Move this iterator to an adapter
|
||||
let num_channels = samples.len();
|
||||
let num_samples = samples[0].len();
|
||||
for channel in &samples[1..] {
|
||||
nih_debug_assert_eq!(channel.len(), num_samples);
|
||||
if channel.len() != num_samples {
|
||||
return ProcessStatus::Error("Mismatching channel buffer sizes");
|
||||
}
|
||||
}
|
||||
|
||||
let phase_delta = self.params.frequency.value / self.sample_rate;
|
||||
for sample_idx in 0..num_samples {
|
||||
let sine = (self.phase * consts::TAU).sin();
|
||||
|
||||
self.phase = self.phase + phase_delta;
|
||||
if self.phase >= 1.0 {
|
||||
self.phase -= 1.0;
|
||||
}
|
||||
|
||||
for channel_idx in 0..num_channels {
|
||||
let sample = unsafe {
|
||||
samples
|
||||
.get_unchecked_mut(channel_idx)
|
||||
.get_unchecked_mut(sample_idx)
|
||||
};
|
||||
|
||||
// TODO: Parameter smoothing
|
||||
*sample = sine * util::db_to_gain(self.params.gain.value);
|
||||
}
|
||||
}
|
||||
|
||||
ProcessStatus::Normal
|
||||
}
|
||||
}
|
||||
|
||||
impl Vst3Plugin for Sine {
|
||||
const VST3_CLASS_ID: [u8; 16] = *b"SineMoistestPlug";
|
||||
const VST3_CATEGORIES: &'static str = "Instrument|Synth|Tools";
|
||||
}
|
||||
|
||||
nih_export_vst3!(Sine);
|
Loading…
Reference in a new issue