44#include "AudioBuffer.h"
46#include "BiquadCascade.h"
48#include "FilterDesign.h"
71template <FloatType T,
int MaxChannels = 16>
82 void prepare(
double sampleRate,
int numChannels)
84 if (!std::isfinite(sampleRate) || !(sampleRate > 0.0) || numChannels <= 0)
return;
86 channels_ = std::min(numChannels, MaxChannels);
87 if (!cascade_) cascade_.reset(
new (std::nothrow)
Cascade());
88 if (!cascade_)
return;
92 for (
int k = 0; k <
kTaps; ++k)
93 tapDelay_[
static_cast<size_t>(k)] = std::max(1,
static_cast<int>(std::lround(kTapMs[k] * 0.001 * fs_)));
94 diffuserDelay_[0] = std::max(1,
static_cast<int>(std::lround(kDiffuserMs[0] * 0.001 * fs_)));
95 diffuserDelay_[1] = std::max(1,
static_cast<int>(std::lround(kDiffuserMs[1] * 0.001 * fs_)));
97 while (size_ <= tapDelay_[
kTaps - 1] + 1 || size_ <= diffuserDelay_[1] + 1) size_ <<= 1;
98 lines_.assign(
static_cast<size_t>(channels_) * 3u *
static_cast<size_t>(size_), T(0));
99 rampSamples_ = std::max(1,
static_cast<int>(0.02 * fs_));
101 distanceNow_ =
static_cast<double>(distance_.load(std::memory_order_relaxed));
102 dirty_.store(
true, std::memory_order_release);
109 if (cascade_) cascade_->reset();
110 std::fill(lines_.begin(), lines_.end(), T(0));
112 distanceNow_ =
static_cast<double>(distance_.load(std::memory_order_relaxed));
121 if (!std::isfinite(size))
return;
122 sizeParam_.store(std::clamp(size, T(0), T(1)), std::memory_order_relaxed);
123 dirty_.store(
true, std::memory_order_release);
131 if (!std::isfinite(brightness))
return;
132 brightness_.store(std::clamp(brightness, T(0), T(1)), std::memory_order_relaxed);
133 dirty_.store(
true, std::memory_order_release);
141 if (!std::isfinite(distance))
return;
142 distance_.store(std::clamp(distance, T(0), T(1)), std::memory_order_relaxed);
145 [[nodiscard]] T
getSize() const noexcept {
return sizeParam_.load(std::memory_order_relaxed); }
146 [[nodiscard]] T
getBrightness() const noexcept {
return brightness_.load(std::memory_order_relaxed); }
147 [[nodiscard]] T
getDistance() const noexcept {
return distance_.load(std::memory_order_relaxed); }
154 return 3000.0 * std::pow(7.0 / 3.0,
static_cast<double>(
getBrightness()));
161 std::snprintf(key,
sizeof(key),
"%ssize", prefix);
163 std::snprintf(key,
sizeof(key),
"%sbrightness", prefix);
165 std::snprintf(key,
sizeof(key),
"%sdistance", prefix);
173 std::snprintf(key,
sizeof(key),
"%ssize", prefix);
174 setSize(
static_cast<T
>(r.read(key, 0.5f)));
175 std::snprintf(key,
sizeof(key),
"%sbrightness", prefix);
177 std::snprintf(key,
sizeof(key),
"%sdistance", prefix);
184 if (!cascade_ || lines_.empty())
return;
185 if (dirty_.load(std::memory_order_relaxed) && dirty_.exchange(
false, std::memory_order_acquire))
187 const int nCh = std::min(buffer.getNumChannels(), channels_);
188 const int nS = buffer.getNumSamples();
189 const double distance =
static_cast<double>(distance_.load(std::memory_order_relaxed));
190 const double distanceStep = (distance - distanceNow_) /
static_cast<double>(std::max(nS, 1));
191 const int mask = size_ - 1;
192 for (
int i = 0; i < nS; ++i)
194 const double d = distanceNow_ + distanceStep *
static_cast<double>(i + 1);
196 const double norm = 1.0 / std::sqrt(1.0 + d * d * kTapEnergy);
197 for (
int ch = 0; ch < nCh; ++ch)
199 T* data = buffer.getChannel(ch);
200 T* direct = lines_.data() +
static_cast<size_t>(ch) * 3u *
static_cast<size_t>(size_);
201 T* first = direct + size_;
202 T* second = first + size_;
203 const double x =
static_cast<double>(data[i]);
204 const double y = cascade_->processSample(std::isfinite(x) ? x : 0.0, ch);
205 direct[pos_] =
static_cast<T
>(y);
207 double reflections = 0.0;
208 for (
int k = 0; k <
kTaps; ++k)
209 reflections += kTapGain[k] *
static_cast<double>(direct[(pos_ - tapDelay_[
static_cast<size_t>(k)]) & mask]);
212 const double late1 =
static_cast<double>(first[(pos_ - diffuserDelay_[0]) & mask]);
213 const double v1 = reflections + kDiffuserGain[0] * late1;
214 first[pos_] =
static_cast<T
>(v1);
215 const double a1 = late1 - kDiffuserGain[0] * v1;
216 const double late2 =
static_cast<double>(second[(pos_ - diffuserDelay_[1]) & mask]);
217 const double v2 = a1 + kDiffuserGain[1] * late2;
218 second[pos_] =
static_cast<T
>(v2);
219 const double a2 = late2 - kDiffuserGain[1] * v2;
221 data[i] =
static_cast<T
>((y + d * a2) * norm);
223 cascade_->advanceRamps(1);
224 pos_ = (pos_ + 1) & mask;
226 distanceNow_ = distance;
233 static constexpr double kTapMs[
kTaps] = { 0.29, 0.71, 1.13, 1.87, 2.63, 3.41 };
234 static constexpr double kTapGain[
kTaps] = { 0.45, -0.32, 0.25, -0.18, 0.12, -0.08 };
235 static constexpr double kTapEnergy = 0.45 * 0.45 + 0.32 * 0.32 + 0.25 * 0.25 + 0.18 * 0.18 + 0.12 * 0.12 + 0.08 * 0.08;
236 static constexpr double kDiffuserMs[2] = { 1.31, 2.27 };
237 static constexpr double kDiffuserGain[2] = { 0.5, 0.4 };
240 void design() noexcept
243 const double rollOff = std::min(
getRollOffHz(), 0.45 * fs_);
244 AnalogCascade parts[4] = {
252 for (
const AnalogCascade& part : parts)
253 count += FilterDesign::digital(part, fit_, sections + count,
kSections - count);
254 const int ramp = settled_ ? rampSamples_ : 0;
256 cascade_->rampCoeffs(s, s < count ? sections[s] : BiquadCoeffs {}, ramp);
260 std::atomic<T> sizeParam_ { T(0.5) };
261 std::atomic<T> brightness_ { T(0.5) };
262 std::atomic<T> distance_ { T(0.3) };
263 std::atomic<bool> dirty_ {
true };
266 std::unique_ptr<Cascade> cascade_;
267 FilterDesign::Band fit_;
268 std::array<int, kTaps> tapDelay_ {};
269 std::array<int, 2> diffuserDelay_ {};
270 std::vector<T> lines_;
271 double fs_ = 48000.0;
272 double distanceNow_ = 0.3;
276 int rampSamples_ = 960;
277 bool settled_ =
false;
Non-owning view over audio channel data.
Up to MaxSections second-order sections in series, run in one pass.
A loudspeaker cabinet: colour filters, sparse reflections and two diffusers, with no latency.
void reset() noexcept
Clears the filters and the reflections (settings are kept).
void writeState(StateWriter &w, const char *prefix) const
Writes the settings under keys that start with a prefix.
void prepare(double sampleRate, int numChannels)
Prepares the model for a sample rate and a channel count, and clears it. Invalid values are ignored....
void setBrightness(T brightness) noexcept
Brightness of the speaker, 0 to 1: the roll-off from 3 kHz (0) up to 7 kHz (1), with the breakup peak...
void readState(const StateReader &r, const char *prefix) noexcept
Reads what writeState() wrote (missing keys read as the defaults).
double getResonanceHz() const noexcept
The cone's resonance for the size set, in hertz.
void processBlock(AudioBufferView< T > buffer) noexcept
Runs a block in place. Channels beyond the prepared count pass through.
static constexpr int kTaps
Reflections of the box.
double getRollOffHz() const noexcept
The roll-off corner for the brightness set, in hertz.
void setSize(T size) noexcept
Size of the box, 0 to 1: the cone's resonance from 140 Hz (0) down to 70 Hz (1). Default 0....
T getSize() const noexcept
static constexpr int kSections
Sections of the colour filters.
void setDistance(T distance) noexcept
Distance of the microphone, 0 to 1: how much of the box's reflections comes with the direct sound (0:...
T getBrightness() const noexcept
T getDistance() const noexcept
void prepare(double sampleRate) noexcept
Sets the sample rate (an invalid one leaves 48 kHz).
static AnalogCascade bell(double hz, double q, double gainDb, double order) noexcept
Bell of any order: the higher the order, the flatter the top and the steeper the sides (6 dB per octa...
static AnalogCascade highCut(double hz, double slopeDbPerOctave, double q=kButterworthQ) noexcept
High cut (low-pass) of any slope from 0 to 96 dB per octave.
static AnalogCascade lowCut(double hz, double slopeDbPerOctave, double q=kButterworthQ) noexcept
Low cut (high-pass) of any slope from 0 to 96 dB per octave: highCut() mirrored about the corner.
Tolerant reader: missing keys yield defaults, unknown keys are skipped.
Serializes key/value parameters into a versioned blob.
void write(const char *key, float value)
Writes a float parameter.
Main namespace for the DSPark framework.