Sound Byte Libs 29c5ff3
C++ firmware library for audio applications on 32-bit ARM Cortex-M processors
Loading...
Searching...
No Matches
allpass.hpp
Go to the documentation of this file.
1// sbl/dsp/allpass.hpp — Schroeder allpass filter
2//
3// y[n] = -g*x[n] + x[n-d] + g*y[n-d]
4//
5// Disperses energy in time without changing frequency magnitude response.
6// Core building block for reverb diffusion networks.
7//
8// Float-domain processing — STM32H7 (Cortex-M7) has hardware FPU with
9// single-cycle float ops.
10//
11// Usage:
12// float buf[256] = {};
13// sbl::dsp::AllpassFilter ap;
14// ap.init(buf, 256);
15// ap.set_feedback(0.5f);
16// float out = ap.process(in);
17
18#pragma once
19
20#include <cstdint>
21
22namespace sbl::dsp {
23
25public:
26 /// @note All public methods are ISR-safe — bounded computation, no I/O.
27
28 /**
29 * @brief Construct with caller-provided buffer
30 * @param buffer Float buffer (must outlive the AllpassFilter)
31 * @param delay Delay length in samples (buffer size)
32 */
33 AllpassFilter(float* buffer, uint32_t delay)
34 : buffer_(buffer), delay_(delay), write_pos_(0) {}
35
36 /// Default constructor for array initialization (must call init() before use)
37 AllpassFilter() = default;
38
39 /**
40 * @brief Initialize after default construction
41 * @param buffer Float buffer (must outlive the AllpassFilter)
42 * @param delay Delay length in samples
43 */
44 void init(float* buffer, uint32_t delay) {
45 buffer_ = buffer;
46 delay_ = delay;
47 write_pos_ = 0;
48 }
49
50 /**
51 * @brief Process a single sample
52 *
53 * Schroeder allpass: y = -g*x + x[n-d] + g*y[n-d]
54 * where the buffer stores the output history.
55 *
56 * @param x Input sample in [-1.0, 1.0]
57 * @return Allpass-filtered output in [-1.0, 1.0]
58 */
59 float process(float x) {
60 float delayed = buffer_[write_pos_];
61
62 float output = -feedback_ * x + delayed;
63 float to_buffer = x + feedback_ * output;
64
65 buffer_[write_pos_] = to_buffer;
66
67 ++write_pos_;
68 if (write_pos_ >= delay_) {
69 write_pos_ = 0;
70 }
71
72 return output;
73 }
74
75 /**
76 * @brief Set feedback coefficient
77 * @param g Feedback gain, typically 0.5–0.7 for reverb diffusion
78 */
79 void set_feedback(float g) { feedback_ = g; }
80
81 /** @brief Current feedback coefficient */
82 float feedback() const { return feedback_; }
83
84 /** @brief Zero the buffer and reset write position */
85 void clear() {
86 if (buffer_) {
87 for (uint32_t i = 0; i < delay_; ++i) buffer_[i] = 0.0f;
88 }
89 write_pos_ = 0;
90 }
91
92 /** @brief Delay length in samples */
93 uint32_t delay() const { return delay_; }
94
95 /** @brief Current write position (for external tap reads) */
96 uint32_t write_pos() const { return write_pos_; }
97
98private:
99 float* buffer_ = nullptr;
100 uint32_t delay_ = 0;
101 uint32_t write_pos_ = 0;
102 float feedback_ = 0.5f;
103};
104
105} // namespace sbl::dsp
uint32_t write_pos() const
Current write position (for external tap reads)
Definition allpass.hpp:96
void clear()
Zero the buffer and reset write position.
Definition allpass.hpp:85
float feedback() const
Current feedback coefficient.
Definition allpass.hpp:82
uint32_t delay() const
Delay length in samples.
Definition allpass.hpp:93
AllpassFilter()=default
Default constructor for array initialization (must call init() before use)
void init(float *buffer, uint32_t delay)
Initialize after default construction.
Definition allpass.hpp:44
void set_feedback(float g)
Set feedback coefficient.
Definition allpass.hpp:79
float process(float x)
Process a single sample.
Definition allpass.hpp:59
AllpassFilter(float *buffer, uint32_t delay)
Construct with caller-provided buffer.
Definition allpass.hpp:33
DSP atoms for audio signal processing.
Definition allpass.hpp:22