Sound Byte Libs 0.5.1-340-gab5b79a
C++ firmware library for audio applications on 32-bit ARM Cortex-M processors
Loading...
Searching...
No Matches
dc_block.hpp
Go to the documentation of this file.
1/**
2 * @file dc_block.hpp
3 * @brief First-order DC removal
4 */
5
6#ifndef SBL_DSP_FILTERS_DC_BLOCK_HPP_
7#define SBL_DSP_FILTERS_DC_BLOCK_HPP_
8
9#include <cstdint>
11
12namespace sbl::dsp {
13
14/**
15 * @brief A one-pole high-pass with its zero at DC
16 *
17 * H(z) = (1 - z^-1) / (1 - r z^-1). The pole r sets the corner; the default,
18 * 0.995, puts the -3 dB point at DEFAULT_CORNER, 38.1 Hz at 48 kHz.
19 * Below the signal transport: the pole is its natural-unit input, set by the
20 * component or app that owns it. It declares no ports; the buffer call takes
21 * none, and PARAMETERS names the pole so a tool can run it alone.
22 */
23class DcBlock {
24public:
25 static constexpr float DEFAULT_POLE = 0.995f; /**< The pole until set_pole() is called */
26 /** The default pole's -3 dB point, in cycles per frame: 38.1 Hz at 48 kHz. A test holds the two together. */
27 static constexpr float DEFAULT_CORNER = 7.9375e-4f;
28
29 /**
30 * @brief Set the pole; closer to 1 is a lower corner.
31 *
32 * @param r Pole, clamped to [0, 1); a NaN is not caught
33 * @context Same thread as process(); real-time safe.
34 */
35 void set_pole(float r) { r_ = r < 0.0f ? 0.0f : (r >= 1.0f ? 0.99999994f : r); }
36
37 /**
38 * @brief One frame.
39 *
40 * @param x Input sample
41 * @return Output sample
42 * @context Audio thread; real-time safe.
43 */
44 float process(float x) {
45 const float y = x - x1_ + r_ * y1_;
46 x1_ = x;
47 y1_ = y;
48 return y;
49 }
50
51 /** @brief The parameters, for tools. */
55
56 /**
57 * @brief A buffer of frames. In place is fine: out may be in.
58 *
59 * @param in Input buffer, `frames` long
60 * @param[out] out Output buffer, `frames` long
61 * @param frames Frame count
62 * @context Audio thread; real-time safe.
63 */
64 void process(const float* in, float* out, uint16_t frames) {
65 for (uint16_t i = 0; i < frames; ++i) {
66 out[i] = process(in[i]);
67 }
68 }
69
70 /** @brief Clear the state. */
71 void reset() {
72 x1_ = 0.0f;
73 y1_ = 0.0f;
74 }
75
76 /** @brief The pole. */
77 float pole() const { return r_; }
78
79private:
80 float r_ = DEFAULT_POLE;
81 float x1_ = 0.0f;
82 float y1_ = 0.0f;
83};
84
85} // namespace sbl::dsp
86
87#endif // SBL_DSP_FILTERS_DC_BLOCK_HPP_
A one-pole high-pass with its zero at DC.
Definition dc_block.hpp:23
static constexpr transport::Parameter< DcBlock > PARAMETERS[]
The parameters, for tools.
Definition dc_block.hpp:52
float process(float x)
One frame.
Definition dc_block.hpp:44
static constexpr float DEFAULT_POLE
Definition dc_block.hpp:25
void reset()
Clear the state.
Definition dc_block.hpp:71
float pole() const
The pole.
Definition dc_block.hpp:77
void set_pole(float r)
Set the pole; closer to 1 is a lower corner.
Definition dc_block.hpp:35
void process(const float *in, float *out, uint16_t frames)
A buffer of frames. In place is fine: out may be in.
Definition dc_block.hpp:64
static constexpr float DEFAULT_CORNER
Definition dc_block.hpp:27
The audio stack.
Definition dry_wet.hpp:15
The processing call's shape as a checked contract.