38#include "../Core/AnalogRandom.h"
39#include "../Core/AudioBuffer.h"
40#include "../Core/AudioSpec.h"
41#include "../Core/DspMath.h"
42#include "../Core/StateBlob.h"
91 generators_.reserve(
static_cast<size_t>(targetChannels));
92 brownState_.assign(
static_cast<size_t>(targetChannels), T(0));
98 constexpr double kLeak48k = 0.995;
99 const double a = std::pow(kLeak48k, 48000.0 / sampleRate_);
100 brownLeak_ =
static_cast<T
>(a);
101 brownGain_ =
static_cast<T
>(0.02 * (kLeak48k / a)
102 * std::sqrt((1.0 - a * a) / (1.0 - kLeak48k * kLeak48k)));
104 for (
int ch = 0; ch < targetChannels; ++ch)
112 gen.
setRateHz(
static_cast<T
>(sampleRate_));
116 gen.
reseed(
static_cast<uint64_t
>(ch + 1) * 0x9E3779B97F4A7C15ULL);
117 generators_.push_back(std::move(gen));
121 currentGain_ = gain_.load(std::memory_order_relaxed);
134 if (generators_.empty())
137 if (typeDirty_.exchange(
false, std::memory_order_acquire))
139 for (
auto& gen : generators_)
143 const int numCh = std::min<int>(buffer.getNumChannels(),
static_cast<int>(generators_.size()));
144 const int numSamples = buffer.getNumSamples();
146 if (numSamples <= 0 || numCh <= 0)
149 const bool brown = type_.load(std::memory_order_relaxed) ==
Type::Brown;
152 const T targetGain = gain_.load(std::memory_order_relaxed);
153 const T startGain = currentGain_;
154 const bool needsSmoothing = std::abs(targetGain - startGain) > T(1e-6);
155 const T gainStep = needsSmoothing ? ((targetGain - startGain) /
static_cast<T
>(numSamples)) : T(0);
157 for (
int ch = 0; ch < numCh; ++ch)
159 T* data = buffer.getChannel(ch);
160 auto& gen = generators_[
static_cast<size_t>(ch)];
164 T y = brownState_[
static_cast<size_t>(ch)];
166 for (
int i = 0; i < numSamples; ++i)
168 y = std::clamp((y + gen.getNextSample() * brownGain_) * brownLeak_, T(-1), T(1));
169 g = needsSmoothing ? g + gainStep : targetGain;
172 brownState_[
static_cast<size_t>(ch)] = y;
179 for (
int i = 0; i < numSamples; ++i)
182 data[i] = gen.getNextSample() * g;
187 for (
int i = 0; i < numSamples; ++i)
189 data[i] = gen.getNextSample() * targetGain;
195 currentGain_ = targetGain;
209 for (
auto& gen : generators_)
212 gen.reseed(
static_cast<uint64_t
>(++ch) * 0x9E3779B97F4A7C15ULL);
214 std::fill(brownState_.begin(), brownState_.end(), T(0));
216 currentGain_ = gain_.load(std::memory_order_relaxed);
230 const int t = std::clamp(
static_cast<int>(type), 0,
232 type_.store(
static_cast<Type>(t), std::memory_order_relaxed);
233 typeDirty_.store(
true, std::memory_order_release);
243 if (std::isnan(levelDb) || levelDb > T(1e6))
return;
254 if (!std::isfinite(gain))
return;
255 gain_.store(gain, std::memory_order_relaxed);
264 return type_.load(std::memory_order_relaxed);
273 return gain_.load(std::memory_order_relaxed);
278 [[nodiscard]] std::vector<uint8_t>
getState()
const
282 w.
write(
"gain",
static_cast<float>(gain_.load(std::memory_order_relaxed)));
283 w.
write(
"type",
static_cast<int32_t
>(type_.load(std::memory_order_relaxed)));
300 switch (type_.load(std::memory_order_relaxed))
315 double sampleRate_ = 44100.0;
318 std::atomic<T> gain_ { T(1) };
320 std::atomic<bool> typeDirty_ {
false };
323 T currentGain_ { T(1) };
326 std::vector<AnalogRandom::Generator<T>> generators_;
327 std::vector<T> brownState_;
328 T brownLeak_ = T(0.995);
329 T brownGain_ = T(0.02);
Main generator class for analog-style random modulation.
void setRateHz(Real rateInHz) noexcept
Free-running retrigger rate in Hz; non-finite values are ignored (a NaN or infinite rate froze the ph...
void reseed(std::uint64_t newSeed) noexcept
Request a lock-free reseed of the internal PRNG.
void prepare(double sampleRate) noexcept
Prepare the generator with the audio sample rate.
void setRange(T min, T max) noexcept
Set the output range for values. Safe from tearing.
Non-owning view over audio channel data.
Generates decorellated noise (white, pink, brown) across multiple channels.
void setLevel(T levelDb) noexcept
Sets the output level in decibels. Thread-safe.
void reset() noexcept
Resets the internal PRNG and filter states.
void prepare(const AudioSpec &spec)
Prepares the noise generator and allocates internal state.
bool setState(const uint8_t *data, size_t size)
Restores parameters from a blob (tolerant; rejects foreign ids).
T getGain() const noexcept
Retrieves the current target gain in linear scale.
void setType(Type type) noexcept
Sets the noise spectrum type.
void setGain(T gain) noexcept
Sets the output level as linear gain. Thread-safe.
Type getType() const noexcept
Retrieves the currently requested noise type.
std::vector< uint8_t > getState() const
Serializes the parameter state (setup/UI threads; allocates).
void processBlock(AudioBufferView< T > buffer) noexcept
Fills the buffer with generated noise.
Tolerant reader: missing keys yield defaults, unknown keys are skipped.
float read(const char *key, float defaultValue) const
Reads a float, or defaultValue when the key is absent.
bool isValid() const noexcept
uint32_t processorId() const noexcept
Serializes key/value parameters into a versioned blob.
std::vector< uint8_t > blob() const
Finalizes and returns the blob.
void write(const char *key, float value)
Writes a float parameter.
@ White
Uncorrelated noise for "sample-and-hold" effects.
@ Pink
1/f noise for organic drift.
Main namespace for the DSPark framework.
T decibelsToGain(T dB, T minusInfinityDb=T(-100)) noexcept
Converts a value in decibels to linear gain.
constexpr uint32_t stateId(const char(&tag)[5]) noexcept
Builds a FOURCC processor id, e.g. dspark::stateId("COMP").
Describes the audio environment for a DSP processor.
constexpr bool isValid() const noexcept
Checks if the specification contains valid, processable parameters.
int numChannels
Number of audio channels (e.g., 1 = mono, 2 = stereo).
double sampleRate
Sample rate in Hz.