Added distortion to the VPS oscillator and tweaked a few more parameters

This commit is contained in:
Weird Constructor 2021-08-05 06:12:50 +02:00
parent b4d6cb1cdb
commit 24379ae521
4 changed files with 96 additions and 36 deletions

View file

@ -164,19 +164,19 @@ impl ButterLowpass {
//
// Which was originally taken from https://github.com/jatinchowdhury18/ChowDSP-VCV/blob/master/src/shared/AAFilter.hpp
// Copyright (c) 2020 jatinchowdhury18
/// Implements oversampling with a ratio of 4 and a 4 times cascade
/// of Butterworth lowpass filters.
#[derive(Debug, Copy, Clone, Default)]
pub struct Oversampling4x4 {
/// Implements oversampling with a ratio of N and a 4 times cascade
/// of Butterworth lowpass filters (~48dB?).
#[derive(Debug, Copy, Clone)]
pub struct Oversampling<const N: usize> {
filters: [Biquad; 4],
buffer: [f32; 4],
buffer: [f32; N],
}
impl Oversampling4x4 {
impl<const N: usize> Oversampling<N> {
pub fn new() -> Self {
let mut this = Self {
filters: [Biquad::new(); 4],
buffer: [0.0; 4],
buffer: [0.0; N],
};
this.set_sample_rate(44100.0);
@ -185,26 +185,26 @@ impl Oversampling4x4 {
}
pub fn reset(&mut self) {
self.buffer = [0.0; 4];
self.buffer = [0.0; N];
for filt in &mut self.filters {
filt.reset();
}
}
pub fn set_sample_rate(&mut self, srate: f32) {
let cutoff = 0.98 * (srate / 2.0);
let cutoff = 0.98 * (0.5 * srate);
let ovr_srate = ((N as f32) * srate);
for filt in &mut self.filters {
filt.set_coefs(BiquadCoefs::butter_lowpass(srate, cutoff));
filt.set_coefs(BiquadCoefs::butter_lowpass(ovr_srate, cutoff));
}
}
#[inline]
pub fn upsample(&mut self, v: f32) {
self.buffer[0] = 4.0 * v;
self.buffer[1] = 0.0;
self.buffer[2] = 0.0;
self.buffer[3] = 0.0;
self.buffer.fill(0.0);
self.buffer[0] = (N as f32) * v;
for s in &mut self.buffer {
for filt in &mut self.filters {
@ -214,7 +214,7 @@ impl Oversampling4x4 {
}
#[inline]
pub fn resample_buffer(&mut self) -> &mut [f32; 4] { &mut self.buffer }
pub fn resample_buffer(&mut self) -> &mut [f32; N] { &mut self.buffer }
#[inline]
pub fn downsample(&mut self) -> f32 {

View file

@ -287,6 +287,7 @@ pub fn note_to_freq(note: f32) -> f32 {
/// threshold: 0.0 - 100.0 default = 0.8
/// i: signal
/// ```
#[inline]
pub fn f_distort(gain: f32, threshold: f32, i: f32) -> f32 {
gain * (
i * ( i.abs() + threshold )
@ -303,6 +304,7 @@ pub fn f_distort(gain: f32, threshold: f32, i: f32) -> f32 {
/// threshold: 0.0 - 100.0 default = 0.8
/// i: signal
/// ```
#[inline]
pub fn f_fold_distort(gain: f32, threshold: f32, i: f32) -> f32 {
if i >= threshold || i < -threshold {
gain
@ -370,6 +372,7 @@ pub fn quicker_tanh64(v: f64) -> f64 {
v / (1.0 + square / (3.0 + square / 5.0))
}
#[inline]
pub fn quicker_tanh(v: f32) -> f32 {
let square = v * v;
v / (1.0 + square / (3.0 + square / 5.0))

View file

@ -72,6 +72,7 @@ use crate::fa_bosc_wtype;
use crate::fa_biqfilt_type;
use crate::fa_biqfilt_ord;
use crate::fa_vosc_ovr;
use crate::fa_vosc_dist;
use node_amp::Amp;
use node_sin::Sin;
@ -433,6 +434,11 @@ macro_rules! f_def { ($formatter: expr, $v: expr, $denorm_v: expr) => {
write!($formatter, "{:6.3}", $denorm_v)
} }
// Default formatting function with low precision
macro_rules! f_deflp { ($formatter: expr, $v: expr, $denorm_v: expr) => {
write!($formatter, "{:5.2}", $denorm_v)
} }
// Default formatting function with very low precision
macro_rules! f_defvlp { ($formatter: expr, $v: expr, $denorm_v: expr) => {
write!($formatter, "{:4.1}", $denorm_v)
@ -489,6 +495,8 @@ define_exp!{n_ftme d_ftme 0.25, 1000.0}
// to reach more exact "1.0".
define_lin!{n_ogin d_ogin 0.0, 2.0}
define_lin!{n_pgin d_pgin 1.0, 10.0}
define_lin!{n_vps d_vps 0.0, 20.0}
// A note about the input-indicies:
@ -591,7 +599,9 @@ macro_rules! node_list {
(2 d n_id n_id r_id f_def stp_d 0.0, 1.0, 0.5)
(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 0 ovr setting(0) fa_vosc_ovr 0 1}
(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 ovr setting(0) fa_vosc_ovr 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)

View file

@ -3,7 +3,8 @@
// See README.md and COPYING for details.
use crate::nodes::{NodeAudioContext, NodeExecContext};
use crate::dsp::biquad::Oversampling4x4;
use crate::dsp::biquad::Oversampling;
use crate::dsp::helpers::{quicker_tanh, f_distort, f_fold_distort};
use crate::dsp::{
NodeId, SAtom, ProcBuf, DspNode, LedPhaseVals, NodeContext,
GraphAtomData, GraphFun,
@ -20,13 +21,42 @@ macro_rules! fa_vosc_ovr { ($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,
oversampling: Box<Oversampling4x4>,
oversampling: Box<Oversampling<OVERSAMPLING>>,
}
impl VOsc {
@ -36,7 +66,7 @@ impl VOsc {
Self {
israte: 1.0 / 44100.0,
phase: init_phase,
oversampling: Box::new(Oversampling4x4::new()),
oversampling: Box::new(Oversampling::new()),
}
}
@ -55,6 +85,10 @@ impl VOsc {
"VOsc v\n\nRange: (0..1)\n";
pub const vs : &'static str =
"VOsc vs\nScaling factor for 'v'.\nRange: (0..1)\n";
pub const dist : &'static str =
"VOsc dist\nDistortion.";
pub const damt : &'static str =
"VOsc damt\nDistortion amount.";
pub const ovr : &'static str =
"VOsc ovr\nEnable/Disable oversampling.";
pub const wtype : &'static str =
@ -98,7 +132,7 @@ impl DspNode for VOsc {
fn outputs() -> usize { 1 }
fn set_sample_rate(&mut self, srate: f32) {
self.israte = 1.0 / (srate * 4.0);
self.israte = 1.0 / (srate * (OVERSAMPLING as f32));
self.oversampling.set_sample_rate(srate);
}
@ -117,16 +151,19 @@ impl DspNode for VOsc {
{
use crate::dsp::{out, inp, denorm, denorm_offs, at};
let freq = inp::VOsc::freq(inputs);
let det = inp::VOsc::det(inputs);
let d = inp::VOsc::d(inputs);
let v = inp::VOsc::v(inputs);
let vs = inp::VOsc::vs(inputs);
let out = out::VOsc::sig(outputs);
let ovr = at::VOsc::ovr(atoms);
let freq = inp::VOsc::freq(inputs);
let det = inp::VOsc::det(inputs);
let d = inp::VOsc::d(inputs);
let v = inp::VOsc::v(inputs);
let vs = inp::VOsc::vs(inputs);
let damt = inp::VOsc::damt(inputs);
let out = out::VOsc::sig(outputs);
let ovr = at::VOsc::ovr(atoms);
let dist = at::VOsc::dist(atoms);
let israte = self.israte;
let dist = dist.i() as u8;
let oversample = ovr.i() == 1;
if oversample {
@ -135,12 +172,13 @@ impl DspNode for VOsc {
let v = denorm::VOsc::v(v, frame).clamp(0.0, 1.0);
let d = denorm::VOsc::d(d, frame).clamp(0.0, 1.0);
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 overbuf = self.oversampling.resample_buffer();
for i in 0..4 {
for b in overbuf {
let s = s(phi_vps(self.phase, v + vs, d));
overbuf[i] = s;
*b = apply_distortion(s, damt, dist);
self.phase += freq * israte;
self.phase = self.phase.fract();
@ -148,18 +186,21 @@ impl DspNode for VOsc {
out.write(frame, self.oversampling.downsample());
}
} else {
for frame in 0..ctx.nframes() {
let freq = denorm_offs::VOsc::freq(freq, det.read(frame), frame);
let v = denorm::VOsc::v(v, frame).clamp(0.0, 1.0);
let d = denorm::VOsc::d(d, frame).clamp(0.0, 1.0);
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 s = s(phi_vps(self.phase, v + vs, d));
let s = apply_distortion(s, damt, dist);
out.write(frame, s);
self.phase += freq * (israte * 4.0);
self.phase += freq * (israte * (OVERSAMPLING as f32));
self.phase = self.phase.fract();
}
}
@ -171,15 +212,21 @@ impl DspNode for VOsc {
let israte = 1.0 / 128.0;
Some(Box::new(move |gd: &dyn GraphAtomData, _init: bool, x: f32, _xn: f32| -> f32 {
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();
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();
let damt = NodeId::VOsc(0).inp_param("damt").unwrap().inp();
let dist = NodeId::VOsc(0).inp_param("dist").unwrap().inp();
let v = gd.get_denorm(v as u32).clamp(0.0, 1.0);
let d = gd.get_denorm(d as u32).clamp(0.0, 1.0);
let vs = gd.get_denorm(vs as u32).clamp(0.0, 20.0);
let v = gd.get_denorm(v as u32).clamp(0.0, 1.0);
let d = gd.get_denorm(d as u32).clamp(0.0, 1.0);
let vs = gd.get_denorm(vs as u32).clamp(0.0, 20.0);
let damt = gd.get_denorm(damt as u32);
let dist = gd.get(dist as u32).map(|a| a.i()).unwrap_or(0);
let s = s(phi_vps(x, v + vs, d));
let s = apply_distortion(s, damt, dist as u8);
(s + 1.0) * 0.5
}))
}