MostlyHarmless 0.0.1
 
Loading...
Searching...
No Matches
marvin_SmoothedBiquadCoefficients.h
Go to the documentation of this file.
1// ========================================================================================================
2// _______ _______ ______ ___ ___ _______ _______
3// | | | _ | __ \ | |_ _| | |
4// | | | < | |_| |_| |
5// |__|_|__|___|___|___|__|\_____/|_______|__|____|
6//
7// This file is part of the Marvin open source library and is licensed under the terms of the MIT License.
8//
9// ========================================================================================================
10
11#ifndef MARVIN_SMOOTHEDBIQUAD_COEFFICIENTS_H
12#define MARVIN_SMOOTHEDBIQUAD_COEFFICIENTS_H
13
17#include <array>
18#include <cassert>
19namespace marvin::dsp::filters {
20
73 template <FloatType SampleType, utils::SmoothingType InterpolationType, size_t NumStages>
74 requires(NumStages > 0)
76 public:
81 void reset(int periodSamples) noexcept {
82 for (auto& stage : m_stages) {
83 stage.reset(periodSamples);
84 }
85 }
86
92 void reset(double sampleRate, double timeMs) noexcept {
93 for (auto& stage : m_stages) {
94 stage.reset(sampleRate, timeMs);
95 }
96 }
97
104 assert(stage < NumStages);
105 m_stages[stage].setCurrentAndTargetCoeffs(target);
106 }
107
114 assert(stage < NumStages);
115 m_stages[stage].setTargetCoeffs(target);
116 }
117
123 [[nodiscard]] BiquadCoefficients<SampleType> current(size_t stage) const noexcept {
124 assert(stage < NumStages);
125 return m_stages[stage].current();
126 }
127
133 [[nodiscard]] BiquadCoefficients<SampleType> target(size_t stage) const noexcept {
134 assert(stage < NumStages);
135 return m_stages[stage].target();
136 }
137
140 void interpolate() noexcept {
141 for (auto& stage : m_stages) {
142 stage.interpolate();
143 }
144 }
145
146 private:
147 class CoeffSmoothingStage final {
148 public:
149 void reset(int periodSamples) noexcept {
150 for (auto& s : m_smoothers) {
151 s.reset(periodSamples);
152 }
153 }
154
155 void reset(double sampleRate, double timeMs) noexcept {
156 for (auto& s : m_smoothers) {
157 s.reset(sampleRate, timeMs);
158 }
159 }
160
161 void setCurrentAndTargetCoeffs(BiquadCoefficients<SampleType> newCoeffs) noexcept {
162 m_smoothers[0].setCurrentAndTargetValue(newCoeffs.a0);
163 m_smoothers[1].setCurrentAndTargetValue(newCoeffs.a1);
164 m_smoothers[2].setCurrentAndTargetValue(newCoeffs.a2);
165 m_smoothers[3].setCurrentAndTargetValue(newCoeffs.b0);
166 m_smoothers[4].setCurrentAndTargetValue(newCoeffs.b1);
167 m_smoothers[5].setCurrentAndTargetValue(newCoeffs.b2);
168 m_current = newCoeffs;
169 }
170
171 void setTargetCoeffs(BiquadCoefficients<SampleType> newCoeffs) noexcept {
172 m_smoothers[0].setTargetValue(newCoeffs.a0);
173 m_smoothers[1].setTargetValue(newCoeffs.a1);
174 m_smoothers[2].setTargetValue(newCoeffs.a2);
175 m_smoothers[3].setTargetValue(newCoeffs.b0);
176 m_smoothers[4].setTargetValue(newCoeffs.b1);
177 m_smoothers[5].setTargetValue(newCoeffs.b2);
178 }
179
180 void interpolate() noexcept {
181 m_current = {
182 .a0 = m_smoothers[0](),
183 .a1 = m_smoothers[1](),
184 .a2 = m_smoothers[2](),
185 .b0 = m_smoothers[3](),
186 .b1 = m_smoothers[4](),
187 .b2 = m_smoothers[5]()
188 };
189 }
190
191 [[nodiscard]] BiquadCoefficients<SampleType> current() const noexcept {
192 return m_current;
193 }
194
195 [[nodiscard]] BiquadCoefficients<SampleType> target() const noexcept {
196 return {
197 .a0 = m_smoothers[0].getTargetValue(),
198 .a1 = m_smoothers[1].getTargetValue(),
199 .a2 = m_smoothers[2].getTargetValue(),
200 .b0 = m_smoothers[3].getTargetValue(),
201 .b1 = m_smoothers[4].getTargetValue(),
202 .b2 = m_smoothers[5].getTargetValue()
203 };
204 }
205
206 private:
207 std::array<marvin::utils::SmoothedValue<SampleType, InterpolationType>, 6> m_smoothers;
208 BiquadCoefficients<SampleType> m_current{};
209 };
210
211 std::array<CoeffSmoothingStage, NumStages> m_stages;
212 };
213} // namespace marvin::dsp::filters
214#endif
Helper class to simplify smoothly changing BiquadCoefficients with no zippering.
Definition marvin_SmoothedBiquadCoefficients.h:75
void setCurrentAndTargetCoeffs(size_t stage, BiquadCoefficients< SampleType > target) noexcept
Definition marvin_SmoothedBiquadCoefficients.h:103
void interpolate() noexcept
Definition marvin_SmoothedBiquadCoefficients.h:140
void reset(int periodSamples) noexcept
Definition marvin_SmoothedBiquadCoefficients.h:81
BiquadCoefficients< SampleType > current(size_t stage) const noexcept
Definition marvin_SmoothedBiquadCoefficients.h:123
BiquadCoefficients< SampleType > target(size_t stage) const noexcept
Definition marvin_SmoothedBiquadCoefficients.h:133
void reset(double sampleRate, double timeMs) noexcept
Definition marvin_SmoothedBiquadCoefficients.h:92
void setTargetCoeffs(size_t stage, BiquadCoefficients< SampleType > target) noexcept
Definition marvin_SmoothedBiquadCoefficients.h:113
Digital filter functions and classes.
Definition marvin_SVF.h:15
A POD type for use with the Biquad class, and the SmoothedBiquadCoefficients class.
Definition marvin_BiquadCoefficients.h:22