refactored the VPSOscillator into it's own struct
This commit is contained in:
parent
16dc1f01f4
commit
ba8e6ec33f
4 changed files with 183 additions and 107 deletions
|
@ -194,7 +194,7 @@ impl<const N: usize> Oversampling<N> {
|
|||
pub fn set_sample_rate(&mut self, srate: f32) {
|
||||
let cutoff = 0.98 * (0.5 * srate);
|
||||
|
||||
let ovr_srate = ((N as f32) * srate);
|
||||
let ovr_srate = (N as f32) * srate;
|
||||
|
||||
for filt in &mut self.filters {
|
||||
filt.set_coefs(BiquadCoefs::butter_lowpass(ovr_srate, cutoff));
|
||||
|
|
|
@ -1475,6 +1475,156 @@ impl PolyBlepOscillator {
|
|||
}
|
||||
}
|
||||
|
||||
// This oscillator is based on the work "VECTOR PHASESHAPING SYNTHESIS"
|
||||
// by: Jari Kleimola*, Victor Lazzarini†, Joseph Timoney†, Vesa Välimäki*
|
||||
// *Aalto University School of Electrical Engineering Espoo, Finland;
|
||||
// †National University of Ireland, Maynooth Ireland
|
||||
//
|
||||
// See also this PDF: http://recherche.ircam.fr/pub/dafx11/Papers/55_e.pdf
|
||||
/// Vector Phase Shaping Oscillator.
|
||||
/// The parameters `d` and `v` control the shape of the sinus
|
||||
/// wave. This leads to interesting modulation properties of those
|
||||
/// control values.
|
||||
///
|
||||
///```
|
||||
/// use hexodsp::dsp::helpers::{VPSOscillator, rand_01};
|
||||
///
|
||||
/// // Randomize the initial phase to make cancellation on summing less
|
||||
/// // likely:
|
||||
/// let mut osc =
|
||||
/// VPSOscillator::new(rand_01() * 0.25);
|
||||
///
|
||||
///
|
||||
/// let freq = 440.0; // Hz
|
||||
/// let israte = 1.0 / 44100.0; // Seconds per Sample
|
||||
/// let d = 0.5; // Range: 0.0 to 1.0
|
||||
/// let v = 0.75; // Range: 0.0 to 1.0
|
||||
///
|
||||
/// let mut block_of_samples = [0.0; 128];
|
||||
/// // in your process function:
|
||||
/// for output_sample in block_of_samples.iter_mut() {
|
||||
/// // It is advised to limit the `v` value, because with certain
|
||||
/// // `d` values the combination creates just a DC offset.
|
||||
/// let v = VPSOscillator::limit_v(d, v);
|
||||
/// *output_sample = osc.next(freq, israte, d, v);
|
||||
/// }
|
||||
///```
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct VPSOscillator {
|
||||
phase: f32,
|
||||
init_phase: f32,
|
||||
}
|
||||
|
||||
impl VPSOscillator {
|
||||
/// Create a new instance of [VPSOscillator].
|
||||
///
|
||||
/// * `init_phase` - The initial phase of the oscillator.
|
||||
pub fn new(init_phase: f32) -> Self {
|
||||
Self {
|
||||
phase: 0.0,
|
||||
init_phase,
|
||||
}
|
||||
}
|
||||
|
||||
/// Reset the phase of the oscillator to the initial phase.
|
||||
#[inline]
|
||||
pub fn reset(&mut self) {
|
||||
self.phase = self.init_phase;
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn s(p: f32) -> f32 {
|
||||
-(std::f32::consts::TAU * p).cos()
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn phi_vps(x: f32, v: f32, d: f32) -> f32 {
|
||||
if x < d {
|
||||
(v * x) / d
|
||||
} else {
|
||||
v + ((1.0 - v) * (x - d))/(1.0 - d)
|
||||
}
|
||||
}
|
||||
|
||||
/// This rather complicated function blends out some
|
||||
/// combinations of 'd' and 'v' that just lead to a constant DC
|
||||
/// offset. Which is not very useful in an audio oscillator
|
||||
/// context.
|
||||
///
|
||||
/// Call this before passing `v` to [VPSOscillator::next].
|
||||
#[inline]
|
||||
pub fn limit_v(d: f32, v: f32) -> f32 {
|
||||
let delta = 0.5 - (d - 0.5).abs();
|
||||
if delta < 0.05 {
|
||||
let x = (0.05 - delta) * 19.99;
|
||||
if d < 0.5 {
|
||||
let mm = x * 0.5;
|
||||
let max = 1.0 - mm;
|
||||
if v > max && v < 1.0 {
|
||||
max
|
||||
} else if v >= 1.0 && v < (1.0 + mm) {
|
||||
1.0 + mm
|
||||
} else {
|
||||
v
|
||||
}
|
||||
} else {
|
||||
if v < 1.0 {
|
||||
v.clamp(x * 0.5, 1.0)
|
||||
} else {
|
||||
v
|
||||
}
|
||||
}
|
||||
} else {
|
||||
v
|
||||
}
|
||||
}
|
||||
|
||||
/// Creates the next sample of this oscillator.
|
||||
///
|
||||
/// * `freq` - The frequency in Hz.
|
||||
/// * `israte` - The inverse sampling rate, or seconds per sample as in eg. `1.0 / 44100.0`.
|
||||
/// * `d` - The phase distortion parameter `d` which must be in the range `0.0` to `1.0`.
|
||||
/// * `v` - The phase distortion parameter `v` which must be in the range `0.0` to `1.0`.
|
||||
///
|
||||
/// It is advised to limit the `v` using the [VPSOscillator::limit_v] function
|
||||
/// before calling this function. To prevent DC offsets when modulating the parameters.
|
||||
pub fn next(&mut self, freq: f32, israte: f32, d: f32, v: f32) -> f32 {
|
||||
let s = Self::s(Self::phi_vps(self.phase, v, d));
|
||||
|
||||
self.phase += freq * israte;
|
||||
self.phase = self.phase.fract();
|
||||
|
||||
s
|
||||
}
|
||||
}
|
||||
|
||||
#[macro_export]
|
||||
macro_rules! fa_distort { ($formatter: expr, $v: expr, $denorm_v: expr) => { {
|
||||
let s =
|
||||
match ($v.round() as usize) {
|
||||
0 => "Off",
|
||||
1 => "tanh",
|
||||
2 => "?!?",
|
||||
3 => "fold",
|
||||
_ => "?",
|
||||
};
|
||||
write!($formatter, "{}", s)
|
||||
} } }
|
||||
|
||||
#[inline]
|
||||
pub fn apply_distortion(s: f32, damt: f32, dist_type: u8) -> f32 {
|
||||
match dist_type {
|
||||
1 => (damt.clamp(0.01, 1.0) * 100.0 * s).tanh(),
|
||||
2 => f_distort(1.0, damt * damt * damt * 1000.0, s),
|
||||
3 => {
|
||||
let damt = damt.clamp(0.0, 0.99);
|
||||
let damt = 1.0 - damt * damt;
|
||||
f_fold_distort(1.0, damt, s) * (1.0 / damt)
|
||||
},
|
||||
_ => s,
|
||||
}
|
||||
}
|
||||
|
||||
//pub struct UnisonBlep {
|
||||
// oscs: Vec<PolyBlepOscillator>,
|
||||
//// dc_block: crate::filter::DCBlockFilter,
|
||||
|
|
|
@ -72,7 +72,7 @@ use crate::fa_bosc_wtype;
|
|||
use crate::fa_biqfilt_type;
|
||||
use crate::fa_biqfilt_ord;
|
||||
use crate::fa_vosc_ovrsmpl;
|
||||
use crate::fa_vosc_dist;
|
||||
use crate::fa_distort;
|
||||
|
||||
use node_amp::Amp;
|
||||
use node_sin::Sin;
|
||||
|
@ -600,8 +600,8 @@ macro_rules! node_list {
|
|||
(3 v n_id n_id r_id f_def stp_d 0.0, 1.0, 0.5)
|
||||
(4 vs n_vps d_vps r_vps f_defvlp stp_d 0.0, 1.0, 0.0)
|
||||
(5 damt n_id n_id r_id f_def stp_d 0.0, 1.0, 0.0)
|
||||
{6 0 dist setting(0) fa_vosc_dist 0 3}
|
||||
{7 1 ovrsmpl setting(0) fa_vosc_ovrsmpl 0 1}
|
||||
{6 0 dist setting(0) fa_distort 0 3}
|
||||
{7 1 ovrsmpl setting(1) fa_vosc_ovrsmpl 0 1}
|
||||
[0 sig],
|
||||
out => Out UIType::Generic UICategory::IOUtil
|
||||
(0 ch1 n_id d_id r_id f_def stp_d -1.0, 1.0, 0.0)
|
||||
|
|
|
@ -4,7 +4,7 @@
|
|||
|
||||
use crate::nodes::{NodeAudioContext, NodeExecContext};
|
||||
use crate::dsp::biquad::Oversampling;
|
||||
use crate::dsp::helpers::{quicker_tanh, f_distort, f_fold_distort};
|
||||
use crate::dsp::helpers::{VPSOscillator, apply_distortion};
|
||||
use crate::dsp::{
|
||||
NodeId, SAtom, ProcBuf, DspNode, LedPhaseVals, NodeContext,
|
||||
GraphAtomData, GraphFun,
|
||||
|
@ -21,41 +21,13 @@ macro_rules! fa_vosc_ovrsmpl { ($formatter: expr, $v: expr, $denorm_v: expr) =>
|
|||
write!($formatter, "{}", s)
|
||||
} } }
|
||||
|
||||
#[macro_export]
|
||||
macro_rules! fa_vosc_dist { ($formatter: expr, $v: expr, $denorm_v: expr) => { {
|
||||
let s =
|
||||
match ($v.round() as usize) {
|
||||
0 => "Off",
|
||||
1 => "tanh",
|
||||
2 => "?!?",
|
||||
3 => "fold",
|
||||
_ => "?",
|
||||
};
|
||||
write!($formatter, "{}", s)
|
||||
} } }
|
||||
|
||||
#[inline]
|
||||
fn apply_distortion(s: f32, damt: f32, dist_type: u8) -> f32 {
|
||||
match dist_type {
|
||||
1 => (damt.clamp(0.01, 1.0) * 100.0 * s).tanh(),
|
||||
2 => f_distort(1.0, damt * damt * damt * 1000.0, s),
|
||||
3 => {
|
||||
let damt = damt.clamp(0.0, 0.99);
|
||||
let damt = 1.0 - damt * damt;
|
||||
f_fold_distort(1.0, damt, s) * (1.0 / damt)
|
||||
},
|
||||
_ => s,
|
||||
}
|
||||
}
|
||||
|
||||
const OVERSAMPLING : usize = 4;
|
||||
|
||||
/// A simple amplifier
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct VOsc {
|
||||
// osc: PolyBlepOscillator,
|
||||
israte: f32,
|
||||
phase: f32,
|
||||
osc: VPSOscillator,
|
||||
oversampling: Box<Oversampling<OVERSAMPLING>>,
|
||||
}
|
||||
|
||||
|
@ -65,7 +37,7 @@ impl VOsc {
|
|||
|
||||
Self {
|
||||
israte: 1.0 / 44100.0,
|
||||
phase: init_phase,
|
||||
osc: VPSOscillator::new(init_phase),
|
||||
oversampling: Box::new(Oversampling::new()),
|
||||
}
|
||||
}
|
||||
|
@ -114,49 +86,6 @@ ways to manipulate them.
|
|||
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn s(p: f32) -> f32 {
|
||||
-(std::f32::consts::TAU * p).cos()
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn phi_vps(x: f32, v: f32, d: f32) -> f32 {
|
||||
if x < d {
|
||||
(v * x) / d
|
||||
} else {
|
||||
v + ((1.0 - v) * (x - d))/(1.0 - d)
|
||||
}
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn limit_v(d: f32, v: f32) -> f32 {
|
||||
let delta = 0.5 - (d - 0.5).abs();
|
||||
if delta < 0.05 {
|
||||
let x = (0.05 - delta) * 19.99;
|
||||
// println!("X: {}, d={}, v={}, delta={}", x, d, v, delta);
|
||||
if d < 0.5 {
|
||||
let max = 1.0 - (x * 0.5);
|
||||
if v > max && v < 1.0 {
|
||||
max
|
||||
|
||||
} else if v >= 1.0 && v < (1.0 + max) {
|
||||
1.0 + max
|
||||
// v.clamp(0.0, max)
|
||||
} else {
|
||||
v
|
||||
}
|
||||
} else {
|
||||
if v < 1.0 {
|
||||
v.clamp(x * 0.5, 1.0)
|
||||
} else {
|
||||
v
|
||||
}
|
||||
}
|
||||
} else {
|
||||
v
|
||||
}
|
||||
}
|
||||
|
||||
impl DspNode for VOsc {
|
||||
fn outputs() -> usize { 1 }
|
||||
|
||||
|
@ -166,9 +95,8 @@ impl DspNode for VOsc {
|
|||
}
|
||||
|
||||
fn reset(&mut self) {
|
||||
self.phase = 0.0;
|
||||
self.oversampling.reset();
|
||||
// self.osc.reset();
|
||||
self.osc.reset();
|
||||
}
|
||||
|
||||
#[inline]
|
||||
|
@ -195,6 +123,8 @@ impl DspNode for VOsc {
|
|||
let dist = dist.i() as u8;
|
||||
let oversample = ovrsmpl.i() == 1;
|
||||
|
||||
let mut osc = &mut self.osc;
|
||||
|
||||
if oversample {
|
||||
for frame in 0..ctx.nframes() {
|
||||
let freq = denorm_offs::VOsc::freq(freq, det.read(frame), frame);
|
||||
|
@ -203,16 +133,12 @@ impl DspNode for VOsc {
|
|||
let vs = denorm::VOsc::vs(vs, frame).clamp(0.0, 20.0);
|
||||
let damt = denorm::VOsc::damt(damt, frame).clamp(0.0, 1.0);
|
||||
|
||||
let v = limit_v(d, v + vs);
|
||||
let v = VPSOscillator::limit_v(d, v + vs);
|
||||
|
||||
let overbuf = self.oversampling.resample_buffer();
|
||||
for b in overbuf {
|
||||
let s = s(phi_vps(self.phase, v, d));
|
||||
|
||||
let s = osc.next(freq, israte, d, v);
|
||||
*b = apply_distortion(s, damt, dist);
|
||||
|
||||
self.phase += freq * israte;
|
||||
self.phase = self.phase.fract();
|
||||
}
|
||||
|
||||
out.write(frame, self.oversampling.downsample());
|
||||
|
@ -226,15 +152,11 @@ impl DspNode for VOsc {
|
|||
let vs = denorm::VOsc::vs(vs, frame).clamp(0.0, 20.0);
|
||||
let damt = denorm::VOsc::damt(damt, frame).clamp(0.0, 1.0);
|
||||
|
||||
let v = limit_v(d, v + vs);
|
||||
|
||||
let s = s(phi_vps(self.phase, v, d));
|
||||
let v = VPSOscillator::limit_v(d, v + vs);
|
||||
let s = osc.next(freq, israte * (OVERSAMPLING as f32), d, v);
|
||||
let s = apply_distortion(s, damt, dist);
|
||||
|
||||
out.write(frame, s);
|
||||
|
||||
self.phase += freq * (israte * (OVERSAMPLING as f32));
|
||||
self.phase = self.phase.fract();
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -242,9 +164,14 @@ impl DspNode for VOsc {
|
|||
}
|
||||
|
||||
fn graph_fun() -> Option<GraphFun> {
|
||||
let mut osc = VPSOscillator::new(0.0);
|
||||
let israte = 1.0 / 128.0;
|
||||
|
||||
Some(Box::new(move |gd: &dyn GraphAtomData, _init: bool, x: f32, _xn: f32| -> f32 {
|
||||
Some(Box::new(move |gd: &dyn GraphAtomData, init: bool, _x: f32, _xn: f32| -> f32 {
|
||||
if init {
|
||||
osc.reset();
|
||||
}
|
||||
|
||||
let v = NodeId::VOsc(0).inp_param("v").unwrap().inp();
|
||||
let vs = NodeId::VOsc(0).inp_param("vs").unwrap().inp();
|
||||
let d = NodeId::VOsc(0).inp_param("d").unwrap().inp();
|
||||
|
@ -257,9 +184,8 @@ impl DspNode for VOsc {
|
|||
let damt = gd.get_denorm(damt as u32);
|
||||
let dist = gd.get(dist as u32).map(|a| a.i()).unwrap_or(0);
|
||||
|
||||
let v = limit_v(d, v + vs);
|
||||
|
||||
let s = s(phi_vps(x, v, d));
|
||||
let v = VPSOscillator::limit_v(d, v + vs);
|
||||
let s = osc.next(1.0, israte, d, v);
|
||||
let s = apply_distortion(s, damt, dist as u8);
|
||||
|
||||
(s + 1.0) * 0.5
|
||||
|
|
Loading…
Reference in a new issue