├── LICENSE_VCV.md ├── EnvFollower.h ├── Schmitt.h ├── TGate.h ├── ParameterInterpolator.h ├── EnvelopeFollowerMod.h ├── Oneiroi_1_2_2Patch.hpp ├── TapTempoOscillator.h ├── Limiter.h ├── DelayLine.h ├── ChaosNoise.h ├── WaveTableBuffer.h ├── CHANGELOG ├── Clock.h ├── Midi.h ├── StereoSineOscillator.h ├── Compressor.h ├── DjFilter.h ├── LorenzAttractor.h ├── Led.h ├── StereoWaveTableOscillator.h ├── README.md ├── StereoSuperSaw.h ├── LooperBuffer.h ├── Modulation.h ├── Oneiroi.h ├── Echo.h ├── Filter.h ├── Resonator.h ├── Ambience.h ├── Looper.h ├── Commons.h └── LICENSE /LICENSE_VCV.md: -------------------------------------------------------------------------------- 1 | All copyright holders (i.e. contributors) listed below have agreed to dual licensing of this repo. The original `GPL-v3-or-later` license still holds. In addition, a commercial license may exist for specific use, for example, for VCV closed source and premium plugins, specifically the Befaco Oneiroi project. 2 | 3 | Contributors (and therefore original copyright holders): 4 | 5 | * hirnlego - Roberto Noris 6 | * hemmer / Ewan Hemingway 7 | -------------------------------------------------------------------------------- /EnvFollower.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include "SignalProcessor.h" 4 | 5 | // Version of EnvelopeFollower with configurable lambda. 6 | class EnvFollower : public SignalProcessor 7 | { 8 | private: 9 | float lambda_; 10 | float y_; 11 | 12 | public: 13 | EnvFollower() 14 | { 15 | lambda_ = 0.995f; 16 | y_ = 0; 17 | } 18 | ~EnvFollower() {} 19 | 20 | void setLambda(float lambda) 21 | { 22 | lambda_ = lambda; 23 | } 24 | 25 | static EnvFollower* create() 26 | { 27 | return new EnvFollower(); 28 | } 29 | static void destroy(EnvFollower* obj) 30 | { 31 | delete obj; 32 | } 33 | 34 | float process(float x) 35 | { 36 | float v = fabs(HardClip(x)); 37 | 38 | y_ = y_ * lambda_ + v * (1.0f - lambda_); 39 | 40 | return Clamp(y_); 41 | } 42 | }; 43 | -------------------------------------------------------------------------------- /Schmitt.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | /** 4 | * @brief A trigger (or gate) generator. 5 | * 6 | */ 7 | class Schmitt 8 | { 9 | public: 10 | Schmitt(bool gate = false, float thres = 0.3f) : g_{gate}, thres_{thres} 11 | { 12 | f_ = false; 13 | t_ = false; 14 | p_ = 0; 15 | } 16 | ~Schmitt() {} 17 | 18 | // If gate, returns true when value is equal or above the threshold, or 19 | // false when it's below. If trigger, returns true at the moment of value 20 | // reaching the threshold and then false. 21 | bool Process(float value) 22 | { 23 | f_ = value < p_; 24 | p_ = value; 25 | 26 | bool t = g_ ? t_ : false; 27 | if (!t_ && !f_ && value >= thres_) 28 | { 29 | t_ = true; 30 | t = true; 31 | } 32 | else if (f_ && value < thres_) 33 | { 34 | t_ = false; 35 | } 36 | 37 | return t; 38 | } 39 | 40 | private: 41 | float thres_; 42 | bool g_, f_, t_; 43 | float p_; 44 | }; -------------------------------------------------------------------------------- /TGate.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include 4 | 5 | /** 6 | * @brief A triggerable gate generator. Takes in a trigger signal, and 7 | * produces a gate whose duration is measured in seconds. 8 | * Ported from https://pbat.ch/sndkit/tgate/ 9 | */ 10 | class TGate 11 | { 12 | public: 13 | TGate() {} 14 | ~TGate() {} 15 | 16 | void Init(float sampleRate) 17 | { 18 | sr_ = sampleRate; 19 | timer_ = 0; 20 | SetDuration(0.002f); 21 | } 22 | 23 | /** 24 | * @param dur In seconds 25 | */ 26 | void SetDuration(float dur) 27 | { 28 | dur_ = dur; 29 | } 30 | 31 | float Process(float trig) 32 | { 33 | float out = 0; 34 | 35 | if (trig != 0) 36 | { 37 | timer_ = dur_ * sr_; 38 | } 39 | 40 | if (timer_ != 0) 41 | { 42 | out = 1.0f; 43 | timer_--; 44 | } 45 | 46 | return out; 47 | } 48 | 49 | private: 50 | uint32_t timer_; 51 | float dur_; 52 | float sr_; 53 | }; -------------------------------------------------------------------------------- /ParameterInterpolator.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include 4 | 5 | class ParameterInterpolator 6 | { 7 | private: 8 | float* state_; 9 | float value_; 10 | float increment_; 11 | 12 | public: 13 | ParameterInterpolator() {} 14 | ParameterInterpolator(float* state, float new_value, size_t size) 15 | { 16 | Init(state, new_value, size); 17 | } 18 | 19 | ParameterInterpolator(float* state, float new_value, float step) 20 | { 21 | state_ = state; 22 | value_ = *state; 23 | increment_ = (new_value - *state) * step; 24 | } 25 | 26 | ~ParameterInterpolator() 27 | { 28 | *state_ = value_; 29 | } 30 | 31 | inline void Init(float* state, float new_value, size_t size) 32 | { 33 | state_ = state; 34 | value_ = *state; 35 | increment_ = (new_value - *state) / static_cast(size); 36 | } 37 | 38 | inline float Next() 39 | { 40 | value_ += increment_; 41 | 42 | return value_; 43 | } 44 | 45 | inline float subsample(float t) 46 | { 47 | return value_ + increment_ * t; 48 | } 49 | }; -------------------------------------------------------------------------------- /EnvelopeFollowerMod.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include "Commons.h" 4 | #include "Oscillator.h" 5 | 6 | class EnvelopeFollowerMod : public OscillatorTemplate 7 | { 8 | private: 9 | PatchCtrls* patchCtrls_; 10 | PatchState* patchState_; 11 | 12 | float s_; 13 | 14 | public: 15 | static constexpr float begin_phase = 0; 16 | static constexpr float end_phase = 1; 17 | 18 | EnvelopeFollowerMod(PatchCtrls* patchCtrls, PatchState* patchState) 19 | { 20 | patchCtrls_ = patchCtrls; 21 | patchState_ = patchState; 22 | s_ = 0; 23 | } 24 | ~EnvelopeFollowerMod() {} 25 | 26 | static EnvelopeFollowerMod* create(PatchCtrls* patchCtrls, PatchState* patchState) 27 | { 28 | return new EnvelopeFollowerMod(patchCtrls, patchState); 29 | } 30 | 31 | static void destroy(EnvelopeFollowerMod* obj) 32 | { 33 | delete obj; 34 | } 35 | 36 | float getSample() 37 | { 38 | return s_; 39 | } 40 | 41 | void setFrequency(float freq) 42 | { 43 | 44 | } 45 | 46 | float generate() override 47 | { 48 | // Smooth the envelope. 49 | float l = MapExpo(patchCtrls_->modSpeed, 0.f, 1.f, 0.998f, 0.85f); 50 | s_ = s_* l + Map(patchState_->inputLevel.getRms(), 0, 0.6f, -0.5f, 0.5f) * (1.f - l); 51 | 52 | return s_; 53 | } 54 | }; -------------------------------------------------------------------------------- /Oneiroi_1_2_2Patch.hpp: -------------------------------------------------------------------------------- 1 | #ifndef __Oneiroi_1_2_2Patch_hpp__ 2 | #define __Oneiroi_1_2_2Patch_hpp__ 3 | 4 | #include "Commons.h" 5 | #include "Ui.h" 6 | #include "Clock.h" 7 | 8 | class Oneiroi_1_2_2Patch : public Patch { 9 | private: 10 | Ui* ui_; 11 | Oneiroi* oneiroi_; 12 | Clock* clock_; 13 | 14 | PatchCtrls patchCtrls; 15 | PatchCvs patchCvs; 16 | PatchState patchState; 17 | 18 | public: 19 | Oneiroi_1_2_2Patch() 20 | { 21 | patchState.sampleRate = getSampleRate(); 22 | patchState.blockRate = getBlockRate(); 23 | patchState.blockSize = getBlockSize(); 24 | ui_ = Ui::create(&patchCtrls, &patchCvs, &patchState); 25 | oneiroi_ = Oneiroi::create(&patchCtrls, &patchCvs, &patchState); 26 | clock_ = Clock::create(&patchCtrls, &patchState); 27 | } 28 | ~Oneiroi_1_2_2Patch() 29 | { 30 | Oneiroi::destroy(oneiroi_); 31 | Ui::destroy(ui_); 32 | Clock::destroy(clock_); 33 | } 34 | 35 | void buttonChanged(PatchButtonId bid, uint16_t value, uint16_t samples) override 36 | { 37 | ui_->ProcessButton(bid, value, samples); 38 | } 39 | 40 | void processMidi(MidiMessage msg) override 41 | { 42 | ui_->ProcessMidi(msg); 43 | } 44 | 45 | void processAudio(AudioBuffer& buffer) override 46 | { 47 | clock_->Process(); 48 | ui_->Poll(); 49 | oneiroi_->Process(buffer); 50 | } 51 | }; 52 | 53 | #endif // __Oneiroi_1_2_2Patch_hpp__ 54 | -------------------------------------------------------------------------------- /TapTempoOscillator.h: -------------------------------------------------------------------------------- 1 | #include "TapTempo.h" 2 | #include "SignalGenerator.h" 3 | #include "Oscillator.h" 4 | 5 | template 6 | class TapTempoOscillator : public AdjustableTapTempo, public SignalGenerator { 7 | protected: 8 | T* oscillator; 9 | public: 10 | TapTempoOscillator(float sr, size_t min_limit, size_t max_limit, T* osc): AdjustableTapTempo(sr, min_limit, max_limit), oscillator(osc) {} 11 | void reset(){ 12 | oscillator->reset(); 13 | } 14 | // void setFrequency(float value){ 15 | // oscillator->setFrequency(value); 16 | // } 17 | float getPhase(){ 18 | return oscillator->getPhase(); 19 | } 20 | void setPhase(float phase){ 21 | oscillator->setPhase(phase); 22 | } 23 | float generate(){ 24 | oscillator->setFrequency(getFrequency()); 25 | return oscillator->generate(); 26 | } 27 | void generate(FloatArray output){ 28 | oscillator->setFrequency(getFrequency()); 29 | oscillator->generate(output); 30 | } 31 | T* getOscillator(){ 32 | return oscillator; 33 | } 34 | static TapTempoOscillator* create(float sample_rate, size_t min_limit, size_t max_limit, float block_rate){ 35 | return new TapTempoOscillator(sample_rate, min_limit, max_limit, T::create(block_rate)); 36 | } 37 | static void destroy(TapTempoOscillator* obj){ 38 | T::destroy(obj->oscillator); 39 | delete obj; 40 | } 41 | }; 42 | 43 | //typedef TapTempoOscillator TapTempoSineOscillator; 44 | //typedef TapTempoOscillator TapTempoAgnesiOscillator; 45 | -------------------------------------------------------------------------------- /Limiter.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include "Commons.h" 4 | 5 | class Limiter 6 | { 7 | private: 8 | float peak_; 9 | 10 | public: 11 | Limiter(float peak) 12 | { 13 | peak_ = peak; 14 | } 15 | ~Limiter() { } 16 | 17 | static Limiter* create(float peak = 0.5f) 18 | { 19 | return new Limiter(peak); 20 | } 21 | 22 | static void destroy(Limiter* obj) 23 | { 24 | delete obj; 25 | } 26 | 27 | // Ported and adapted from Emilie Gillet's Limiter in Rings. 28 | void Process(AudioBuffer& input, AudioBuffer& output, float preGain = 1.f) 29 | { 30 | for (size_t i = 0; i < output.getSize(); i++) 31 | { 32 | float l_pre = input.getSamples(LEFT_CHANNEL).getElement(i) * preGain; 33 | float r_pre = input.getSamples(RIGHT_CHANNEL).getElement(i) * preGain; 34 | 35 | float l_peak = fabs(l_pre); 36 | float r_peak = fabs(r_pre); 37 | float s_peak = fabs(r_pre - l_pre); 38 | float peak = Max(Max(l_peak, r_peak), s_peak); 39 | SLOPE(peak_, peak, 0.05f, 0.00002f); 40 | // Clamp to 8Vpp, clipping softly towards 10Vpp 41 | float gain = (peak_ <= 1.0f ? 1.0f : 1.0f / peak_); 42 | 43 | output.getSamples(LEFT_CHANNEL).setElement(i, SoftLimit(l_pre * gain * 0.8f)); 44 | output.getSamples(RIGHT_CHANNEL).setElement(i, SoftLimit(r_pre * gain * 0.8f)); 45 | } 46 | } 47 | 48 | void ProcessSoft(AudioBuffer& input, AudioBuffer& output) 49 | { 50 | for (size_t i = 0; i < output.getSize(); i++) 51 | { 52 | output.getSamples(LEFT_CHANNEL).setElement(i, SoftLimit(input.getSamples(LEFT_CHANNEL).getElement(i))); 53 | output.getSamples(RIGHT_CHANNEL).setElement(i, SoftLimit(input.getSamples(RIGHT_CHANNEL).getElement(i))); 54 | } 55 | } 56 | }; 57 | -------------------------------------------------------------------------------- /DelayLine.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include "Commons.h" 4 | #include "Interpolator.h" 5 | #include 6 | 7 | class DelayLine 8 | { 9 | private: 10 | FloatArray buffer_; 11 | uint32_t size_, writeIndex_, delay_; 12 | 13 | public: 14 | DelayLine(uint32_t size) 15 | { 16 | size_ = size; 17 | buffer_ = FloatArray::create(size_); 18 | delay_ = size_ - 1; 19 | writeIndex_ = 0; 20 | } 21 | ~DelayLine() 22 | { 23 | FloatArray::destroy(buffer_); 24 | } 25 | 26 | static DelayLine* create(uint32_t size) 27 | { 28 | return new DelayLine(size); 29 | } 30 | 31 | static void destroy(DelayLine* line) 32 | { 33 | delete line; 34 | } 35 | 36 | void clear() 37 | { 38 | buffer_.clear(); 39 | } 40 | 41 | void setDelay(uint32_t delay) 42 | { 43 | delay_ = delay; 44 | } 45 | 46 | inline float readAt(int index) 47 | { 48 | int i = writeIndex_ - index - 1; 49 | if (i < 0) 50 | { 51 | i += size_; 52 | } 53 | 54 | return Clamp(buffer_[i], -3.f, 3.f); 55 | } 56 | 57 | inline float read(float index) 58 | { 59 | size_t idx = (size_t)index; 60 | float y0 = readAt(idx); 61 | float y1 = readAt(idx + 1); 62 | float frac = index - idx; 63 | 64 | return Interpolator::linear(y0, y1, frac); 65 | } 66 | 67 | inline float read(float index1, float index2, float x) 68 | { 69 | float v = read(index1); 70 | if (x == 0) 71 | { 72 | return v; 73 | } 74 | 75 | return v * (1.f - x) + read(index2) * x; 76 | } 77 | 78 | inline void write(float value, int stride = 1) 79 | { 80 | buffer_[writeIndex_] = value; 81 | writeIndex_ += stride; 82 | if (writeIndex_ >= size_) 83 | { 84 | writeIndex_ -= size_; 85 | } 86 | } 87 | }; -------------------------------------------------------------------------------- /ChaosNoise.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include 4 | #include 5 | 6 | constexpr int32_t kHlskChaosNoisePhsMax = 0x1000000L; 7 | constexpr int32_t kHlskChaosNoisePhsMsk = 0x0FFFFFFL; 8 | 9 | /** 10 | * @brief A noise generator that uses a chaos function to produce sound. 11 | * Ported from https://pbat.ch/sndkit/chaosnoise/ 12 | */ 13 | class ChaosNoise 14 | { 15 | public: 16 | ChaosNoise() {} 17 | ~ChaosNoise() {} 18 | 19 | /** 20 | * @param sampleRate 21 | * @param init Initial value, must be between 0 and 1 22 | */ 23 | void Init(float sampleRate, float init = 0.f) 24 | { 25 | y_[0] = init; 26 | y_[1] = 0; 27 | 28 | phs_ = 0; 29 | 30 | maxlens_ = kHlskChaosNoisePhsMax / sampleRate; 31 | 32 | SetChaos(1.5f); 33 | SetFreq(8000.f); 34 | } 35 | 36 | /** 37 | * @param chaos Usually between 1 and 2, but between 0.1 and 0.3 makes 38 | * for an interesting oscillator :) 39 | */ 40 | void SetChaos(float chaos) 41 | { 42 | chaos_ = chaos; 43 | } 44 | 45 | float GetFreq() 46 | { 47 | return freq_; 48 | } 49 | 50 | void SetFreq(float freq) 51 | { 52 | freq_ = freq; 53 | } 54 | 55 | float noise() 56 | { 57 | phs_ += floor(freq_ * maxlens_); 58 | 59 | if (phs_ >= kHlskChaosNoisePhsMax) { 60 | float y; 61 | 62 | phs_ &= kHlskChaosNoisePhsMsk; 63 | y = fabs(chaos_ * y_[0] - y_[1] - 0.05f); 64 | y_[1] = y_[0]; 65 | y_[0] = y; 66 | } 67 | 68 | return y_[0]; 69 | } 70 | 71 | void process(AudioBuffer &buffer) 72 | { 73 | float n = noise(); 74 | buffer.add(n); 75 | } 76 | 77 | float Process() 78 | { 79 | return noise(); 80 | } 81 | 82 | private: 83 | float y_[2]; 84 | float maxlens_, chaos_, freq_; 85 | int32_t phs_; 86 | }; 87 | -------------------------------------------------------------------------------- /WaveTableBuffer.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include "Commons.h" 4 | #include "Interpolator.h" 5 | 6 | class WaveTableBuffer 7 | { 8 | private: 9 | FloatArray* buffer_; 10 | int writeHead_; 11 | 12 | public: 13 | WaveTableBuffer(FloatArray* buffer) 14 | { 15 | buffer_ = buffer; 16 | writeHead_ = 0; 17 | } 18 | ~WaveTableBuffer() {} 19 | 20 | static WaveTableBuffer* create(FloatArray* buffer) 21 | { 22 | return new WaveTableBuffer(buffer); 23 | } 24 | 25 | static void destroy(WaveTableBuffer* obj) 26 | { 27 | delete obj; 28 | } 29 | 30 | inline float ReadLeft(uint32_t position) 31 | { 32 | while (position >= kLooperChannelBufferLength) 33 | { 34 | position -= kLooperChannelBufferLength; 35 | } 36 | while (position < 0) 37 | { 38 | position += kLooperChannelBufferLength; 39 | } 40 | 41 | return buffer_->getElement(position); 42 | } 43 | 44 | inline float ReadRight(uint32_t position) 45 | { 46 | position += kLooperChannelBufferLength; 47 | 48 | while (position >= kLooperTotalBufferLength) 49 | { 50 | position -= kLooperChannelBufferLength; 51 | } 52 | while (position < kLooperChannelBufferLength) 53 | { 54 | position += kLooperChannelBufferLength; 55 | } 56 | 57 | return buffer_->getElement(position); 58 | } 59 | 60 | inline void ReadLinear(float p1, float p2, float x, float &left, float &right) 61 | { 62 | float l0, l1; 63 | float r0, r1; 64 | 65 | uint32_t i1 = uint32_t(p1); 66 | uint32_t i2 = uint32_t(p2); 67 | float f1 = p1 - i1; 68 | float f2 = p2 - i2; 69 | 70 | float x0 = 1.f - x; 71 | 72 | left = Interpolator::linear(ReadLeft(i1), ReadLeft(i1 + 1), f1) * x0 + Interpolator::linear(ReadLeft(i2), ReadLeft(i2 + 1), f2) * x; 73 | right = Interpolator::linear(ReadRight(i1), ReadRight(i1 + 1), f1) * x0 + Interpolator::linear(ReadRight(i2), ReadRight(i2 + 1), f2) * x; 74 | } 75 | }; 76 | -------------------------------------------------------------------------------- /CHANGELOG: -------------------------------------------------------------------------------- 1 | # Changelog 2 | 3 | ## 1.2.2 (2025-10-03) 4 | 5 | - fixed unwanted resonator harmonics (https://github.com/Befaco/Oneiroi/issues/38) 6 | - raised maximum resonator feedback 7 | 8 | ## 1.2.1 (2025-09-22) 9 | 10 | - fixed looper's start and length knobs not working when speed = 0 (https://github.com/Befaco/Oneiroi/issues/50) 11 | - removed LED indication of patch version (relying on the "loaded patch" information here 12 | https://www.rebeltech.org/patch-library/device/) 13 | 14 | ## 1.2.0 (2025-05-12) 15 | 16 | - fixed 0v-1v V/Oct CV range (needs hw fix) () 17 | - fixed supersaw pitch (it was half of both wt and sine) (https://github.com/Befaco/Oneiroi/issues/32) 18 | - removed V/Oct CV attenuation feature 19 | - not using calibration data for knobs and faders anymore 20 | 21 | ## 1.1.1 (2024-12-14) 22 | 23 | - Changed BP filter from biguad to SVF to prevent potential instabilities (from 24 | VCV version, see https://github.com/Befaco/Oneiroi/commit/4bf7fddd1839bae72684bd95c76c348c0f2ba5d1) 25 | 26 | ## 1.1.0 (2024-11-29) 27 | 28 | - Fixed modulation and CV mappings when using attenuverters (#2) 29 | - Fixed looper's backwards playback (#3) 30 | - New crossfade for looper's loop point (#4) 31 | - Fixed sine osc frequency boundaries (#6) 32 | - Raised looper's crossfading time / Fixed crossfading when going backwards (#11) 33 | - Fixed wavetable pitch jittery 34 | - Smoothed wavetable offset 35 | - Improved wavetable sound by passing it through a fixed low shelf filter 36 | - Fixed sine waves unison (#12) 37 | - Fixed wavetable offset CV (#16) 38 | - Removed a bit of lag from osc pitch control and V/OCT input 39 | - Tweaked the oscillators' levels (#17) 40 | - Swapped biquad filters for SVF in DjFilter to save some CPU, especially when 41 | looper is recording 42 | - Added fade in/out when starting/stopping recording to avoid clicks 43 | - Smoothed echo's density (#9) 44 | - Raised looper's start parameter resolution 45 | - DC blocking filters at the beginning and at the end of the audio chain to 46 | reduce low end rumble 47 | - A bit of gain to DjFilter's extremes 48 | - Better fade when looper is triggered 49 | - Fixed clicking of echo's density when sync'd 50 | - Added compressors to echo and ambience 51 | - Displaying patch version with LEDs 52 | - Fixed ambience's reverse spacetime 53 | 54 | ## 1.0.0 (2024-07-16) 55 | 56 | - First release version. 57 | -------------------------------------------------------------------------------- /Clock.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include "Commons.h" 4 | #include "TapTempo.h" 5 | #include "Schmitt.h" 6 | 7 | class Clock 8 | { 9 | private: 10 | PatchCtrls* patchCtrls_; 11 | PatchState* patchState_; 12 | 13 | Schmitt trigger_; 14 | 15 | uint32_t samplesSinceSyncIn_; 16 | ClockSource clockSource_; 17 | bool firstSyncIn_; 18 | 19 | public: 20 | Clock(PatchCtrls* patchCtrls, PatchState* patchState) 21 | { 22 | patchCtrls_ = patchCtrls; 23 | patchState_ = patchState; 24 | 25 | clockSource_ = ClockSource::CLOCK_SOURCE_EXTERNAL; 26 | 27 | patchState_->tempo = TapTempo::create(patchState_->blockRate, kLooperChannelBufferLength); 28 | patchState_->tempo->setFrequency(kInternalClockFreq); 29 | samplesSinceSyncIn_ = kExternalClockLimit; 30 | } 31 | ~Clock() {} 32 | 33 | static Clock* create(PatchCtrls* patchCtrls, PatchState* patchState) 34 | { 35 | return new Clock(patchCtrls, patchState); 36 | } 37 | 38 | static void destroy(Clock* obj) 39 | { 40 | delete obj; 41 | } 42 | 43 | void Process() 44 | { 45 | patchState_->tempo->clock(1); 46 | 47 | patchState_->clockReset = false; 48 | 49 | // Listen to sync in. 50 | if (patchState_->syncIn) 51 | { 52 | samplesSinceSyncIn_ = 0; 53 | firstSyncIn_ = true; 54 | } 55 | 56 | bool externalClock = samplesSinceSyncIn_ < kExternalClockLimit && firstSyncIn_; 57 | if (externalClock) 58 | { 59 | // We received a sync, keep listening. 60 | samplesSinceSyncIn_++; 61 | } 62 | else 63 | { 64 | // We didn't receive a sync, or too much time has passed. 65 | firstSyncIn_ = false; 66 | } 67 | 68 | if (ClockSource::CLOCK_SOURCE_EXTERNAL == patchState_->clockSource && !externalClock) 69 | { 70 | // It looks like that the external clock stopped or is too slow, 71 | // switch to the internal clock. 72 | patchState_->clockSource = ClockSource::CLOCK_SOURCE_INTERNAL; 73 | patchState_->tempo->setFrequency(kInternalClockFreq); 74 | patchState_->clockReset = true; 75 | } 76 | else if (ClockSource::CLOCK_SOURCE_INTERNAL == patchState_->clockSource && externalClock) 77 | { 78 | // Switch to the external clock. 79 | patchState_->clockSource = ClockSource::CLOCK_SOURCE_EXTERNAL; 80 | patchState_->clockReset = true; 81 | } 82 | 83 | size_t s = patchState_->tempo->getPeriodInSamples(); 84 | if (fabs(patchState_->clockSamples - s) > kClockTempoSamplesMin) 85 | { 86 | patchState_->clockSamples = s; 87 | } 88 | 89 | patchState_->clockTick = trigger_.Process(patchState_->tempo->isOn()); 90 | } 91 | }; 92 | -------------------------------------------------------------------------------- /Midi.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include "Commons.h" 4 | 5 | extern PatchProcessor* getInitialisingPatchProcessor(); 6 | 7 | enum ParamMidi { 8 | PARAM_MIDI_LOOPER_SPEED, 9 | PARAM_MIDI_LOOPER_START, 10 | PARAM_MIDI_LOOPER_LENGTH, 11 | PARAM_MIDI_LOOPER_SOS, 12 | PARAM_MIDI_LOOPER_FILTER, 13 | PARAM_MIDI_LOOPER_RECORDING, 14 | PARAM_MIDI_LOOPER_RESAMPLING, 15 | PARAM_MIDI_LOOPER_VOL, 16 | PARAM_MIDI_OSC_PITCH, 17 | PARAM_MIDI_OSC_DETUNE, 18 | PARAM_MIDI_OSC_UNISON, 19 | PARAM_MIDI_OSC1_VOL, 20 | PARAM_MIDI_OSC2_VOL, 21 | PARAM_MIDI_FILTER_CUTOFF, 22 | PARAM_MIDI_FILTER_RESONANCE, 23 | PARAM_MIDI_FILTER_MODE, 24 | PARAM_MIDI_FILTER_POSITION, 25 | PARAM_MIDI_FILTER_VOL, 26 | PARAM_MIDI_RESONATOR_TUNE, 27 | PARAM_MIDI_RESONATOR_FEEDBACK, 28 | PARAM_MIDI_RESONATOR_DISSONANCE, 29 | PARAM_MIDI_RESONATOR_VOL, 30 | PARAM_MIDI_ECHO_REPEATS, 31 | PARAM_MIDI_ECHO_DENSITY, 32 | PARAM_MIDI_ECHO_FILTER, 33 | PARAM_MIDI_ECHO_VOL, 34 | PARAM_MIDI_AMBIENCE_DECAY, 35 | PARAM_MIDI_AMBIENCE_SPACETIME, 36 | PARAM_MIDI_AMBIENCE_AUTOPAN, 37 | PARAM_MIDI_AMBIENCE_VOL, 38 | PARAM_MIDI_MOD_LEVEL, 39 | PARAM_MIDI_MOD_SPEED, 40 | PARAM_MIDI_MOD_TYPE, 41 | PARAM_MIDI_INPUT_VOL, 42 | PARAM_MIDI_RANDOM_MODE, 43 | PARAM_MIDI_RANDOM_AMOUNT, 44 | PARAM_MIDI_OSC_USE_SSWT, 45 | PARAM_MIDI_RANDOMIZE, 46 | PARAM_MIDI_LOOPER_SPEED_CV, 47 | PARAM_MIDI_LOOPER_START_CV, 48 | PARAM_MIDI_LOOPER_LENGTH_CV, 49 | PARAM_MIDI_OSC_PITCH_CV, 50 | PARAM_MIDI_OSC_DETUNE_CV, 51 | PARAM_MIDI_FILTER_CUTOFF_CV, 52 | PARAM_MIDI_RESONATOR_TUNE_CV, 53 | PARAM_MIDI_ECHO_DENSITY_CV, 54 | PARAM_MIDI_AMBIENCE_SPACETIME_CV, 55 | PARAM_MIDI_LAST 56 | }; 57 | 58 | class MidiController 59 | { 60 | private: 61 | float* param_; 62 | 63 | uint8_t cc_; 64 | uint8_t value_; 65 | uint8_t channel_; 66 | uint8_t delta_; 67 | 68 | float offset_; 69 | float mult_; 70 | 71 | public: 72 | MidiController( 73 | float* param, 74 | uint8_t cc, 75 | uint8_t channel, 76 | float offset, 77 | float mult, 78 | uint8_t delta 79 | ) { 80 | param_ = param; 81 | cc_ = cc; 82 | channel_ = channel; 83 | value_ = 0; 84 | offset_ = offset; 85 | mult_ = mult; 86 | delta_ = delta; 87 | } 88 | ~MidiController() {} 89 | 90 | static MidiController* create( 91 | float* param, 92 | uint8_t cc, 93 | uint8_t channel = 0, 94 | float offset = 0, 95 | float mult = 1, 96 | uint8_t delta = 1 97 | ) { 98 | return new MidiController(param, cc, channel, offset, mult, delta); 99 | } 100 | 101 | static void destroy(MidiController* obj) 102 | { 103 | delete obj; 104 | } 105 | 106 | inline void SetValue(float value) 107 | { 108 | *param_ = value; 109 | } 110 | 111 | // Called at block rate 112 | inline void Process() 113 | { 114 | uint8_t value = static_cast(((*param_ + offset_) * mult_) * INT8_MAX); 115 | if (abs(value_ - value) > delta_) 116 | { 117 | value_ = value; 118 | getInitialisingPatchProcessor()->patch->sendMidi(MidiMessage::cc(channel_, cc_, value_)); 119 | } 120 | } 121 | }; 122 | -------------------------------------------------------------------------------- /StereoSineOscillator.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include "Commons.h" 4 | #include "SineOscillator.h" 5 | #include "Schmitt.h" 6 | 7 | class StereoSineOscillator 8 | { 9 | private: 10 | PatchCtrls* patchCtrls_; 11 | PatchCvs* patchCvs_; 12 | PatchState* patchState_; 13 | 14 | SineOscillator* oscs_[2]; 15 | 16 | Schmitt trigger_; 17 | 18 | float oldFreqs_[2]; 19 | 20 | bool fadeOut_, fadeIn_; 21 | float sine1Volume_, sine2Volume_; 22 | 23 | void FadeOut() 24 | { 25 | sine2Volume_ -= kOscSineFadeInc; 26 | if (sine2Volume_ <= 0) 27 | { 28 | sine2Volume_ = 0; 29 | fadeOut_ = false; 30 | fadeIn_ = true; 31 | } 32 | } 33 | 34 | void FadeIn() 35 | { 36 | sine2Volume_ += kOscSineFadeInc; 37 | if (sine2Volume_ >= 0.5f) 38 | { 39 | sine2Volume_ = 0.5f; 40 | fadeIn_ = false; 41 | } 42 | } 43 | 44 | public: 45 | StereoSineOscillator(PatchCtrls* patchCtrls, PatchCvs* patchCvs, PatchState* PatchState) 46 | { 47 | patchCtrls_ = patchCtrls; 48 | patchCvs_ = patchCvs; 49 | patchState_ = PatchState; 50 | 51 | for (size_t i = 0; i < 2; i++) 52 | { 53 | oscs_[i] = SineOscillator::create(patchState_->sampleRate); 54 | } 55 | 56 | fadeOut_ = false; 57 | fadeIn_ = false; 58 | sine1Volume_ = 0.5f; 59 | sine2Volume_ = 0.5f; 60 | } 61 | ~StereoSineOscillator() 62 | { 63 | for (size_t i = 0; i < 2; i++) 64 | { 65 | SineOscillator::destroy(oscs_[i]); 66 | } 67 | } 68 | 69 | static StereoSineOscillator* create(PatchCtrls* patchCtrls, PatchCvs* patchCvs, PatchState* PatchState) 70 | { 71 | return new StereoSineOscillator(patchCtrls, patchCvs, PatchState); 72 | } 73 | 74 | static void destroy(StereoSineOscillator* obj) 75 | { 76 | delete obj; 77 | } 78 | 79 | void Process(AudioBuffer &output) 80 | { 81 | size_t size = output.getSize(); 82 | 83 | float u; 84 | if (patchCtrls_->oscUnison < 0) 85 | { 86 | u = Map(patchCtrls_->oscUnison, -1.f, 0.f, 0.5f, 1.f); 87 | } 88 | else 89 | { 90 | u = Map(patchCtrls_->oscUnison, 0.f, 1.f, 1.f, 2.f); 91 | } 92 | 93 | float f[2]; 94 | f[0] = Clamp(patchCtrls_->oscPitch, kOscFreqMin, kOscFreqMax); 95 | f[1] = Clamp(f[0] * u, kOscFreqMin, kOscFreqMax); 96 | ParameterInterpolator freqParams[2] = {ParameterInterpolator(&oldFreqs_[0], f[0], size), ParameterInterpolator(&oldFreqs_[1], f[1], size)}; 97 | 98 | for (size_t i = 0; i < size; i++) 99 | { 100 | for (size_t j = 0; j < 2; j++) 101 | { 102 | oscs_[j]->setFrequency(freqParams[j].Next()); 103 | } 104 | 105 | if (trigger_.Process(patchState_->oscUnisonCenterFlag) && !fadeOut_) 106 | { 107 | fadeOut_ = true; 108 | } 109 | if (fadeOut_) 110 | { 111 | FadeOut(); 112 | } 113 | else if (fadeIn_) 114 | { 115 | oscs_[1]->setPhase(oscs_[0]->getPhase()); 116 | FadeIn(); 117 | } 118 | 119 | float out = oscs_[0]->generate() * sine1Volume_ + oscs_[1]->generate() * sine2Volume_; 120 | 121 | out *= patchCtrls_->osc1Vol * kOScSineGain; 122 | 123 | output.getSamples(LEFT_CHANNEL).setElement(i, out); 124 | output.getSamples(RIGHT_CHANNEL).setElement(i, out); 125 | } 126 | } 127 | }; -------------------------------------------------------------------------------- /Compressor.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include 4 | #include "Commons.h" 5 | 6 | /** 7 | * https://git.iem.at/audioplugins/IEMPluginSuite/blob/master/resources/Compressor.h 8 | */ 9 | class Compressor 10 | { 11 | private: 12 | float sampleRate_; 13 | float threshold_; 14 | float ratio_, expo_; 15 | float attack_; 16 | float release_; 17 | 18 | // Internal variables 19 | float thrlin_, thrlinr_; 20 | float cteAT_; 21 | float cteRL_; 22 | 23 | // State variables 24 | float leftS1_ = 0.f, rightS1_ = 0.f; 25 | 26 | public: 27 | Compressor(float sampleRate) 28 | { 29 | sampleRate_ = sampleRate; 30 | 31 | setRatio(4.f); 32 | setAttack(1.f); 33 | setRelease(100.f); 34 | setThreshold(-10.f); 35 | }; 36 | ~Compressor() 37 | { 38 | 39 | }; 40 | 41 | static Compressor* create(float sampleRate) 42 | { 43 | return new Compressor(sampleRate); 44 | } 45 | 46 | static void destroy(Compressor* obj) 47 | { 48 | delete obj; 49 | } 50 | 51 | void setThreshold(float value) 52 | { 53 | if (fabs(value - threshold_) < 1.f) 54 | { 55 | return; 56 | } 57 | 58 | threshold_ = value; 59 | thrlin_ = Db2A(threshold_); 60 | thrlinr_ = 1.f / thrlin_; 61 | } 62 | 63 | void setRatio(float value) 64 | { 65 | ratio_ = value; 66 | expo_ = 1.f / ratio_ - 1.f; 67 | } 68 | 69 | void setAttack(float value) 70 | { 71 | attack_ = value; 72 | cteAT_ = exp (-2.f * M_PI * 1000.f / attack_ / sampleRate_); 73 | } 74 | 75 | void setRelease(float value) 76 | { 77 | release_ = value; 78 | cteRL_ = exp (-2.f * M_PI * 1000.f / release_ / sampleRate_); 79 | } 80 | 81 | float process(float input) 82 | { 83 | float leftSideInput = fabs(input); 84 | 85 | // Ballistics filter and envelope generation 86 | float leftCte = (leftSideInput >= leftS1_ ? cteAT_ : cteRL_); 87 | float leftEnv = leftSideInput + leftCte * (leftS1_ - leftSideInput); 88 | leftS1_ = leftEnv; 89 | 90 | // Compressor transfer function 91 | float leftCv = (leftEnv <= thrlin_ ? 1.f : fast_powf(leftEnv * thrlinr_, expo_)); 92 | 93 | // Processing 94 | return input * leftCv; 95 | } 96 | 97 | void process(AudioBuffer &input, AudioBuffer &output) 98 | { 99 | int size = input.getSize(); 100 | FloatArray leftIn = input.getSamples(0); 101 | FloatArray rightIn = input.getSamples(1); 102 | FloatArray leftOut = output.getSamples(0); 103 | FloatArray rightOut = output.getSamples(1); 104 | 105 | for (int i = 0; i < size; i++) 106 | { 107 | // Detector (peak) 108 | float leftSideInput = abs(leftIn[i]); 109 | float rightSideInput = abs(rightIn[i]); 110 | 111 | // Ballistics filter and envelope generation 112 | float leftCte = (leftSideInput >= leftS1_ ? cteAT_ : cteRL_); 113 | float leftEnv = leftSideInput + leftCte * (leftS1_ - leftSideInput); 114 | leftS1_ = leftEnv; 115 | 116 | float rightCte = (rightSideInput >= rightS1_ ? cteAT_ : cteRL_); 117 | float rightEnv = rightSideInput + rightCte * (rightS1_ - rightSideInput); 118 | rightS1_ = rightEnv; 119 | 120 | // Compressor transfer function 121 | float leftCv = (leftEnv <= thrlin_ ? 1.f : fast_powf(leftEnv / thrlin_, expo_)); 122 | float rightCv = (rightEnv <= thrlin_ ? 1.f : fast_powf(rightEnv / thrlin_, expo_)); 123 | 124 | // Processing 125 | leftOut[i] = leftIn[i] * leftCv; 126 | rightOut[i] = rightIn[i] * rightCv; 127 | } 128 | } 129 | }; 130 | -------------------------------------------------------------------------------- /DjFilter.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include "Commons.h" 4 | #include "StateVariableFilter.h" 5 | 6 | class DjFilter 7 | { 8 | private: 9 | enum FilterType 10 | { 11 | NO_FILTER, 12 | LP, 13 | HP, 14 | }; 15 | 16 | StateVariableFilter* lpfs_[2]; 17 | StateVariableFilter* hpfs_[2]; 18 | 19 | FilterType filter_ = FilterType::NO_FILTER; 20 | 21 | float freq_; 22 | float lpfMix_; 23 | float hpfMix_; 24 | float amp_; 25 | 26 | void UpdateFilter() 27 | { 28 | switch (filter_) 29 | { 30 | case FilterType::LP: 31 | lpfs_[LEFT_CHANNEL]->setLowPass(freq_, 0.55f); 32 | lpfs_[RIGHT_CHANNEL]->setLowPass(freq_, 0.55f); 33 | break; 34 | case FilterType::HP: 35 | hpfs_[LEFT_CHANNEL]->setHighPass(freq_, 0.55f); 36 | hpfs_[RIGHT_CHANNEL]->setHighPass(freq_, 0.55f); 37 | break; 38 | 39 | default: 40 | break; 41 | } 42 | } 43 | 44 | public: 45 | DjFilter(float sampleRate) 46 | { 47 | for (size_t i = 0; i < 2; i++) 48 | { 49 | lpfs_[i] = StateVariableFilter::create(sampleRate); 50 | hpfs_[i] = StateVariableFilter::create(sampleRate); 51 | } 52 | 53 | filter_ = FilterType::NO_FILTER; 54 | lpfMix_ = 1.f; 55 | hpfMix_ = 0.f; 56 | amp_ = 1.f; 57 | } 58 | ~DjFilter() 59 | { 60 | for (size_t i = 0; i < 2; i++) 61 | { 62 | StateVariableFilter::destroy(lpfs_[i]); 63 | StateVariableFilter::destroy(hpfs_[i]); 64 | } 65 | } 66 | 67 | static DjFilter* create(float sampleRate) 68 | { 69 | return new DjFilter(sampleRate); 70 | } 71 | 72 | static void destroy(DjFilter* obj) 73 | { 74 | delete obj; 75 | } 76 | 77 | void SetFilter(float value) 78 | { 79 | if (value <= 0.45f) 80 | { 81 | filter_ = FilterType::LP; 82 | lpfMix_ = Map(value, 0.f, 0.45f, 0.f, 1.f); 83 | freq_ = Map(lpfMix_, 0.f, 1.f, 500.f, 2000.f); 84 | UpdateFilter(); 85 | amp_ = Map(lpfMix_, 0.f, 1.f, kDjFilterMakeupGainMax, kDjFilterMakeupGainMin); 86 | } 87 | else if (value >= 0.55f) 88 | { 89 | filter_ = FilterType::HP; 90 | hpfMix_ = Map(value, 0.55f, 1.f, 0.f, 1.f); 91 | freq_ = Map(hpfMix_, 0.f, 1.f, 1000.f, 4000.f); 92 | UpdateFilter(); 93 | amp_ = Map(hpfMix_, 0.f, 1.f, kDjFilterMakeupGainMin, kDjFilterMakeupGainMax); 94 | } 95 | else 96 | { 97 | filter_ = FilterType::NO_FILTER; 98 | } 99 | } 100 | 101 | void Process(float leftIn, float rightIn, float &leftOut, float &rightOut) 102 | { 103 | switch (filter_) 104 | { 105 | case FilterType::LP: 106 | leftOut = LinearCrossFade(lpfs_[LEFT_CHANNEL]->process(leftIn), leftIn, lpfMix_); 107 | rightOut = LinearCrossFade(lpfs_[RIGHT_CHANNEL]->process(rightIn), rightIn, lpfMix_); 108 | break; 109 | case FilterType::HP: 110 | leftOut = LinearCrossFade(leftIn, hpfs_[LEFT_CHANNEL]->process(leftIn), hpfMix_); 111 | rightOut = LinearCrossFade(rightIn, hpfs_[RIGHT_CHANNEL]->process(rightIn), hpfMix_); 112 | break; 113 | default: 114 | leftOut = leftIn; 115 | rightOut = rightIn; 116 | break; 117 | } 118 | 119 | leftOut = Clamp(leftOut, -3.f, 3.f); 120 | rightOut = Clamp(rightOut, -3.f, 3.f); 121 | } 122 | 123 | void Process(AudioBuffer &input, AudioBuffer &output) 124 | { 125 | size_t size = output.getSize(); 126 | 127 | FloatArray leftIn = input.getSamples(LEFT_CHANNEL); 128 | FloatArray rightIn = input.getSamples(RIGHT_CHANNEL); 129 | FloatArray leftOut = output.getSamples(LEFT_CHANNEL); 130 | FloatArray rightOut = output.getSamples(RIGHT_CHANNEL); 131 | 132 | for (size_t i = 0; i < size; i++) 133 | { 134 | Process(leftIn[i] * amp_, rightIn[i] * amp_, leftOut[i], rightOut[i]); 135 | } 136 | } 137 | }; -------------------------------------------------------------------------------- /LorenzAttractor.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include "Commons.h" 4 | #include "Oscillator.h" 5 | 6 | /** 7 | * @brief Lorenz attractor. 8 | * https://en.wikipedia.org/wiki/Lorenz_system 9 | * With adaptations taken from https://github.com/belangeo/pyo/blob/master/src/objects/oscilmodule.c 10 | * 11 | */ 12 | class LorenzAttractor : public OscillatorTemplate 13 | { 14 | private: 15 | float sampleRate_; 16 | float x_, y_, z_, a_, b_, c_, t_, s_; 17 | float xAtt_, yAtt_, zAtt_, max_, min_; 18 | 19 | public: 20 | static constexpr float begin_phase = 0; 21 | static constexpr float end_phase = 1; 22 | 23 | LorenzAttractor(float sampleRate) : sampleRate_{sampleRate} 24 | { 25 | x_ = y_ = z_ = 1.f; 26 | //xAtt_ = 0.044f; 27 | xAtt_ = 1.f; 28 | yAtt_ = 0.0328f; 29 | zAtt_ = 0.0078125f; 30 | max_ = 0; min_ = 0; 31 | 32 | setType(LorenzAttractor::Type::TYPE_TORUS); 33 | setFrequency(1.f); 34 | } 35 | ~LorenzAttractor() {} 36 | 37 | static LorenzAttractor* create(float sr) 38 | { 39 | return new LorenzAttractor(sr); 40 | } 41 | 42 | static void destroy(LorenzAttractor* obj) 43 | { 44 | delete obj; 45 | } 46 | 47 | enum class Type 48 | { 49 | TYPE_DEFAULT, 50 | TYPE_STABLE, 51 | TYPE_ELLIPSE, 52 | TYPE_TORUS, // Unstable 53 | TYPE_LAST, 54 | }; 55 | 56 | void setXAtt(float att) 57 | { 58 | xAtt_ = att; 59 | } 60 | 61 | void setYAtt(float att) 62 | { 63 | yAtt_ = att; 64 | } 65 | 66 | /** 67 | * @param a 1.f/100.f 68 | */ 69 | void SetA(float a) 70 | { 71 | a_ = a; 72 | } 73 | 74 | /** 75 | * @param b For small values, the system is stable and evolves to one of 76 | * two fixed point attractors. When > 24.74f, the fixed points 77 | * become repulsors and the trajectory is repelled by them in a 78 | * very complex way. 79 | */ 80 | void SetB(float b) 81 | { 82 | b_ = b; 83 | } 84 | 85 | /** 86 | * @param c Chaos, 1.f/10.f, optimal values in range 0.5f/3.f 87 | */ 88 | void setChaos(float c) 89 | { 90 | c_ = c; 91 | } 92 | 93 | void setType(LorenzAttractor::Type type) 94 | { 95 | switch (type) 96 | { 97 | case LorenzAttractor::Type::TYPE_DEFAULT: 98 | a_ = 10.f; 99 | b_ = 28.f; 100 | c_ = 8.f / 3.f; 101 | break; 102 | case LorenzAttractor::Type::TYPE_STABLE: 103 | a_ = 10.f; 104 | b_ = 17.f; 105 | c_ = 8.f / 3.f; 106 | break; 107 | case LorenzAttractor::Type::TYPE_ELLIPSE: 108 | a_ = 10.f; 109 | b_ = 20.f; 110 | c_ = 8.f / 3.f; 111 | break; 112 | case LorenzAttractor::Type::TYPE_TORUS: 113 | a_ = 10.f; 114 | b_ = 99.96f; 115 | c_ = 8.f / 3.f; 116 | break; 117 | 118 | default: 119 | a_ = 10.f; 120 | b_ = 28.f; 121 | c_ = 8.f / 3.f; 122 | break; 123 | } 124 | } 125 | 126 | float getSample() 127 | { 128 | return s_; 129 | } 130 | 131 | void setFrequency(float freq) 132 | { 133 | freq *= 0.5f; 134 | t_ = 1.f / (sampleRate_ / Clamp(freq, 0.001, 10.f)); 135 | } 136 | 137 | void process() 138 | { 139 | float xt = x_ + t_ * a_ * (y_ - x_); 140 | float yt = y_ + t_ * (x_ * (b_ - z_) - y_); 141 | float zt = z_ + t_ * (x_ * y_ - c_ * z_); 142 | 143 | x_ = xt; 144 | y_ = yt; 145 | z_ = zt; 146 | } 147 | 148 | float generate() override 149 | { 150 | process(); 151 | 152 | max_ = Max(max_, x_); 153 | min_ = Min(min_, x_); 154 | 155 | s_ = Map(x_, min_, max_, -xAtt_, xAtt_); 156 | 157 | return s_; 158 | } 159 | 160 | void generate(FloatArray xOut, FloatArray yOut) 161 | { 162 | for (size_t i = 0; i < xOut.getSize(); i++) 163 | { 164 | process(); 165 | 166 | xOut[i] = Map(x_, -20.f, 50.f, -xAtt_, xAtt_); 167 | yOut[i] = Map(y_, -20.f, 50.f, -yAtt_, yAtt_); 168 | } 169 | } 170 | }; -------------------------------------------------------------------------------- /Led.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include "Commons.h" 4 | 5 | static const int BLINK_LIMIT = 75; // 50ms (1500 = 1s) 6 | 7 | enum LedName 8 | { 9 | LED_INPUT, 10 | LED_INPUT_PEAK, 11 | LED_SYNC, 12 | LED_MOD, 13 | LED_RECORD, 14 | LED_RANDOM, 15 | LED_ARROW_LEFT, 16 | LED_ARROW_RIGHT, 17 | LED_SHIFT, 18 | LED_MOD_AMOUNT, 19 | LED_CV_AMOUNT, 20 | LED_LAST 21 | }; 22 | 23 | enum LedType 24 | { 25 | LED_TYPE_BUTTON, 26 | LED_TYPE_PARAM, 27 | }; 28 | 29 | class Led 30 | { 31 | private: 32 | Patch* patch_; 33 | 34 | TGate trigger_; 35 | LedType type_; 36 | 37 | int id_; 38 | int blinks_; 39 | int samplesBetweenBlinks_; 40 | 41 | float value_; 42 | 43 | bool trig_; 44 | bool doBlink_; 45 | bool fast_; 46 | 47 | inline void SetLed(float value) 48 | { 49 | if (LedType::LED_TYPE_BUTTON == type_) 50 | { 51 | patch_->setButton(PatchButtonId(id_), value); 52 | } 53 | else 54 | { 55 | patch_->setParameterValue(PatchParameterId(id_), value); 56 | } 57 | } 58 | 59 | public: 60 | Led(int id, LedType type) 61 | { 62 | id_ = id; 63 | type_ = type; 64 | value_ = 0; 65 | trig_ = false; 66 | doBlink_ = false; 67 | trigger_.Init(48000); 68 | samplesBetweenBlinks_ = 0; 69 | } 70 | 71 | ~Led() {} 72 | 73 | static Led* create(int id, LedType type = LedType::LED_TYPE_BUTTON) 74 | { 75 | return new Led(id, type); 76 | } 77 | 78 | static void destroy(Led* obj) 79 | { 80 | delete obj; 81 | } 82 | 83 | inline void Set(float value) 84 | { 85 | value_ = value; 86 | 87 | SetLed(value_); 88 | } 89 | 90 | inline void On() 91 | { 92 | Set(1); 93 | } 94 | 95 | inline void Off() 96 | { 97 | Set(0); 98 | } 99 | 100 | inline void Toggle() 101 | { 102 | Set(value_ == 0 ? 1.f : 1.f - value_); 103 | } 104 | 105 | inline bool IsOn() 106 | { 107 | return value_; 108 | } 109 | 110 | inline bool IsBlinking() 111 | { 112 | return blinks_ > 0; 113 | } 114 | 115 | /** 116 | * @brief Blink the led. 117 | * 118 | * @param blinks number of blinks or -1 for continuous blinking or 0 for stopping 119 | * @param fast 120 | * @param initial the initial state of the led 121 | */ 122 | inline void Blink(int blinks = 1, bool fast = false, bool initial = true) 123 | { 124 | if (blinks == -1 && blinks_ == -1) 125 | { 126 | return; 127 | } 128 | 129 | blinks_ = blinks; 130 | 131 | if (blinks == 0) 132 | { 133 | // Stop blinking. 134 | Off(); 135 | doBlink_ = false; 136 | trig_ = false; 137 | 138 | return; 139 | } 140 | 141 | Set(initial); 142 | trig_ = true; 143 | doBlink_ = true; 144 | fast_ = fast; 145 | if (fast_ && blinks > 0) 146 | { 147 | trigger_.SetDuration(0.002f / blinks_); 148 | } 149 | } 150 | 151 | // Called at block rate 152 | inline void Read() 153 | { 154 | if (doBlink_) 155 | { 156 | if (!trigger_.Process(trig_)) 157 | { 158 | Toggle(); 159 | doBlink_ = false; 160 | // Decrement the number of blinks remaining, 161 | // unless it's -1 (continuous blinking). 162 | if (blinks_ > 0) 163 | { 164 | blinks_--; 165 | if (blinks_ == 0) 166 | { 167 | Off(); 168 | } 169 | } 170 | samplesBetweenBlinks_ = 0; 171 | } 172 | trig_ = false; 173 | } 174 | 175 | if (!doBlink_ && blinks_ != 0) 176 | { 177 | // If we must blink another time, wait a bit before the next one. 178 | if (samplesBetweenBlinks_ < (fast_ ? 75 : 150)) 179 | { 180 | samplesBetweenBlinks_++; 181 | } 182 | else 183 | { 184 | Toggle(); 185 | doBlink_ = true; 186 | trig_ = true; 187 | } 188 | } 189 | } 190 | }; 191 | -------------------------------------------------------------------------------- /StereoWaveTableOscillator.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include "Commons.h" 4 | #include "WaveTableBuffer.h" 5 | #include "BiquadFilter.h" 6 | #include "EnvFollower.h" 7 | #include 8 | #include 9 | #include 10 | 11 | class StereoWaveTableOscillator 12 | { 13 | private: 14 | PatchCtrls* patchCtrls_; 15 | PatchCvs* patchCvs_; 16 | PatchState* patchState_; 17 | 18 | WaveTableBuffer* wtBuffer_; 19 | BiquadFilter* filters_[2]; 20 | EnvFollower* ef_[2]; 21 | 22 | HysteresisQuantizer offsetQuantizer_; 23 | 24 | float amp_; 25 | float oldFreq_; 26 | float oldOffset_; 27 | float phase_; 28 | float incR_; 29 | float xi_; 30 | 31 | public: 32 | StereoWaveTableOscillator(PatchCtrls* patchCtrls, PatchCvs* patchCvs, PatchState* patchState, WaveTableBuffer* wtBuffer) 33 | { 34 | patchCtrls_ = patchCtrls; 35 | patchCvs_ = patchCvs; 36 | patchState_ = patchState; 37 | wtBuffer_ = wtBuffer; 38 | 39 | phase_ = 0; 40 | incR_ = kWaveTableLength / patchState_->sampleRate; 41 | 42 | offsetQuantizer_.Init(kWaveTableNofTables, 0.f, false); 43 | 44 | oldOffset_ = 0; 45 | xi_ = 1.f / patchState_->blockSize; 46 | 47 | for (size_t i = 0; i < 2; i++) 48 | { 49 | filters_[i] = BiquadFilter::create(patchState_->sampleRate); 50 | filters_[i]->setLowShelf(2000, 1); 51 | ef_[i] = EnvFollower::create(); 52 | } 53 | } 54 | ~StereoWaveTableOscillator() 55 | { 56 | for (size_t i = 0; i < 2; i++) 57 | { 58 | BiquadFilter::destroy(filters_[i]); 59 | EnvFollower::destroy(ef_[i]); 60 | } 61 | } 62 | 63 | static StereoWaveTableOscillator* create(PatchCtrls* patchCtrls, PatchCvs* patchCvs, PatchState* patchState, WaveTableBuffer* wtBuffer) 64 | { 65 | return new StereoWaveTableOscillator(patchCtrls, patchCvs, patchState, wtBuffer); 66 | } 67 | 68 | static void destroy(StereoWaveTableOscillator* osc) 69 | { 70 | delete osc; 71 | } 72 | 73 | void Process(AudioBuffer &output) 74 | { 75 | size_t size = output.getSize(); 76 | 77 | float u = patchCtrls_->oscUnison; 78 | if (patchCtrls_->oscUnison < 0) 79 | { 80 | u *= 0.5f; 81 | } 82 | 83 | float f = Modulate(patchCtrls_->oscPitch + patchCtrls_->oscPitch * u, patchCtrls_->oscPitchModAmount, patchState_->modValue, 0, 0, kOscFreqMin, kOscFreqMax, patchState_->modAttenuverters, patchState_->cvAttenuverters); 84 | // Avoid frequency jittering translating to jumps in the wavetable. 85 | if (fabs(oldFreq_ - f) < 1.f) 86 | { 87 | f = oldFreq_; 88 | } 89 | ParameterInterpolator freqParam(&oldFreq_, f, size); 90 | 91 | float o = Modulate(patchCtrls_->oscDetune, patchCtrls_->oscDetuneModAmount, patchState_->modValue, patchCtrls_->oscDetuneCvAmount, patchCvs_->oscDetune, 0, 1.f, patchState_->modAttenuverters, patchState_->cvAttenuverters); 92 | ParameterInterpolator offsetParam(&oldOffset_, o, size); 93 | 94 | for (size_t i = 0; i < size; i++) 95 | { 96 | phase_ += freqParam.Next() * incR_; 97 | if (phase_ >= kWaveTableLength) 98 | { 99 | phase_ -= kWaveTableLength; 100 | } 101 | 102 | float p = offsetParam.Next(); 103 | int q = offsetQuantizer_.Process(p); 104 | float p1 = q * kWaveTableStepLength + phase_; 105 | float p2 = p1 + kWaveTableStepLength; 106 | float x = Clamp((p - kWaveTableNofTablesR * q) / kWaveTableNofTablesR); 107 | 108 | float left; 109 | float right; 110 | wtBuffer_->ReadLinear(p1, p2, x, left, right); 111 | 112 | left *= Map(ef_[LEFT_CHANNEL]->process(left), 0.f, 0.3f, kOScWaveTablePreGain, 1.f); 113 | right *= Map(ef_[RIGHT_CHANNEL]->process(right), 0.f, 0.3f, kOScWaveTablePreGain, 1.f); 114 | 115 | left = SoftClip(left); 116 | right = SoftClip(right); 117 | 118 | left = filters_[LEFT_CHANNEL]->process(left); 119 | right = filters_[RIGHT_CHANNEL]->process(right); 120 | 121 | output.getSamples(LEFT_CHANNEL)[i] = left * patchCtrls_->osc2Vol * kOScWaveTableGain; 122 | output.getSamples(RIGHT_CHANNEL)[i] = right * patchCtrls_->osc2Vol * kOScWaveTableGain; 123 | } 124 | } 125 | }; -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Oneiroi 2 | 3 | Oneiroi is a multi-functional, self-contained experimental digital synth focused on ambient pads and drone-like landscapes. 4 | It features a full stereo signal path, 3 oscillators (2 of them are mutually exclusive), 4 effects and a looper. It also includes self-modulation and a randomizer. 5 | 6 | Oneiroi has been developed by Roberto Noris () and is based on Rebel Technology’s OWL platform. 7 | 8 | Manual: 9 | 10 | ## Updating The patch via browser (Plain human way) 11 | 12 | Open a Web-MIDI-enabled browser. We recommend using Chrome or Chromium. 13 | Other Browsers would maybe work but it has not been tested. 14 | 15 | Find the latest patch from the release page: 16 | 17 | 18 | 19 | Click on "Downloads" and download the file named "patch.syx" 20 | 21 | Connect Oneiroi to a standard Eurorack power supply and your computer using any USBs on the bottom. 22 | 23 | On your browser, go to the OpenWare Laboratory: 24 | 25 | 26 | 27 | Check if Oneiroi is connected to the page: The page should say: "Connected to Oneiroi vx.x.x" 28 | 29 | Click "Choose file" and upload the patch.syx from your computer 30 | 31 | Push "Store". A popup will appear, asking for the slot. Accept to store on slot 1. 32 | 33 | Done! 34 | 35 | 36 | 37 | ## Building and flashing the patch (Nerdy way) 38 | 39 | To build and flash the patch you can either run 40 | 41 | `make PLATFORM=OWL3 PATCHNAME=Oneiroi CONFIG=Release clean load` 42 | 43 | to load it in RAM (temporarily), or 44 | 45 | `make PLATFORM=OWL3 PATCHNAME=Oneiroi CONFIG=Release clean store SLOT=1` 46 | 47 | to store it in FLASH (permanently), or 48 | 49 | `make PLATFORM=OWL3 PATCHNAME=Oneiroi CONFIG=Release clean sysex` 50 | 51 | to generate `./Build/patch.syx` that can be flashed with 52 | the online tool 53 | 54 | 55 | 56 | ## Calibration Procedure for >1.2 Patch/Firmware 57 | 58 | It calibrates V/OCT IN, and Pitch/Speed Knobs mid position 59 | 60 | Updating the patch do not erase the calibration data, so no need to perform calibration after patch update. 61 | 62 | Only needed after module's assembly (usually preformed only at factory or by DIYers). 63 | 64 | 65 | 1- Ensure that there is 0 volts at the CV IN (either with no cable connected or with a cable that measures 0 volts). 66 | Then, power on the module while simultaneously holding down the [MOD AMT] and [SHIFT] buttons. 67 | The [RECORD] button should light up red, indicating that you are in Calibration Mode. 68 | 69 | 2- Place both the PITCH knob and the VARISPEED knob in the middle, this allows for calibrating the center point 70 | 71 | 3- Plug 2 Volts into V/OCT IN and push [RECORD] button. Now [SHIFT] button will light up red. 72 | 73 | 4- Plug 5 Volts into V/OCT IN and push [SHIFT] button. Now [RANDOM] button will light up red. 74 | 75 | 5- Plug 8 Volts into V/OCT IN and push [RANDOM] button. Now [MOD AMT] button will light up green. 76 | 77 | 6- Push [MOD AMT] button to exit calibration mode. 78 | 79 | 80 | 81 | ## Calibration Procedure for 1.1 or older Patch/Firmware versions. 82 | 83 | It calibrates V/OCT IN, Pitch/Speed Knobs mid position and manual Controls range. 84 | 85 | Updating the patch do not erase the calibration data, so no need to perform calibration after patch update. 86 | 87 | Only needed after module's assembly (usually preformed only at factory or by DIYers). 88 | 89 | 90 | 1- Power the module while holding [MOD AMT + SHIFT] buttons at the same time. 91 | The [RECORD] button should light up red indicating you are in Calibration Mode. 92 | 93 | 94 | 2- Place both the PITCH knob and the VARISPEED knob in the middle, this allows for calibrating the center point 95 | 96 | 3- Plug 2 Volts into V/OCT IN and push [RECORD] button. Now [SHIFT] button will light up red. 97 | 98 | 4- Plug 5 Volts into V/OCT IN and push [SHIFT] button. Now [RANDOM] button will light up red. 99 | 100 | 5- Plug 8 Volts into V/OCT IN and push [RANDOM] button. Now [MOD AMT] button will light up green. 101 | 102 | 6- Move all manual controls to the minimum position, this is all pots counter-clockwise and all faders down. 103 | 104 | 7- Move all manual controls to the maximun position, this is all pots clockwise and all faders up. 105 | 106 | 7- Push [MOD AMT] button to exit calibration mode. 107 | 108 | 109 | -------------------------------------------------------------------------------- /StereoSuperSaw.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include "Commons.h" 4 | #include "RampOscillator.h" 5 | 6 | /** 7 | * @brief 7 sawtooth oscillators with detuning and mixing. 8 | * Adapted from 9 | * https://web.archive.org/web/20110627045129/https://www.nada.kth.se/utbildning/grukth/exjobb/rapportlistor/2010/rapporter10/szabo_adam_10131.pdf 10 | * 11 | */ 12 | class SuperSaw 13 | { 14 | private: 15 | AntialiasedRampOscillator* oscs_[7]; 16 | float detunes_[7]; 17 | float volumes_[7]; 18 | float oldFreq_; 19 | float detune_; 20 | 21 | public: 22 | SuperSaw(float sampleRate) 23 | { 24 | for (size_t i = 0; i < 7; i++) 25 | { 26 | oscs_[i] = AntialiasedRampOscillator::create(sampleRate); 27 | detunes_[i] = 0; 28 | volumes_[i] = 0; 29 | } 30 | detune_ = 0; 31 | } 32 | ~SuperSaw() 33 | { 34 | for (size_t i = 0; i < 7; i++) 35 | { 36 | AntialiasedRampOscillator::destroy(oscs_[i]); 37 | } 38 | } 39 | 40 | void SetFreq(float value) 41 | { 42 | for (size_t i = 0; i < 7; i++) 43 | { 44 | oscs_[i]->setFrequency(value * detunes_[i]); 45 | } 46 | } 47 | 48 | void SetDetune(float value, bool minor = false) 49 | { 50 | detune_ = value * 0.4f; 51 | 52 | if (minor) 53 | { 54 | detunes_[0] = 1 - detune_ * 0.877538f; 55 | detunes_[1] = 1 - detune_ * 0.66516f; 56 | detunes_[2] = 1 - detune_ * 0.318207f; 57 | detunes_[3] = 1; 58 | detunes_[4] = 1 + detune_ * 0.189207f; 59 | detunes_[5] = 1 + detune_ * 0.498307f; 60 | detunes_[6] = 1 + detune_ * 0.781797f; 61 | } 62 | else 63 | { 64 | detunes_[0] = 1 - detune_ * 0.11002313f; 65 | detunes_[1] = 1 - detune_ * 0.06288439f; 66 | detunes_[2] = 1 - detune_ * 0.01952356f; 67 | detunes_[3] = 1; 68 | detunes_[4] = 1 + detune_ * 0.01991221f; 69 | detunes_[5] = 1 + detune_ * 0.06216538f; 70 | detunes_[6] = 1 + detune_ * 0.10745242f; 71 | } 72 | 73 | value = Clamp(value * 0.5f, 0.005f, 0.5f); 74 | 75 | float y = -0.73764f * fast_powf(value, 2.f) + 1.2841f * value + 0.044372f; 76 | volumes_[0] = y; 77 | volumes_[1] = y; 78 | volumes_[2] = y; 79 | volumes_[3] = -0.55366f * value + 0.99785f; 80 | volumes_[4] = y; 81 | volumes_[5] = y; 82 | volumes_[6] = y; 83 | } 84 | 85 | static SuperSaw* create(float sampleRate) 86 | { 87 | return new SuperSaw(sampleRate); 88 | } 89 | 90 | static void destroy(SuperSaw* obj) 91 | { 92 | delete obj; 93 | } 94 | 95 | void Process(float freq, FloatArray output) 96 | { 97 | size_t size = output.getSize(); 98 | 99 | ParameterInterpolator freqParam(&oldFreq_, freq, size); 100 | 101 | for (size_t i = 0; i < size; i++) 102 | { 103 | SetFreq(freqParam.Next()); 104 | for (size_t j = 0; j < 7; j++) 105 | { 106 | output[i] += oscs_[j]->generate() * volumes_[j]; 107 | } 108 | } 109 | 110 | output.multiply(0.3f * (1.4f - detune_)); 111 | } 112 | }; 113 | 114 | class StereoSuperSaw 115 | { 116 | private: 117 | PatchCtrls* patchCtrls_; 118 | PatchCvs* patchCvs_; 119 | PatchState* patchState_; 120 | SuperSaw* saws_[2]; 121 | 122 | void SetDetune(float value, bool minor = false) 123 | { 124 | for (int i = 0; i < 2; i++) 125 | { 126 | saws_[i]->SetDetune(value); 127 | } 128 | } 129 | 130 | public: 131 | StereoSuperSaw(PatchCtrls* patchCtrls, PatchCvs* patchCvs, PatchState* patchState) 132 | { 133 | patchCtrls_ = patchCtrls; 134 | patchCvs_ = patchCvs; 135 | patchState_ = patchState; 136 | 137 | for (int i = 0; i < 2; i++) 138 | { 139 | saws_[i] = SuperSaw::create(patchState_->sampleRate); 140 | } 141 | } 142 | ~StereoSuperSaw() 143 | { 144 | for (int i = 0; i < 2; i++) 145 | { 146 | SuperSaw::destroy(saws_[i]); 147 | } 148 | } 149 | 150 | static StereoSuperSaw* create(PatchCtrls* patchCtrls, PatchCvs* patchCvs, PatchState* patchState) 151 | { 152 | return new StereoSuperSaw(patchCtrls, patchCvs, patchState); 153 | } 154 | 155 | static void destroy(StereoSuperSaw* obj) 156 | { 157 | delete obj; 158 | } 159 | 160 | void Process(AudioBuffer &output) 161 | { 162 | float u = patchCtrls_->oscUnison; 163 | if (patchCtrls_->oscUnison < 0) 164 | { 165 | u *= 0.5f; 166 | } 167 | 168 | float f = Modulate(patchCtrls_->oscPitch + patchCtrls_->oscPitch * u, patchCtrls_->oscPitchModAmount, patchState_->modValue, 0, 0, kOscFreqMin, kOscFreqMax, patchState_->modAttenuverters, patchState_->cvAttenuverters); 169 | 170 | float d = Modulate(patchCtrls_->oscDetune, patchCtrls_->oscDetuneModAmount, patchState_->modValue, patchCtrls_->oscDetuneCvAmount, patchCvs_->oscDetune, -1.f, 1.f, patchState_->modAttenuverters, patchState_->cvAttenuverters); 171 | SetDetune(d); 172 | 173 | saws_[LEFT_CHANNEL]->Process(f, output.getSamples(LEFT_CHANNEL)); 174 | saws_[RIGHT_CHANNEL]->Process(f, output.getSamples(RIGHT_CHANNEL)); 175 | 176 | output.multiply(patchCtrls_->osc2Vol * kOScSuperSawGain); 177 | } 178 | }; 179 | -------------------------------------------------------------------------------- /LooperBuffer.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include "Commons.h" 4 | #include "EnvFollower.h" 5 | #include 6 | 7 | enum PlaybackDirection 8 | { 9 | PLAYBACK_STALLED, 10 | PLAYBACK_FORWARD, 11 | PLAYBACK_BACKWARDS = -1 12 | }; 13 | 14 | class WriteHead 15 | { 16 | private: 17 | enum WriteStatus 18 | { 19 | WRITE_STATUS_INACTIVE, 20 | WRITE_STATUS_FADE_IN, 21 | WRITE_STATUS_FADE_OUT, 22 | WRITE_STATUS_ACTIVE, 23 | }; 24 | 25 | FloatArray* buffer_; 26 | 27 | WriteStatus status_; 28 | 29 | uint32_t index_; 30 | uint32_t start_; 31 | uint32_t end_; 32 | 33 | int fadeIndex_; 34 | 35 | bool doFade_; 36 | 37 | public: 38 | WriteHead(FloatArray* buffer) 39 | { 40 | buffer_ = buffer; 41 | 42 | status_ = WRITE_STATUS_INACTIVE; 43 | 44 | fadeIndex_ = 0; 45 | 46 | doFade_ = false; 47 | } 48 | ~WriteHead() {} 49 | 50 | static WriteHead* create(FloatArray* buffer) 51 | { 52 | return new WriteHead(buffer); 53 | } 54 | 55 | static void destroy(WriteHead* obj) 56 | { 57 | delete obj; 58 | } 59 | 60 | inline bool IsWriting() 61 | { 62 | return WRITE_STATUS_INACTIVE != status_; 63 | } 64 | 65 | inline void Start() 66 | { 67 | if (WRITE_STATUS_INACTIVE == status_) 68 | { 69 | status_ = WRITE_STATUS_FADE_IN; 70 | doFade_ = true; 71 | fadeIndex_ = 0; 72 | } 73 | } 74 | 75 | inline void Stop() 76 | { 77 | if (WRITE_STATUS_ACTIVE == status_) 78 | { 79 | status_ = WRITE_STATUS_FADE_OUT; 80 | doFade_ = true; 81 | fadeIndex_ = 0; 82 | } 83 | } 84 | 85 | inline void Write(uint32_t position, float value) 86 | { 87 | while (position >= kLooperTotalBufferLength) 88 | { 89 | position -= kLooperTotalBufferLength; 90 | } 91 | while (position < 0) 92 | { 93 | position += kLooperTotalBufferLength; 94 | } 95 | 96 | if (doFade_) 97 | { 98 | float x = fadeIndex_ * kLooperFadeSamplesR; 99 | if (WRITE_STATUS_FADE_IN == status_) 100 | { 101 | x = 1.f - x; 102 | } 103 | fadeIndex_++; 104 | if (fadeIndex_ == kLooperFadeSamples) 105 | { 106 | x = WRITE_STATUS_FADE_OUT == status_; 107 | doFade_ = false; 108 | status_ = (WRITE_STATUS_FADE_IN == status_ ? WRITE_STATUS_ACTIVE : WRITE_STATUS_INACTIVE); 109 | } 110 | value = CheapEqualPowerCrossFade(value, buffer_->getElement(position), x); 111 | } 112 | 113 | if (WRITE_STATUS_INACTIVE != status_) 114 | { 115 | buffer_->setElement(position, value); 116 | } 117 | } 118 | }; 119 | 120 | class LooperBuffer 121 | { 122 | private: 123 | FloatArray buffer_; 124 | 125 | float* clearBlock_; 126 | 127 | WriteHead* writeHeads_[2]; 128 | 129 | public: 130 | LooperBuffer() 131 | { 132 | buffer_ = FloatArray::create(kLooperTotalBufferLength); 133 | buffer_.noise(); 134 | buffer_.multiply(kLooperNoiseLevel); // Tame the noise a bit 135 | 136 | clearBlock_ = buffer_.getData(); 137 | 138 | for (size_t i = 0; i < 2; i++) 139 | { 140 | writeHeads_[i] = WriteHead::create(&buffer_); 141 | } 142 | } 143 | ~LooperBuffer() 144 | { 145 | for (size_t i = 0; i < 2; i++) 146 | { 147 | WriteHead::destroy(writeHeads_[i]); 148 | } 149 | } 150 | 151 | static LooperBuffer* create() 152 | { 153 | return new LooperBuffer(); 154 | } 155 | 156 | static void destroy(LooperBuffer* obj) 157 | { 158 | delete obj; 159 | } 160 | 161 | FloatArray* GetBuffer() 162 | { 163 | return &buffer_; 164 | } 165 | 166 | inline bool Clear() 167 | { 168 | if (clearBlock_ == buffer_.getData() + kLooperTotalBufferLength) 169 | { 170 | clearBlock_ = buffer_.getData(); 171 | 172 | return true; 173 | } 174 | 175 | memset(clearBlock_, 0, kLooperClearBlockTypeSize); 176 | clearBlock_ += kLooperClearBlockSize; 177 | 178 | return false; 179 | } 180 | 181 | inline void Write(uint32_t i, float left, float right) 182 | { 183 | writeHeads_[LEFT_CHANNEL]->Write(i, left); 184 | writeHeads_[RIGHT_CHANNEL]->Write(i + kLooperChannelBufferLength, right); 185 | } 186 | 187 | inline bool IsRecording() 188 | { 189 | return writeHeads_[LEFT_CHANNEL]->IsWriting() && writeHeads_[RIGHT_CHANNEL]->IsWriting(); 190 | } 191 | 192 | inline void StartRecording() 193 | { 194 | writeHeads_[LEFT_CHANNEL]->Start(); 195 | writeHeads_[RIGHT_CHANNEL]->Start(); 196 | } 197 | 198 | inline void StopRecording() 199 | { 200 | writeHeads_[LEFT_CHANNEL]->Stop(); 201 | writeHeads_[RIGHT_CHANNEL]->Stop(); 202 | } 203 | 204 | inline float ReadLeft(int32_t position) 205 | { 206 | while (position >= kLooperChannelBufferLength) 207 | { 208 | position -= kLooperChannelBufferLength; 209 | } 210 | while (position < 0) 211 | { 212 | position += kLooperChannelBufferLength; 213 | } 214 | 215 | return buffer_[position]; 216 | } 217 | 218 | inline float ReadRight(int32_t position) 219 | { 220 | position += kLooperChannelBufferLength; 221 | 222 | while (position >= kLooperTotalBufferLength) 223 | { 224 | position -= kLooperChannelBufferLength; 225 | } 226 | while (position < kLooperChannelBufferLength) 227 | { 228 | position += kLooperChannelBufferLength; 229 | } 230 | 231 | return buffer_[position]; 232 | } 233 | 234 | inline void Read(float p, float &left, float &right, PlaybackDirection direction = PLAYBACK_FORWARD) 235 | { 236 | float l0, l1; 237 | float r0, r1; 238 | 239 | int32_t i = int32_t(p); 240 | 241 | float f = p - i; 242 | 243 | l0 = ReadLeft(i); 244 | l1 = ReadLeft(i + direction); 245 | left = l0 + direction * (l1 - l0) * f; 246 | 247 | r0 = ReadRight(i); 248 | r1 = ReadRight(i + direction); 249 | right = r0 + direction * (r1 - r0) * f; 250 | } 251 | }; -------------------------------------------------------------------------------- /Modulation.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include "Commons.h" 4 | #include "MorphingOscillator.h" 5 | #include "NoiseOscillator.h" 6 | #include "LorenzAttractor.h" 7 | #include "EnvelopeFollowerMod.h" 8 | #include "Schmitt.h" 9 | 10 | enum ModulationSource 11 | { 12 | MOD_SOURCE_LFO, 13 | MOD_SOURCE_INPUT, 14 | }; 15 | 16 | enum Shape { LORENZ, SINE, RAMP, INVERTED_RAMP, SQUARE, SH, EF, NOF_SHAPES }; 17 | 18 | class Modulation 19 | { 20 | private: 21 | PatchCtrls* patchCtrls_; 22 | PatchCvs* patchCvs_; 23 | PatchState* patchState_; 24 | 25 | ModulationSource source_; 26 | MorphingOscillator* lfo_; 27 | 28 | Schmitt resetTrigger_, speedResetTrigger_, typeResetTrigger_; 29 | 30 | float prevType_; 31 | float prevFreq_; 32 | float phase_; 33 | 34 | bool reset_; 35 | bool speedReset_; 36 | bool typeReset_; 37 | bool freqReset_; 38 | 39 | public: 40 | Modulation(PatchCtrls* patchCtrls, PatchCvs* patchCvs, PatchState* patchState) 41 | { 42 | patchCtrls_ = patchCtrls; 43 | patchCvs_ = patchCvs; 44 | patchState_ = patchState; 45 | 46 | source_ = ModulationSource::MOD_SOURCE_LFO; 47 | 48 | lfo_ = MorphingOscillator::create(NOF_SHAPES, patchState_->blockSize); 49 | lfo_->setOscillator(LORENZ, LorenzAttractor::create(patchState_->blockRate)); 50 | lfo_->setOscillator(SINE, PhaseShiftOscillator::create(0, patchState_->blockRate)); 51 | lfo_->setOscillator(INVERTED_RAMP, PhaseShiftOscillator::create(0, patchState_->blockRate)); 52 | lfo_->setOscillator(RAMP, PhaseShiftOscillator::create(0, patchState_->blockRate)); 53 | lfo_->setOscillator(SQUARE, PhaseShiftOscillator::create(0, patchState_->blockRate)); 54 | lfo_->setOscillator(SH, NoiseOscillator::create(patchState_->blockRate)); 55 | lfo_->setOscillator(EF, EnvelopeFollowerMod::create(patchCtrls_, patchState_)); 56 | lfo_->setFrequency(kInternalClockFreq); 57 | lfo_->morph(0.f); 58 | 59 | prevType_ = 0; 60 | prevFreq_ = 0; 61 | phase_ = 0; 62 | 63 | reset_ = false; 64 | speedReset_ = false; 65 | typeReset_ = false; 66 | freqReset_ = false; 67 | } 68 | ~Modulation() 69 | { 70 | MorphingOscillator::destroy(lfo_); 71 | } 72 | 73 | static Modulation* create(PatchCtrls* patchCtrls, PatchCvs* patchCvs, PatchState* patchState) 74 | { 75 | return new Modulation(patchCtrls, patchCvs, patchState); 76 | } 77 | 78 | static void destroy(Modulation* obj) 79 | { 80 | delete obj; 81 | } 82 | 83 | void Process() 84 | { 85 | // 0 - 0.1667 - 0.3333 - 0.5 - 0.6667 - 0.8333 - 1 86 | if (patchCtrls_->modType <= 0.05f) 87 | { 88 | // Chaos. 89 | patchCtrls_->modType = 0; 90 | patchState_->modTypeLockFlag = true; 91 | typeReset_ = true; 92 | } 93 | else if (patchCtrls_->modType >= 0.1167f && patchCtrls_->modType < 0.2167f) 94 | { 95 | // Sine. 96 | patchCtrls_->modType = 0.1667f; 97 | patchState_->modTypeLockFlag = true; 98 | typeReset_ = true; 99 | } 100 | else if (patchCtrls_->modType >= 0.2833f && patchCtrls_->modType < 0.3833f) 101 | { 102 | // Ramp. 103 | patchCtrls_->modType = 0.3333f; 104 | patchState_->modTypeLockFlag = true; 105 | typeReset_ = true; 106 | } 107 | else if (patchCtrls_->modType >= 0.45f && patchCtrls_->modType < 0.55f) 108 | { 109 | // Saw. 110 | patchCtrls_->modType = 0.5f; 111 | patchState_->modTypeLockFlag = true; 112 | typeReset_ = true; 113 | } 114 | else if (patchCtrls_->modType >= 0.6167f && patchCtrls_->modType < 0.7167f) 115 | { 116 | // Square. 117 | patchCtrls_->modType = 0.6667f; 118 | patchState_->modTypeLockFlag = true; 119 | typeReset_ = true; 120 | } 121 | else if (patchCtrls_->modType >= 0.7833f && patchCtrls_->modType < 0.8833f) 122 | { 123 | // Random. 124 | patchCtrls_->modType = 0.8333f; 125 | patchState_->modTypeLockFlag = true; 126 | typeReset_ = true; 127 | } 128 | else if (patchCtrls_->modType >= 0.95f) 129 | { 130 | // Envelope. 131 | patchCtrls_->modType = 1.f; 132 | patchState_->modTypeLockFlag = true; 133 | typeReset_ = true; 134 | } 135 | else 136 | { 137 | patchState_->modTypeLockFlag = false; 138 | } 139 | 140 | lfo_->morph(patchCtrls_->modType); 141 | lfo_->setFrequency(prevFreq_); 142 | 143 | float s = patchCtrls_->modSpeed; 144 | 145 | // Speed deadband around 12 o'clock. 146 | if (s >= 0.47f && s <= 0.53f) 147 | { 148 | s = 0.5f; 149 | patchState_->modSpeedLockFlag = true; 150 | speedReset_ = true; 151 | } 152 | else 153 | { 154 | patchState_->modSpeedLockFlag = false; 155 | speedReset_ = false; 156 | } 157 | 158 | int i = QuantizeInt(s, kClockNofRatios); 159 | float f = Clamp(patchState_->tempo->getFrequency() * kModClockRatios[i], kClockFreqMin, kClockFreqMax); 160 | if (fabsf(f - prevFreq_) > 0.005f) 161 | { 162 | lfo_->setFrequency(f); 163 | prevFreq_ = f; 164 | } 165 | 166 | // Keep slow LFOs from drifting. 167 | if (patchState_->clockTick) 168 | { 169 | if (s >= 0.5f) 170 | { 171 | reset_ = true; 172 | } 173 | else if (s < 0.5f) 174 | { 175 | phase_++; 176 | if (phase_ == kRModClockRatios[i]) 177 | { 178 | reset_ = true; 179 | phase_ = 0; 180 | } 181 | } 182 | } 183 | 184 | if (patchState_->clockReset) 185 | { 186 | reset_ = true; 187 | } 188 | 189 | // Wait for a clock tick in case we need to reset the LFO. 190 | //if (reset_ && patchState_->clockTick) 191 | if (resetTrigger_.Process(reset_) || speedResetTrigger_.Process(speedReset_) || typeResetTrigger_.Process(typeReset_)) 192 | { 193 | lfo_->reset(); 194 | reset_ = false; 195 | } 196 | 197 | float l = MapExpo(patchCtrls_->modLevel); 198 | patchState_->modValue = l > 0.02f ? lfo_->generate() * l : 0; 199 | } 200 | }; 201 | -------------------------------------------------------------------------------- /Oneiroi.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include "Commons.h" 4 | #include "WaveTableBuffer.h" 5 | #include "StereoSineOscillator.h" 6 | #include "StereoSuperSaw.h" 7 | #include "StereoWaveTableOscillator.h" 8 | #include "Ambience.h" 9 | #include "Filter.h" 10 | #include "Resonator.h" 11 | #include "Echo.h" 12 | #include "Looper.h" 13 | #include "Schmitt.h" 14 | #include "TGate.h" 15 | #include "EnvFollower.h" 16 | #include "DcBlockingFilter.h" 17 | #include "SmoothValue.h" 18 | #include "Modulation.h" 19 | #include "Limiter.h" 20 | 21 | class Oneiroi 22 | { 23 | private: 24 | PatchCtrls* patchCtrls_; 25 | PatchCvs* patchCvs_; 26 | PatchState* patchState_; 27 | 28 | StereoSineOscillator* sine_; 29 | StereoSuperSaw* saw_; 30 | StereoWaveTableOscillator* wt_; 31 | WaveTableBuffer* wtBuffer_; 32 | Filter* filter_; 33 | Resonator* resonator_; 34 | Echo* echo_; 35 | Ambience* ambience_; 36 | Looper* looper_; 37 | Limiter* limiter_; 38 | 39 | Modulation* modulation_; 40 | 41 | AudioBuffer* input_; 42 | AudioBuffer* resample_; 43 | AudioBuffer* osc1Out_; 44 | AudioBuffer* osc2Out_; 45 | 46 | StereoDcBlockingFilter* inputDcFilter_; 47 | StereoDcBlockingFilter* outputDcFilter_; 48 | 49 | EnvFollower* inEnvFollower_[2]; 50 | 51 | FilterPosition filterPosition_, lastFilterPosition_; 52 | 53 | public: 54 | Oneiroi(PatchCtrls* patchCtrls, PatchCvs* patchCvs, PatchState* patchState) 55 | { 56 | patchCtrls_ = patchCtrls; 57 | patchCvs_ = patchCvs; 58 | patchState_ = patchState; 59 | 60 | looper_ = Looper::create(patchCtrls_, patchCvs_, patchState_); 61 | wtBuffer_ = WaveTableBuffer::create(looper_->GetBuffer()); 62 | 63 | sine_ = StereoSineOscillator::create(patchCtrls_, patchCvs_, patchState_); 64 | saw_ = StereoSuperSaw::create(patchCtrls_, patchCvs_, patchState_); 65 | wt_ = StereoWaveTableOscillator::create(patchCtrls_, patchCvs_, patchState_, wtBuffer_); 66 | 67 | filter_ = Filter::create(patchCtrls_, patchCvs_, patchState_); 68 | resonator_ = Resonator::create(patchCtrls_, patchCvs_, patchState_); 69 | echo_ = Echo::create(patchCtrls_, patchCvs_, patchState_); 70 | ambience_ = Ambience::create(patchCtrls_, patchCvs_, patchState_); 71 | 72 | modulation_ = Modulation::create(patchCtrls_, patchCvs_, patchState_); 73 | 74 | limiter_ = Limiter::create(); 75 | 76 | input_ = AudioBuffer::create(2, patchState_->blockSize); 77 | resample_ = AudioBuffer::create(2, patchState_->blockSize); 78 | osc1Out_ = AudioBuffer::create(2, patchState_->blockSize); 79 | osc2Out_ = AudioBuffer::create(2, patchState_->blockSize); 80 | 81 | for (size_t i = 0; i < 2; i++) 82 | { 83 | inEnvFollower_[i] = EnvFollower::create(); 84 | inEnvFollower_[i]->setLambda(0.9f); 85 | } 86 | 87 | inputDcFilter_ = StereoDcBlockingFilter::create(); 88 | outputDcFilter_ = StereoDcBlockingFilter::create(); 89 | } 90 | ~Oneiroi() 91 | { 92 | AudioBuffer::destroy(input_); 93 | AudioBuffer::destroy(resample_); 94 | AudioBuffer::destroy(osc1Out_); 95 | AudioBuffer::destroy(osc2Out_); 96 | WaveTableBuffer::destroy(wtBuffer_); 97 | Looper::destroy(looper_); 98 | StereoSineOscillator::destroy(sine_); 99 | StereoSuperSaw::destroy(saw_); 100 | StereoWaveTableOscillator::destroy(wt_); 101 | Filter::destroy(filter_); 102 | Resonator::destroy(resonator_); 103 | Echo::destroy(echo_); 104 | Ambience::destroy(ambience_); 105 | Modulation::destroy(modulation_); 106 | Limiter::destroy(limiter_); 107 | 108 | for (size_t i = 0; i < 2; i++) 109 | { 110 | EnvFollower::destroy(inEnvFollower_[i]); 111 | } 112 | } 113 | 114 | static Oneiroi* create(PatchCtrls* patchCtrls, PatchCvs* patchCvs, PatchState* patchState) 115 | { 116 | return new Oneiroi(patchCtrls, patchCvs, patchState); 117 | } 118 | 119 | static void destroy(Oneiroi* obj) 120 | { 121 | delete obj; 122 | } 123 | 124 | inline void Process(AudioBuffer &buffer) 125 | { 126 | FloatArray left = buffer.getSamples(LEFT_CHANNEL); 127 | FloatArray right = buffer.getSamples(RIGHT_CHANNEL); 128 | 129 | inputDcFilter_->process(buffer, buffer); 130 | 131 | const int size = buffer.getSize(); 132 | 133 | // Input leds. 134 | for (size_t i = 0; i < size; i++) 135 | { 136 | float l; 137 | if (patchCtrls_->looperResampling) 138 | { 139 | l = Mix2(inEnvFollower_[0]->process(resample_->getSamples(LEFT_CHANNEL)[i]), inEnvFollower_[1]->process(resample_->getSamples(RIGHT_CHANNEL)[i])) * kLooperResampleLedAtt; 140 | } 141 | else 142 | { 143 | l = Mix2(inEnvFollower_[0]->process(left[i]), inEnvFollower_[1]->process(right[i])); 144 | } 145 | patchState_->inputLevel[i] = l; 146 | } 147 | 148 | modulation_->Process(); 149 | 150 | input_->copyFrom(buffer); 151 | input_->multiply(patchCtrls_->inputVol); 152 | 153 | if (patchCtrls_->looperResampling) 154 | { 155 | looper_->Process(*resample_, buffer); 156 | } 157 | else 158 | { 159 | looper_->Process(buffer, buffer); 160 | } 161 | buffer.add(*input_); 162 | 163 | sine_->Process(*osc1Out_); 164 | buffer.add(*osc1Out_); 165 | patchCtrls_->oscUseWavetable > 0.5f ? wt_->Process(*osc2Out_) : saw_->Process(*osc2Out_); 166 | buffer.add(*osc2Out_); 167 | 168 | buffer.multiply(kSourcesMakeupGain); 169 | 170 | if (patchCtrls_->filterPosition < 0.25f) 171 | { 172 | filterPosition_ = FilterPosition::POSITION_1; 173 | } 174 | else if (patchCtrls_->filterPosition >= 0.25f && patchCtrls_->filterPosition < 0.5f) 175 | { 176 | filterPosition_ = FilterPosition::POSITION_2; 177 | } 178 | else if (patchCtrls_->filterPosition >= 0.5f && patchCtrls_->filterPosition < 0.75f) 179 | { 180 | filterPosition_ = FilterPosition::POSITION_3; 181 | } 182 | else if (patchCtrls_->filterPosition >= 0.75f) 183 | { 184 | filterPosition_ = FilterPosition::POSITION_4; 185 | } 186 | 187 | if (filterPosition_ != lastFilterPosition_) 188 | { 189 | lastFilterPosition_ = filterPosition_; 190 | patchState_->filterPositionFlag = true; 191 | } 192 | else 193 | { 194 | patchState_->filterPositionFlag = false; 195 | } 196 | 197 | if (FilterPosition::POSITION_1 == filterPosition_) 198 | { 199 | filter_->process(buffer, buffer); 200 | } 201 | resonator_->process(buffer, buffer); 202 | if (FilterPosition::POSITION_2 == filterPosition_) 203 | { 204 | filter_->process(buffer, buffer); 205 | } 206 | echo_->process(buffer, buffer); 207 | if (FilterPosition::POSITION_3 == filterPosition_) 208 | { 209 | filter_->process(buffer, buffer); 210 | } 211 | ambience_->process(buffer, buffer); 212 | if (FilterPosition::POSITION_4 == filterPosition_) 213 | { 214 | filter_->process(buffer, buffer); 215 | } 216 | 217 | outputDcFilter_->process(buffer, buffer); 218 | 219 | buffer.multiply(kOutputMakeupGain); 220 | limiter_->ProcessSoft(buffer, buffer); 221 | 222 | if (StartupPhase::STARTUP_DONE == patchState_->startupPhase) 223 | { 224 | buffer.multiply(patchState_->outLevel); 225 | } 226 | else 227 | { 228 | // TODO: Fade in 229 | buffer.clear(); 230 | } 231 | 232 | resample_->copyFrom(buffer); 233 | } 234 | }; 235 | 236 | -------------------------------------------------------------------------------- /Echo.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include "Commons.h" 4 | #include "DelayLine.h" 5 | #include "EnvFollower.h" 6 | #include "SineOscillator.h" 7 | #include "ParameterInterpolator.h" 8 | #include "DjFilter.h" 9 | #include "Compressor.h" 10 | #include 11 | 12 | enum EchoTap 13 | { 14 | TAP_LEFT_A, 15 | TAP_LEFT_B, 16 | TAP_RIGHT_A, 17 | TAP_RIGHT_B, 18 | }; 19 | 20 | class Echo 21 | { 22 | private: 23 | PatchCtrls* patchCtrls_; 24 | PatchCvs* patchCvs_; 25 | PatchState* patchState_; 26 | 27 | DelayLine* lines_[kEchoTaps]; 28 | DjFilter* filter_; 29 | EnvFollower* ef_[2]; 30 | Compressor* comp_[2]; 31 | 32 | HysteresisQuantizer densityQuantizer_; 33 | 34 | int clockRatiosIndex_; 35 | float echoDensity_, oldDensity_; 36 | 37 | float levels_[kEchoTaps], outs_[kEchoTaps]; 38 | float tapsTimes_[kEchoTaps], newTapsTimes_[kEchoTaps], maxTapsTimes_[kEchoTaps]; 39 | float repeats_, filterValue_; 40 | float xi_; 41 | 42 | bool externalClock_; 43 | bool infinite_; 44 | 45 | void SetTapTime(int idx, float time) 46 | { 47 | newTapsTimes_[idx] = Clamp(time, kEchoMinLengthSamples * kEchoTapsRatios[idx], (kEchoMaxLengthSamples - 1) * kEchoTapsRatios[idx]); 48 | } 49 | 50 | void SetMaxTapTime(int idx, float time) 51 | { 52 | maxTapsTimes_[idx] = time; 53 | SetTapTime(idx, Max(echoDensity_ * maxTapsTimes_[idx], kEchoMinLengthSamples)); 54 | } 55 | 56 | void SetLevel(int idx, float value) 57 | { 58 | levels_[idx] = value; 59 | } 60 | 61 | void SetFilter(float value) 62 | { 63 | filterValue_ = value; 64 | filter_->SetFilter(value); 65 | } 66 | 67 | void SetRepeats(float value) 68 | { 69 | repeats_ = value; 70 | 71 | float r = repeats_; 72 | 73 | infinite_ = false; 74 | 75 | // Infinite feedback. 76 | if (r > kEchoInfiniteFeedbackThreshold) 77 | { 78 | r = kEchoInfiniteFeedbackLevel; 79 | infinite_ = true; 80 | } 81 | else if (r > 0.99f) 82 | { 83 | r = 1.f; 84 | } 85 | 86 | SetLevel(TAP_LEFT_A, r * kEchoTapsFeedbacks[TAP_LEFT_A]); 87 | SetLevel(TAP_LEFT_B, r * kEchoTapsFeedbacks[TAP_LEFT_B]); 88 | SetLevel(TAP_RIGHT_A, r * kEchoTapsFeedbacks[TAP_RIGHT_A]); 89 | SetLevel(TAP_RIGHT_B, r * kEchoTapsFeedbacks[TAP_RIGHT_B]); 90 | 91 | float thrs = Map(repeats_, 0.f, 1.f, kEchoCompThresMin, kEchoCompThresMax); 92 | comp_[LEFT_CHANNEL]->setThreshold(thrs); 93 | comp_[RIGHT_CHANNEL]->setThreshold(thrs); 94 | } 95 | 96 | void SetDensity(float value) 97 | { 98 | if (ClockSource::CLOCK_SOURCE_EXTERNAL == patchState_->clockSource) 99 | { 100 | int newIndex = densityQuantizer_.Process(value); 101 | if (newIndex == clockRatiosIndex_ && externalClock_) 102 | { 103 | return; 104 | } 105 | clockRatiosIndex_ = newIndex; 106 | 107 | float d = kModClockRatios[clockRatiosIndex_] * patchState_->clockSamples * kEchoExternalClockMultiplier; 108 | for (size_t i = 0; i < kEchoTaps; i++) 109 | { 110 | SetTapTime(i, d * kEchoTapsRatios[i]); 111 | } 112 | 113 | // Reset max tap time the next time (...) the clock switches to internal. 114 | if (!externalClock_) 115 | { 116 | externalClock_ = true; 117 | } 118 | } 119 | else 120 | { 121 | if (externalClock_) 122 | { 123 | int32_t t = kEchoMaxLengthSamples - 1; 124 | for (size_t i = 0; i < kEchoTaps; i++) 125 | { 126 | SetMaxTapTime(i, t * kEchoTapsRatios[i]); 127 | } 128 | externalClock_ = false; 129 | } 130 | 131 | echoDensity_ = value; 132 | 133 | float d = MapExpo(echoDensity_, 0.f, 1.f, kEchoMinLengthSamples, patchState_->clockSamples * kEchoInternalClockMultiplier); 134 | size_t s = kEchoFadeSamples; 135 | ParameterInterpolator densityParam(&oldDensity_, d, s); 136 | 137 | for (size_t i = 0; i < kEchoTaps; i++) 138 | { 139 | SetTapTime(i, densityParam.Next() * kEchoTapsRatios[i]); 140 | } 141 | } 142 | } 143 | 144 | public: 145 | Echo(PatchCtrls* patchCtrls, PatchCvs* patchCvs, PatchState* patchState) 146 | { 147 | patchCtrls_ = patchCtrls; 148 | patchCvs_ = patchCvs; 149 | patchState_ = patchState; 150 | 151 | for (size_t i = 0; i < kEchoTaps; i++) 152 | { 153 | lines_[i] = DelayLine::create(kEchoMaxLengthSamples); 154 | tapsTimes_[i] = kEchoMaxLengthSamples - 1; 155 | SetMaxTapTime(i, tapsTimes_[i] * kEchoTapsRatios[i]); 156 | levels_[i] = 0; 157 | outs_[i] = 0; 158 | } 159 | 160 | echoDensity_ = 1.f; 161 | clockRatiosIndex_ = 0; 162 | 163 | xi_ = 1.f / patchState_->blockSize; 164 | 165 | externalClock_ = false; 166 | infinite_ = false; 167 | 168 | filter_ = DjFilter::create(patchState_->sampleRate); 169 | 170 | for (size_t i = 0; i < 2; i++) 171 | { 172 | comp_[i] = Compressor::create(patchState_->sampleRate); 173 | comp_[i]->setThreshold(-16); 174 | ef_[i] = EnvFollower::create(); 175 | } 176 | 177 | densityQuantizer_.Init(kClockUnityRatioIndex, 0.15f, false); 178 | } 179 | ~Echo() 180 | { 181 | for (size_t i = 0; i < kEchoTaps; i++) 182 | { 183 | DelayLine::destroy(lines_[i]); 184 | } 185 | DjFilter::destroy(filter_); 186 | for (size_t i = 0; i < 2; i++) 187 | { 188 | Compressor::destroy(comp_[i]); 189 | EnvFollower::destroy(ef_[i]); 190 | } 191 | } 192 | 193 | static Echo* create(PatchCtrls* patchCtrls, PatchCvs* patchCvs, PatchState* patchState) 194 | { 195 | return new Echo(patchCtrls, patchCvs, patchState); 196 | } 197 | 198 | static void destroy(Echo* obj) 199 | { 200 | delete obj; 201 | } 202 | 203 | void process(AudioBuffer &input, AudioBuffer &output) 204 | { 205 | size_t size = output.getSize(); 206 | FloatArray leftIn = input.getSamples(LEFT_CHANNEL); 207 | FloatArray rightIn = input.getSamples(RIGHT_CHANNEL); 208 | FloatArray leftOut = output.getSamples(LEFT_CHANNEL); 209 | FloatArray rightOut = output.getSamples(RIGHT_CHANNEL); 210 | 211 | SetFilter(patchCtrls_->echoFilter); 212 | 213 | float d = Modulate(patchCtrls_->echoDensity, patchCtrls_->echoDensityModAmount, patchState_->modValue, patchCtrls_->echoDensityCvAmount, patchCvs_->echoDensity, -1.f, 1.f, patchState_->modAttenuverters, patchState_->cvAttenuverters); 214 | if (externalClock_) 215 | { 216 | SetDensity(d); 217 | } 218 | 219 | float r = Modulate(patchCtrls_->echoRepeats, patchCtrls_->echoRepeatsModAmount, patchState_->modValue, patchCtrls_->echoRepeatsCvAmount, patchCvs_->echoRepeats, -1.f, 1.f, patchState_->modAttenuverters, patchState_->cvAttenuverters); 220 | SetRepeats(r); 221 | 222 | if (StartupPhase::STARTUP_DONE != patchState_->startupPhase) 223 | { 224 | return; 225 | } 226 | 227 | float x = 0; 228 | 229 | for (int i = 0; i < size; i++) 230 | { 231 | // Using crossfade between two different tap times when the clock is 232 | // external and a filtered density param for when the clock is 233 | // internal (for pitch shifting effect). 234 | if (externalClock_) 235 | { 236 | outs_[TAP_LEFT_A] = lines_[TAP_LEFT_A]->read(tapsTimes_[TAP_LEFT_A], newTapsTimes_[TAP_LEFT_A], x); // A 237 | outs_[TAP_LEFT_B] = lines_[TAP_LEFT_B]->read(tapsTimes_[TAP_LEFT_B], newTapsTimes_[TAP_LEFT_B], x); // B 238 | outs_[TAP_RIGHT_A] = lines_[TAP_RIGHT_A]->read(tapsTimes_[TAP_RIGHT_A], newTapsTimes_[TAP_RIGHT_A], x); // A 239 | outs_[TAP_RIGHT_B] = lines_[TAP_RIGHT_B]->read(tapsTimes_[TAP_RIGHT_B], newTapsTimes_[TAP_RIGHT_B], x); // B 240 | 241 | x += xi_; 242 | } 243 | else 244 | { 245 | SetDensity(d); 246 | outs_[TAP_LEFT_A] = lines_[TAP_LEFT_A]->read(newTapsTimes_[TAP_LEFT_A]); // A 247 | outs_[TAP_LEFT_B] = lines_[TAP_LEFT_B]->read(newTapsTimes_[TAP_LEFT_B]); // B 248 | outs_[TAP_RIGHT_A] = lines_[TAP_RIGHT_A]->read(newTapsTimes_[TAP_RIGHT_A]); // A 249 | outs_[TAP_RIGHT_B] = lines_[TAP_RIGHT_B]->read(newTapsTimes_[TAP_RIGHT_B]); // B 250 | } 251 | 252 | float leftFb = HardClip(outs_[TAP_LEFT_A] * levels_[TAP_LEFT_A] + outs_[TAP_RIGHT_A] * levels_[TAP_RIGHT_A]); 253 | float rightFb = HardClip(outs_[TAP_LEFT_B] * levels_[TAP_LEFT_B] + outs_[TAP_RIGHT_B] * levels_[TAP_RIGHT_B]); 254 | 255 | if (infinite_) 256 | { 257 | leftFb *= 1.08f - ef_[LEFT_CHANNEL]->process(leftFb); 258 | rightFb *= 1.08f - ef_[RIGHT_CHANNEL]->process(rightFb); 259 | } 260 | 261 | float lIn = Clamp(leftIn[i], -3.f, 3.f); 262 | float rIn = Clamp(rightIn[i], -3.f, 3.f); 263 | 264 | float leftFilter; 265 | float rightFilter; 266 | 267 | filter_->Process(lIn, rIn, leftFilter, rightFilter); 268 | 269 | leftFb += leftFilter; 270 | rightFb += rightFilter; 271 | 272 | lines_[TAP_LEFT_A]->write(leftFb); 273 | lines_[TAP_LEFT_B]->write(leftFb); 274 | lines_[TAP_RIGHT_A]->write(rightFb); 275 | lines_[TAP_RIGHT_B]->write(rightFb); 276 | 277 | float left = Mix2(outs_[TAP_LEFT_A], outs_[TAP_LEFT_B]); 278 | float right = Mix2(outs_[TAP_RIGHT_A], outs_[TAP_RIGHT_B]); 279 | 280 | left = comp_[LEFT_CHANNEL]->process(left) * kEchoMakeupGain; 281 | right = comp_[RIGHT_CHANNEL]->process(right) * kEchoMakeupGain; 282 | 283 | leftOut[i] = CheapEqualPowerCrossFade(lIn, left, patchCtrls_->echoVol, 1.8f); 284 | rightOut[i] = CheapEqualPowerCrossFade(rIn, right, patchCtrls_->echoVol, 1.8f); 285 | } 286 | 287 | if (externalClock_) 288 | { 289 | for (size_t j = 0; j < kEchoTaps; j++) 290 | { 291 | tapsTimes_[j] = newTapsTimes_[j]; 292 | } 293 | } 294 | } 295 | }; -------------------------------------------------------------------------------- /Filter.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include "Commons.h" 4 | #include "StateVariableFilter.h" 5 | #include "ChaosNoise.h" 6 | #include "DcBlockingFilter.h" 7 | #include "EnvFollower.h" 8 | 9 | enum FilterMode 10 | { 11 | LP, 12 | BP, 13 | HP, 14 | CF, 15 | }; 16 | 17 | enum FilterPosition 18 | { 19 | POSITION_1, 20 | POSITION_2, 21 | POSITION_3, 22 | POSITION_4, 23 | }; 24 | 25 | class Allpass 26 | { 27 | private: 28 | FloatArray line_; 29 | int s_, w_; 30 | float d_, c_, sr_; 31 | 32 | public: 33 | Allpass(float sampleRate, int size) 34 | { 35 | sr_ = sampleRate; 36 | s_ = size; 37 | line_ = FloatArray::create(size); 38 | w_ = 0; 39 | d_ = 1.f; 40 | c_ = 0.7f; 41 | } 42 | ~Allpass() 43 | { 44 | FloatArray::destroy(line_); 45 | } 46 | 47 | static Allpass* create(float sampleRate, int size) 48 | { 49 | return new Allpass(sampleRate, size); 50 | } 51 | 52 | static void destroy(Allpass* obj) 53 | { 54 | delete obj; 55 | } 56 | 57 | void SetDelay(float d) 58 | { 59 | d_ = d; 60 | } 61 | 62 | void SetC(float c) 63 | { 64 | c_ = c; 65 | } 66 | 67 | inline float readAt(int index) 68 | { 69 | int r = w_ - index; 70 | if (r < 0) 71 | { 72 | r += s_; 73 | } 74 | 75 | return line_.getElement(r); 76 | } 77 | 78 | float Process(float in) 79 | { 80 | size_t idx = (size_t)d_; 81 | float y0 = readAt(idx); 82 | float y1 = readAt(idx + 1); 83 | float frac = d_ - idx; 84 | 85 | float out = Interpolator::linear(y0, y1, frac) + (-c_ * in); 86 | 87 | line_.setElement(w_, in + (c_ * out)); 88 | 89 | w_ = (w_ + 1) % s_; 90 | 91 | return out; 92 | } 93 | 94 | float ProcessFixed(float in) 95 | { 96 | float out = 0; 97 | float delayedSample = line_.getElement(w_); 98 | 99 | out += c_ * delayedSample; 100 | out += in - c_ * out; 101 | 102 | line_.setElement(w_, out); 103 | w_ = (w_ + 1) % s_; 104 | 105 | return out; 106 | } 107 | }; 108 | 109 | class CombFilter 110 | { 111 | private: 112 | Allpass* poles_[4]; 113 | EnvFollower* ef_; 114 | float sampleRate_, reso_, out_; 115 | 116 | public: 117 | CombFilter(float sampleRate) 118 | { 119 | sampleRate_ = sampleRate; 120 | poles_[0] = Allpass::create(sampleRate, 2); // Fixed 121 | poles_[1] = Allpass::create(sampleRate, 9600); // Variable 122 | poles_[2] = Allpass::create(sampleRate, 2); // Fixed 123 | poles_[3] = Allpass::create(sampleRate, 9600); // Variable 124 | ef_ = EnvFollower::create(); 125 | reso_ = 0; 126 | out_ = 0; 127 | } 128 | ~CombFilter() 129 | { 130 | for (size_t i = 0; i < 4; i++) 131 | { 132 | Allpass::destroy(poles_[i]); 133 | } 134 | EnvFollower::destroy(ef_); 135 | } 136 | 137 | static CombFilter* create(float sampleRate) 138 | { 139 | return new CombFilter(sampleRate); 140 | } 141 | 142 | static void destroy(CombFilter* obj) 143 | { 144 | delete obj; 145 | } 146 | 147 | void SetFrequency(float freq) 148 | { 149 | float d = sampleRate_ / freq; 150 | //poles_[0]->SetDelay(d); 151 | poles_[1]->SetDelay(d); 152 | //poles_[2]->SetDelay(d); 153 | poles_[3]->SetDelay(d + d); 154 | } 155 | 156 | void SetResonance(float reso) 157 | { 158 | reso_ = reso; 159 | } 160 | 161 | float Process(float in) 162 | { 163 | float i = in + reso_ * out_; 164 | i *= 1.f - ef_->process(i); 165 | 166 | float o = poles_[0]->ProcessFixed(i); 167 | o = poles_[1]->Process(o); 168 | o = poles_[2]->ProcessFixed(o); 169 | o = poles_[3]->Process(o); 170 | 171 | out_ = o; 172 | 173 | return out_; 174 | } 175 | }; 176 | 177 | class Filter 178 | { 179 | private: 180 | PatchCtrls* patchCtrls_; 181 | PatchCvs* patchCvs_; 182 | PatchState* patchState_; 183 | StateVariableFilter* filters_[2]; 184 | CombFilter* combs_[2]; 185 | ChaosNoise noise_; 186 | FilterMode mode_, lastMode_; 187 | DcBlockingFilter* dc_[2]; 188 | EnvFollower* ef_[2]; 189 | 190 | float drive_; 191 | float freq_; 192 | float reso_, resoValue_; 193 | float amp_; 194 | float filterGain_; 195 | float dryWet_; 196 | float noiseLevel_; 197 | float feedback_; 198 | 199 | void SetMode(float value) 200 | { 201 | FilterMode mode; 202 | 203 | if (value >= 0.75f) 204 | { 205 | mode = FilterMode::CF; 206 | } 207 | else if (value >= 0.5f) 208 | { 209 | mode = FilterMode::HP; 210 | } 211 | else if (value >= 0.25f) 212 | { 213 | mode = FilterMode::BP; 214 | } 215 | else 216 | { 217 | mode = FilterMode::LP; 218 | } 219 | 220 | if (mode == mode_) 221 | { 222 | return; 223 | } 224 | 225 | mode_ = mode; 226 | } 227 | 228 | void SetFreq(float value) 229 | { 230 | filterGain_ = kFilterLpGainMin; 231 | 232 | float cutoff = Clamp(MapLog(value, 0.f, 1.f, 10.f, 22000.f), 10.f, 22000.f); 233 | 234 | switch (mode_) 235 | { 236 | case FilterMode::LP: 237 | { 238 | filters_[LEFT_CHANNEL]->setLowPass(cutoff, reso_); 239 | filters_[RIGHT_CHANNEL]->setLowPass(cutoff, reso_); 240 | // Shut the filter off when the frequency is really low. 241 | float g = MapExpo(resoValue_, 0.f, 1.f, kFilterLpGainMax, kFilterLpGainMin); 242 | filterGain_ = cutoff <= 15.f ? Map(cutoff, 10.f, 15.f, 0.f, g) : g; 243 | break; 244 | } 245 | case FilterMode::BP: 246 | { 247 | filters_[LEFT_CHANNEL]->setBandPass(cutoff, reso_); 248 | filters_[RIGHT_CHANNEL]->setBandPass(cutoff, reso_); 249 | filterGain_ = MapExpo(resoValue_, 0.f, 1.f, kFilterBpGainMin, kFilterBpGainMax); 250 | } 251 | break; 252 | case FilterMode::HP: 253 | { 254 | filters_[LEFT_CHANNEL]->setHighPass(cutoff, reso_); 255 | filters_[RIGHT_CHANNEL]->setHighPass(cutoff, reso_); 256 | // Shut the filter off when the frequency is really high. 257 | float g = MapExpo(resoValue_, 0.f, 1.f, kFilterHpGainMax, kFilterHpGainMin); 258 | filterGain_ = cutoff >= 20000.f ? Map(cutoff, 15000, 20000, g, 0.f) : g; 259 | break; 260 | } 261 | case FilterMode::CF: 262 | float f = Clamp(Map(value, 0.f, 1.f, 100.f, 15000.f), 100.f, 15000.f); 263 | float r = Clamp(VariableCrossFade(0.f, 0.8f, resoValue_, 0.9f), 0.f, 0.8f); 264 | combs_[LEFT_CHANNEL]->SetFrequency(f); 265 | combs_[LEFT_CHANNEL]->SetResonance(r); 266 | combs_[RIGHT_CHANNEL]->SetFrequency(f); 267 | combs_[RIGHT_CHANNEL]->SetResonance(r); 268 | filterGain_ = MapExpo(resoValue_, 0.f, 1.f, kFilterCombGainMax, kFilterCombGainMin); 269 | break; 270 | } 271 | noise_.SetFreq(cutoff); 272 | } 273 | 274 | void SetReso(float value) 275 | { 276 | resoValue_ = Clamp(value); 277 | reso_ = VariableCrossFade(0.5f, 10.f, value, 0.9f); 278 | drive_ = VariableCrossFade(0.f, 0.02f, value, 0.15f, 0.9f); 279 | noiseLevel_ = VariableCrossFade(0.f, 0.1f, value, 0.15f, 0.9f); 280 | } 281 | 282 | public: 283 | Filter(PatchCtrls* patchCtrls, PatchCvs* patchCvs, PatchState* patchState) 284 | { 285 | patchCtrls_ = patchCtrls; 286 | patchCvs_ = patchCvs; 287 | patchState_ = patchState; 288 | 289 | noise_.Init(patchState_->sampleRate); 290 | noise_.SetChaos(kFilterChaosNoise); 291 | 292 | for (size_t i = 0; i < 2; i++) 293 | { 294 | filters_[i] = StateVariableFilter::create(patchState_->sampleRate); 295 | combs_[i] = CombFilter::create(patchState_->sampleRate); 296 | dc_[i] = DcBlockingFilter::create(); 297 | ef_[i] = EnvFollower::create(); 298 | } 299 | 300 | mode_ = lastMode_ = FilterMode::LP; 301 | freq_ = 22000.f; 302 | amp_ = Db2A(120); 303 | } 304 | ~Filter() 305 | { 306 | for (size_t i = 0; i < 2; i++) 307 | { 308 | StateVariableFilter::destroy(filters_[i]); 309 | CombFilter::destroy(combs_[i]); 310 | DcBlockingFilter::destroy(dc_[i]); 311 | EnvFollower::destroy(ef_[i]); 312 | } 313 | } 314 | 315 | static Filter* create(PatchCtrls* patchCtrls, PatchCvs* patchCvs, PatchState* patchState) 316 | { 317 | return new Filter(patchCtrls, patchCvs, patchState); 318 | } 319 | 320 | static void destroy(Filter* obj) 321 | { 322 | delete obj; 323 | } 324 | 325 | void process(AudioBuffer &input, AudioBuffer &output) 326 | { 327 | size_t size = output.getSize(); 328 | 329 | FloatArray leftIn = input.getSamples(LEFT_CHANNEL); 330 | FloatArray rightIn = input.getSamples(RIGHT_CHANNEL); 331 | FloatArray leftOut = output.getSamples(LEFT_CHANNEL); 332 | FloatArray rightOut = output.getSamples(RIGHT_CHANNEL); 333 | 334 | SetMode(patchCtrls_->filterMode); 335 | if (mode_ != lastMode_) 336 | { 337 | lastMode_ = mode_; 338 | patchState_->filterModeFlag = true; 339 | } 340 | else 341 | { 342 | patchState_->filterModeFlag = false; 343 | } 344 | 345 | float r = Modulate(patchCtrls_->filterResonance, patchCtrls_->filterResonanceModAmount, patchState_->modValue, patchCtrls_->filterResonanceCvAmount, patchCvs_->filterResonance, -1.f, 1.f, patchState_->modAttenuverters, patchState_->cvAttenuverters); 346 | SetReso(r); 347 | 348 | float c = Modulate(patchCtrls_->filterCutoff, patchCtrls_->filterCutoffModAmount, patchState_->modValue, patchCtrls_->filterCutoffCvAmount, patchCvs_->filterCutoff, -1.f, 1.f, patchState_->modAttenuverters, patchState_->cvAttenuverters); 349 | SetFreq(c); 350 | 351 | if (StartupPhase::STARTUP_DONE != patchState_->startupPhase) 352 | { 353 | return; 354 | } 355 | 356 | for (size_t i = 0; i < size; i++) 357 | { 358 | float n = noise_.Process() * noiseLevel_; 359 | 360 | float lIn = Clamp(leftIn[i], -3.f, 3.f); 361 | float rIn = Clamp(rightIn[i], -3.f, 3.f); 362 | 363 | float ls = SoftClip(lIn * amp_ + n); 364 | float rs = SoftClip(rIn * amp_ + n); 365 | 366 | float lf = LinearCrossFade(lIn + n, ls, drive_); 367 | float rf = LinearCrossFade(rIn + n, rs, drive_); 368 | 369 | float lo, ro; 370 | if (FilterMode::CF == mode_) 371 | { 372 | lo = HardClip(combs_[LEFT_CHANNEL]->Process(lf) * filterGain_); 373 | ro = HardClip(combs_[RIGHT_CHANNEL]->Process(rf) * filterGain_); 374 | lo = dc_[LEFT_CHANNEL]->process(lo); 375 | ro = dc_[RIGHT_CHANNEL]->process(ro); 376 | } 377 | else 378 | { 379 | lo = filters_[LEFT_CHANNEL]->process(lf) * filterGain_; 380 | ro = filters_[RIGHT_CHANNEL]->process(rf) * filterGain_; 381 | lo *= 1.f - ef_[LEFT_CHANNEL]->process(lo); 382 | ro *= 1.f - ef_[RIGHT_CHANNEL]->process(ro); 383 | } 384 | 385 | leftOut[i] = CheapEqualPowerCrossFade(lIn, lo * kFilterMakeupGain, patchCtrls_->filterVol); 386 | rightOut[i] = CheapEqualPowerCrossFade(rIn, ro * kFilterMakeupGain, patchCtrls_->filterVol); 387 | } 388 | } 389 | }; -------------------------------------------------------------------------------- /Resonator.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include "Commons.h" 4 | #include "DelayLine.h" 5 | #include "BiquadFilter.h" 6 | #include "EnvFollower.h" 7 | #include "DcBlockingFilter.h" 8 | #include "Compressor.h" 9 | 10 | class Pole 11 | { 12 | public: 13 | Pole(float sampleRate) 14 | { 15 | sampleRate_ = sampleRate; 16 | msr_ = sampleRate_ / 1000.f; 17 | 18 | for (size_t i = 0; i < 2; i++) 19 | { 20 | delays_[i] = DelayLine::create(kResoBufferSize); 21 | lpfs_[i] = BiquadFilter::create(sampleRate_); 22 | dc_[i] = DcBlockingFilter::create(); 23 | ef_[i] = EnvFollower::create(); 24 | } 25 | 26 | reso_ = FilterStage::BUTTERWORTH_Q; 27 | offset_ = 0; 28 | feedback_ = 0; 29 | filter_ = 0; 30 | detune_ = 0; 31 | infinite_ = false; 32 | } 33 | ~Pole() 34 | { 35 | for (size_t i = 0; i < 2; i++) 36 | { 37 | DelayLine::destroy(delays_[i]); 38 | BiquadFilter::destroy(lpfs_[i]); 39 | DcBlockingFilter::destroy(dc_[i]); 40 | EnvFollower::destroy(ef_[i]); 41 | } 42 | } 43 | 44 | static Pole* create(float sampleRate) 45 | { 46 | return new Pole(sampleRate); 47 | } 48 | 49 | static void destroy(Pole* pole) 50 | { 51 | delete pole; 52 | } 53 | 54 | float GetSemiOffset() 55 | { 56 | return offset_; 57 | } 58 | 59 | /** 60 | * @param offset Semitones, -24 to 24 61 | */ 62 | void SetSemiOffset(float offset) 63 | { 64 | offset_ = offset * -0.5f + 17.667f; 65 | SetNote(); 66 | } 67 | 68 | void SetFilter(float filter) 69 | { 70 | filter_ = filter; 71 | SetFreq(); 72 | } 73 | 74 | void SetFeedback(float feedback) 75 | { 76 | infinite_ = feedback > kResoInfiniteFeedbackThreshold; 77 | feedback_ = VariableCrossFade(0.f, 1.f, feedback, kResoInfiniteFeedbackThreshold - 0.01f); 78 | } 79 | 80 | void SetDissonance(float detune) 81 | { 82 | detune_ = detune; 83 | SetNote(); 84 | } 85 | 86 | void SetReso(float reso) 87 | { 88 | reso_ = reso; 89 | SetFreq(); 90 | } 91 | 92 | // Process just one of the two channels. 93 | float Process(float in, int channel) 94 | { 95 | float out = lpfs_[channel]->process(outs_[channel]) * feedback_; 96 | 97 | float mix = HardClip(dc_[channel]->process(in + out)); 98 | 99 | // Handle infinite feedback. 100 | if (infinite_) 101 | { 102 | mix *= feedback_ * kResoInfiniteFeedbackLevel - ef_[channel]->process(mix); 103 | } 104 | 105 | delays_[channel]->write(mix); 106 | outs_[channel] = delays_[channel]->read(delayTimes_[channel]); 107 | 108 | return out; 109 | } 110 | 111 | void Process(float leftIn, float rightIn, float &leftOut, float &rightOut) 112 | { 113 | leftOut = lpfs_[LEFT_CHANNEL]->process(outs_[LEFT_CHANNEL]) * feedback_; 114 | rightOut = lpfs_[RIGHT_CHANNEL]->process(outs_[RIGHT_CHANNEL]) * feedback_; 115 | 116 | float leftMix = HardClip(dc_[LEFT_CHANNEL]->process(leftIn + leftOut)); 117 | float rightMix = HardClip(dc_[RIGHT_CHANNEL]->process(rightIn + rightOut)); 118 | 119 | // Handle infinite feedback. 120 | if (infinite_) 121 | { 122 | leftMix *= feedback_ * kResoInfiniteFeedbackLevel - ef_[LEFT_CHANNEL]->process(leftMix); 123 | rightMix *= feedback_ * kResoInfiniteFeedbackLevel - ef_[RIGHT_CHANNEL]->process(rightMix); 124 | } 125 | 126 | delays_[LEFT_CHANNEL]->write(leftMix); 127 | delays_[RIGHT_CHANNEL]->write(rightMix); 128 | 129 | outs_[LEFT_CHANNEL] = delays_[LEFT_CHANNEL]->read(delayTimes_[LEFT_CHANNEL]); 130 | outs_[RIGHT_CHANNEL] = delays_[RIGHT_CHANNEL]->read(delayTimes_[RIGHT_CHANNEL]); 131 | } 132 | 133 | private: 134 | DelayLine *delays_[2]; 135 | BiquadFilter *lpfs_[2]; 136 | EnvFollower *ef_[2]; 137 | DcBlockingFilter* dc_[2]; 138 | float delayTimes_[2], outs_[2]; 139 | 140 | float sampleRate_, msr_; 141 | float lf_, rf_; 142 | float reso_; 143 | float offset_; 144 | float feedback_; 145 | float filter_; 146 | float detune_; 147 | 148 | bool infinite_; 149 | 150 | void SetFreq() 151 | { 152 | lpfs_[LEFT_CHANNEL]->setLowPass(M2F(lf_) + filter_, reso_); 153 | lpfs_[RIGHT_CHANNEL]->setLowPass(M2F(rf_) + filter_, reso_); 154 | } 155 | 156 | void SetNote() 157 | { 158 | lf_ = offset_ + detune_; 159 | rf_ = offset_ - detune_; 160 | 161 | delayTimes_[LEFT_CHANNEL] = Clamp(msr_ * Db2A(lf_), 0, kResoBufferSize); 162 | delayTimes_[RIGHT_CHANNEL] = Clamp(msr_ * Db2A(rf_), 0, kResoBufferSize); 163 | 164 | SetFreq(); 165 | } 166 | }; 167 | 168 | /** 169 | * @brief This is taken from my Reaktor ensemble Aerosynth. 170 | * https://www.native-instruments.com/de/reaktor-community/reaktor-user-library/entry/show/3431/ 171 | * 172 | */ 173 | class Resonator 174 | { 175 | private: 176 | PatchCtrls* patchCtrls_; 177 | PatchCvs* patchCvs_; 178 | PatchState* patchState_; 179 | Pole* poles_[3]; 180 | 181 | BiquadFilter *notches_[2]; 182 | BiquadFilter *hs_[2]; 183 | EnvFollower *ef_[2]; 184 | 185 | float amp_; 186 | float dryWet_; 187 | float range_; 188 | float tune_; 189 | float oldTuning_; 190 | int ranges_[3]; 191 | 192 | int task_; 193 | 194 | /** 195 | * @param idx 0 - 3 196 | * @param offset -24/24 197 | */ 198 | void SetSemiOffset(int idx, float offset) 199 | { 200 | if (idx == 0) 201 | { 202 | poles_[0]->SetSemiOffset(offset); 203 | poles_[1]->SetSemiOffset(offset + poles_[1]->GetSemiOffset()); 204 | poles_[2]->SetSemiOffset(offset + poles_[2]->GetSemiOffset()); 205 | } 206 | else if (idx == 1) 207 | { 208 | poles_[1]->SetSemiOffset(offset + poles_[1]->GetSemiOffset()); 209 | } 210 | else if (idx == 2) 211 | { 212 | poles_[2]->SetSemiOffset(offset + poles_[2]->GetSemiOffset()); 213 | } 214 | } 215 | 216 | void SetTune(float value) 217 | { 218 | tune_ = value; 219 | 220 | // Spread the updates in time. 221 | task_ = (task_ + 1) % 6; 222 | if (task_ == 0) 223 | { 224 | SetSemiOffset(0, Map(tune_, 0.f, 1.f, -24, ranges_[0])); 225 | } 226 | if (tune_ < 0.5f && task_ == 1) 227 | { 228 | SetSemiOffset(1, Map(tune_, 0.f, 0.5f, -12, ranges_[1])); 229 | } 230 | if (tune_ < 0.3f && task_ == 2) 231 | { 232 | SetSemiOffset(2, Map(tune_, 0.f, 0.3f, -6, ranges_[2])); 233 | } 234 | if (tune_ >= 0.3f && tune_ < 0.7f && task_ == 3) 235 | { 236 | SetSemiOffset(2, Map(tune_, 0.3f, 0.7f, -6, ranges_[2])); 237 | } 238 | if (tune_ >= 0.5f && task_ == 4) 239 | { 240 | SetSemiOffset(1, Map(tune_, 0.5f, 1.f, -12, ranges_[1])); 241 | } 242 | if (tune_ >= 0.7f && task_ == 5) 243 | { 244 | SetSemiOffset(2, Map(tune_, 0.7f, 1.f, -6, ranges_[2])); 245 | } 246 | } 247 | 248 | void SetFeedback(float value, bool init = false) 249 | { 250 | float feedback = Map(value, 0.f, 1.f, 0.85f, 1.f); 251 | float reso = Map(value, 0.f, 1.f, 0.5f, 0.6f); 252 | float filter = Map(value, 0.f, 1.f, 5000.f, 10000.f); 253 | amp_ = Map(value, 0.f, 1.f, kResoGainMax, kResoGainMin) * 0.577f; 254 | for (int i = 0; i < 3; i++) 255 | { 256 | poles_[i]->SetFeedback(feedback); 257 | poles_[i]->SetReso(reso); 258 | poles_[i]->SetFilter(filter); 259 | } 260 | } 261 | 262 | /* 263 | void SetRange(float range) 264 | { 265 | range_ = Map(range, 0.f, kOne, 0.1f, 1.f); 266 | } 267 | */ 268 | 269 | void SetDissonance(float value) 270 | { 271 | ranges_[0] = Map(value, 0.f, 1.f, 24, 16); 272 | ranges_[1] = Map(value, 0.f, 1.f, 12, 7); 273 | ranges_[2] = Map(value, 0.f, 1.f, 6, 13); 274 | 275 | poles_[0]->SetDissonance(value); 276 | poles_[1]->SetDissonance(value * 2.f); 277 | poles_[2]->SetDissonance(value * 3.f); 278 | } 279 | 280 | public: 281 | Resonator(PatchCtrls* patchCtrls, PatchCvs* patchCvs, PatchState* patchState) 282 | { 283 | patchCtrls_ = patchCtrls; 284 | patchCvs_ = patchCvs; 285 | patchState_ = patchState; 286 | 287 | for (int i = 0; i < 3; i++) 288 | { 289 | poles_[i] = Pole::create(patchState_->sampleRate); 290 | } 291 | 292 | for (size_t i = 0; i < 2; i++) 293 | { 294 | notches_[i] = BiquadFilter::create(patchState_->sampleRate); 295 | notches_[i]->setNotch(8000.f, FilterStage::SALLEN_KEY_Q); 296 | hs_[i] = BiquadFilter::create(patchState_->sampleRate); 297 | hs_[i]->setHighShelf(8000.f, -24.f); 298 | ef_[i] = EnvFollower::create(); 299 | } 300 | 301 | amp_ = 1.f; 302 | range_ = 1.f; 303 | task_ = 0; 304 | 305 | SetDissonance(0); 306 | SetTune(0); 307 | SetFeedback(0); 308 | } 309 | ~Resonator() 310 | { 311 | for (int i = 0; i < 3; i++) 312 | { 313 | Pole::destroy(poles_[i]); 314 | } 315 | for (size_t i = 0; i < 2; i++) 316 | { 317 | BiquadFilter::destroy(notches_[i]); 318 | BiquadFilter::destroy(hs_[i]); 319 | EnvFollower::destroy(ef_[i]); 320 | } 321 | } 322 | 323 | static Resonator* create(PatchCtrls* patchCtrls, PatchCvs* patchCvs, PatchState* patchState) 324 | { 325 | return new Resonator(patchCtrls, patchCvs, patchState); 326 | } 327 | 328 | static void destroy(Resonator* obj) 329 | { 330 | delete obj; 331 | } 332 | 333 | void process(AudioBuffer &input, AudioBuffer &output) 334 | { 335 | size_t size = output.getSize(); 336 | FloatArray leftIn = input.getSamples(LEFT_CHANNEL); 337 | FloatArray rightIn = input.getSamples(RIGHT_CHANNEL); 338 | FloatArray leftOut = output.getSamples(LEFT_CHANNEL); 339 | FloatArray rightOut = output.getSamples(RIGHT_CHANNEL); 340 | 341 | SetDissonance(patchCtrls_->resonatorDissonance); 342 | 343 | float t = Modulate(patchCtrls_->resonatorTune, patchCtrls_->resonatorTuneModAmount, patchState_->modValue, patchCtrls_->resonatorTuneCvAmount, patchCvs_->resonatorTune, -1.f, 1.f, patchState_->modAttenuverters, patchState_->cvAttenuverters); 344 | ParameterInterpolator tuningParam(&oldTuning_, t, size); 345 | 346 | float f = Modulate(patchCtrls_->resonatorFeedback, patchCtrls_->resonatorFeedbackModAmount, patchState_->modValue, patchCtrls_->resonatorFeedbackCvAmount, patchCvs_->resonatorFeedback, -1.f, 1.f, patchState_->modAttenuverters, patchState_->cvAttenuverters); 347 | SetFeedback(f); 348 | 349 | for (size_t i = 0; i < size; i++) 350 | { 351 | SetTune(tuningParam.Next()); 352 | 353 | float lIn = Clamp(leftIn[i], -3.f, 3.f); 354 | float rIn = Clamp(rightIn[i], -3.f, 3.f); 355 | 356 | float left = poles_[1]->Process(lIn, LEFT_CHANNEL); 357 | float right = poles_[2]->Process(rIn, RIGHT_CHANNEL); 358 | 359 | float oLeft = left * 0.75f + right * 0.25f; 360 | float oRight = left * 0.25f + right * 0.75f; 361 | 362 | left = 0; 363 | right = 0; 364 | poles_[0]->Process(lIn, rIn, left, right); 365 | oLeft += left; 366 | oRight += right; 367 | 368 | oLeft *= 1.f - ef_[LEFT_CHANNEL]->process(oLeft); 369 | oRight *= 1.f - ef_[RIGHT_CHANNEL]->process(oRight); 370 | 371 | oLeft *= amp_; 372 | oRight *= amp_; 373 | 374 | oLeft = notches_[LEFT_CHANNEL]->process(oLeft); 375 | oRight = notches_[RIGHT_CHANNEL]->process(oRight); 376 | 377 | oLeft = hs_[LEFT_CHANNEL]->process(oLeft); 378 | oRight = hs_[RIGHT_CHANNEL]->process(oRight); 379 | 380 | leftOut[i] = CheapEqualPowerCrossFade(lIn, oLeft * kResoMakeupGain, patchCtrls_->resonatorVol, 1.4f); 381 | rightOut[i] = CheapEqualPowerCrossFade(rIn, oRight * kResoMakeupGain, patchCtrls_->resonatorVol, 1.4f); 382 | } 383 | } 384 | }; -------------------------------------------------------------------------------- /Ambience.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include "Commons.h" 4 | #include "BiquadFilter.h" 5 | #include "DelayLine.h" 6 | #include "SineOscillator.h" 7 | #include "EnvFollower.h" 8 | #include "DcBlockingFilter.h" 9 | #include "Compressor.h" 10 | 11 | class Damp 12 | { 13 | private: 14 | BiquadFilter *highShelf, *lowShelf; 15 | float hi_, lo_; 16 | int hp_, lp_, fHp_, fLp_; 17 | 18 | public: 19 | Damp(float sampleRate) 20 | { 21 | hi_ = 0; 22 | lo_ = 0; 23 | hp_ = 96; 24 | lp_ = 60; 25 | fHp_ = M2F(hp_); 26 | fLp_ = M2F(lp_); 27 | highShelf = BiquadFilter::create(sampleRate); 28 | lowShelf = BiquadFilter::create(sampleRate); 29 | } 30 | ~Damp() 31 | { 32 | BiquadFilter::destroy(highShelf); 33 | BiquadFilter::destroy(lowShelf); 34 | } 35 | 36 | static Damp* create(float sampleRate) 37 | { 38 | return new Damp(sampleRate); 39 | } 40 | 41 | static void destroy(Damp* damp) 42 | { 43 | delete damp; 44 | } 45 | 46 | void SetHi(float hi) 47 | { 48 | if (hi == hi_) 49 | { 50 | return; 51 | } 52 | 53 | hi_ = hi; 54 | highShelf->setHighShelf(fHp_, hi_); 55 | } 56 | 57 | void SetLo(float lo) 58 | { 59 | if (lo == lo_) 60 | { 61 | return; 62 | } 63 | 64 | lo_ = lo; 65 | lowShelf->setLowShelf(fLp_, lo_); 66 | } 67 | 68 | void SetHp(int hp) 69 | { 70 | if (hp == hp_) 71 | { 72 | return; 73 | } 74 | 75 | hp_ = hp; 76 | fHp_ = M2F(hp_); 77 | highShelf->setHighShelf(fHp_, hi_); 78 | } 79 | 80 | void SetLp(float lp) 81 | { 82 | if (lp == lp_) 83 | { 84 | return; 85 | } 86 | 87 | lp_ = lp; 88 | fLp_ = M2F(lp_); 89 | lowShelf->setLowShelf(fLp_, lo_); 90 | } 91 | 92 | float Process(float in) 93 | { 94 | return lowShelf->process(highShelf->process(in)); 95 | } 96 | }; // End Damp 97 | 98 | class Diffuse 99 | { 100 | public: 101 | Diffuse() 102 | { 103 | for (int i = 0; i < kAmbienceNofDiffusers; i++) 104 | { 105 | diffuse_[i] = DelayLine::create(kAmbienceBufferSize); 106 | } 107 | 108 | fbOut_ = 0; 109 | df_ = 0; 110 | needsUpdate_ = false; 111 | 112 | SetSZ(1); 113 | SetRT(0); 114 | } 115 | ~Diffuse() 116 | { 117 | for (int i = 0; i < kAmbienceNofDiffusers; i++) 118 | { 119 | DelayLine::destroy(diffuse_[i]); 120 | } 121 | } 122 | 123 | static Diffuse* create() 124 | { 125 | return new Diffuse(); 126 | } 127 | 128 | static void destroy(Diffuse* diffuse) 129 | { 130 | delete diffuse; 131 | } 132 | 133 | void SetSZ(float size) 134 | { 135 | size_ = size; 136 | for (size_t i = 0; i < kAmbienceNofDiffusers - 1; i++) 137 | { 138 | newDelayTimes_[i] = M2D(size + 2.f * (i + 1)); 139 | } 140 | 141 | newDelayTimes_[kAmbienceNofDiffusers - 1] = M2D(size - 7.f); 142 | SetRT(time_); 143 | needsUpdate_ = true; 144 | } 145 | 146 | void SetRT(float time) 147 | { 148 | time_ = time; 149 | rt_ = Db2A((delayTimes_[kAmbienceNofDiffusers - 1] / M2D(time)) * -60.f); 150 | if (rt_ >= kOne) { 151 | rt_ = 1.f; 152 | } 153 | } 154 | 155 | void SetDf(float _df) 156 | { 157 | df_ = _df; 158 | } 159 | 160 | float GetFbOut() 161 | { 162 | return fbOut_; 163 | } 164 | 165 | void UpdateDelayTimes() 166 | { 167 | if (!needsUpdate_) 168 | { 169 | return; 170 | } 171 | 172 | for (int i = 0; i < kAmbienceNofDiffusers; i++) 173 | { 174 | delayTimes_[i] = newDelayTimes_[i]; 175 | } 176 | needsUpdate_ = false; 177 | } 178 | 179 | float Process(const float in, const float x) 180 | { 181 | float out = in; 182 | 183 | for (int i = 0; i < kAmbienceNofDiffusers - 1; i++) 184 | { 185 | float prev = HardClip(out - outs_[i] * df_); 186 | diffuse_[i]->write(prev); 187 | out = HardClip(prev * df_ + outs_[i]); 188 | outs_[i] = diffuse_[i]->read(delayTimes_[i], newDelayTimes_[i], x); 189 | } 190 | 191 | int lastDiff = kAmbienceNofDiffusers - 1; 192 | fbOut_ = outs_[lastDiff] * rt_; 193 | diffuse_[lastDiff]->write(out); 194 | outs_[lastDiff] = diffuse_[lastDiff]->read(delayTimes_[lastDiff], newDelayTimes_[lastDiff], x); 195 | 196 | return out; 197 | } 198 | 199 | private: 200 | DelayLine *diffuse_[kAmbienceNofDiffusers]; 201 | float delayTimes_[kAmbienceNofDiffusers], newDelayTimes_[kAmbienceNofDiffusers]; 202 | float size_, time_, rt_, df_, fbOut_, outs_[kAmbienceNofDiffusers]; 203 | bool needsUpdate_; 204 | }; // End Diffuse 205 | 206 | class ReversedBuffer 207 | { 208 | public: 209 | ReversedBuffer(int32_t s) : s_{s} 210 | { 211 | line_ = FloatArray::create(s); 212 | i_ = 0; // Input pointer 213 | o_ = s_ - 1; // Output pointer 214 | bs_ = s_ >> 1; // Reverse max block size is half the buffer size 215 | b_ = bs_; // Block pointer 216 | rb_ = 1.f / b_; 217 | } 218 | ~ReversedBuffer() 219 | { 220 | FloatArray::destroy(line_); 221 | } 222 | 223 | static ReversedBuffer* create(int32_t size) 224 | { 225 | return new ReversedBuffer(size); 226 | } 227 | 228 | static void destroy(ReversedBuffer* line) 229 | { 230 | delete line; 231 | } 232 | 233 | void Clear() 234 | { 235 | line_.clear(); 236 | out_ = 0.f; 237 | } 238 | 239 | int32_t GetDelay() 240 | { 241 | return d_; 242 | } 243 | 244 | void SetDelay(int32_t d) 245 | { 246 | bs_ = Clamp(d, 1, s_ >> 1); 247 | } 248 | 249 | float LastOut() 250 | { 251 | return out_; 252 | } 253 | 254 | float NextOut() 255 | { 256 | return line_[o_]; 257 | } 258 | 259 | float Process(const float input) 260 | { 261 | line_[i_++] = input; 262 | if (i_ == s_) 263 | { 264 | i_ = 0; 265 | } 266 | 267 | float x = b_ * rb_; 268 | float g = 4.f * x * (1.f - x); 269 | out_ = Clamp(line_[o_--] * g, -3.f, 3.f); 270 | b_--; 271 | if (b_ == 0) 272 | { 273 | o_ = i_ - 1; 274 | b_ = bs_; 275 | } 276 | while (o_ < 0) 277 | { 278 | o_ += s_; 279 | } 280 | 281 | return out_; 282 | } 283 | 284 | private: 285 | FloatArray line_; 286 | int32_t s_, d_, i_, o_, bs_, b_; 287 | float rb_; 288 | float out_; 289 | }; // End ReversedBuffer 290 | 291 | /** 292 | * @brief This is taken from my Reaktor ensemble Aerosynth. 293 | * https://www.native-instruments.com/de/reaktor-community/reaktor-user-library/entry/show/3431/ 294 | */ 295 | class Ambience 296 | { 297 | private: 298 | PatchCtrls* patchCtrls_; 299 | PatchCvs* patchCvs_; 300 | PatchState* patchState_; 301 | 302 | SineOscillator *panner_; 303 | 304 | Damp *dampFilters_[2]; 305 | Diffuse *diffusers_[2]; 306 | ReversedBuffer *reversers_[2]; 307 | 308 | EnvFollower* ef_[2]; 309 | Compressor* comp_[2]; 310 | DcBlockingFilter* dc_[2]; 311 | 312 | float amp_, pan_, decay_, spaceTime_; 313 | float reverse_; 314 | float xi_; 315 | 316 | Lut decayLUT{0.f, -160.f, Lut::Type::LUT_TYPE_EXPO}; 317 | 318 | /** 319 | * @param damp Attenuation in Db 320 | */ 321 | void SetHighDamp(float damp) 322 | { 323 | dampFilters_[LEFT_CHANNEL]->SetHi(damp); 324 | dampFilters_[RIGHT_CHANNEL]->SetHi(damp); 325 | } 326 | 327 | /** 328 | * @param damp Attenuation in Db 329 | */ 330 | void SetLowDamp(float damp) 331 | { 332 | dampFilters_[LEFT_CHANNEL]->SetLo(damp); 333 | dampFilters_[RIGHT_CHANNEL]->SetLo(damp); 334 | } 335 | 336 | void SetDecayTime(float time) 337 | { 338 | diffusers_[LEFT_CHANNEL]->SetRT(time); 339 | diffusers_[RIGHT_CHANNEL]->SetRT(time); 340 | } 341 | 342 | void SetSize(float size) 343 | { 344 | float sz = -(size - 30.f); 345 | diffusers_[LEFT_CHANNEL]->SetSZ(sz); 346 | diffusers_[RIGHT_CHANNEL]->SetSZ(sz); 347 | 348 | float df = (size * 0.004166667f) + 0.5f; // 1 / 240 349 | diffusers_[LEFT_CHANNEL]->SetDf(df); 350 | diffusers_[RIGHT_CHANNEL]->SetDf(df); 351 | } 352 | 353 | void SetPan(float value) 354 | { 355 | float f = Clamp(kModClockRatios[QuantizeInt(patchCtrls_->ambienceAutoPan, kClockNofRatios)] * patchState_->tempo->getFrequency(), 0.f, 261.63f); 356 | panner_->setFrequency(f); 357 | 358 | pan_ = 0.5f + panner_->generate() * patchCtrls_->ambienceAutoPan * 0.5f; 359 | } 360 | 361 | void SetDecay(float value) 362 | { 363 | decay_ = value; 364 | SetDecayTime(decayLUT.Quantized(decay_)); 365 | } 366 | 367 | void SetSpacetime(float value) 368 | { 369 | spaceTime_ = CenterMap(value); 370 | 371 | float lowDamp = kAmbienceLowDampMin; 372 | float highDamp = kAmbienceHighDampMin; 373 | float size; 374 | 375 | if (spaceTime_ < 0.f) { 376 | if (spaceTime_ < -0.4f) 377 | { 378 | highDamp = Map(spaceTime_, -1.f, -0.4f, kAmbienceHighDampMax, kAmbienceHighDampMin); 379 | } 380 | else 381 | { 382 | lowDamp = Map(spaceTime_, -0.4f, 0.f, kAmbienceLowDampMin, kAmbienceLowDampMax); 383 | } 384 | size = 60.1f - MapExpo(spaceTime_, -1.f, 0.f, 0.1f, 60.f); 385 | amp_ = kAmbienceRevGainMax + kAmbienceRevGainMin - MapExpo(spaceTime_, -1.f, 0.f, kAmbienceRevGainMin, kAmbienceRevGainMax); 386 | } else { 387 | if (spaceTime_ < 0.4f) 388 | { 389 | lowDamp = Map(spaceTime_, 0.f, 0.4f, kAmbienceLowDampMax, kAmbienceLowDampMin); 390 | } 391 | else 392 | { 393 | highDamp = Map(spaceTime_, 0.4f, 1.f, kAmbienceHighDampMin, kAmbienceHighDampMax); 394 | } 395 | size = MapExpo(spaceTime_, 0.f, 1.f, 0.1f, 60.f); 396 | amp_ = MapExpo(spaceTime_, 0.f, 1.f, kAmbienceGainMin, kAmbienceGainMax); 397 | } 398 | 399 | SetLowDamp(lowDamp); 400 | SetHighDamp(highDamp); 401 | SetSize(size); 402 | 403 | if (spaceTime_ < -0.2f) 404 | { 405 | reverse_ = 1.f; 406 | } 407 | else if (spaceTime_ > 0.2f) 408 | { 409 | reverse_ = 0.f; 410 | } 411 | else 412 | { 413 | reverse_ = Map(spaceTime_, -0.2f, 0.2f, 1.f, 0.f); 414 | } 415 | } 416 | 417 | public: 418 | Ambience(PatchCtrls* patchCtrls, PatchCvs* patchCvs, PatchState* patchState) 419 | { 420 | patchCtrls_ = patchCtrls; 421 | patchCvs_ = patchCvs; 422 | patchState_ = patchState; 423 | 424 | for (size_t i = 0; i < 2; i++) 425 | { 426 | dampFilters_[i] = Damp::create(patchState_->sampleRate); 427 | diffusers_[i] = Diffuse::create(); 428 | reversers_[i] = ReversedBuffer::create(kAmbienceBufferSize); 429 | ef_[i] = EnvFollower::create(); 430 | dc_[i] = DcBlockingFilter::create(); 431 | comp_[i] = Compressor::create(patchState_->sampleRate); 432 | comp_[i]->setThreshold(-20); 433 | } 434 | 435 | dampFilters_[LEFT_CHANNEL]->SetHp(112); 436 | dampFilters_[LEFT_CHANNEL]->SetLp(60); 437 | 438 | dampFilters_[RIGHT_CHANNEL]->SetHp(96); 439 | dampFilters_[RIGHT_CHANNEL]->SetLp(51); 440 | 441 | panner_ = SineOscillator::create(patchState_->blockRate); 442 | 443 | amp_ = 1.f; 444 | pan_ = 0.5f; 445 | xi_ = 1.f / patchState_->blockSize; 446 | } 447 | ~Ambience() 448 | { 449 | for (size_t i = 0; i < 2; i++) 450 | { 451 | Damp::destroy(dampFilters_[i]); 452 | Diffuse::destroy(diffusers_[i]); 453 | ReversedBuffer::destroy(reversers_[i]); 454 | EnvFollower::destroy(ef_[i]); 455 | DcBlockingFilter::destroy(dc_[i]); 456 | Compressor::destroy(comp_[i]); 457 | } 458 | SineOscillator::destroy(panner_); 459 | } 460 | 461 | static Ambience* create(PatchCtrls* patchCtrls, PatchCvs* patchCvs, PatchState* patchState) 462 | { 463 | return new Ambience(patchCtrls, patchCvs, patchState); 464 | } 465 | 466 | static void destroy(Ambience* obj) 467 | { 468 | delete obj; 469 | } 470 | 471 | void process(AudioBuffer &input, AudioBuffer &output) 472 | { 473 | size_t size = output.getSize(); 474 | FloatArray leftIn = input.getSamples(LEFT_CHANNEL); 475 | FloatArray rightIn = input.getSamples(RIGHT_CHANNEL); 476 | FloatArray leftOut = output.getSamples(LEFT_CHANNEL); 477 | FloatArray rightOut = output.getSamples(RIGHT_CHANNEL); 478 | 479 | SetPan(patchCtrls_->ambienceAutoPan); 480 | 481 | float d = Modulate(patchCtrls_->ambienceDecay, patchCtrls_->ambienceDecayModAmount, patchState_->modValue, patchCtrls_->ambienceDecayCvAmount, patchCvs_->ambienceDecay, -1.f, 1.f, patchState_->modAttenuverters, patchState_->cvAttenuverters); 482 | SetDecay(d); 483 | 484 | float t = Modulate(patchCtrls_->ambienceSpacetime, patchCtrls_->ambienceSpacetimeModAmount, patchState_->modValue, patchCtrls_->ambienceSpacetimeCvAmount, patchCvs_->ambienceSpacetime, -1.f, 1.f, patchState_->modAttenuverters, patchState_->cvAttenuverters); 485 | SetSpacetime(t); 486 | 487 | if (StartupPhase::STARTUP_DONE != patchState_->startupPhase) 488 | { 489 | return; 490 | } 491 | 492 | float r = 1.f - reverse_; 493 | float x = 0; 494 | 495 | for (size_t i = 0; i < size; i++) 496 | { 497 | float lIn = Clamp(leftIn[i], -3.f, 3.f); 498 | float rIn = Clamp(rightIn[i], -3.f, 3.f); 499 | 500 | float left = reversers_[LEFT_CHANNEL]->LastOut() * reverse_ + lIn * r; 501 | float right = reversers_[RIGHT_CHANNEL]->LastOut() * reverse_ + rIn * r; 502 | 503 | reversers_[LEFT_CHANNEL]->Process(lIn); 504 | reversers_[RIGHT_CHANNEL]->Process(rIn); 505 | 506 | float leftFb = dampFilters_[LEFT_CHANNEL]->Process(left + diffusers_[RIGHT_CHANNEL]->GetFbOut()); 507 | float rightFb = dampFilters_[RIGHT_CHANNEL]->Process(right + diffusers_[LEFT_CHANNEL]->GetFbOut()); 508 | 509 | leftFb = HardClip(left * (1.f - pan_) + leftFb); 510 | rightFb = HardClip(right * pan_ + rightFb); 511 | 512 | leftFb *= 1.f - ef_[LEFT_CHANNEL]->process(leftFb); 513 | rightFb *= 1.f - ef_[RIGHT_CHANNEL]->process(rightFb); 514 | 515 | leftFb = dc_[LEFT_CHANNEL]->process(leftFb); 516 | rightFb = dc_[RIGHT_CHANNEL]->process(rightFb); 517 | 518 | left = diffusers_[LEFT_CHANNEL]->Process(leftFb, x); 519 | right = diffusers_[RIGHT_CHANNEL]->Process(rightFb, x); 520 | 521 | x += xi_; 522 | 523 | float a = Map(decay_, 0.f, 1.f, amp_ * 1.3f, amp_); 524 | 525 | left = comp_[LEFT_CHANNEL]->process(left * a) * kAmbienceMakeupGain; 526 | right = comp_[RIGHT_CHANNEL]->process(right * a) * kAmbienceMakeupGain; 527 | 528 | leftOut[i] = CheapEqualPowerCrossFade(lIn, left, patchCtrls_->ambienceVol, 1.4f); 529 | rightOut[i] = CheapEqualPowerCrossFade(rIn, right, patchCtrls_->ambienceVol, 1.4f); 530 | } 531 | 532 | diffusers_[LEFT_CHANNEL]->UpdateDelayTimes(); 533 | diffusers_[RIGHT_CHANNEL]->UpdateDelayTimes(); 534 | } 535 | }; -------------------------------------------------------------------------------- /Looper.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include "Commons.h" 4 | #include "LooperBuffer.h" 5 | #include "WaveTableBuffer.h" 6 | #include "SquareWaveOscillator.h" 7 | #include "Schmitt.h" 8 | #include "DjFilter.h" 9 | #include "Limiter.h" 10 | #include "EnvFollower.h" 11 | #include "ParameterInterpolator.h" 12 | #include 13 | #include 14 | 15 | class Looper 16 | { 17 | private: 18 | PatchCtrls* patchCtrls_; 19 | PatchCvs* patchCvs_; 20 | PatchState* patchState_; 21 | LooperBuffer* buffer_; 22 | DjFilter* filter_; 23 | Limiter* limiter_; 24 | EnvFollower* ef_[2]; 25 | 26 | AudioBuffer* sosOut_; 27 | 28 | PlaybackDirection direction_; 29 | 30 | float wPhase_; 31 | float phase_; 32 | float speed_; 33 | float speedValue_; 34 | float filterValue_; 35 | float oldStartValue_, oldLengthValue_, oldSpeedValue_; 36 | float inputGain_, feedback_, triggerFadeVolume_, speedVolume_; 37 | float length_, start_; 38 | float newLength_, newStart_; 39 | float fadePhase_, fadeSamples_, fadeSamplesR_; 40 | float fadeThreshold_; 41 | 42 | bool triggered_; 43 | bool boc_; 44 | bool cleared_; 45 | bool fade_, startFade_, triggerFadeOut_, triggerFadeIn_; 46 | 47 | int triggerFadeIndex_; 48 | 49 | uint32_t bufferPhase_; 50 | 51 | Schmitt trigger_; 52 | 53 | Lut startLUT_{0, kLooperChannelBufferLength - 1}; 54 | Lut lengthLUT_{kLooperLoopLengthMin, kLooperChannelBufferLength, Lut::Type::LUT_TYPE_EXPO}; 55 | 56 | void MapSpeed() 57 | { 58 | speedValue_ = CenterMap(patchCtrls_->looperSpeed, -2.f, 2.f, patchState_->speedZero); 59 | 60 | // Deadband around 0x speed. 61 | if (speedValue_ >= -0.1f && speedValue_ <= 0.1f) 62 | { 63 | patchState_->looperSpeedLockFlag = true; 64 | } 65 | else if (speedValue_ > 0.1f) 66 | { 67 | // Deadband around 1x speed. 68 | if (speedValue_ >= 0.95f && speedValue_ <= 1.05f) 69 | { 70 | patchState_->looperSpeedLockFlag = true; 71 | } 72 | else 73 | { 74 | patchState_->looperSpeedLockFlag = false; 75 | } 76 | } 77 | else 78 | { 79 | // Deadband around -1x speed. 80 | if (speedValue_ >= -1.05f && speedValue_ <= -0.95f) 81 | { 82 | patchState_->looperSpeedLockFlag = true; 83 | } 84 | else 85 | { 86 | patchState_->looperSpeedLockFlag = false; 87 | } 88 | } 89 | } 90 | 91 | void SetSpeed(float value) 92 | { 93 | // Lower the volume as the speed approaches 0. 94 | speedVolume_ = 1.f; 95 | if (value >= -0.2f && value <= 0.2f) 96 | { 97 | speedVolume_ = Map(fabs(value), 0.1f, 0.2f, 0.f, 1.f); 98 | } 99 | 100 | // Deadband around 0x speed. 101 | if (value >= -0.1f && value <= 0.1f) 102 | { 103 | direction_ = PlaybackDirection::PLAYBACK_STALLED; 104 | value = 0; 105 | } 106 | else if (value > 0.1f) 107 | { 108 | direction_ = PlaybackDirection::PLAYBACK_FORWARD; 109 | // Deadband around 1x speed. 110 | if (value >= 0.95f && value <= 1.05f) 111 | { 112 | value = 1.f; 113 | } 114 | } 115 | else 116 | { 117 | direction_ = PlaybackDirection::PLAYBACK_BACKWARDS; 118 | // Deadband around -1x speed. 119 | if (value >= -1.05f && value <= -0.95f) 120 | { 121 | value = -1.f; 122 | } 123 | } 124 | 125 | speed_ = value; 126 | } 127 | 128 | void SetStart(float value) 129 | { 130 | if (value > 0.98f) 131 | { 132 | value = 1.f; 133 | } 134 | 135 | uint32_t v = startLUT_.Quantized(value); 136 | 137 | if (PlaybackDirection::PLAYBACK_STALLED == direction_) 138 | { 139 | fade_ = false; 140 | startFade_ = false; 141 | newStart_ = v; 142 | fadePhase_ = phase_ = 0; 143 | 144 | return; 145 | } 146 | 147 | if (fade_) 148 | { 149 | return; 150 | } 151 | 152 | if (v != start_) 153 | { 154 | newStart_ = v; 155 | 156 | // Always fade, because the start point offsets the phase. 157 | fade_ = true; 158 | startFade_ = true; 159 | } 160 | } 161 | 162 | bool lengthFade_ = false; 163 | 164 | void SetLength(float value) 165 | { 166 | if (value > 0.98f) 167 | { 168 | value = 1.f; 169 | } 170 | 171 | uint32_t v = lengthLUT_.Quantized(value); 172 | 173 | if (PlaybackDirection::PLAYBACK_STALLED == direction_) 174 | { 175 | fade_ = false; 176 | lengthFade_ = false; 177 | newLength_ = v; 178 | fadePhase_ = phase_ = 0; 179 | fadeThreshold_ = Min(newLength_ * 0.1f, kLooperFadeSamples); 180 | 181 | return; 182 | } 183 | 184 | if (fade_) 185 | { 186 | return; 187 | } 188 | 189 | if (v != length_) 190 | { 191 | newLength_ = v; 192 | 193 | fadeThreshold_ = Min(newLength_ * 0.1f, kLooperFadeSamples); 194 | 195 | // Fade is only necessary if phase goes outside of the loop. 196 | if (phase_ > newLength_) 197 | { 198 | fadeSamples_ = Clamp(phase_ - newLength_, kLooperLoopLengthMin * 0.1f, kLooperFadeSamples); 199 | fadeSamplesR_ = 1.f / fadeSamples_ * fabs(speed_); 200 | fade_ = true; 201 | lengthFade_ = true; 202 | } 203 | } 204 | } 205 | 206 | void SetFilter(float value) 207 | { 208 | filterValue_ = fabs(value * 2.f - 1.f); 209 | filter_->SetFilter(value); 210 | } 211 | 212 | inline void WriteRead(AudioBuffer& input, AudioBuffer& output) 213 | { 214 | size_t size = input.getSize(); 215 | 216 | if (triggered_) 217 | { 218 | triggered_ = false; 219 | if (length_ >= kLooperTriggerFadeSamples * 2) 220 | { 221 | // Fade only if we have enough space. 222 | triggerFadeOut_ = true; 223 | } 224 | else 225 | { 226 | // Otherwise just reset the phase. 227 | phase_ = 0.f; 228 | } 229 | } 230 | 231 | boc_ = false; 232 | 233 | for (size_t i = 0; i < size; i++) 234 | { 235 | if (buffer_->IsRecording()) 236 | { 237 | float left, right; 238 | filter_->Process(input.getSamples(LEFT_CHANNEL)[i], input.getSamples(RIGHT_CHANNEL)[i], left, right); 239 | 240 | left = HardClip(sosOut_->getSamples(LEFT_CHANNEL)[i] * patchCtrls_->looperSos + left); 241 | right = HardClip(sosOut_->getSamples(RIGHT_CHANNEL)[i] * patchCtrls_->looperSos + right); 242 | 243 | left *= 1.f - ef_[LEFT_CHANNEL]->process(left); 244 | right *= 1.f - ef_[RIGHT_CHANNEL]->process(right); 245 | 246 | buffer_->Write(wPhase_, left, right); 247 | 248 | wPhase_++; 249 | if (wPhase_ >= kLooperChannelBufferLength) 250 | { 251 | wPhase_ -= kLooperChannelBufferLength; 252 | } 253 | } 254 | 255 | float left = 0; 256 | float right = 0; 257 | 258 | buffer_->Read(start_ + phase_, left, right, direction_); 259 | 260 | if (fade_) 261 | { 262 | float start = newStart_; 263 | if (!startFade_ && !lengthFade_) 264 | { 265 | start -= newLength_ * direction_; 266 | } 267 | if (lengthFade_ && PlaybackDirection::PLAYBACK_BACKWARDS == direction_) 268 | { 269 | start += newLength_ - (length_ - phase_); 270 | } 271 | else 272 | { 273 | start += phase_; 274 | } 275 | 276 | float fadeLeft; 277 | float fadeRight; 278 | buffer_->Read(start, fadeLeft, fadeRight, direction_); 279 | 280 | if (fadePhase_ < 1.f) 281 | { 282 | left = CheapEqualPowerCrossFade(left, fadeLeft, fadePhase_); 283 | right = CheapEqualPowerCrossFade(right, fadeRight, fadePhase_); 284 | fadePhase_ += fadeSamplesR_; 285 | } 286 | else 287 | { 288 | if (!startFade_ && !lengthFade_) 289 | { 290 | phase_ = PlaybackDirection::PLAYBACK_FORWARD == direction_ ? Max(phase_ - newLength_, 0) : newLength_; 291 | } 292 | if (lengthFade_ && PlaybackDirection::PLAYBACK_BACKWARDS == direction_) 293 | { 294 | phase_ = newLength_ - (length_ - phase_); 295 | } 296 | 297 | fadePhase_ = 0; 298 | start_ = newStart_; 299 | length_ = newLength_; 300 | left = fadeLeft; 301 | right = fadeRight; 302 | fade_ = false; 303 | startFade_ = false; 304 | lengthFade_ = false; 305 | } 306 | } 307 | else 308 | { 309 | start_ = newStart_; 310 | length_ = newLength_; 311 | } 312 | 313 | if (triggerFadeOut_ || triggerFadeIn_) 314 | { 315 | triggerFadeVolume_ = triggerFadeIndex_ * kLooperTriggerFadeSamplesR; 316 | if (triggerFadeOut_) 317 | { 318 | triggerFadeVolume_ = 1.f - triggerFadeVolume_; 319 | } 320 | triggerFadeIndex_++; 321 | if (triggerFadeIndex_ >= kLooperTriggerFadeSamples) 322 | { 323 | triggerFadeIndex_ = 0; 324 | if (triggerFadeOut_) 325 | { 326 | // Reset phase when fade out is complete. 327 | phase_ = 0.f; 328 | triggerFadeVolume_ = 0.f; 329 | triggerFadeOut_ = false; 330 | triggerFadeIn_ = true; 331 | } 332 | else 333 | { 334 | triggerFadeVolume_ = 1.f; 335 | triggerFadeIn_ = false; 336 | } 337 | } 338 | left *= triggerFadeVolume_; 339 | right *= triggerFadeVolume_; 340 | } 341 | 342 | if (!fade_) 343 | { 344 | if ((PlaybackDirection::PLAYBACK_FORWARD == direction_ && phase_ >= length_ - fadeThreshold_) || 345 | (PlaybackDirection::PLAYBACK_BACKWARDS == direction_ && phase_ <= fadeThreshold_)) 346 | { 347 | fadeSamples_ = PlaybackDirection::PLAYBACK_FORWARD == direction_ ? length_ - phase_ : phase_; 348 | fadeSamples_ = Clamp(fadeSamples_, kLooperLoopLengthMin * 0.1f, kLooperFadeSamples); 349 | fadeSamplesR_ = 1.f / fadeSamples_ * fabs(speed_); 350 | fade_ = true; 351 | } 352 | } 353 | 354 | phase_ += speed_; 355 | 356 | sosOut_->getSamples(LEFT_CHANNEL)[i] = left; 357 | sosOut_->getSamples(RIGHT_CHANNEL)[i] = right; 358 | 359 | output.getSamples(LEFT_CHANNEL)[i] = left * speedVolume_; 360 | output.getSamples(RIGHT_CHANNEL)[i] = right * speedVolume_; 361 | 362 | bufferPhase_++; 363 | if (bufferPhase_ == kLooperChannelBufferLength) 364 | { 365 | boc_ = true; 366 | bufferPhase_ = 0; 367 | } 368 | } 369 | } 370 | 371 | public: 372 | Looper(PatchCtrls* patchCtrls, PatchCvs* patchCvs, PatchState* patchState) 373 | { 374 | patchCtrls_ = patchCtrls; 375 | patchCvs_ = patchCvs; 376 | patchState_ = patchState; 377 | 378 | buffer_ = LooperBuffer::create(); 379 | filter_ = DjFilter::create(patchState_->sampleRate); 380 | sosOut_ = AudioBuffer::create(2, patchState_->blockSize); 381 | limiter_ = Limiter::create(); 382 | 383 | direction_ = PlaybackDirection::PLAYBACK_FORWARD; 384 | 385 | inputGain_ = 1.f; 386 | feedback_ = 0; 387 | speedVolume_ = 1.f; 388 | oldSpeedValue_ = speedValue_ = 0.7f; 389 | speed_ = 1.f; 390 | phase_ = 0; 391 | wPhase_ = bufferPhase_ = 0; 392 | length_ = newLength_ = kLooperChannelBufferLength; 393 | start_ = newStart_ = 0; 394 | filterValue_ = 0; 395 | fadePhase_ = 0; 396 | fadeThreshold_ = kLooperFadeSamples; 397 | fadeSamples_ = kLooperFadeSamples; 398 | fadeSamplesR_ = 1.f / fadeSamples_; 399 | startFade_ = false; 400 | lengthFade_ = false; 401 | boc_ = true; 402 | cleared_ = false; 403 | fade_ = false; 404 | triggerFadeIndex_ = 0; 405 | triggerFadeVolume_ = 0; 406 | triggerFadeOut_ = false; 407 | triggerFadeIn_ = false; 408 | oldStartValue_ = 0; 409 | oldLengthValue_ = 1.f; 410 | 411 | for (size_t i = 0; i < 2; i++) 412 | { 413 | ef_[i] = EnvFollower::create(); 414 | } 415 | } 416 | ~Looper() 417 | { 418 | LooperBuffer::destroy(buffer_); 419 | DjFilter::destroy(filter_); 420 | AudioBuffer::destroy(sosOut_); 421 | Limiter::destroy(limiter_); 422 | 423 | for (size_t i = 0; i < 2; i++) 424 | { 425 | EnvFollower::destroy(ef_[i]); 426 | } 427 | } 428 | 429 | static Looper* create(PatchCtrls* patchCtrls, PatchCvs* patchCvs, PatchState* patchState) 430 | { 431 | return new Looper(patchCtrls, patchCvs, patchState); 432 | } 433 | 434 | static void destroy(Looper* obj) 435 | { 436 | delete obj; 437 | } 438 | 439 | FloatArray *GetBuffer() 440 | { 441 | return buffer_->GetBuffer(); 442 | } 443 | 444 | void Process(AudioBuffer &input, AudioBuffer &output) 445 | { 446 | input.multiply(patchCtrls_->looperResampling ? kLooperResampleGain : kLooperInputGain); 447 | 448 | if (ClockSource::CLOCK_SOURCE_EXTERNAL == patchState_->clockSource && (trigger_.Process(patchState_->clockReset || patchState_->clockTick))) 449 | { 450 | triggered_ = true; 451 | } 452 | else if (ClockSource::CLOCK_SOURCE_INTERNAL == patchState_->clockSource) 453 | { 454 | // When the clock is internal, synchronize it with the looper's 455 | // begin of cycle. 456 | patchState_->tempo->trigger(boc_); 457 | } 458 | 459 | MapSpeed(); 460 | ParameterInterpolator speedParam(&oldSpeedValue_, speedValue_, kLooperInterpolationBlocks); 461 | float rs = Modulate(speedParam.Next(), patchCtrls_->looperSpeedModAmount, patchState_->modValue, patchCtrls_->looperSpeedCvAmount, patchCvs_->looperSpeed, -2.f, 2.f, patchState_->modAttenuverters, patchState_->cvAttenuverters); 462 | SetSpeed(rs); 463 | 464 | ParameterInterpolator startParam(&oldStartValue_, patchCtrls_->looperStart, kLooperInterpolationBlocks); 465 | float t = Modulate(startParam.Next(), patchCtrls_->looperStartModAmount, patchState_->modValue, patchCtrls_->looperStartCvAmount, patchCvs_->looperStart, -1.f, 1.f, patchState_->modAttenuverters, patchState_->cvAttenuverters); 466 | SetStart(t); 467 | 468 | ParameterInterpolator lengthParam(&oldLengthValue_, patchCtrls_->looperLength, kLooperInterpolationBlocks); 469 | float l = Modulate(lengthParam.Next(), patchCtrls_->looperLengthModAmount, patchState_->modValue, patchCtrls_->looperLengthCvAmount, patchCvs_->looperLength, -1.f, 1.f, patchState_->modAttenuverters, patchState_->cvAttenuverters); 470 | SetLength(l); 471 | 472 | SetFilter(patchCtrls_->looperFilter); 473 | 474 | if (StartupPhase::STARTUP_DONE != patchState_->startupPhase) 475 | { 476 | return; 477 | } 478 | 479 | if (patchCtrls_->looperRecording && !buffer_->IsRecording()) 480 | { 481 | buffer_->StartRecording(); 482 | } 483 | else if (!patchCtrls_->looperRecording && buffer_->IsRecording()) 484 | { 485 | buffer_->StopRecording(); 486 | } 487 | 488 | if (patchState_->clearLooperFlag) 489 | { 490 | output.clear(); 491 | patchState_->clearLooperFlag = false; 492 | cleared_ = true; 493 | } 494 | else if (cleared_) 495 | { 496 | output.clear(); 497 | if (buffer_->Clear()) 498 | { 499 | cleared_ = false; 500 | } 501 | } 502 | 503 | WriteRead(input, output); 504 | 505 | if (PlaybackDirection::PLAYBACK_STALLED == direction_) 506 | { 507 | output.clear(); 508 | } 509 | else 510 | { 511 | output.multiply(patchCtrls_->looperVol * kLooperMakeupGain); 512 | limiter_->ProcessSoft(output, output); 513 | } 514 | } 515 | }; 516 | -------------------------------------------------------------------------------- /Commons.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include "Patch.h" 4 | #include "ParameterInterpolator.h" 5 | #include "TapTempo.h" 6 | #include 7 | #include 8 | #include 9 | 10 | //#define USE_RECORD_THRESHOLD 11 | #define MAX_PATCH_SETTINGS 16 // Max number of available MIDI channels 12 | #define PATCH_SETTINGS_NAME "oneiroi" 13 | #define PATCH_VERSION_MAJOR 1 14 | #define PATCH_VERSION_MINOR 1 15 | 16 | // Taken from pichenettes' stmlib. 17 | #define CONSTRAIN(var, min, max) \ 18 | if (var < (min)) { \ 19 | var = (min); \ 20 | } else if (var > (max)) { \ 21 | var = (max); \ 22 | } 23 | #define ONE_POLE(out, in, coefficient) out += (coefficient) * ((in) - out); 24 | #define SLOPE(out, in, positive, negative) \ 25 | { \ 26 | float error = (in)-out; \ 27 | out += (error > 0 ? positive : negative) * error; \ 28 | } 29 | 30 | #define SHIFT_BUTTON PUSHBUTTON 31 | #define LEFT_ARROW_PARAM GREEN_BUTTON 32 | #define RIGHT_ARROW_PARAM RED_BUTTON 33 | #define RECORD_BUTTON BUTTON_1 34 | #define RECORD_IN BUTTON_2 35 | #define RANDOM_BUTTON BUTTON_3 36 | #define RANDOM_IN BUTTON_4 37 | #define SYNC_IN BUTTON_5 38 | #define INPUT_PEAK_LED_PARAM BUTTON_6 39 | #define PREPOST_SWITCH BUTTON_7 40 | #define SSWT_SWITCH BUTTON_8 41 | #define MOD_CV_GREEN_LED_PARAM BUTTON_9 42 | #define MOD_CV_RED_LED_PARAM BUTTON_10 43 | #define MOD_CV_BUTTON BUTTON_11 44 | 45 | #define INPUT_LED_PARAM PARAMETER_AF 46 | #define MOD_LED_PARAM PARAMETER_AG 47 | 48 | constexpr float kEqualCrossFadeP = 1.f; 49 | constexpr float kEps = 0.0001f; // Commodity for minimum float 50 | constexpr float kPi = 3.1415927410125732421875f; 51 | const float k2Pi = kPi * 2.0f; 52 | const float kHPi = kPi * 0.5f; 53 | constexpr float kSqrt2 = 1.414213562373095f; 54 | const float kRSqrt2 = 1.f / kSqrt2; 55 | constexpr float kOne = 0.975f; //4095.f / 4096.f; 56 | constexpr float k2One = kOne * 2; 57 | static const float kOneHalf = kOne / 2.f; 58 | 59 | constexpr float kCvLpCoeff = 0.7f; 60 | constexpr float kCvOffset = -0.46035f; 61 | constexpr float kCvMult = 1.485f; 62 | constexpr float kCvDelta = 0.02f; 63 | constexpr float kCvMinThreshold = 0.007f; 64 | 65 | constexpr int kStartupWaitSamples = 450; // 300ms (1500 = 1s @ block rate) 66 | 67 | constexpr int kRandomSlewSamples = 128; 68 | 69 | constexpr float kA4Freq = 440.f; 70 | constexpr int kA4Note = 69; 71 | constexpr float kSemi4Oct = 12; 72 | constexpr float kOscFreqMin = 16.35f; // C0 73 | constexpr float kOscFreqMax = 8219.f; // C9 74 | 75 | constexpr size_t kLooperInterpolationBlocks = 4; // This number * block size = samples 76 | constexpr int kLooperLoopLengthMin = 367; // Almost C3 (48000 / 130.81f) 77 | constexpr int kLooperFadeSamples = 2400; // 50ms @ audio rate 78 | static const float kLooperFadeSamplesR = 1.f / kLooperFadeSamples; 79 | constexpr int kLooperTriggerFadeSamples = 240; // 5ms @ audio rate 80 | static const float kLooperTriggerFadeSamplesR = 1.f / kLooperTriggerFadeSamples; 81 | static const int32_t kLooperTotalBufferLength = 1 << 19; // 524288 samples for both channels (interleaved) = 5.46 seconds stereo buffer 82 | //static const int32_t kLooperTotalBufferLength = 480000; // samples for both channels (interleaved) = ~8 seconds stereo buffer 83 | static const int32_t kLooperChannelBufferLength = kLooperTotalBufferLength / 2; 84 | constexpr float kLooperNoiseLevel = 0.2f; 85 | constexpr float kLooperInputGain = 1.f; 86 | constexpr float kLooperResampleGain = 1.f; 87 | constexpr float kLooperResampleLedAtt = 1.f; 88 | constexpr float kLooperMakeupGain = 1.3f; 89 | constexpr int kLooperClearBlocks = 128; // Number of blocks of the buffer to be cleared 90 | static const int32_t kLooperClearBlockSize = kLooperTotalBufferLength / kLooperClearBlocks; 91 | static const int32_t kLooperClearBlockTypeSize = kLooperClearBlockSize * 4; // Float 92 | 93 | constexpr float kRecordOnsetLevel = 0.005f; 94 | constexpr float kRecordWindupLevel = 0.00001f; 95 | constexpr int kRecordGateLimit = 375; // 250ms (1500 = 1s @ block rate) 96 | constexpr int kRecordOnsetLimit = 375; // 250ms (1500 = 1s @ block rate) 97 | 98 | constexpr int kWaveTableLength = 2048; 99 | constexpr int kWaveTableNofTables = 32; 100 | static const int kWaveTableStepLength = kLooperChannelBufferLength / kWaveTableNofTables; 101 | static const float kWaveTableNofTablesR = 1.f / kWaveTableNofTables; 102 | 103 | // When internally clocked, base frequency is ~0.18Hz 104 | // When externally clocked, min bpm is 30 (0.5Hz), max is 300 (5Hz) 105 | constexpr float kClockFreqMin = 0.01f; 106 | constexpr float kClockFreqMax = 80.f; 107 | constexpr int kExternalClockLimit = 3000; // Samples required to detect a steady external clock - 2s (1500 = 1s @ block rate) 108 | static const float kInternalClockFreq = (48000.f / kLooperChannelBufferLength); 109 | constexpr int kClockNofRatios = 17; 110 | constexpr int kClockUnityRatioIndex = 9; 111 | static const float kModClockRatios[kClockNofRatios] = { 0.015625f, 0.03125f, 0.0625f, 0.125f, 0.2f, 0.25f, 0.33f, 0.5f, 1, 2, 3, 4, 5, 8, 16, 32, 64}; 112 | static const float kRModClockRatios[kClockNofRatios] = { 64, 32, 16, 8, 5, 4, 3, 2, 1, 0.5f, 0.33f, 0.25f, 0.2f, 0.125f, 0.0625f, 0.03125f, 0.015625f}; 113 | constexpr float kClockTempoSamplesMin = 48; // Minimum number of tempo's samples required to detect a change 114 | 115 | constexpr float kOScSineGain = 0.3f; 116 | static const float kOscSineFadeInc = 1.f / 2400; 117 | constexpr float kOScSuperSawGain = 0.4f; 118 | constexpr float kOScWaveTablePreGain = 6.f; 119 | constexpr float kOScWaveTableGain = 0.3f; 120 | constexpr float kSourcesMakeupGain = 0.2f; 121 | 122 | constexpr float kDjFilterMakeupGainMin = 1.f; 123 | constexpr float kDjFilterMakeupGainMax = 1.4f; 124 | 125 | constexpr float kFilterFreqMin = 10.f; 126 | constexpr float kFilterFreqMax = 22000.f; 127 | constexpr float kFilterMakeupGain = 2.4f; 128 | constexpr float kFilterChaosNoise = 1.8f; 129 | constexpr float kFilterLpGainMin = 0.3f; 130 | constexpr float kFilterLpGainMax = 0.4f; 131 | constexpr float kFilterHpGainMin = 0.2f; 132 | constexpr float kFilterHpGainMax = 0.4f; 133 | constexpr float kFilterBpGainMin = 0.2f; 134 | constexpr float kFilterBpGainMax = 0.4f; 135 | constexpr float kFilterCombGainMin = 0.1f; 136 | constexpr float kFilterCombGainMax = 0.2f; 137 | 138 | constexpr float kResoGainMin = 0.5f; 139 | constexpr float kResoGainMax = 1.2f; 140 | constexpr float kResoMakeupGain = 1.2f; 141 | constexpr int32_t kResoBufferSize = 2400; 142 | constexpr float kResoInfiniteFeedbackThreshold = 0.99f; 143 | constexpr float kResoInfiniteFeedbackLevel = 1.05f; 144 | 145 | constexpr int32_t kEchoFadeSamples = 2400; // 50 ms @ audio rate 146 | constexpr int32_t kEchoMinLengthSamples = 480; // 10 ms @ audio rate 147 | constexpr int32_t kEchoMaxLengthSamples = 192000; // 4 seconds @ audio rate 148 | constexpr int kEchoTaps = 4; 149 | const float kEchoTapsRatios[kEchoTaps] = { 0.75f, 0.25f, 0.375f, 1.f }; // TAP_LEFT_A (1/2 dot), TAP_LEFT_B (1/8), TAP_RIGHT_A (1/8 dot), TAP_RIGHT_B (1) 150 | const float kEchoTapsFeedbacks[kEchoTaps] = { 0.35f, 0.65f, 0.55f, 0.45f }; 151 | const int32_t kEchoMaxExternalClockSamples = kEchoMaxLengthSamples / kModClockRatios[kClockNofRatios - 1]; // Maximum period for the external clock 152 | constexpr int kEchoExternalClockMultiplier = 32; 153 | constexpr int kEchoInternalClockMultiplier = 23; // ~192000 / 8192 (period of the buffer) 154 | constexpr float kEchoInfiniteFeedbackThreshold = 0.999f; 155 | constexpr float kEchoInfiniteFeedbackLevel = 1.001f; 156 | constexpr int kEchoCompThresMin = -16; 157 | constexpr int kEchoCompThresMax = -22; 158 | constexpr float kEchoMakeupGain = 1.2f; 159 | 160 | constexpr int32_t kAmbienceBufferSize = 48000; 161 | constexpr int kAmbienceNofDiffusers = 4; 162 | constexpr float kAmbienceLowDampMin = -0.5f; 163 | constexpr float kAmbienceLowDampMax = -40.f; 164 | constexpr float kAmbienceHighDampMin = -0.5f; 165 | constexpr float kAmbienceHighDampMax = -40.f; 166 | constexpr float kAmbienceGainMin = 1.f; 167 | constexpr float kAmbienceGainMax = 1.2f; 168 | constexpr float kAmbienceRevGainMin = 1.4f; 169 | constexpr float kAmbienceRevGainMax = 1.2f; 170 | constexpr float kAmbienceMakeupGain = 1.2f; 171 | 172 | static const float kOutputFadeInc = 1.f / 16.f; 173 | constexpr float kOutputMakeupGain = 6.f; 174 | 175 | constexpr float kParamCatchUpDelta = 0.005f; 176 | 177 | constexpr int kParamStartMovementLimit = 75; // Samples required to detect the start of a movement - 50ms (1500 = 1s @ block rate) 178 | constexpr int kParamStopMovementLimit = 225; // Samples required to detect the stop of a movement - 150ms (1500 = 1s @ block rate) 179 | constexpr int kResetLimit = 75; // Samples waited for both RECORD and RANDOM buttons to be pressed for resetting parameters - 50ms (1500 = 1s @ block rate) 180 | constexpr int kSaveLimit = 3000; // Samples waited for MOD/CV button to be pressed for saving parameters - 2s (1500 = 1s @ block rate) 181 | constexpr int kGateLimit = 750; // Samples waited for a button gate to go off - 500ms (1500 = 1s @ block rate) 182 | constexpr int kHoldLimit = 75; // Samples waited for a pressed button to be considered held - 50ms (1500 = 1s @ block rate) 183 | 184 | struct PatchCtrls 185 | { 186 | float inputVol; 187 | 188 | float looperVol; 189 | float looperSos; 190 | float looperFilter; 191 | float looperSpeed; 192 | float looperSpeedModAmount; 193 | float looperSpeedCvAmount; 194 | float looperStart; 195 | float looperStartModAmount; 196 | float looperStartCvAmount; 197 | float looperLength; 198 | float looperLengthModAmount; 199 | float looperLengthCvAmount; 200 | float looperRecording; 201 | float looperResampling; 202 | 203 | float osc1Vol; 204 | float osc2Vol; 205 | float oscOctave; 206 | float oscUnison; 207 | float oscPitch; 208 | float oscPitchModAmount; 209 | float oscPitchCvAmount; 210 | float oscDetune; 211 | float oscDetuneModAmount; 212 | float oscDetuneCvAmount; 213 | float oscUseWavetable; 214 | 215 | float filterVol; 216 | float filterMode; 217 | float filterNoiseLevel; 218 | float filterCutoff; 219 | float filterCutoffModAmount; 220 | float filterCutoffCvAmount; 221 | float filterResonance; 222 | float filterResonanceModAmount; 223 | float filterResonanceCvAmount; 224 | float filterPosition; 225 | 226 | float resonatorVol; 227 | float resonatorTune; 228 | float resonatorTuneModAmount; 229 | float resonatorTuneCvAmount; 230 | float resonatorFeedback; 231 | float resonatorFeedbackModAmount; 232 | float resonatorFeedbackCvAmount; 233 | float resonatorDissonance; 234 | 235 | float echoVol; 236 | float echoRepeats; 237 | float echoRepeatsModAmount; 238 | float echoRepeatsCvAmount; 239 | float echoDensity; 240 | float echoDensityModAmount; 241 | float echoDensityCvAmount; 242 | float echoFilter; 243 | 244 | float ambienceVol; 245 | float ambienceDecay; 246 | float ambienceDecayModAmount; 247 | float ambienceDecayCvAmount; 248 | float ambienceSpacetime; 249 | float ambienceSpacetimeModAmount; 250 | float ambienceSpacetimeCvAmount; 251 | float ambienceAutoPan; 252 | 253 | float modLevel; 254 | float modSpeed; 255 | float modType; 256 | 257 | float randomMode; 258 | float randomAmount; 259 | }; 260 | 261 | struct PatchCvs 262 | { 263 | float looperSpeed; 264 | float looperStart; 265 | float looperLength; 266 | float oscPitch; 267 | float oscDetune; 268 | float filterCutoff; 269 | float filterResonance; 270 | float resonatorTune; 271 | float resonatorFeedback; 272 | float echoRepeats; 273 | float echoDensity; 274 | float ambienceDecay; 275 | float ambienceSpacetime; 276 | }; 277 | 278 | enum FuncMode 279 | { 280 | FUNC_MODE_NONE, 281 | FUNC_MODE_ALT, 282 | FUNC_MODE_MOD, 283 | FUNC_MODE_CV, 284 | FUNC_MODE_LAST 285 | }; 286 | 287 | enum ClockSource 288 | { 289 | CLOCK_SOURCE_INTERNAL, 290 | CLOCK_SOURCE_EXTERNAL, 291 | }; 292 | 293 | enum StartupPhase 294 | { 295 | STARTUP_1, 296 | STARTUP_2, 297 | STARTUP_3, 298 | STARTUP_4, 299 | STARTUP_5, 300 | STARTUP_DONE, 301 | }; 302 | 303 | struct PatchState 304 | { 305 | float sampleRate; 306 | float blockRate; 307 | int blockSize; 308 | 309 | FloatArray inputLevel; 310 | FloatArray efModLevel; 311 | 312 | bool modActive; 313 | float modValue; 314 | 315 | ClockSource clockSource; 316 | TapTempo* tempo; 317 | 318 | bool syncIn; 319 | bool clockReset; 320 | bool clockTick; 321 | size_t clockSamples; 322 | 323 | bool clearLooperFlag; 324 | bool oscPitchCenterFlag; 325 | bool oscUnisonCenterFlag; 326 | bool oscOctaveFlag; 327 | bool looperSpeedLockFlag; 328 | bool modTypeLockFlag; 329 | bool modSpeedLockFlag; 330 | bool filterModeFlag; 331 | bool filterPositionFlag; 332 | 333 | bool moving[23]; 334 | 335 | float c2; 336 | float c5; 337 | float pitchZero; 338 | float speedZero; 339 | 340 | float outLevel; 341 | float randomSlew; 342 | 343 | bool randomHasSlew; 344 | bool softTakeover; 345 | bool modAttenuverters; 346 | bool cvAttenuverters; 347 | 348 | FuncMode funcMode; 349 | 350 | StartupPhase startupPhase; 351 | }; 352 | 353 | inline bool AreEquals(float val1, float val2, float d = kEps) 354 | { 355 | return fabs(val1 - val2) <= d; 356 | } 357 | 358 | /** 359 | * @brief Taken from DaisySP. 360 | * 361 | * @param a 362 | * @param b 363 | * @return float 364 | */ 365 | inline float Max(float a, float b) 366 | { 367 | float r; 368 | #ifdef __arm__ 369 | asm("vmaxnm.f32 %[d], %[n], %[m]" : [d] "=t"(r) : [n] "t"(a), [m] "t"(b) :); 370 | #else 371 | r = (a > b) ? a : b; 372 | #endif // __arm__ 373 | return r; 374 | } 375 | 376 | /** 377 | * @brief Taken from DaisySP. 378 | * 379 | * @param a 380 | * @param b 381 | * @return float 382 | */ 383 | inline float Min(float a, float b) 384 | { 385 | float r; 386 | #ifdef __arm__ 387 | asm("vminnm.f32 %[d], %[n], %[m]" : [d] "=t"(r) : [n] "t"(a), [m] "t"(b) :); 388 | #else 389 | r = (a < b) ? a : b; 390 | #endif // __arm__ 391 | return r; 392 | } 393 | 394 | /** 395 | * @brief Taken from DaisySP. 396 | * 397 | * @param in 398 | * @param min 399 | * @param max 400 | * @return float 401 | */ 402 | inline float Clamp(float in, float min = 0.f, float max = 1.f) 403 | { 404 | return Min(Max(in, min), max); 405 | } 406 | 407 | /** 408 | * @brief Taken from DaisySP. 409 | * 410 | * @param Base 411 | * @param Exponent 412 | * @return float 413 | */ 414 | float Power(float f, float n = 2, bool approx = false) 415 | { 416 | if (approx) 417 | { 418 | long *lp, l; 419 | lp = (long *)(&f); 420 | l = *lp; 421 | l -= 0x3F800000; 422 | l <<= ((int) n - 1); 423 | l += 0x3F800000; 424 | *lp = l; 425 | 426 | return f; 427 | } 428 | 429 | return pow(f, n); 430 | } 431 | 432 | /** 433 | * @brief Frequency to period in samples conversion. 434 | * 435 | * @param freq Frequency in Hz 436 | * @return float Samples 437 | */ 438 | inline float F2S(float freq, float sampleRate = 48000.f) 439 | { 440 | return freq == 0.f ? 0.f : sampleRate / freq; 441 | } 442 | 443 | /** 444 | * @brief MIDI note to frequency conversion. 445 | * @note Taken from DaisySP. 446 | * 447 | * @param m MIDI note 448 | * @return float Frequency in Hz 449 | */ 450 | inline float M2F(float m) 451 | { 452 | return Power(2.f, (m - kA4Note) / kSemi4Oct) * kA4Freq; 453 | } 454 | 455 | /** 456 | * @brief MIDI note to delay time conversion. 457 | * 458 | * @param note MIDI note 459 | * @return float Delay time in samples 460 | */ 461 | inline float M2D(float note, float sampleRate = 48000.f) 462 | { 463 | return F2S(M2F(note), sampleRate); 464 | } 465 | 466 | /** 467 | * @brief Keeps the value between 0 and size by wrapping it back from the 468 | * opposite boundary if necessary. 469 | * 470 | * @param val 471 | * @param size 472 | * @return float 473 | */ 474 | inline float Wrap(float val, float size = 1.f) 475 | { 476 | while (val < 0) 477 | { 478 | val += size; 479 | } 480 | while (val > size) 481 | { 482 | val -= size; 483 | } 484 | 485 | return val; 486 | } 487 | 488 | enum RandomAmount 489 | { 490 | RANDOM_MID, 491 | RANDOM_LOW, 492 | RANDOM_HIGH, 493 | RANDOM_CUSTOM, 494 | }; 495 | 496 | inline float RandomFloat(float min = 0.f, float max = kOne) 497 | { 498 | return min == max ? min : min + randf() * (max - min); 499 | } 500 | 501 | /** 502 | * @brief Maps the value that ranges from aMin to aMax to a value that 503 | * ranges from bMin to bMax. Supports inverted ranges. 504 | */ 505 | inline float Map(float value, float aMin, float aMax, float bMin, float bMax) 506 | { 507 | float k = abs(bMax - bMin) / abs(aMax - aMin) * (bMax > bMin ? 1 : -1); 508 | 509 | return bMin + k * (value - aMin); 510 | } 511 | 512 | inline float MapExpo(float value, float aMin = 0.f, float aMax = 1.f, float bMin = 0.f, float bMax = 1.f) 513 | { 514 | value = (value - aMin) / (aMax - aMin); 515 | 516 | return bMin + (value * value) * (bMax - bMin); 517 | } 518 | 519 | inline float MapLog(float value, float aMin = 0.f, float aMax = 1.f, float bMin = 0.f, float bMax = 1.f) 520 | { 521 | bMin = fast_logf(bMin < 0.0000001f ? 0.0000001f : bMin); 522 | bMax = fast_logf(bMax); 523 | 524 | return fast_expf(Map(value, aMin, aMax, bMin, bMax)); 525 | } 526 | 527 | // Maps the value to a range by considering where the original center actually is. 528 | inline float CenterMap(float value, float min = -1.f, float max = 1.f, float center = 0.55f) 529 | { 530 | if (value < center) 531 | { 532 | value = Map(value, 0.0f, center, min, 0.0f); 533 | } 534 | else 535 | { 536 | value = Map(value, center, 0.99f, 0.0f, max); 537 | } 538 | 539 | return value; 540 | } 541 | 542 | /** 543 | * @brief Returns -1 for negative numbers and +1 for positive numbers. 544 | * 545 | * @param val 546 | * @return float 547 | */ 548 | inline float Sign(float val) 549 | { 550 | return (0.f < val) - (val < 0.f); 551 | } 552 | 553 | /** 554 | * @brief Quantizes in steps a value between 0.f and 1.f, returning a value between 0.f and 1.f 555 | * 556 | * @param value 557 | * @param steps 558 | * @return float 559 | */ 560 | inline float Quantize(float value, int32_t steps) 561 | { 562 | return rintf(value * steps) * (1.f / steps); 563 | } 564 | 565 | /** 566 | * @brief Quantizes in steps a value between 0.f and 1.f, returning a value between 0 and steps - 1 567 | * 568 | * @param value 569 | * @param steps 570 | * @return int 571 | */ 572 | inline int QuantizeInt(float value, int32_t steps) 573 | { 574 | return rintf(Clamp(value, 0, 1) * (steps - 1)); 575 | } 576 | 577 | // (a + b) * 1 / sqrt(2) 578 | inline float Mix2(float a, float b) 579 | { 580 | return (a + b) * 0.707f; 581 | } 582 | inline void Mix2(FloatArray a, FloatArray b, FloatArray o) 583 | { 584 | o.copyFrom(a); 585 | o.add(b); 586 | o.multiply(0.707f); 587 | } 588 | inline void Mix2(AudioBuffer& a, AudioBuffer& b, AudioBuffer& o) 589 | { 590 | o.copyFrom(a); 591 | o.add(b); 592 | o.multiply(0.707f); 593 | } 594 | 595 | // (a + b + c) * 1 / sqrt(3) 596 | inline float Mix3(float a, float b, float c) 597 | { 598 | return (a + b + c) * 0.577f; 599 | } 600 | 601 | // (a + b + c + d) * 1 / sqrt(4) 602 | inline float Mix4(float a, float b, float c, float d) 603 | { 604 | return (a + b + c + d) * 0.5f; 605 | } 606 | 607 | inline float Db2A(float db) 608 | { 609 | return Power(10.f, db / 20.f); 610 | } 611 | 612 | inline float LinearCrossFade(float a, float b, float pos) 613 | { 614 | return a * (1.f - pos) + b * pos; 615 | } 616 | inline void LinearCrossFade(FloatArray a, FloatArray b, FloatArray o, float pos) 617 | { 618 | a.multiply(1.f - pos); 619 | b.multiply(pos); 620 | o.copyFrom(a); 621 | o.add(b); 622 | } 623 | inline void LinearCrossFade(AudioBuffer& a, AudioBuffer& b, AudioBuffer& o, float pos) 624 | { 625 | AudioBuffer* t = AudioBuffer::create(2, o.getSize()); 626 | 627 | for (size_t i = 0; i < 2; ++i) 628 | { 629 | a.getSamples(i).multiply(1.f - pos, o.getSamples(i)); 630 | b.getSamples(i).multiply(pos, t->getSamples(i)); 631 | } 632 | o.add(*t); 633 | 634 | AudioBuffer::destroy(t); 635 | } 636 | 637 | inline float VariableCrossFade(float a, float b, float pos, float length = 1.f, float offset = 0.f) 638 | { 639 | if (offset > 0) 640 | { 641 | length = Min(length, 1.f - offset); 642 | } 643 | if (pos >= offset && pos <= offset + length) 644 | { 645 | float l = (pos - offset) * (1.f / length); 646 | 647 | return a * (1.f - l) + b * l; 648 | } 649 | if (pos > length + offset) 650 | { 651 | return b; 652 | } 653 | 654 | return a; 655 | } 656 | 657 | /** 658 | * @brief Energy preserving crossfade 659 | * @see https://signalsmith-audio.co.uk/writing/2021/cheap-energy-crossfade/ 660 | * 661 | * @param from 662 | * @param to 663 | * @param pos 664 | * @return float 665 | */ 666 | float CheapEqualPowerCrossFade(float from, float to, float pos, float p = kEqualCrossFadeP) 667 | { 668 | float invPos = 1.f - pos; 669 | float k = -6.0026608f + p * (6.8773512f - 1.5838104f * p); 670 | float a = pos * invPos; 671 | float b = a * (1.f + k * a); 672 | float c = (b + pos); 673 | float d = (b + invPos); 674 | 675 | return from * d * d + to * c * c; 676 | } 677 | void CheapEqualPowerCrossFade(AudioBuffer &from, AudioBuffer &to, float pos, AudioBuffer &out, float p = kEqualCrossFadeP) 678 | { 679 | float invPos = 1.f - pos; 680 | float k = -6.0026608f + p * (6.8773512f - 1.5838104f * p); 681 | float a = pos * invPos; 682 | float b = a * (1.f + k * a); 683 | float c = (b + pos); 684 | float d = (b + invPos); 685 | 686 | from.multiply(d * d); 687 | to.multiply(c * c); 688 | 689 | out.copyFrom(from); 690 | out.add(to); 691 | } 692 | 693 | 694 | inline void LR2MS(const float left, const float right, float &mid, float &side, float width = 1.f) 695 | { 696 | mid = (left + right) / kSqrt2; 697 | side = ((left - right) / kSqrt2) * width; 698 | } 699 | 700 | inline void MS2LR(const float mid, const float side, float &left, float &right) 701 | { 702 | left = (mid + side) / kSqrt2; 703 | right = (mid - side) / kSqrt2; 704 | } 705 | 706 | inline void StereoControl(float &left, float &right, float width) 707 | { 708 | float mid, side; 709 | LR2MS(left, right, mid, side, width); 710 | MS2LR(mid, side, left, right); 711 | } 712 | 713 | /** 714 | * @brief Generates a sampled period of a square wave. 715 | * 716 | * @param tab Table to be filled 717 | * @param sz Table size 718 | * @param p Period size 719 | * @param a Amplification 720 | * @param d Duty cycle (0/1.f) 721 | * @param o Phase offset 722 | */ 723 | void SquareTable(float *tab, uint32_t sz, uint32_t p = 0, float a = 1.f, float d = 0.5f, uint32_t o = 0) 724 | { 725 | if (sz <= 0) return; 726 | if (p <= 0) 727 | { 728 | p = sz; 729 | } 730 | 731 | uint32_t h = rintf(p * d); 732 | for (uint32_t i = 0; i < sz; i++) 733 | { 734 | tab[i] = ((i + o) % p) < h ? a : -a; 735 | } 736 | } 737 | 738 | float SoftLimit(float x) 739 | { 740 | return x * (27.f + x * x) / (27.f + 9.f * x * x); 741 | } 742 | 743 | float SoftClip(float x) 744 | { 745 | if (x <= -3.f) 746 | { 747 | return -1.f; 748 | } 749 | else if (x >= 3.f) 750 | { 751 | return 1.f; 752 | } 753 | else 754 | { 755 | return SoftLimit(x); 756 | } 757 | } 758 | 759 | float HardClip(float x, float limit = 1.f) 760 | { 761 | return Clamp(x, -limit, limit); 762 | } 763 | 764 | float AudioClip(float x, float s = 1.f) 765 | { 766 | return SoftClip(x * s); 767 | } 768 | 769 | // Taken and adapted from stmlib 770 | class HysteresisQuantizer 771 | { 772 | public: 773 | HysteresisQuantizer() { } 774 | ~HysteresisQuantizer() { } 775 | 776 | void Init(int num_steps, float hysteresis, bool symmetric) 777 | { 778 | num_steps_ = num_steps; 779 | hysteresis_ = hysteresis; 780 | 781 | scale_ = static_cast(symmetric ? num_steps - 1 : num_steps); 782 | offset_ = symmetric ? 0.0f : -0.5f; 783 | 784 | quantized_value_ = 0; 785 | } 786 | 787 | inline int Process(float value) 788 | { 789 | return Process(0, value); 790 | } 791 | 792 | inline int Process(int base, float value) 793 | { 794 | value *= scale_; 795 | value += offset_; 796 | value += static_cast(base); 797 | 798 | float hysteresis_sign = value > static_cast(quantized_value_) 799 | ? -1.0f 800 | : +1.0f; 801 | int q = static_cast(value + hysteresis_sign * hysteresis_ + 0.5f); 802 | CONSTRAIN(q, 0, num_steps_ - 1); 803 | quantized_value_ = q; 804 | 805 | return q; 806 | } 807 | 808 | template 809 | const T& Lookup(const T* array, float value) 810 | { 811 | return array[Process(value)]; 812 | } 813 | 814 | inline int num_steps() const 815 | { 816 | return num_steps_; 817 | } 818 | 819 | inline int quantized_value() const 820 | { 821 | return quantized_value_; 822 | } 823 | 824 | private: 825 | int num_steps_; 826 | float hysteresis_; 827 | 828 | float scale_; 829 | float offset_; 830 | 831 | int quantized_value_; 832 | }; 833 | 834 | template 835 | class Lut 836 | { 837 | public: 838 | enum Type 839 | { 840 | LUT_TYPE_EXPO, 841 | LUT_TYPE_LINEAR, 842 | }; 843 | 844 | private: 845 | T lut_[size]; 846 | T min_; 847 | T max_; 848 | Type type_; 849 | float expoY_ = 3; 850 | HysteresisQuantizer quantizer_; 851 | 852 | void Linear() 853 | { 854 | float d = (1.f / (size - 1)); 855 | for (size_t i = 0; i < size; i++) 856 | { 857 | float x = d * i; 858 | lut_[i] = min_ + (max_ - min_) * x; 859 | } 860 | } 861 | 862 | void Expo() 863 | { 864 | float d = (1.f / (size - 1)); 865 | for (size_t i = 0; i < size; i++) 866 | { 867 | float x = d * i; 868 | lut_[i] = min_ + (max_ - min_) * (x == 1 ? 1 : fast_powf(x, expoY_)); 869 | } 870 | } 871 | 872 | public: 873 | Lut(T min, T max, Type type = LUT_TYPE_LINEAR) : min_{min}, max_{max}, type_{type} 874 | { 875 | quantizer_.Init(size, 0.15f, false); 876 | 877 | if (LUT_TYPE_EXPO == type) 878 | { 879 | Expo(); 880 | } 881 | else if (LUT_TYPE_LINEAR == type) 882 | { 883 | Linear(); 884 | } 885 | } 886 | ~Lut() {} 887 | 888 | void SetExpo(float y) 889 | { 890 | expoY_ = y; 891 | } 892 | 893 | T GetValue(int pos) 894 | { 895 | return lut_[pos]; 896 | } 897 | 898 | T Quantized(float pos) 899 | { 900 | return quantizer_.Lookup(lut_, pos); 901 | } 902 | }; 903 | 904 | // These is taken and adapted from code found in Emilie Gillet's eurorack repo. 905 | float Modulate( 906 | float baseValue, 907 | float modAmount, 908 | float modValue, 909 | float cvAmount = 0, 910 | float cvValue = 0, 911 | float minValue = -1.f, 912 | float maxValue = 1.f, 913 | bool modAttenuverters = false, 914 | bool cvAttenuverters = false 915 | ) { 916 | if (modAttenuverters) 917 | { 918 | modAmount = CenterMap(modAmount); 919 | // Deadband in the center. 920 | if (modAmount >= -0.1f && modAmount <= 0.1f) 921 | { 922 | modAmount = 0.f; 923 | } 924 | } 925 | 926 | if (cvAttenuverters) 927 | { 928 | cvAmount = CenterMap(cvAmount); 929 | // Deadband in the center. 930 | if (cvAmount >= -0.1f && cvAmount <= 0.1f) 931 | { 932 | cvAmount = 0.f; 933 | } 934 | } 935 | 936 | // Reduce noise when there's nothing connected to the CV. 937 | if (cvValue >= -kCvMinThreshold && cvValue <= kCvMinThreshold) 938 | { 939 | cvValue = kCvMinThreshold; 940 | } 941 | 942 | baseValue += modAmount * modValue + cvAmount * cvValue; 943 | CONSTRAIN(baseValue, minValue, maxValue); 944 | 945 | return baseValue; 946 | } 947 | 948 | float Attenuate( 949 | float baseValue, 950 | float modAmount, 951 | float modValue, 952 | float minValue = 0.f, 953 | float maxValue = 1.f 954 | ) { 955 | baseValue *= (1.f - modAmount + modAmount * modValue); 956 | CONSTRAIN(baseValue, minValue, maxValue); 957 | 958 | return baseValue; 959 | } -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | GNU GENERAL PUBLIC LICENSE 2 | Version 3, 29 June 2007 3 | 4 | Copyright (C) 2007 Free Software Foundation, Inc. 5 | Everyone is permitted to copy and distribute verbatim copies 6 | of this license document, but changing it is not allowed. 7 | 8 | Preamble 9 | 10 | The GNU General Public License is a free, copyleft license for 11 | software and other kinds of works. 12 | 13 | The licenses for most software and other practical works are designed 14 | to take away your freedom to share and change the works. By contrast, 15 | the GNU General Public License is intended to guarantee your freedom to 16 | share and change all versions of a program--to make sure it remains free 17 | software for all its users. We, the Free Software Foundation, use the 18 | GNU General Public License for most of our software; it applies also to 19 | any other work released this way by its authors. You can apply it to 20 | your programs, too. 21 | 22 | When we speak of free software, we are referring to freedom, not 23 | price. Our General Public Licenses are designed to make sure that you 24 | have the freedom to distribute copies of free software (and charge for 25 | them if you wish), that you receive source code or can get it if you 26 | want it, that you can change the software or use pieces of it in new 27 | free programs, and that you know you can do these things. 28 | 29 | To protect your rights, we need to prevent others from denying you 30 | these rights or asking you to surrender the rights. Therefore, you have 31 | certain responsibilities if you distribute copies of the software, or if 32 | you modify it: responsibilities to respect the freedom of others. 33 | 34 | For example, if you distribute copies of such a program, whether 35 | gratis or for a fee, you must pass on to the recipients the same 36 | freedoms that you received. You must make sure that they, too, receive 37 | or can get the source code. And you must show them these terms so they 38 | know their rights. 39 | 40 | Developers that use the GNU GPL protect your rights with two steps: 41 | (1) assert copyright on the software, and (2) offer you this License 42 | giving you legal permission to copy, distribute and/or modify it. 43 | 44 | For the developers' and authors' protection, the GPL clearly explains 45 | that there is no warranty for this free software. For both users' and 46 | authors' sake, the GPL requires that modified versions be marked as 47 | changed, so that their problems will not be attributed erroneously to 48 | authors of previous versions. 49 | 50 | Some devices are designed to deny users access to install or run 51 | modified versions of the software inside them, although the manufacturer 52 | can do so. This is fundamentally incompatible with the aim of 53 | protecting users' freedom to change the software. The systematic 54 | pattern of such abuse occurs in the area of products for individuals to 55 | use, which is precisely where it is most unacceptable. Therefore, we 56 | have designed this version of the GPL to prohibit the practice for those 57 | products. If such problems arise substantially in other domains, we 58 | stand ready to extend this provision to those domains in future versions 59 | of the GPL, as needed to protect the freedom of users. 60 | 61 | Finally, every program is threatened constantly by software patents. 62 | States should not allow patents to restrict development and use of 63 | software on general-purpose computers, but in those that do, we wish to 64 | avoid the special danger that patents applied to a free program could 65 | make it effectively proprietary. To prevent this, the GPL assures that 66 | patents cannot be used to render the program non-free. 67 | 68 | The precise terms and conditions for copying, distribution and 69 | modification follow. 70 | 71 | TERMS AND CONDITIONS 72 | 73 | 0. Definitions. 74 | 75 | "This License" refers to version 3 of the GNU General Public License. 76 | 77 | "Copyright" also means copyright-like laws that apply to other kinds of 78 | works, such as semiconductor masks. 79 | 80 | "The Program" refers to any copyrightable work licensed under this 81 | License. Each licensee is addressed as "you". "Licensees" and 82 | "recipients" may be individuals or organizations. 83 | 84 | To "modify" a work means to copy from or adapt all or part of the work 85 | in a fashion requiring copyright permission, other than the making of an 86 | exact copy. The resulting work is called a "modified version" of the 87 | earlier work or a work "based on" the earlier work. 88 | 89 | A "covered work" means either the unmodified Program or a work based 90 | on the Program. 91 | 92 | To "propagate" a work means to do anything with it that, without 93 | permission, would make you directly or secondarily liable for 94 | infringement under applicable copyright law, except executing it on a 95 | computer or modifying a private copy. Propagation includes copying, 96 | distribution (with or without modification), making available to the 97 | public, and in some countries other activities as well. 98 | 99 | To "convey" a work means any kind of propagation that enables other 100 | parties to make or receive copies. Mere interaction with a user through 101 | a computer network, with no transfer of a copy, is not conveying. 102 | 103 | An interactive user interface displays "Appropriate Legal Notices" 104 | to the extent that it includes a convenient and prominently visible 105 | feature that (1) displays an appropriate copyright notice, and (2) 106 | tells the user that there is no warranty for the work (except to the 107 | extent that warranties are provided), that licensees may convey the 108 | work under this License, and how to view a copy of this License. If 109 | the interface presents a list of user commands or options, such as a 110 | menu, a prominent item in the list meets this criterion. 111 | 112 | 1. Source Code. 113 | 114 | The "source code" for a work means the preferred form of the work 115 | for making modifications to it. "Object code" means any non-source 116 | form of a work. 117 | 118 | A "Standard Interface" means an interface that either is an official 119 | standard defined by a recognized standards body, or, in the case of 120 | interfaces specified for a particular programming language, one that 121 | is widely used among developers working in that language. 122 | 123 | The "System Libraries" of an executable work include anything, other 124 | than the work as a whole, that (a) is included in the normal form of 125 | packaging a Major Component, but which is not part of that Major 126 | Component, and (b) serves only to enable use of the work with that 127 | Major Component, or to implement a Standard Interface for which an 128 | implementation is available to the public in source code form. A 129 | "Major Component", in this context, means a major essential component 130 | (kernel, window system, and so on) of the specific operating system 131 | (if any) on which the executable work runs, or a compiler used to 132 | produce the work, or an object code interpreter used to run it. 133 | 134 | The "Corresponding Source" for a work in object code form means all 135 | the source code needed to generate, install, and (for an executable 136 | work) run the object code and to modify the work, including scripts to 137 | control those activities. However, it does not include the work's 138 | System Libraries, or general-purpose tools or generally available free 139 | programs which are used unmodified in performing those activities but 140 | which are not part of the work. For example, Corresponding Source 141 | includes interface definition files associated with source files for 142 | the work, and the source code for shared libraries and dynamically 143 | linked subprograms that the work is specifically designed to require, 144 | such as by intimate data communication or control flow between those 145 | subprograms and other parts of the work. 146 | 147 | The Corresponding Source need not include anything that users 148 | can regenerate automatically from other parts of the Corresponding 149 | Source. 150 | 151 | The Corresponding Source for a work in source code form is that 152 | same work. 153 | 154 | 2. Basic Permissions. 155 | 156 | All rights granted under this License are granted for the term of 157 | copyright on the Program, and are irrevocable provided the stated 158 | conditions are met. This License explicitly affirms your unlimited 159 | permission to run the unmodified Program. The output from running a 160 | covered work is covered by this License only if the output, given its 161 | content, constitutes a covered work. This License acknowledges your 162 | rights of fair use or other equivalent, as provided by copyright law. 163 | 164 | You may make, run and propagate covered works that you do not 165 | convey, without conditions so long as your license otherwise remains 166 | in force. You may convey covered works to others for the sole purpose 167 | of having them make modifications exclusively for you, or provide you 168 | with facilities for running those works, provided that you comply with 169 | the terms of this License in conveying all material for which you do 170 | not control copyright. Those thus making or running the covered works 171 | for you must do so exclusively on your behalf, under your direction 172 | and control, on terms that prohibit them from making any copies of 173 | your copyrighted material outside their relationship with you. 174 | 175 | Conveying under any other circumstances is permitted solely under 176 | the conditions stated below. Sublicensing is not allowed; section 10 177 | makes it unnecessary. 178 | 179 | 3. Protecting Users' Legal Rights From Anti-Circumvention Law. 180 | 181 | No covered work shall be deemed part of an effective technological 182 | measure under any applicable law fulfilling obligations under article 183 | 11 of the WIPO copyright treaty adopted on 20 December 1996, or 184 | similar laws prohibiting or restricting circumvention of such 185 | measures. 186 | 187 | When you convey a covered work, you waive any legal power to forbid 188 | circumvention of technological measures to the extent such circumvention 189 | is effected by exercising rights under this License with respect to 190 | the covered work, and you disclaim any intention to limit operation or 191 | modification of the work as a means of enforcing, against the work's 192 | users, your or third parties' legal rights to forbid circumvention of 193 | technological measures. 194 | 195 | 4. Conveying Verbatim Copies. 196 | 197 | You may convey verbatim copies of the Program's source code as you 198 | receive it, in any medium, provided that you conspicuously and 199 | appropriately publish on each copy an appropriate copyright notice; 200 | keep intact all notices stating that this License and any 201 | non-permissive terms added in accord with section 7 apply to the code; 202 | keep intact all notices of the absence of any warranty; and give all 203 | recipients a copy of this License along with the Program. 204 | 205 | You may charge any price or no price for each copy that you convey, 206 | and you may offer support or warranty protection for a fee. 207 | 208 | 5. Conveying Modified Source Versions. 209 | 210 | You may convey a work based on the Program, or the modifications to 211 | produce it from the Program, in the form of source code under the 212 | terms of section 4, provided that you also meet all of these conditions: 213 | 214 | a) The work must carry prominent notices stating that you modified 215 | it, and giving a relevant date. 216 | 217 | b) The work must carry prominent notices stating that it is 218 | released under this License and any conditions added under section 219 | 7. This requirement modifies the requirement in section 4 to 220 | "keep intact all notices". 221 | 222 | c) You must license the entire work, as a whole, under this 223 | License to anyone who comes into possession of a copy. This 224 | License will therefore apply, along with any applicable section 7 225 | additional terms, to the whole of the work, and all its parts, 226 | regardless of how they are packaged. This License gives no 227 | permission to license the work in any other way, but it does not 228 | invalidate such permission if you have separately received it. 229 | 230 | d) If the work has interactive user interfaces, each must display 231 | Appropriate Legal Notices; however, if the Program has interactive 232 | interfaces that do not display Appropriate Legal Notices, your 233 | work need not make them do so. 234 | 235 | A compilation of a covered work with other separate and independent 236 | works, which are not by their nature extensions of the covered work, 237 | and which are not combined with it such as to form a larger program, 238 | in or on a volume of a storage or distribution medium, is called an 239 | "aggregate" if the compilation and its resulting copyright are not 240 | used to limit the access or legal rights of the compilation's users 241 | beyond what the individual works permit. Inclusion of a covered work 242 | in an aggregate does not cause this License to apply to the other 243 | parts of the aggregate. 244 | 245 | 6. Conveying Non-Source Forms. 246 | 247 | You may convey a covered work in object code form under the terms 248 | of sections 4 and 5, provided that you also convey the 249 | machine-readable Corresponding Source under the terms of this License, 250 | in one of these ways: 251 | 252 | a) Convey the object code in, or embodied in, a physical product 253 | (including a physical distribution medium), accompanied by the 254 | Corresponding Source fixed on a durable physical medium 255 | customarily used for software interchange. 256 | 257 | b) Convey the object code in, or embodied in, a physical product 258 | (including a physical distribution medium), accompanied by a 259 | written offer, valid for at least three years and valid for as 260 | long as you offer spare parts or customer support for that product 261 | model, to give anyone who possesses the object code either (1) a 262 | copy of the Corresponding Source for all the software in the 263 | product that is covered by this License, on a durable physical 264 | medium customarily used for software interchange, for a price no 265 | more than your reasonable cost of physically performing this 266 | conveying of source, or (2) access to copy the 267 | Corresponding Source from a network server at no charge. 268 | 269 | c) Convey individual copies of the object code with a copy of the 270 | written offer to provide the Corresponding Source. This 271 | alternative is allowed only occasionally and noncommercially, and 272 | only if you received the object code with such an offer, in accord 273 | with subsection 6b. 274 | 275 | d) Convey the object code by offering access from a designated 276 | place (gratis or for a charge), and offer equivalent access to the 277 | Corresponding Source in the same way through the same place at no 278 | further charge. You need not require recipients to copy the 279 | Corresponding Source along with the object code. If the place to 280 | copy the object code is a network server, the Corresponding Source 281 | may be on a different server (operated by you or a third party) 282 | that supports equivalent copying facilities, provided you maintain 283 | clear directions next to the object code saying where to find the 284 | Corresponding Source. Regardless of what server hosts the 285 | Corresponding Source, you remain obligated to ensure that it is 286 | available for as long as needed to satisfy these requirements. 287 | 288 | e) Convey the object code using peer-to-peer transmission, provided 289 | you inform other peers where the object code and Corresponding 290 | Source of the work are being offered to the general public at no 291 | charge under subsection 6d. 292 | 293 | A separable portion of the object code, whose source code is excluded 294 | from the Corresponding Source as a System Library, need not be 295 | included in conveying the object code work. 296 | 297 | A "User Product" is either (1) a "consumer product", which means any 298 | tangible personal property which is normally used for personal, family, 299 | or household purposes, or (2) anything designed or sold for incorporation 300 | into a dwelling. In determining whether a product is a consumer product, 301 | doubtful cases shall be resolved in favor of coverage. For a particular 302 | product received by a particular user, "normally used" refers to a 303 | typical or common use of that class of product, regardless of the status 304 | of the particular user or of the way in which the particular user 305 | actually uses, or expects or is expected to use, the product. A product 306 | is a consumer product regardless of whether the product has substantial 307 | commercial, industrial or non-consumer uses, unless such uses represent 308 | the only significant mode of use of the product. 309 | 310 | "Installation Information" for a User Product means any methods, 311 | procedures, authorization keys, or other information required to install 312 | and execute modified versions of a covered work in that User Product from 313 | a modified version of its Corresponding Source. The information must 314 | suffice to ensure that the continued functioning of the modified object 315 | code is in no case prevented or interfered with solely because 316 | modification has been made. 317 | 318 | If you convey an object code work under this section in, or with, or 319 | specifically for use in, a User Product, and the conveying occurs as 320 | part of a transaction in which the right of possession and use of the 321 | User Product is transferred to the recipient in perpetuity or for a 322 | fixed term (regardless of how the transaction is characterized), the 323 | Corresponding Source conveyed under this section must be accompanied 324 | by the Installation Information. But this requirement does not apply 325 | if neither you nor any third party retains the ability to install 326 | modified object code on the User Product (for example, the work has 327 | been installed in ROM). 328 | 329 | The requirement to provide Installation Information does not include a 330 | requirement to continue to provide support service, warranty, or updates 331 | for a work that has been modified or installed by the recipient, or for 332 | the User Product in which it has been modified or installed. Access to a 333 | network may be denied when the modification itself materially and 334 | adversely affects the operation of the network or violates the rules and 335 | protocols for communication across the network. 336 | 337 | Corresponding Source conveyed, and Installation Information provided, 338 | in accord with this section must be in a format that is publicly 339 | documented (and with an implementation available to the public in 340 | source code form), and must require no special password or key for 341 | unpacking, reading or copying. 342 | 343 | 7. Additional Terms. 344 | 345 | "Additional permissions" are terms that supplement the terms of this 346 | License by making exceptions from one or more of its conditions. 347 | Additional permissions that are applicable to the entire Program shall 348 | be treated as though they were included in this License, to the extent 349 | that they are valid under applicable law. If additional permissions 350 | apply only to part of the Program, that part may be used separately 351 | under those permissions, but the entire Program remains governed by 352 | this License without regard to the additional permissions. 353 | 354 | When you convey a copy of a covered work, you may at your option 355 | remove any additional permissions from that copy, or from any part of 356 | it. (Additional permissions may be written to require their own 357 | removal in certain cases when you modify the work.) You may place 358 | additional permissions on material, added by you to a covered work, 359 | for which you have or can give appropriate copyright permission. 360 | 361 | Notwithstanding any other provision of this License, for material you 362 | add to a covered work, you may (if authorized by the copyright holders of 363 | that material) supplement the terms of this License with terms: 364 | 365 | a) Disclaiming warranty or limiting liability differently from the 366 | terms of sections 15 and 16 of this License; or 367 | 368 | b) Requiring preservation of specified reasonable legal notices or 369 | author attributions in that material or in the Appropriate Legal 370 | Notices displayed by works containing it; or 371 | 372 | c) Prohibiting misrepresentation of the origin of that material, or 373 | requiring that modified versions of such material be marked in 374 | reasonable ways as different from the original version; or 375 | 376 | d) Limiting the use for publicity purposes of names of licensors or 377 | authors of the material; or 378 | 379 | e) Declining to grant rights under trademark law for use of some 380 | trade names, trademarks, or service marks; or 381 | 382 | f) Requiring indemnification of licensors and authors of that 383 | material by anyone who conveys the material (or modified versions of 384 | it) with contractual assumptions of liability to the recipient, for 385 | any liability that these contractual assumptions directly impose on 386 | those licensors and authors. 387 | 388 | All other non-permissive additional terms are considered "further 389 | restrictions" within the meaning of section 10. If the Program as you 390 | received it, or any part of it, contains a notice stating that it is 391 | governed by this License along with a term that is a further 392 | restriction, you may remove that term. If a license document contains 393 | a further restriction but permits relicensing or conveying under this 394 | License, you may add to a covered work material governed by the terms 395 | of that license document, provided that the further restriction does 396 | not survive such relicensing or conveying. 397 | 398 | If you add terms to a covered work in accord with this section, you 399 | must place, in the relevant source files, a statement of the 400 | additional terms that apply to those files, or a notice indicating 401 | where to find the applicable terms. 402 | 403 | Additional terms, permissive or non-permissive, may be stated in the 404 | form of a separately written license, or stated as exceptions; 405 | the above requirements apply either way. 406 | 407 | 8. Termination. 408 | 409 | You may not propagate or modify a covered work except as expressly 410 | provided under this License. Any attempt otherwise to propagate or 411 | modify it is void, and will automatically terminate your rights under 412 | this License (including any patent licenses granted under the third 413 | paragraph of section 11). 414 | 415 | However, if you cease all violation of this License, then your 416 | license from a particular copyright holder is reinstated (a) 417 | provisionally, unless and until the copyright holder explicitly and 418 | finally terminates your license, and (b) permanently, if the copyright 419 | holder fails to notify you of the violation by some reasonable means 420 | prior to 60 days after the cessation. 421 | 422 | Moreover, your license from a particular copyright holder is 423 | reinstated permanently if the copyright holder notifies you of the 424 | violation by some reasonable means, this is the first time you have 425 | received notice of violation of this License (for any work) from that 426 | copyright holder, and you cure the violation prior to 30 days after 427 | your receipt of the notice. 428 | 429 | Termination of your rights under this section does not terminate the 430 | licenses of parties who have received copies or rights from you under 431 | this License. If your rights have been terminated and not permanently 432 | reinstated, you do not qualify to receive new licenses for the same 433 | material under section 10. 434 | 435 | 9. Acceptance Not Required for Having Copies. 436 | 437 | You are not required to accept this License in order to receive or 438 | run a copy of the Program. Ancillary propagation of a covered work 439 | occurring solely as a consequence of using peer-to-peer transmission 440 | to receive a copy likewise does not require acceptance. However, 441 | nothing other than this License grants you permission to propagate or 442 | modify any covered work. These actions infringe copyright if you do 443 | not accept this License. Therefore, by modifying or propagating a 444 | covered work, you indicate your acceptance of this License to do so. 445 | 446 | 10. Automatic Licensing of Downstream Recipients. 447 | 448 | Each time you convey a covered work, the recipient automatically 449 | receives a license from the original licensors, to run, modify and 450 | propagate that work, subject to this License. You are not responsible 451 | for enforcing compliance by third parties with this License. 452 | 453 | An "entity transaction" is a transaction transferring control of an 454 | organization, or substantially all assets of one, or subdividing an 455 | organization, or merging organizations. If propagation of a covered 456 | work results from an entity transaction, each party to that 457 | transaction who receives a copy of the work also receives whatever 458 | licenses to the work the party's predecessor in interest had or could 459 | give under the previous paragraph, plus a right to possession of the 460 | Corresponding Source of the work from the predecessor in interest, if 461 | the predecessor has it or can get it with reasonable efforts. 462 | 463 | You may not impose any further restrictions on the exercise of the 464 | rights granted or affirmed under this License. For example, you may 465 | not impose a license fee, royalty, or other charge for exercise of 466 | rights granted under this License, and you may not initiate litigation 467 | (including a cross-claim or counterclaim in a lawsuit) alleging that 468 | any patent claim is infringed by making, using, selling, offering for 469 | sale, or importing the Program or any portion of it. 470 | 471 | 11. Patents. 472 | 473 | A "contributor" is a copyright holder who authorizes use under this 474 | License of the Program or a work on which the Program is based. The 475 | work thus licensed is called the contributor's "contributor version". 476 | 477 | A contributor's "essential patent claims" are all patent claims 478 | owned or controlled by the contributor, whether already acquired or 479 | hereafter acquired, that would be infringed by some manner, permitted 480 | by this License, of making, using, or selling its contributor version, 481 | but do not include claims that would be infringed only as a 482 | consequence of further modification of the contributor version. For 483 | purposes of this definition, "control" includes the right to grant 484 | patent sublicenses in a manner consistent with the requirements of 485 | this License. 486 | 487 | Each contributor grants you a non-exclusive, worldwide, royalty-free 488 | patent license under the contributor's essential patent claims, to 489 | make, use, sell, offer for sale, import and otherwise run, modify and 490 | propagate the contents of its contributor version. 491 | 492 | In the following three paragraphs, a "patent license" is any express 493 | agreement or commitment, however denominated, not to enforce a patent 494 | (such as an express permission to practice a patent or covenant not to 495 | sue for patent infringement). To "grant" such a patent license to a 496 | party means to make such an agreement or commitment not to enforce a 497 | patent against the party. 498 | 499 | If you convey a covered work, knowingly relying on a patent license, 500 | and the Corresponding Source of the work is not available for anyone 501 | to copy, free of charge and under the terms of this License, through a 502 | publicly available network server or other readily accessible means, 503 | then you must either (1) cause the Corresponding Source to be so 504 | available, or (2) arrange to deprive yourself of the benefit of the 505 | patent license for this particular work, or (3) arrange, in a manner 506 | consistent with the requirements of this License, to extend the patent 507 | license to downstream recipients. "Knowingly relying" means you have 508 | actual knowledge that, but for the patent license, your conveying the 509 | covered work in a country, or your recipient's use of the covered work 510 | in a country, would infringe one or more identifiable patents in that 511 | country that you have reason to believe are valid. 512 | 513 | If, pursuant to or in connection with a single transaction or 514 | arrangement, you convey, or propagate by procuring conveyance of, a 515 | covered work, and grant a patent license to some of the parties 516 | receiving the covered work authorizing them to use, propagate, modify 517 | or convey a specific copy of the covered work, then the patent license 518 | you grant is automatically extended to all recipients of the covered 519 | work and works based on it. 520 | 521 | A patent license is "discriminatory" if it does not include within 522 | the scope of its coverage, prohibits the exercise of, or is 523 | conditioned on the non-exercise of one or more of the rights that are 524 | specifically granted under this License. You may not convey a covered 525 | work if you are a party to an arrangement with a third party that is 526 | in the business of distributing software, under which you make payment 527 | to the third party based on the extent of your activity of conveying 528 | the work, and under which the third party grants, to any of the 529 | parties who would receive the covered work from you, a discriminatory 530 | patent license (a) in connection with copies of the covered work 531 | conveyed by you (or copies made from those copies), or (b) primarily 532 | for and in connection with specific products or compilations that 533 | contain the covered work, unless you entered into that arrangement, 534 | or that patent license was granted, prior to 28 March 2007. 535 | 536 | Nothing in this License shall be construed as excluding or limiting 537 | any implied license or other defenses to infringement that may 538 | otherwise be available to you under applicable patent law. 539 | 540 | 12. No Surrender of Others' Freedom. 541 | 542 | If conditions are imposed on you (whether by court order, agreement or 543 | otherwise) that contradict the conditions of this License, they do not 544 | excuse you from the conditions of this License. If you cannot convey a 545 | covered work so as to satisfy simultaneously your obligations under this 546 | License and any other pertinent obligations, then as a consequence you may 547 | not convey it at all. For example, if you agree to terms that obligate you 548 | to collect a royalty for further conveying from those to whom you convey 549 | the Program, the only way you could satisfy both those terms and this 550 | License would be to refrain entirely from conveying the Program. 551 | 552 | 13. Use with the GNU Affero General Public License. 553 | 554 | Notwithstanding any other provision of this License, you have 555 | permission to link or combine any covered work with a work licensed 556 | under version 3 of the GNU Affero General Public License into a single 557 | combined work, and to convey the resulting work. The terms of this 558 | License will continue to apply to the part which is the covered work, 559 | but the special requirements of the GNU Affero General Public License, 560 | section 13, concerning interaction through a network will apply to the 561 | combination as such. 562 | 563 | 14. Revised Versions of this License. 564 | 565 | The Free Software Foundation may publish revised and/or new versions of 566 | the GNU General Public License from time to time. Such new versions will 567 | be similar in spirit to the present version, but may differ in detail to 568 | address new problems or concerns. 569 | 570 | Each version is given a distinguishing version number. If the 571 | Program specifies that a certain numbered version of the GNU General 572 | Public License "or any later version" applies to it, you have the 573 | option of following the terms and conditions either of that numbered 574 | version or of any later version published by the Free Software 575 | Foundation. If the Program does not specify a version number of the 576 | GNU General Public License, you may choose any version ever published 577 | by the Free Software Foundation. 578 | 579 | If the Program specifies that a proxy can decide which future 580 | versions of the GNU General Public License can be used, that proxy's 581 | public statement of acceptance of a version permanently authorizes you 582 | to choose that version for the Program. 583 | 584 | Later license versions may give you additional or different 585 | permissions. However, no additional obligations are imposed on any 586 | author or copyright holder as a result of your choosing to follow a 587 | later version. 588 | 589 | 15. Disclaimer of Warranty. 590 | 591 | THERE IS NO WARRANTY FOR THE PROGRAM, TO THE EXTENT PERMITTED BY 592 | APPLICABLE LAW. EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT 593 | HOLDERS AND/OR OTHER PARTIES PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY 594 | OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, 595 | THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 596 | PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE PROGRAM 597 | IS WITH YOU. SHOULD THE PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF 598 | ALL NECESSARY SERVICING, REPAIR OR CORRECTION. 599 | 600 | 16. Limitation of Liability. 601 | 602 | IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING 603 | WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MODIFIES AND/OR CONVEYS 604 | THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY 605 | GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE 606 | USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED TO LOSS OF 607 | DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD 608 | PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER PROGRAMS), 609 | EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF 610 | SUCH DAMAGES. 611 | 612 | 17. Interpretation of Sections 15 and 16. 613 | 614 | If the disclaimer of warranty and limitation of liability provided 615 | above cannot be given local legal effect according to their terms, 616 | reviewing courts shall apply local law that most closely approximates 617 | an absolute waiver of all civil liability in connection with the 618 | Program, unless a warranty or assumption of liability accompanies a 619 | copy of the Program in return for a fee. 620 | 621 | END OF TERMS AND CONDITIONS 622 | 623 | How to Apply These Terms to Your New Programs 624 | 625 | If you develop a new program, and you want it to be of the greatest 626 | possible use to the public, the best way to achieve this is to make it 627 | free software which everyone can redistribute and change under these terms. 628 | 629 | To do so, attach the following notices to the program. It is safest 630 | to attach them to the start of each source file to most effectively 631 | state the exclusion of warranty; and each file should have at least 632 | the "copyright" line and a pointer to where the full notice is found. 633 | 634 | 635 | Copyright (C) 636 | 637 | This program is free software: you can redistribute it and/or modify 638 | it under the terms of the GNU General Public License as published by 639 | the Free Software Foundation, either version 3 of the License, or 640 | (at your option) any later version. 641 | 642 | This program is distributed in the hope that it will be useful, 643 | but WITHOUT ANY WARRANTY; without even the implied warranty of 644 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 645 | GNU General Public License for more details. 646 | 647 | You should have received a copy of the GNU General Public License 648 | along with this program. If not, see . 649 | 650 | Also add information on how to contact you by electronic and paper mail. 651 | 652 | If the program does terminal interaction, make it output a short 653 | notice like this when it starts in an interactive mode: 654 | 655 | Copyright (C) 656 | This program comes with ABSOLUTELY NO WARRANTY; for details type `show w'. 657 | This is free software, and you are welcome to redistribute it 658 | under certain conditions; type `show c' for details. 659 | 660 | The hypothetical commands `show w' and `show c' should show the appropriate 661 | parts of the General Public License. Of course, your program's commands 662 | might be different; for a GUI interface, you would use an "about box". 663 | 664 | You should also get your employer (if you work as a programmer) or school, 665 | if any, to sign a "copyright disclaimer" for the program, if necessary. 666 | For more information on this, and how to apply and follow the GNU GPL, see 667 | . 668 | 669 | The GNU General Public License does not permit incorporating your program 670 | into proprietary programs. If your program is a subroutine library, you 671 | may consider it more useful to permit linking proprietary applications with 672 | the library. If this is what you want to do, use the GNU Lesser General 673 | Public License instead of this License. But first, please read 674 | . 675 | 676 | --------------------------------------------------------------------------------