implemented oversampling for the VPS oscillator, but it does not seem to do much...

This commit is contained in:
Weird Constructor 2021-08-05 05:48:00 +02:00
parent f8be4ca565
commit 820c0bd69f
3 changed files with 65 additions and 30 deletions

View file

@ -166,7 +166,8 @@ impl ButterLowpass {
// Copyright (c) 2020 jatinchowdhury18 // Copyright (c) 2020 jatinchowdhury18
/// Implements oversampling with a ratio of 4 and a 4 times cascade /// Implements oversampling with a ratio of 4 and a 4 times cascade
/// of Butterworth lowpass filters. /// of Butterworth lowpass filters.
struct Oversampling4x4 { #[derive(Debug, Copy, Clone, Default)]
pub struct Oversampling4x4 {
filters: [Biquad; 4], filters: [Biquad; 4],
buffer: [f32; 4], buffer: [f32; 4],
} }
@ -219,8 +220,9 @@ impl Oversampling4x4 {
pub fn downsample(&mut self) -> f32 { pub fn downsample(&mut self) -> f32 {
let mut ret = 0.0; let mut ret = 0.0;
for s in &mut self.buffer { for s in &mut self.buffer {
ret = *s;
for filt in &mut self.filters { for filt in &mut self.filters {
ret = filt.tick(*s); ret = filt.tick(ret);
} }
} }

View file

@ -71,6 +71,7 @@ use crate::fa_sfilter_type;
use crate::fa_bosc_wtype; use crate::fa_bosc_wtype;
use crate::fa_biqfilt_type; use crate::fa_biqfilt_type;
use crate::fa_biqfilt_ord; use crate::fa_biqfilt_ord;
use crate::fa_vosc_ovr;
use node_amp::Amp; use node_amp::Amp;
use node_sin::Sin; use node_sin::Sin;
@ -488,7 +489,7 @@ define_exp!{n_ftme d_ftme 0.25, 1000.0}
// to reach more exact "1.0". // to reach more exact "1.0".
define_lin!{n_ogin d_ogin 0.0, 2.0} define_lin!{n_ogin d_ogin 0.0, 2.0}
define_lin!{n_vps d_vps 1.0, 10.0} define_lin!{n_vps d_vps 0.0, 20.0}
// A note about the input-indicies: // A note about the input-indicies:
// //
@ -589,7 +590,8 @@ macro_rules! node_list {
(1 det n_det d_det r_det f_det stp_f -0.2, 0.2, 0.0) (1 det n_det d_det r_det f_det stp_f -0.2, 0.2, 0.0)
(2 d n_id n_id r_id f_def stp_d 0.0, 1.0, 0.5) (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) (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, 1.0) (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}
[0 sig], [0 sig],
out => Out UIType::Generic UICategory::IOUtil out => Out UIType::Generic UICategory::IOUtil
(0 ch1 n_id d_id r_id f_def stp_d -1.0, 1.0, 0.0) (0 ch1 n_id d_id r_id f_def stp_d -1.0, 1.0, 0.0)

View file

@ -3,23 +3,22 @@
// See README.md and COPYING for details. // See README.md and COPYING for details.
use crate::nodes::{NodeAudioContext, NodeExecContext}; use crate::nodes::{NodeAudioContext, NodeExecContext};
use crate::dsp::biquad::Oversampling4x4;
use crate::dsp::{ use crate::dsp::{
NodeId, SAtom, ProcBuf, DspNode, LedPhaseVals, NodeContext, NodeId, SAtom, ProcBuf, DspNode, LedPhaseVals, NodeContext,
GraphAtomData, GraphFun, GraphAtomData, GraphFun,
}; };
//#[macro_export] #[macro_export]
//macro_rules! fa_bosc_wtype { ($formatter: expr, $v: expr, $denorm_v: expr) => { { macro_rules! fa_vosc_ovr { ($formatter: expr, $v: expr, $denorm_v: expr) => { {
// let s = let s =
// match ($v.round() as usize) { match ($v.round() as usize) {
// 0 => "Sin", 0 => "Off",
// 1 => "Tri", 1 => "On",
// 2 => "Saw", _ => "?",
// 3 => "Pulse", };
// _ => "?", write!($formatter, "{}", s)
// }; } } }
// write!($formatter, "{}", s)
//} } }
/// A simple amplifier /// A simple amplifier
#[derive(Debug, Clone)] #[derive(Debug, Clone)]
@ -27,6 +26,7 @@ pub struct VOsc {
// osc: PolyBlepOscillator, // osc: PolyBlepOscillator,
israte: f32, israte: f32,
phase: f32, phase: f32,
oversampling: Box<Oversampling4x4>,
} }
impl VOsc { impl VOsc {
@ -36,6 +36,7 @@ impl VOsc {
Self { Self {
israte: 1.0 / 44100.0, israte: 1.0 / 44100.0,
phase: init_phase, phase: init_phase,
oversampling: Box::new(Oversampling4x4::new()),
} }
} }
@ -54,6 +55,8 @@ impl VOsc {
"VOsc v\n\nRange: (0..1)\n"; "VOsc v\n\nRange: (0..1)\n";
pub const vs : &'static str = pub const vs : &'static str =
"VOsc vs\nScaling factor for 'v'.\nRange: (0..1)\n"; "VOsc vs\nScaling factor for 'v'.\nRange: (0..1)\n";
pub const ovr : &'static str =
"VOsc ovr\nEnable/Disable oversampling.";
pub const wtype : &'static str = pub const wtype : &'static str =
"VOsc wtype\nWaveform type\nAvailable waveforms:\n\ "VOsc wtype\nWaveform type\nAvailable waveforms:\n\
Sin - Sine Waveform\n\ Sin - Sine Waveform\n\
@ -95,11 +98,13 @@ impl DspNode for VOsc {
fn outputs() -> usize { 1 } fn outputs() -> usize { 1 }
fn set_sample_rate(&mut self, srate: f32) { fn set_sample_rate(&mut self, srate: f32) {
self.israte = 1.0 / srate; self.israte = 1.0 / (srate * 4.0);
self.oversampling.set_sample_rate(srate);
} }
fn reset(&mut self) { fn reset(&mut self) {
self.phase = 0.0; self.phase = 0.0;
self.oversampling.reset();
// self.osc.reset(); // self.osc.reset();
} }
@ -118,20 +123,46 @@ impl DspNode for VOsc {
let v = inp::VOsc::v(inputs); let v = inp::VOsc::v(inputs);
let vs = inp::VOsc::vs(inputs); let vs = inp::VOsc::vs(inputs);
let out = out::VOsc::sig(outputs); let out = out::VOsc::sig(outputs);
let ovr = at::VOsc::ovr(atoms);
let israte = self.israte; let israte = self.israte;
for frame in 0..ctx.nframes() { let oversample = ovr.i() == 1;
let freq = denorm_offs::VOsc::freq(freq, det.read(frame), frame);
let v = denorm::VOsc::v(v, frame);
let d = denorm::VOsc::d(d, frame);
let vs = denorm::VOsc::vs(vs, frame);
let s = s(phi_vps(self.phase, v * vs, d)); if oversample {
out.write(frame, s); for frame in 0..ctx.nframes() {
let overbuf = self.oversampling.resample_buffer();
self.phase += freq * israte; for i in 0..4 {
self.phase = self.phase.fract(); 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 s = s(phi_vps(self.phase, v + vs, d));
overbuf[i] = s;
self.phase += freq * israte;
self.phase = self.phase.fract();
}
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 s = s(phi_vps(self.phase, v + vs, d));
out.write(frame, s);
self.phase += freq * (israte * 4.0);
self.phase = self.phase.fract();
}
} }
ctx_vals[0].set(out.read(ctx.nframes() - 1)); ctx_vals[0].set(out.read(ctx.nframes() - 1));
@ -145,11 +176,11 @@ impl DspNode for VOsc {
let vs = NodeId::VOsc(0).inp_param("vs").unwrap().inp(); let vs = NodeId::VOsc(0).inp_param("vs").unwrap().inp();
let d = NodeId::VOsc(0).inp_param("d").unwrap().inp(); let d = NodeId::VOsc(0).inp_param("d").unwrap().inp();
let v = gd.get_denorm(v as u32); let v = gd.get_denorm(v as u32).clamp(0.0, 1.0);
let vs = gd.get_denorm(vs as u32); let d = gd.get_denorm(d as u32).clamp(0.0, 1.0);
let d = gd.get_denorm(d as u32); let vs = gd.get_denorm(vs as u32).clamp(0.0, 20.0);
let s = s(phi_vps(x, v * vs, d)); let s = s(phi_vps(x, v + vs, d));
(s + 1.0) * 0.5 (s + 1.0) * 0.5
})) }))
} }