DaisySP
Loading...
Searching...
No Matches
ladder.h
1/* Ported from Audio Library for Teensy, Ladder Filter
2 * Copyright (c) 2021, Richard van Hoesel
3 * Copyright (c) 2024, Infrasonic Audio LLC
4 *
5 * Permission is hereby granted, free of charge, to any person obtaining a copy
6 * of this software and associated documentation files (the "Software"), to deal
7 * in the Software without restriction, including without limitation the rights
8 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 * copies of the Software, and to permit persons to whom the Software is
10 * furnished to do so, subject to the following conditions:
11 *
12 * The above copyright notice, development funding notice, and this permission
13 * notice shall be included in all copies or substantial portions of the Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21 * THE SOFTWARE.
22 */
23
24//-----------------------------------------------------------
25// Huovilainen New Moog (HNM) model as per CMJ jun 2006
26// Richard van Hoesel, v. 1.03, Feb. 14 2021
27// v1.7 (Infrasonic/Daisy) add configurable filter mode
28// v1.6 (Infrasonic/Daisy) removes polyphase FIR, uses 4x linear
29// oversampling for performance reasons
30// v1.5 adds polyphase FIR or Linear interpolation
31// v1.4 FC extended to 18.7kHz, max res to 1.8, 4x oversampling,
32// and a minor Q-tuning adjustment
33// v.1.03 adds oversampling, extended resonance,
34// and exposes parameters input_drive and passband_gain
35// v.1.02 now includes both cutoff and resonance "CV" modulation inputs
36// please retain this header if you use this code.
37//-----------------------------------------------------------
38
39#pragma once
40#ifndef DSY_LADDER_H
41#define DSY_LADDER_H
42
43#include <stdlib.h>
44#include <stdint.h>
45#include <array>
46#ifdef __cplusplus
47
48namespace daisysp
49{
57{
58 public:
59 enum class FilterMode
60 {
61 LP24,
62 LP12,
63 BP24,
64 BP12,
65 HP24,
66 HP12
67 };
68
69 LadderFilter() = default;
70 ~LadderFilter() = default;
71
74 void Init(float sample_rate);
75
77 float Process(float in);
78
80 void ProcessBlock(float* buf, size_t size);
81
87 void SetFreq(float freq);
88
95 void SetRes(float res);
96
104 void SetPassbandGain(float pbg);
105
110 void SetInputDrive(float drv);
111
116 inline void SetFilterMode(FilterMode mode) { mode_ = mode; }
117
118 private:
119 static constexpr uint8_t kInterpolation = 4;
120 static constexpr float kInterpolationRecip = 1.0f / kInterpolation;
121 static constexpr float kMaxResonance = 1.8f;
122
123 float sample_rate_, sr_int_recip_;
124 float alpha_;
125 float beta_[4] = {0.0, 0.0, 0.0, 0.0};
126 float z0_[4] = {0.0, 0.0, 0.0, 0.0};
127 float z1_[4] = {0.0, 0.0, 0.0, 0.0};
128 float K_;
129 float Fbase_;
130 float Qadjust_;
131 float pbg_;
132 float drive_, drive_scaled_;
133 float oldinput_;
134 FilterMode mode_;
135
136 float LPF(float s, int i);
137 void compute_coeffs(float fc);
138 float weightedSumForCurrentMode(const std::array<float, 5>& stage_outs);
139};
140
141
142} // namespace daisysp
143#endif
144#endif
Definition delayline.h:29
Definition ladder.h:57
void SetInputDrive(float drv)
Definition ladder.cpp:122
void Init(float sample_rate)
Definition ladder.cpp:54
void SetFreq(float freq)
Definition ladder.cpp:103
void SetFilterMode(FilterMode mode)
Definition ladder.h:116
void ProcessBlock(float *buf, size_t size)
void SetRes(float res)
Definition ladder.cpp:109
float Process(float in)
Definition ladder.cpp:71
void SetPassbandGain(float pbg)
Definition ladder.cpp:116
FIR Filter implementation, generic and ARM CMSIS DSP based.
Definition adenv.h:16