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
wavetable_reader.hpp
Go to the documentation of this file.
1// sbl/dsp/primitives/wavetable_reader.hpp — Wavetable reader with interpolation
2//
3// Thin wrapper around lut::lookup_linear/cubic for reading LUTE-generated
4// float tables. Templated on Size to match compile-time table sizes (256, 1024).
5//
6// Usage:
7// sbl::dsp::WavetableReader<1024> reader;
8// reader.set_table(sbl::dsp::lut::sin_1024);
9// float s = reader.read(phase); // linear interpolation
10// float s = reader.read_cubic(phase); // cubic Hermite
11//
12// Block processing:
13// reader.read_block(phase_buf, out_buf, frames);
14
15#ifndef SBL_DSP_PRIMITIVES_WAVETABLE_READER_HPP_
16#define SBL_DSP_PRIMITIVES_WAVETABLE_READER_HPP_
17
18#include <cstdint>
19
20#include <sbl/dsp/math/lut.hpp>
21
22namespace sbl::dsp::primitives {
23
24template<uint16_t Size>
26public:
27 /// @note All public methods are ISR-safe — bounded computation, no I/O.
28
29 /** @brief Set the wavetable to read from (must have Size+1 guard points for linear) */
30 void set_table(const float* table) { table_ = table; }
31
32 /** @brief Current table pointer */
33 const float* table() const { return table_; }
34
35 /**
36 * @brief Read a single sample with linear interpolation
37 * @param phase 32-bit phase (full range = one table cycle)
38 * @return Interpolated sample value in [-1.0, 1.0]
39 */
40 float read(uint32_t phase) const {
41 return lut::lookup_linear<Size>(table_, phase);
42 }
43
44 /**
45 * @brief Read a single sample with cubic Hermite interpolation
46 * @param phase 32-bit phase
47 * @return Interpolated sample value (smoother, higher cost)
48 */
49 float read_cubic(uint32_t phase) const {
50 return lut::lookup_cubic<Size>(table_, phase);
51 }
52
53 /**
54 * @brief Read a block of samples using linear interpolation
55 * @param phases Array of per-sample phase values
56 * @param out Output sample buffer
57 * @param frames Number of frames to process
58 */
59 void read_block(const uint32_t* phases, float* out, uint16_t frames) const {
60 for (uint16_t i = 0; i < frames; ++i) {
61 out[i] = lut::lookup_linear<Size>(table_, phases[i]);
62 }
63 }
64
65private:
66 const float* table_ = nullptr;
67};
68
69} // namespace sbl::dsp::primitives
70
71#endif // SBL_DSP_PRIMITIVES_WAVETABLE_READER_HPP_
float read_cubic(uint32_t phase) const
Read a single sample with cubic Hermite interpolation.
const float * table() const
Current table pointer.
void set_table(const float *table)
Set the wavetable to read from (must have Size+1 guard points for linear)
float read(uint32_t phase) const
Read a single sample with linear interpolation.
void read_block(const uint32_t *phases, float *out, uint16_t frames) const
Read a block of samples using linear interpolation.
Lookup table interpolation functions.
Stateful, single-concern building blocks.