Sound Byte Libs 0.5.1-121-g3358a44
C++ firmware library for audio applications on 32-bit ARM Cortex-M processors
Loading...
Searching...
No Matches
folder.hpp
Go to the documentation of this file.
1// sbl/dsp/folder.hpp — Wavefold primitive
2//
3// Reflects a signal at ±1.0 boundaries via triangle-wave reflection.
4// The mathematical fold operation, without antialiasing.
5//
6// For production real-time audio, prefer WavefoldAA (dsp/wavefold.hpp)
7// which eliminates aliasing via second-order ADAA.
8//
9// Usage:
10// sbl::dsp::Folder folder;
11// float out = folder.process(in * depth);
12
13#ifndef SBL_DSP_PRIMITIVES_FOLDER_HPP_
14#define SBL_DSP_PRIMITIVES_FOLDER_HPP_
15
16#include <cmath>
17#include <cstdint>
18
19namespace sbl::dsp::primitives {
20
21class Folder {
22public:
23 /// Process a single sample — fold x into [-1, 1] via triangle-wave reflection
24 float process(float x) { return fold(x); }
25
26 /// Process a block in-place
27 void process(float* buf, uint16_t frames) {
28 for (uint16_t i = 0; i < frames; ++i) {
29 buf[i] = fold(buf[i]);
30 }
31 }
32
33 /// The fold function, available for math use (e.g., WavefoldAA antiderivatives)
34 static float fold(float x) {
35 const float p = x + 1.0f;
36 const float q = p - 4.0f * floorf(p * 0.25f); // mod 4, range [0, 4)
37 return (q < 2.0f) ? (q - 1.0f) : (3.0f - q);
38 }
39};
40
41} // namespace sbl::dsp::primitives
42
43#endif // SBL_DSP_PRIMITIVES_FOLDER_HPP_
void process(float *buf, uint16_t frames)
Process a block in-place.
Definition folder.hpp:27
static float fold(float x)
The fold function, available for math use (e.g., WavefoldAA antiderivatives)
Definition folder.hpp:34
float process(float x)
Process a single sample — fold x into [-1, 1] via triangle-wave reflection.
Definition folder.hpp:24
Stateful, single-concern building blocks.