Crate synfx_dsp

Source
Expand description

synfx-dsp DSP real time audio synthesis, effect algorithms and utilities for Rust

Most of the algorithms and implementations in this library have been implemented for HexoDSP and used in HexoSynth. I factored them out, because they seem useful in other contexts too, for instance the synfx-dsp-jit crate.

I collected most of the algorithms in this crate from various GPLv3 compatible sources. They also were partially translated from multiple different C++ projects. I tried to document the source and source license diligently in the comments of this crate. I apologize if any attribution is missing and would welcome patches or reports.

Feel free to use these algorithms and utilities. Help, patches and additions are appreciated if they comply with the GPL-3.0-or-later license and don’t break the test suite in HexoDSP.

Attention: HexoDSP comes with a large test suite that also covers these algorithms. And that is the one that also has to pass if these algorithms are touched. The flip side is, that these implementations are actually covered by a test suite.

Requires Nightly as of 2022-10-02 due to std::simd!

Here is a list of sources parts of this library copied or translated code from:

  • crate::quicker_tanh64 / crate::quicker_tanh
    quickerTanh / quickerTanh64 credits to mopo synthesis library:
    Under GPLv3 or any later.
    Little IO <littleioaudio@gmail.com>
    Matt Tytel <matthewtytel@gmail.com>
  • crate::quick_tanh64 / crate::quick_tanh
    quickTanh / quickTanh64 credits to mopo synthesis library:
    Under GPLv3 or any later.
    Little IO <littleioaudio@gmail.com>
    Matt Tytel <matthewtytel@gmail.com>
  • crate::tanh_approx_drive
    Taken from ValleyAudio
    Copyright Dale Johnson
    https://github.dev/ValleyAudio/ValleyRackFree/tree/v2.0
    Under GPLv3 license
  • crate::AtomicFloat
    Implementation from vst-rs
    https://github.com/RustAudio/vst-rs/blob/master/src/util/atomic_float.rs
    Under MIT License
    Copyright (c) 2015 Marko Mijalkovic
  • crate::Biquad
    The implementation of this Biquad Filter has been adapted from
    SamiPerttu, Copyright (c) 2020, under the MIT License.
    See also: https://github.com/SamiPerttu/fundsp/blob/master/src/filter.rs
  • crate::DattorroReverb
    This file contains a reverb implementation that is based
    on Jon Dattorro's 1997 reverb algorithm. It's also largely
    based on the C++ implementation from ValleyAudio / ValleyRackFree
    
    ValleyRackFree Copyright (C) 2020, Valley Audio Soft, Dale Johnson
    Adapted under the GPL-3.0-or-later License.
    
    See also: https://github.com/ValleyAudio/ValleyRackFree/blob/v1.0/src/Plateau/Dattorro.cpp
         and: https://github.com/ValleyAudio/ValleyRackFree/blob/v1.0/src/Plateau/Dattorro.hpp
    
    And: https://ccrma.stanford.edu/~dattorro/music.html
    And: https://ccrma.stanford.edu/~dattorro/EffectDesignPart1.pdf
  • crate::process_1pole_lowpass / crate::process_1pole_highpass
    one pole lp from valley rack free:
    https://github.com/ValleyAudio/ValleyRackFree/blob/v1.0/src/Common/DSP/OnePoleFilters.cpp
  • crate::process_1pole_tpt_lowpass / crate::process_1pole_tpt_highpass
    one pole from:
    http://www.willpirkle.com/Downloads/AN-4VirtualAnalogFilters.pdf
    (page 5)
  • crate::FixedOnePole
    Fixed one pole with setable pole and gain.
    Implementation taken from tubonitaub / alec-deason
    from https://github.com/alec-deason/virtual_modular/blob/4025f1ef343c2eb9cd74eac07b5350c1e7ec9c09/src/simd_graph.rs#L4292
    under MIT License
  • crate::process_hal_chamberlin_svf
    Hal Chamberlin's State Variable (12dB/oct) filter
    https://www.earlevel.com/main/2003/03/02/the-digital-state-variable-filter/
    Inspired by SynthV1 by Rui Nuno Capela, under the terms of
    GPLv2 or any later:
  • crate::process_simper_svf
    Simper SVF implemented from
    https://cytomic.com/files/dsp/SvfLinearTrapezoidalSin.pdf
    Big thanks go to Andrew Simper @ Cytomic for developing and publishing
    the paper.
  • crate::process_stilson_moog
    Stilson/Moog implementation partly translated from here:
    https://github.com/ddiakopoulos/MoogLadders/blob/master/src/MusicDSPModel.h
    without any copyright as found on musicdsp.org
    (http://www.musicdsp.org/showone.php?id=24).
    
    It's also found on MusicDSP and has probably no proper license anyways.
    See also: https://github.com/ddiakopoulos/MoogLadders
    and https://github.com/rncbc/synthv1/blob/master/src/synthv1_filter.h#L103
    and https://github.com/ddiakopoulos/MoogLadders/blob/master/src/MusicDSPModel.h
  • crate::DCBlockFilter
    translated from Odin 2 Synthesizer Plugin
    Copyright (C) 2020 TheWaveWarden
    under GPLv3 or any later
  • crate::cubic_interpolate
    Hermite interpolation, take from
    https://github.com/eric-wood/delay/blob/main/src/delay.rs#L52
    
    Thanks go to Eric Wood!
    
    For the interpolation code:
    MIT License, Copyright (c) 2021 Eric Wood
  • crate::TriSawLFO
    Adapted from https://github.com/ValleyAudio/ValleyRackFree/blob/v1.0/src/Common/DSP/LFO.hpp
    
    ValleyRackFree Copyright (C) 2020, Valley Audio Soft, Dale Johnson
    Adapted under the GPL-3.0-or-later License.
  • crate::PolyBlepOscillator
    PolyBLEP by Tale
    (slightly modified)
    http://www.kvraudio.com/forum/viewtopic.php?t=375517
    from http://www.martin-finke.de/blog/articles/audio-plugins-018-polyblep-oscillator/
  • crate::VPSOscillator
    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
  • crate::Oversampling
    Loosely adapted from https://github.com/VCVRack/Befaco/blob/v1/src/ChowDSP.hpp
    Copyright (c) 2019-2020 Andrew Belt and Befaco contributors
    Under GPLv-3.0-or-later
    
    Which was originally taken from https://github.com/jatinchowdhury18/ChowDSP-VCV/blob/master/src/shared/AAFilter.hpp
    Copyright (c) 2020 jatinchowdhury18
  • crate::next_xoroshiro128
    Taken from xoroshiro128 crate under MIT License
    Implemented by Matthew Scharley (Copyright 2016)
    https://github.com/mscharley/rust-xoroshiro128
  • crate::u64_to_open01
    Taken from rand::distributions
    Licensed under the Apache License, Version 2.0
    Copyright 2018 Developers of the Rand project.
  • crate::SplitMix64
    Copyright 2018 Developers of the Rand project.
    
    Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
    https://www.apache.org/licenses/LICENSE-2.0> or the MIT license
    <LICENSE-MIT or https://opensource.org/licenses/MIT>, at your
    option. This file may not be copied, modified, or distributed
    except according to those terms.
    - splitmix64 (http://xoroshiro.di.unimi.it/splitmix64.c)
  • crate::f_distort / crate::f_fold_distort
    Ported from LMMS under GPLv2
    * DspEffectLibrary.h - library with template-based inline-effects
    * Copyright (c) 2006-2014 Tobias Doerffel <tobydox/at/users.sourceforge.net>
    
    Original source seems to be musicdsp.org, Author: Bram de Jong
    see also: https://www.musicdsp.org/en/latest/Effects/41-waveshaper.html
  • crate::PolyIIRHalfbandFilter
    Taken from va-filter by Fredemus aka Frederik Halkjær aka RocketPhysician
    https://github.com/Fredemus/va-filter
    Under License GPL-3.0-or-later
    
    originally translated from the freely available source code
    at https://www.musicdsp.org/en/latest/Filters/39-polyphase-filters.html
  • crate::DCFilterX4
    Basic 4 channel SIMD DC-filter from Understanding Digital Signal Processing by Richard Lyons.
    
    Taken from va-filter by Fredemus aka Frederik Halkjær aka RocketPhysician
    https://github.com/Fredemus/va-filter
    Under License GPL-3.0-or-later
  • crate::fh_va::LadderFilter / crate::fh_va::Svf / crate::fh_va::SallenKey
    Taken from va-filter by Fredemus aka Frederik Halkjær aka RocketPhysician
    https://github.com/Fredemus/va-filter
    Under License GPL-3.0-or-later

Modules§

fh_va
This module contains the VA filter code of Fredemus’ aka Frederik Halkjær aka RocketPhysician.

Macros§

assert_decimated_feq
This macro allows you to float compare two vectors to a precision of 0.0001, only every $decimate element will be looked at though. Useful for keeping the fixed value tables in your DSP code small.
assert_decimated_slope_feq
Calculates the (linear) slope between consequtive values in $vec and compares the slopes with $cmp_vec with a precision of 0.0001. This macro only looks at every $decimate slope.
assert_decimated_slope_feq_fine
Calculates the (linear) slope between consequtive values in $vec and compares the slopes with $cmp_vec with a precision of 0.0000001. This macro only looks at every $decimate slope.
assert_decimated_slope_feq_sfine
Calculates the (linear) slope between consequtive values in $vec and compares the slopes with $cmp_vec with a precision of 0.000000001. This macro only looks at every $decimate slope.
assert_slope_feq
Calculates the (linear) slope between consequtive values in $vec and compares the slopes with $cmp_vec with a precision of 0.0001.
assert_vec_feq
This macro allows you to float compare two vectors to a precision of 0.0001.
env_hold_stage
Holds the previous state.current value for $time_ms.
env_sustain_stage
Holds the previous state.current value until $gate drops below crate::TRIG_LOW_THRES.
env_target_stage
Increases/Decreases state.current value until you are at $value within $time_ms.
env_target_stage_lin_time_adj
Increases/Decreases state.current value until you are at $value within an adjusted $time_ms. Depending on how close state.start is to $value, $time_ms is linearily shortened. For this to work, you need to supply the supposed starting value of the envelope.
fa_distort
Returns the name of the distortion selected by the dist_type parameter of the apply_distortion function.

Structs§

AllPass
An all-pass filter based on a delay line.
AtomicFloat
An atomic float that can be asynchronously read/written. Best combined with an Arc<...>.
AtomicFloatPair
The AtomicFloatPair can store two f32 numbers atomically.
Biquad
2nd order IIR filter implemented in normalized Direct Form I.
BiquadCoefs
ChangeTrig
Signal change detector that emits a trigger when the input signal changed.
Comb
CtrlPitchQuantizer
CustomTrigger
Trigger signal detector with custom range.
DCBlockFilter
DCFilterX4
Basic 4 channel SIMD DC-filter from Understanding Digital Signal Processing by Richard Lyons.
DattorroReverb
Dattorro plate reverb implementation.
DelayBuffer
This is a delay buffer/line with linear and cubic interpolation.
EnvADSRParams
EnvRetrigAD
A retriggerable AD (Attack & Decay) envelope with modifyable shapes for the attack and decay.
EnvRetrigADSR
EnvState
Envelope state structure for the macros crate::env_hold_stage, crate::env_target_stage and crate::env_sustain_stage.
FixedOnePole
GateSignal
Gate signal generator for HexoDSP nodes.
OnePoleHPF
OnePoleLPF
Oversampling
Implements oversampling with a ratio of N and a 4 times cascade of Butterworth lowpass filters (~48dB?).
PolyBlepOscillator
This is a band-limited oscillator based on the PolyBlep technique.
PolyIIRHalfbandFilter
This is a polyphase iir halfband filter (cutoff at fs/4) consisting of cascades of allpasses. translated from the freely available source code at https://www.musicdsp.org/en/latest/Filters/39-polyphase-filters.html
Quantizer
RampValue
A ramped value changer, with a configurable time to reach the target value.
RandGen
Random number generator based on xoroshiro128. Requires two internal state variables. You may prefer SplitMix64 or Rng which only use one u64 as state.
Rng
Random number generator based on SplitMix64. Requires two internal state variables. You may prefer SplitMix64 or Rng.
SlewValue
A slew rate limiter, with a configurable time per 1.0 increase.
SplitMix64
A splitmix64 random number generator.
TriSawLFO
An LFO with a variable reverse point, which can go from reverse Saw, to Tri and to Saw, depending on the reverse point.
TrigSignal
Trigger signal generator for HexoDSP nodes.
Trigger
Trigger signal detector for HexoDSP.
TriggerPhaseClock
Generates a phase signal from a trigger/gate input signal.
TriggerSampleClock
VPSOscillator
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.

Constants§

TRIG_HIGH_THRES
The threshold, once reached, will cause a trigger event and signals a logical ‘1’. Anything below this is a logical ‘0’.
TRIG_LOW_THRES
The lower threshold for the schmidt trigger to reset.
TRIG_SIGNAL_LENGTH_MS
! Contains various utilities for trigger signals in a modular synthesizer.

Traits§

DattorroReverbParams
Flt

Functions§

apply_distortion
coef2gain_db
Converts a coefficient/factor to decibels
crossfade
Linear crossfade.
crossfade_clip
Linear crossfade with clipping the v2 result.
crossfade_cpow
Constant power crossfade.
crossfade_drive_tanh
Linear (f32) crossfade with driving the v2 result through a tanh().
crossfade_exp
Exponential crossfade.
crossfade_log
Logarithmic crossfade.
cubic_interpolate
Hermite / Cubic interpolation of a buffer full of samples at the given index. len is the buffer length to consider and wrap the index into. And fract is the fractional part of the index.
f_distort
Signal distortion by Bram de Jong.
f_fold_distort
Foldback Signal distortion
fast_cos
A faster implementation of cosine. It’s not that much faster than Rust’s built in cosine function. But YMMV.
fast_sin
A faster implementation of sine. It’s not that much faster than Rust’s built in sine function. But YMMV.
gain_db2coef
Converts gain in decibels to a factor/coeffient
init_cos_tab
Initializes the cosine wave table for fast_cos and fast_sin.
init_white_noise_tab
Initializes [WHITE_NOISE_TAB]
lerp
Apply linear interpolation between the value a and b.
lerp64
Apply 64bit linear interpolation between the value a and b.
next_xoroshiro128
Given the mutable state generates the next pseudo random number.
note_to_freq
Converts a midi note (0 to 128) to a frequency
process_1pole_highpass
Process a very simple one pole 6dB high pass filter. Useful in various applications.
process_1pole_lowpass
Process a very simple one pole 6dB low pass filter. Useful in various applications, from usage in a synthesizer to damping stuff in a reverb/delay.
process_1pole_tpt_highpass
Process a very simple one pole 6dB high pass filter in TPT form. Useful in various applications.
process_1pole_tpt_lowpass
Process a very simple one pole 6dB low pass filter in TPT form. Useful in various applications, from usage in a synthesizer to damping stuff in a reverb/delay.
process_hal_chamberlin_svf
Process a HAL Chamberlin filter with two delays/state variables that is 12dB. The filter does internal oversampling with very simple decimation to rise the stability for cutoff frequency up to 16kHz.
process_simper_svf
This function processes a Simper SVF with 12dB. It’s a much newer algorithm for filtering and provides easy to calculate multiple outputs.
process_stilson_moog
This function implements a simple Stilson/Moog low pass filter with 24dB. It provides only a low pass output.
quick_tanh
quick_tanh64
quicker_tanh
quicker_tanh64
rand_01
rand_u64
sqrt4_to_pow4
A pow like shape function for exponential envelopes. It’s a bit faster than calling the pow function of Rust.
square_35
square_135
tanh_approx_drive
tanh_levien
Cheap 4 channel tanh to make the filter faster.
tanh_levien_f64
Another tanh approximation. See also tanh_levien.
u64_to_open01
Maps any u64 to a f64 in the open interval [0.0, 1.0).