79 T initialRate = rate_.load(std::memory_order_relaxed);
80 currentRate_ = initialRate;
81 lfo_.prepare(sampleRate_);
82 lfo_.setFrequency(initialRate);
84 depthSmoother_.
reset(sampleRate_, kDepthRampMs,
85 static_cast<float>(depth_.load(std::memory_order_relaxed)));
88 activeShape_ = shape_.load(std::memory_order_relaxed);
89 offsetR_ = stereo_.load(std::memory_order_relaxed) ? T(0.5) : T(0);
90 fadeLength_ = std::max(1,
static_cast<int>(sampleRate_ * (kFadeMs / 1000.0)));
100 const int numCh = std::min(buffer.getNumChannels(), numChannels_);
101 const int numSamples = buffer.getNumSamples();
103 if (numCh == 0 || numSamples == 0)
109 updateInternalState();
115 if (fadeRemaining_ > 0)
117 start = std::min(numSamples, fadeRemaining_);
118 processFade(buffer, numCh, start);
120 if (start == numSamples)
return;
123 switch (activeShape_)
125 case Shape::Sine: processBlockShape<Shape::Sine>(buffer, numCh, start, numSamples);
break;
126 case Shape::Triangle: processBlockShape<Shape::Triangle>(buffer, numCh, start, numSamples);
break;
127 case Shape::Square: processBlockShape<Shape::Square>(buffer, numCh, start, numSamples);
break;
148 if (!std::isfinite(hz))
return;
149 rate_.store(std::max(T(0), hz), std::memory_order_relaxed);
159 if (!std::isfinite(depth))
return;
160 depth_.store(std::clamp(depth, T(0), T(1)), std::memory_order_relaxed);
169 const int s = std::clamp(
static_cast<int>(shape), 0,
171 shape_.store(
static_cast<Shape>(s), std::memory_order_relaxed);
178 void setStereo(
bool enabled)
noexcept { stereo_.store(enabled, std::memory_order_relaxed); }
180 [[nodiscard]] T
getRate() const noexcept {
return rate_.load(std::memory_order_relaxed); }
181 [[nodiscard]] T
getDepth() const noexcept {
return depth_.load(std::memory_order_relaxed); }
182 [[nodiscard]]
Shape getShape() const noexcept {
return shape_.load(std::memory_order_relaxed); }
183 [[nodiscard]]
bool isStereo() const noexcept {
return stereo_.load(std::memory_order_relaxed); }
187 [[nodiscard]] std::vector<uint8_t>
getState()
const
192 w.
write(
"rate",
static_cast<float>(rate_.load(std::memory_order_relaxed)));
193 w.
write(
"depth",
static_cast<float>(depth_.load(std::memory_order_relaxed)));
194 w.
write(
"shape",
static_cast<int32_t
>(shape_.load(std::memory_order_relaxed)));
195 w.
write(
"stereo", stereo_.load(std::memory_order_relaxed));
212 static constexpr float kDepthRampMs = 5.0f;
213 static constexpr double kFadeMs = 5.0;
214 static constexpr T kSquareSlewTime = T(0.02);
216 double sampleRate_ = 44100.0;
217 int numChannels_ = 2;
219 std::atomic<T> rate_{ T(4) };
220 std::atomic<T> depth_{ T(0.5) };
222 std::atomic<bool> stereo_{
false };
225 T currentRate_ = T(4);
229 T fadeOffsetR_ = T(0);
231 int fadeRemaining_ = 0;
233 Smoothers::LinearSmoother depthSmoother_;
239 inline void updateInternalState() noexcept
241 T targetRate = rate_.load(std::memory_order_relaxed);
242 if (targetRate != currentRate_)
244 currentRate_ = targetRate;
245 lfo_.setFrequency(currentRate_);
248 const Shape shape = shape_.load(std::memory_order_relaxed);
249 const T offset = stereo_.load(std::memory_order_relaxed) ? T(0.5) : T(0);
250 if (shape != activeShape_ || offset != offsetR_)
253 fadeShape_ = activeShape_;
254 fadeOffsetR_ = offsetR_;
255 activeShape_ = shape;
257 fadeRemaining_ = fadeLength_;
260 depthSmoother_.setTargetValue(
static_cast<float>(depth_.load(std::memory_order_relaxed)));
264 [[nodiscard]]
static inline T wrapPhase(T phase)
noexcept
266 return phase >= T(1) ? phase - T(1) : phase;
270 [[nodiscard]]
inline T evalShape(
Shape shape, T phase)
const noexcept
275 case Shape::Square:
return computeShape<Shape::Square>(phase);
277 default:
return computeShape<Shape::Sine>(phase);
286 void processFade(AudioBufferView<T>& buffer,
int numCh,
int n)
noexcept
288 T*
const channelL = buffer.getChannel(0);
289 T*
const channelR = (numCh > 1) ? buffer.getChannel(1) :
nullptr;
290 const T invLength = T(1) /
static_cast<T
>(fadeLength_);
292 for (
int i = 0; i < n; ++i)
294 const T depthVal =
static_cast<T
>(depthSmoother_.getNextValue());
295 const T phaseL = lfo_.advance();
296 const T w =
static_cast<T
>(fadeLength_ - fadeRemaining_ + 1) * invLength;
299 const T oldL = evalShape(fadeShape_, phaseL);
300 const T modL = oldL + w * (evalShape(activeShape_, phaseL) - oldL);
301 const T gainL = T(1) - depthVal * (T(1) - modL) * T(0.5);
302 channelL[i] *= gainL;
304 if (channelR !=
nullptr)
306 const T oldR = evalShape(fadeShape_, wrapPhase(phaseL + fadeOffsetR_));
307 const T modR = oldR + w * (evalShape(activeShape_, wrapPhase(phaseL + offsetR_)) - oldR);
308 channelR[i] *= T(1) - depthVal * (T(1) - modR) * T(0.5);
311 for (
int ch = 2; ch < numCh; ++ch)
312 buffer.getChannel(ch)[i] *= gainL;
320 [[nodiscard]]
inline T computeShape(T phase)
const noexcept
325 return fastSin(phase * twoPi<T>);
330 if (t < T(1))
return t;
331 if (t < T(3))
return T(2) - t;
337 if (phase < kSquareSlewTime)
return T(-1) + (phase / kSquareSlewTime) * T(2);
338 if (phase < T(0.5))
return T(1);
339 if (phase < T(0.5) + kSquareSlewTime)
return T(1) - ((phase - T(0.5)) / kSquareSlewTime) * T(2);
348 void processBlockShape(AudioBufferView<T>& buffer,
int numCh,
int start,
int end)
noexcept
350 T*
const channelL = buffer.getChannel(0);
351 T*
const channelR = (numCh > 1) ? buffer.getChannel(1) :
nullptr;
352 const bool stereo = offsetR_ != T(0);
354 for (
int i = start; i < end; ++i)
356 T depthVal =
static_cast<T
>(depthSmoother_.getNextValue());
357 T phaseL = lfo_.advance();
359 T modL = computeShape<S>(phaseL);
360 T gainL = T(1) - depthVal * (T(1) - modL) * T(0.5);
362 channelL[i] *= gainL;
364 if (channelR !=
nullptr)
368 T modR = computeShape<S>(wrapPhase(phaseL + offsetR_));
369 T gainR = T(1) - depthVal * (T(1) - modR) * T(0.5);
370 channelR[i] *= gainR;
374 channelR[i] *= gainL;
379 for (
int ch = 2; ch < numCh; ++ch)
381 buffer.getChannel(ch)[i] *= gainL;