Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor: change DSPBlock to take new buffer type with separate containers per-channel #13

Merged
merged 15 commits into from
Mar 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .idea/codeStyles/codeStyleConfig.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

112 changes: 61 additions & 51 deletions examples/diodeclipper/src/dsp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@ use std::fmt::Formatter;
use enum_map::Enum;
use num_traits::Zero;

use valib::dsp::buffer::{AudioBufferMut, AudioBufferRef};
use valib::dsp::parameter::{HasParameters, Parameter, SmoothedParam};
use valib::dsp::{DSPBlock, DSP};
use valib::dsp::{DSPMeta, DSPProcess, DSPProcessBlock};
use valib::filters::biquad::Biquad;
use valib::oversample::{Oversample, Oversampled};
use valib::saturators::clippers::{DiodeClipper, DiodeClipperModel};
Expand All @@ -29,27 +30,29 @@ impl<T> DcBlocker<T> {
}
}

impl<T: Scalar> DSP<1, 1> for DcBlocker<T> {
impl<T: Scalar> DSPMeta for DcBlocker<T> {
type Sample = T;

fn process(&mut self, x: [Self::Sample; 1]) -> [Self::Sample; 1] {
self.0.process(x)
fn set_samplerate(&mut self, samplerate: f32) {
self.0.set_samplerate(samplerate);
self.0.update_coefficients(&Biquad::highpass(
T::from_f64((Self::CUTOFF_HZ / samplerate) as f64),
T::from_f64(Self::Q as f64),
));
}

fn reset(&mut self) {
self.0.reset()
fn latency(&self) -> usize {
self.0.latency()
}

fn latency(&self) -> usize {
DSP::latency(&self.0)
fn reset(&mut self) {
self.0.reset()
}
}

fn set_samplerate(&mut self, samplerate: f32) {
DSP::set_samplerate(&mut self.0, samplerate);
self.0.update_coefficients(&Biquad::highpass(
T::from_f64((Self::CUTOFF_HZ / samplerate) as f64),
T::from_f64(Self::Q as f64),
));
impl<T: Scalar> DSPProcess<1, 1> for DcBlocker<T> {
fn process(&mut self, x: [Self::Sample; 1]) -> [Self::Sample; 1] {
self.0.process(x)
}
}

Expand Down Expand Up @@ -151,12 +154,32 @@ impl HasParameters for DspInner {
}
}

impl DSP<1, 1> for DspInner {
impl DSPMeta for DspInner {
type Sample = Sample;

fn set_samplerate(&mut self, samplerate: f32) {
self.nr_model.set_samplerate(samplerate);
self.nr_nr.set_samplerate(samplerate);
}

fn latency(&self) -> usize {
if self.model_switch.get_bool() {
self.nr_model.latency()
} else {
self.nr_nr.latency()
}
}

fn reset(&mut self) {
self.nr_model.reset();
self.nr_nr.reset();
}
}

impl DSPProcess<1, 1> for DspInner {
fn process(&mut self, x: [Self::Sample; 1]) -> [Self::Sample; 1] {
if self.force_reset.has_changed() {
DSP::reset(self)
self.reset()
}
self.update_from_params();

Expand All @@ -170,24 +193,6 @@ impl DSP<1, 1> for DspInner {
.map(|x| x / drive.simd_asinh())
.map(|x| x.cast())
}

fn set_samplerate(&mut self, samplerate: f32) {
DSP::set_samplerate(&mut self.nr_model, samplerate);
DSP::set_samplerate(&mut self.nr_nr, samplerate);
}

fn latency(&self) -> usize {
if self.model_switch.get_bool() {
DSP::latency(&self.nr_model)
} else {
DSP::latency(&self.nr_nr)
}
}

fn reset(&mut self) {
DSP::reset(&mut self.nr_model);
DSP::reset(&mut self.nr_nr);
}
}

pub struct Dsp {
Expand All @@ -196,33 +201,38 @@ pub struct Dsp {
dc_blocker: DcBlocker<Sample>,
}

impl DSPBlock<1, 1> for Dsp {
impl DSPMeta for Dsp {
type Sample = Sample;

fn process_block(&mut self, inputs: &[[Self::Sample; 1]], outputs: &mut [[Self::Sample; 1]]) {
self.inner.process_block(inputs, outputs);

for o in outputs {
*o = self.dc_blocker.process(*o);
}
fn set_samplerate(&mut self, samplerate: f32) {
self.inner.set_samplerate(samplerate);
self.dc_blocker.set_samplerate(samplerate);
}

fn set_samplerate(&mut self, samplerate: f32) {
DSPBlock::set_samplerate(&mut self.inner, samplerate);
DSPBlock::set_samplerate(&mut self.dc_blocker, samplerate);
fn latency(&self) -> usize {
self.inner.latency() + self.dc_blocker.latency()
}

fn max_block_size(&self) -> Option<usize> {
DSPBlock::max_block_size(&self.inner)
fn reset(&mut self) {
self.inner.reset();
self.dc_blocker.reset();
}
}

fn latency(&self) -> usize {
DSPBlock::latency(&self.inner) + DSPBlock::latency(&self.dc_blocker)
impl DSPProcessBlock<1, 1> for Dsp {
fn process_block(
&mut self,
inputs: AudioBufferRef<Self::Sample, 1>,
mut outputs: AudioBufferMut<Self::Sample, 1>,
) {
self.inner.process_block(inputs, outputs.as_mut());
for i in 0..outputs.samples() {
outputs.set_frame(i, self.dc_blocker.process(outputs.get_frame(i)));
}
}

fn reset(&mut self) {
DSPBlock::reset(&mut self.inner);
DSPBlock::reset(&mut self.dc_blocker);
fn max_block_size(&self) -> Option<usize> {
self.inner.max_block_size()
}
}

Expand Down
2 changes: 1 addition & 1 deletion examples/diodeclipper/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use nih_plug::util::db_to_gain;

use dsp::Dsp;
use valib::contrib::nih_plug::{enum_int_param, process_buffer_simd, NihParamsController};
use valib::dsp::DSPBlock;
use valib::dsp::{DSPMeta, DSPProcessBlock};

use crate::dsp::{create_dsp, DiodeType, DspParams};

Expand Down
22 changes: 12 additions & 10 deletions examples/dirty-biquad/src/dsp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use std::fmt::Formatter;

use crate::{MAX_BLOCK_SIZE, OVERSAMPLE};
use valib::dsp::parameter::{HasParameters, Parameter, SmoothedParam};
use valib::dsp::DSP;
use valib::dsp::{DSPMeta, DSPProcess};
use valib::filters::biquad::Biquad;
use valib::oversample::{Oversample, Oversampled};
use valib::saturators::clippers::DiodeClipperModel;
Expand Down Expand Up @@ -108,17 +108,9 @@ impl DspInner {
}
}

impl DSP<1, 1> for DspInner {
impl DSPMeta for DspInner {
type Sample = Sample;

fn process(&mut self, x: [Self::Sample; 1]) -> [Self::Sample; 1] {
self.update_params();
let drive = Sample::splat(self.drive.next_sample());
println!("Drive {}", drive.extract(0));
let x = x.map(|x| x * drive);
self.biquad.process(x).map(|x| x / drive.simd_asinh())
}

fn set_samplerate(&mut self, samplerate: f32) {
self.samplerate = samplerate;
self.drive.set_samplerate(samplerate);
Expand All @@ -139,6 +131,16 @@ impl DSP<1, 1> for DspInner {
}
}

impl DSPProcess<1, 1> for DspInner {
fn process(&mut self, x: [Self::Sample; 1]) -> [Self::Sample; 1] {
self.update_params();
let drive = Sample::splat(self.drive.next_sample());
println!("Drive {}", drive.extract(0));
let x = x.map(|x| x * drive);
self.biquad.process(x).map(|x| x / drive.simd_asinh())
}
}

impl HasParameters for DspInner {
type Enum = DspParameters;

Expand Down
3 changes: 2 additions & 1 deletion examples/dirty-biquad/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ use nih_plug::prelude::*;
use nih_plug::util::db_to_gain;

use valib::contrib::nih_plug::{enum_int_param, process_buffer_simd, NihParamsController};
use valib::dsp::DSPBlock;
use valib::dsp::DSPMeta;

use valib::simd::AutoF32x2;

use crate::dsp::{DspParameters, FilterType, SaturatorType};
Expand Down
47 changes: 27 additions & 20 deletions examples/ladder/src/dsp.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,17 @@
use crate::{MAX_BUFFER_SIZE, OVERSAMPLE};
use enum_map::Enum;
use std::fmt;
use std::fmt::Formatter;

use enum_map::Enum;

use valib::dsp::parameter::{HasParameters, Parameter, SmoothedParam};
use valib::dsp::DSP;
use valib::dsp::{DSPMeta, DSPProcess};
use valib::filters::ladder::{Ideal, Ladder, Transistor, OTA};
use valib::oversample::{Oversample, Oversampled};
use valib::saturators::clippers::DiodeClipperModel;
use valib::saturators::Tanh;
use valib::simd::{AutoF32x2, SimdComplexField, SimdValue};
use valib::simd::{AutoF32x2, SimdValue};

use crate::{MAX_BUFFER_SIZE, OVERSAMPLE};

pub type Sample = AutoF32x2;

Expand All @@ -19,17 +22,9 @@ enum DspLadder {
OTA(Ladder<Sample, OTA<Tanh>>),
}

impl DSP<1, 1> for DspLadder {
impl DSPMeta for DspLadder {
type Sample = Sample;

fn process(&mut self, x: [Self::Sample; 1]) -> [Self::Sample; 1] {
match self {
Self::Ideal(ladder) => ladder.process(x),
Self::Transistor(ladder) => ladder.process(x),
Self::OTA(ladder) => ladder.process(x),
}
}

fn set_samplerate(&mut self, samplerate: f32) {
match self {
Self::Ideal(ladder) => ladder.set_samplerate(samplerate),
Expand All @@ -51,6 +46,16 @@ impl DSP<1, 1> for DspLadder {
}
}

impl DSPProcess<1, 1> for DspLadder {
fn process(&mut self, x: [Self::Sample; 1]) -> [Self::Sample; 1] {
match self {
Self::Ideal(ladder) => ladder.process(x),
Self::Transistor(ladder) => ladder.process(x),
Self::OTA(ladder) => ladder.process(x),
}
}
}

impl DspLadder {
fn set_cutoff(&mut self, fc: Sample) {
match self {
Expand Down Expand Up @@ -148,15 +153,9 @@ impl DspInner {
}
}

impl DSP<1, 1> for DspInner {
impl DSPMeta for DspInner {
type Sample = Sample;

fn process(&mut self, x: [Self::Sample; 1]) -> [Self::Sample; 1] {
self.update_from_parameters();
let drive = Sample::splat(self.drive.next_sample() / 4.0);
self.ladder.process(x.map(|x| x * drive)).map(|x| x / drive)
}

fn set_samplerate(&mut self, samplerate: f32) {
self.samplerate = samplerate;
self.drive.set_samplerate(samplerate);
Expand All @@ -177,6 +176,14 @@ impl DSP<1, 1> for DspInner {
}
}

impl DSPProcess<1, 1> for DspInner {
fn process(&mut self, x: [Self::Sample; 1]) -> [Self::Sample; 1] {
self.update_from_parameters();
let drive = Sample::splat(self.drive.next_sample() / 4.0);
self.ladder.process(x.map(|x| x * drive)).map(|x| x / drive)
}
}

impl HasParameters for DspInner {
type Enum = DspParameters;

Expand Down
9 changes: 6 additions & 3 deletions examples/ladder/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
use crate::dsp::{DspParameters, LadderType};
use std::sync::Arc;

use nih_plug::prelude::*;
use std::sync::{atomic::AtomicBool, Arc};

use valib::contrib::nih_plug::{enum_int_param, process_buffer_simd, NihParamsController};
use valib::dsp::DSPBlock;
use valib::dsp::DSPMeta;

use crate::dsp::{DspParameters, LadderType};

mod dsp;

Expand Down
Loading
Loading