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
bit_crusher.hpp
Go to the documentation of this file.
1// sbl/dsp/comp/bit_crusher.hpp — Bit depth reduction composition
2//
3// Reduces effective bit depth of audio signal.
4// Wraps BitReducer primitive with a _su port for normalized input.
5// Depth is deliberately unsmoothed (see set_depth_su).
6//
7// Signal Unit API (FDP-052):
8// set_depth_su(float) — [0,1] port. 0.0 = full resolution (16-bit, bypass),
9// 1.0 = minimum resolution (~2 bits). Widget owns mapping and smoothing.
10//
11// Early-out: when depth is 0.0, processing is skipped entirely —
12// no quantization noise when the effect is off.
13//
14// Usage:
15// sbl::dsp::comp::BitCrusher bc;
16// bc.set_depth_su(0.4f); // moderate reduction
17// bc.process(buf, frames);
18
19#ifndef SBL_DSP_COMP_BIT_CRUSHER_HPP_
20#define SBL_DSP_COMP_BIT_CRUSHER_HPP_
21
22#include <cstdint>
23
26
27namespace sbl::dsp::comp {
28
30public:
31 static constexpr float kMaxBits = 16.0f;
32 static constexpr float kMinBits = 2.0f;
33
34 /// Port: set bit reduction depth [0, 1].
35 /// 0.0 = full resolution (bypass), 1.0 = minimum bits.
36 /// No smoothing — quantization grid changes must be instant.
37 /// Smoothing a quantizer creates inter-grid artifacts (crackling).
38 /// The pot deadband + cam table interpolation provide sufficient continuity.
39 void set_depth_su(float su) { depth_su_ = su; }
40
41 /// Process a block of samples in-place.
42 /// Depth is constant across the block.
43 void process(float* buf, uint16_t n) {
44 if (depth_su_ < 1e-5f) return;
45
46 float bits = kMaxBits - depth_su_ * (kMaxBits - kMinBits);
47 reducer_.set_bits(bits);
48 reducer_.process(buf, n);
49 }
50
51 void reset() { depth_su_ = 0.0f; }
52
53private:
55 float depth_su_ = 0.0f;
56};
57
58} // namespace sbl::dsp::comp
59
60#endif // SBL_DSP_COMP_BIT_CRUSHER_HPP_
Amplitude bit-depth reduction.
void process(float *buf, uint16_t n)
static constexpr float kMaxBits
static constexpr float kMinBits
float process(float x) const
Process a single sample.
Fixed-point constants and audio sample types.
Compositions: subcircuits of primitives.