2021-08-04 04:51:31 +00:00
|
|
|
// Copyright (c) 2021 Weird Constructor <weirdconstructor@gmail.com>
|
|
|
|
// This file is a part of HexoDSP. Released under GPL-3.0-or-later.
|
|
|
|
// See README.md and COPYING for details.
|
|
|
|
|
|
|
|
use crate::nodes::{NodeAudioContext, NodeExecContext};
|
2021-08-05 04:12:50 +00:00
|
|
|
use crate::dsp::biquad::Oversampling;
|
|
|
|
use crate::dsp::helpers::{quicker_tanh, f_distort, f_fold_distort};
|
2021-08-04 04:51:31 +00:00
|
|
|
use crate::dsp::{
|
|
|
|
NodeId, SAtom, ProcBuf, DspNode, LedPhaseVals, NodeContext,
|
|
|
|
GraphAtomData, GraphFun,
|
|
|
|
};
|
|
|
|
|
2021-08-05 03:48:00 +00:00
|
|
|
#[macro_export]
|
2021-08-05 04:57:31 +00:00
|
|
|
macro_rules! fa_vosc_ovrsmpl { ($formatter: expr, $v: expr, $denorm_v: expr) => { {
|
2021-08-05 03:48:00 +00:00
|
|
|
let s =
|
|
|
|
match ($v.round() as usize) {
|
|
|
|
0 => "Off",
|
|
|
|
1 => "On",
|
|
|
|
_ => "?",
|
|
|
|
};
|
|
|
|
write!($formatter, "{}", s)
|
|
|
|
} } }
|
2021-08-04 04:51:31 +00:00
|
|
|
|
2021-08-05 04:12:50 +00:00
|
|
|
#[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;
|
|
|
|
|
2021-08-04 04:51:31 +00:00
|
|
|
/// A simple amplifier
|
|
|
|
#[derive(Debug, Clone)]
|
|
|
|
pub struct VOsc {
|
|
|
|
// osc: PolyBlepOscillator,
|
|
|
|
israte: f32,
|
|
|
|
phase: f32,
|
2021-08-05 04:12:50 +00:00
|
|
|
oversampling: Box<Oversampling<OVERSAMPLING>>,
|
2021-08-04 04:51:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl VOsc {
|
|
|
|
pub fn new(nid: &NodeId) -> Self {
|
|
|
|
let init_phase = nid.init_phase();
|
|
|
|
|
|
|
|
Self {
|
|
|
|
israte: 1.0 / 44100.0,
|
|
|
|
phase: init_phase,
|
2021-08-05 04:12:50 +00:00
|
|
|
oversampling: Box::new(Oversampling::new()),
|
2021-08-04 04:51:31 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub const freq : &'static str =
|
|
|
|
"VOsc freq\nBase frequency of the oscillator.\n\nRange: (-1..1)\n";
|
|
|
|
pub const det : &'static str =
|
|
|
|
"VOsc det\nDetune the oscillator in semitones and cents. \
|
|
|
|
the input of this value is rounded to semitones on coarse input. \
|
|
|
|
Fine input lets you detune in cents (rounded). \
|
|
|
|
A signal sent to this port is not rounded.\n\
|
|
|
|
Note: The signal input allows detune +-10 octaves.\
|
|
|
|
\nRange: (Knob -0.2 .. 0.2) / (Signal -1.0 .. 1.0)\n";
|
|
|
|
pub const d : &'static str =
|
|
|
|
"VOsc d\n\nRange: (0..1)\n";
|
|
|
|
pub const v : &'static str =
|
|
|
|
"VOsc v\n\nRange: (0..1)\n";
|
|
|
|
pub const vs : &'static str =
|
|
|
|
"VOsc vs\nScaling factor for 'v'.\nRange: (0..1)\n";
|
2021-08-05 04:12:50 +00:00
|
|
|
pub const dist : &'static str =
|
|
|
|
"VOsc dist\nDistortion.";
|
|
|
|
pub const damt : &'static str =
|
|
|
|
"VOsc damt\nDistortion amount.";
|
2021-08-05 04:57:31 +00:00
|
|
|
pub const ovrsmpl : &'static str =
|
|
|
|
"VOsc ovrsmpl\nEnable/Disable oversampling.";
|
2021-08-04 04:51:31 +00:00
|
|
|
pub const wtype : &'static str =
|
|
|
|
"VOsc wtype\nWaveform type\nAvailable waveforms:\n\
|
|
|
|
Sin - Sine Waveform\n\
|
|
|
|
Tri - Triangle Waveform\n\
|
|
|
|
Saw - Sawtooth Waveform\n\
|
|
|
|
Pulse - Pulse Waveform with configurable pulse width";
|
|
|
|
pub const sig : &'static str =
|
|
|
|
"VOsc sig\nOscillator output\nRange: (-1..1)\n";
|
|
|
|
pub const DESC : &'static str =
|
|
|
|
r#"V Oscillator
|
|
|
|
|
|
|
|
A vector phase shaping oscillator, to create interesting waveforms and
|
|
|
|
ways to manipulate them.
|
|
|
|
"#;
|
|
|
|
pub const HELP : &'static str =
|
|
|
|
r#"VOsc - Vector Phase Shaping Oscillator
|
|
|
|
|
|
|
|
A vector phase shaping oscillator, to create interesting waveforms and
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-08-05 04:32:54 +00:00
|
|
|
#[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 {
|
2021-08-05 04:57:31 +00:00
|
|
|
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
|
|
|
|
}
|
2021-08-05 04:32:54 +00:00
|
|
|
} else {
|
2021-08-05 04:57:31 +00:00
|
|
|
if v < 1.0 {
|
|
|
|
v.clamp(x * 0.5, 1.0)
|
|
|
|
} else {
|
|
|
|
v
|
|
|
|
}
|
2021-08-05 04:32:54 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
v
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-08-04 04:51:31 +00:00
|
|
|
impl DspNode for VOsc {
|
|
|
|
fn outputs() -> usize { 1 }
|
|
|
|
|
|
|
|
fn set_sample_rate(&mut self, srate: f32) {
|
2021-08-05 04:12:50 +00:00
|
|
|
self.israte = 1.0 / (srate * (OVERSAMPLING as f32));
|
2021-08-05 03:48:00 +00:00
|
|
|
self.oversampling.set_sample_rate(srate);
|
2021-08-04 04:51:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fn reset(&mut self) {
|
|
|
|
self.phase = 0.0;
|
2021-08-05 03:48:00 +00:00
|
|
|
self.oversampling.reset();
|
2021-08-04 04:51:31 +00:00
|
|
|
// self.osc.reset();
|
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
|
|
|
fn process<T: NodeAudioContext>(
|
|
|
|
&mut self, ctx: &mut T, _ectx: &mut NodeExecContext,
|
|
|
|
_nctx: &NodeContext,
|
|
|
|
atoms: &[SAtom], inputs: &[ProcBuf],
|
|
|
|
outputs: &mut [ProcBuf], ctx_vals: LedPhaseVals)
|
|
|
|
{
|
|
|
|
use crate::dsp::{out, inp, denorm, denorm_offs, at};
|
|
|
|
|
2021-08-05 04:12:50 +00:00
|
|
|
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);
|
2021-08-05 04:57:31 +00:00
|
|
|
let ovrsmpl = at::VOsc::ovrsmpl(atoms);
|
2021-08-05 04:12:50 +00:00
|
|
|
let dist = at::VOsc::dist(atoms);
|
2021-08-04 04:51:31 +00:00
|
|
|
|
|
|
|
let israte = self.israte;
|
|
|
|
|
2021-08-05 04:12:50 +00:00
|
|
|
let dist = dist.i() as u8;
|
2021-08-05 04:57:31 +00:00
|
|
|
let oversample = ovrsmpl.i() == 1;
|
2021-08-04 04:51:31 +00:00
|
|
|
|
2021-08-05 03:48:00 +00:00
|
|
|
if oversample {
|
|
|
|
for frame in 0..ctx.nframes() {
|
2021-08-05 03:48:27 +00:00
|
|
|
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);
|
2021-08-05 04:32:54 +00:00
|
|
|
let damt = denorm::VOsc::damt(damt, frame).clamp(0.0, 1.0);
|
|
|
|
|
2021-08-05 04:57:31 +00:00
|
|
|
let v = limit_v(d, v + vs);
|
2021-08-04 04:51:31 +00:00
|
|
|
|
2021-08-05 03:48:27 +00:00
|
|
|
let overbuf = self.oversampling.resample_buffer();
|
2021-08-05 04:12:50 +00:00
|
|
|
for b in overbuf {
|
2021-08-05 04:57:31 +00:00
|
|
|
let s = s(phi_vps(self.phase, v, d));
|
2021-08-05 03:48:00 +00:00
|
|
|
|
2021-08-05 04:12:50 +00:00
|
|
|
*b = apply_distortion(s, damt, dist);
|
2021-08-05 03:48:00 +00:00
|
|
|
|
|
|
|
self.phase += freq * israte;
|
|
|
|
self.phase = self.phase.fract();
|
|
|
|
}
|
|
|
|
|
|
|
|
out.write(frame, self.oversampling.downsample());
|
|
|
|
}
|
2021-08-05 04:12:50 +00:00
|
|
|
|
2021-08-05 03:48:00 +00:00
|
|
|
} else {
|
|
|
|
for frame in 0..ctx.nframes() {
|
|
|
|
let freq = denorm_offs::VOsc::freq(freq, det.read(frame), frame);
|
2021-08-05 03:48:27 +00:00
|
|
|
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);
|
2021-08-05 04:12:50 +00:00
|
|
|
let damt = denorm::VOsc::damt(damt, frame).clamp(0.0, 1.0);
|
2021-08-05 03:48:00 +00:00
|
|
|
|
2021-08-05 04:57:31 +00:00
|
|
|
let v = limit_v(d, v + vs);
|
|
|
|
|
|
|
|
let s = s(phi_vps(self.phase, v, d));
|
2021-08-05 04:12:50 +00:00
|
|
|
let s = apply_distortion(s, damt, dist);
|
2021-08-05 03:48:00 +00:00
|
|
|
|
|
|
|
out.write(frame, s);
|
|
|
|
|
2021-08-05 04:12:50 +00:00
|
|
|
self.phase += freq * (israte * (OVERSAMPLING as f32));
|
2021-08-05 03:48:00 +00:00
|
|
|
self.phase = self.phase.fract();
|
|
|
|
}
|
2021-08-04 04:51:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
ctx_vals[0].set(out.read(ctx.nframes() - 1));
|
|
|
|
}
|
|
|
|
|
|
|
|
fn graph_fun() -> Option<GraphFun> {
|
|
|
|
let israte = 1.0 / 128.0;
|
|
|
|
|
|
|
|
Some(Box::new(move |gd: &dyn GraphAtomData, _init: bool, x: f32, _xn: f32| -> f32 {
|
2021-08-05 04:12:50 +00:00
|
|
|
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 damt = gd.get_denorm(damt as u32);
|
|
|
|
let dist = gd.get(dist as u32).map(|a| a.i()).unwrap_or(0);
|
2021-08-04 04:51:31 +00:00
|
|
|
|
2021-08-05 04:57:31 +00:00
|
|
|
let v = limit_v(d, v + vs);
|
2021-08-05 04:32:54 +00:00
|
|
|
|
2021-08-05 04:57:31 +00:00
|
|
|
let s = s(phi_vps(x, v, d));
|
2021-08-05 04:12:50 +00:00
|
|
|
let s = apply_distortion(s, damt, dist as u8);
|
|
|
|
|
2021-08-04 04:51:31 +00:00
|
|
|
(s + 1.0) * 0.5
|
|
|
|
}))
|
|
|
|
}
|
|
|
|
}
|