Sound Byte Libs 29c5ff3
C++ firmware library for audio applications on 32-bit ARM Cortex-M processors
Loading...
Searching...
No Matches
waveshaper.hpp
Go to the documentation of this file.
1// sbl/dsp/waveshaper.hpp — Table-driven waveshaping / transfer function
2//
3// Maps input samples through a lookup table for distortion, saturation,
4// or other nonlinear transfer functions. Input range [-1.0, 1.0] maps
5// to the full table.
6//
7// Usage:
8// sbl::dsp::Waveshaper<256> shaper;
9// shaper.set_table(my_transfer_table);
10// float out = shaper.process(in);
11
12#pragma once
13
14#include <cstdint>
15
16#include <sbl/dsp/lut.hpp>
17
18namespace sbl::dsp {
19
20template<uint16_t Size>
22public:
23 /// @note All public methods are ISR-safe — bounded computation, no I/O.
24
25 /** @brief Set the transfer function table (Size+1 entries with guard) */
26 void set_table(const float* table) { table_ = table; }
27
28 /**
29 * @brief Process a single sample through the transfer function
30 *
31 * Input [-1.0, 1.0] maps to full table range.
32 * Values outside this range are clamped.
33 */
34 float process(float x) const {
35 // Clamp to [-1.0, 1.0]
36 if (x > 1.0f) x = 1.0f;
37 if (x < -1.0f) x = -1.0f;
38
39 // Map [-1.0, 1.0] to uint32_t phase [0, 2^32)
40 // -1.0 → 0x00000000, 0.0 → 0x80000000, 1.0 → 0xFFFFFF00
41 float norm = x * 0.5f + 0.5f; // [0.0, 1.0]
42 uint32_t phase = static_cast<uint32_t>(norm * 4294967040.0f); // 0xFFFFFF00 avoids float→uint32 overflow
43
44 return lut::lookup_linear<Size>(table_, phase);
45 }
46
47 /**
48 * @brief Process a block of samples in-place
49 */
50 void process(float* buf, uint16_t frames) const {
51 for (uint16_t i = 0; i < frames; ++i) {
52 buf[i] = process(buf[i]);
53 }
54 }
55
56private:
57 const float* table_ = nullptr;
58};
59
60} // namespace sbl::dsp
float process(float x) const
Process a single sample through the transfer function.
void process(float *buf, uint16_t frames) const
Process a block of samples in-place.
void set_table(const float *table)
Set the transfer function table (Size+1 entries with guard)
uint16_t lookup_linear(const uint16_t *table, uint32_t phase)
Definition lut.hpp:39
DSP atoms for audio signal processing.
Definition allpass.hpp:22