Sound Byte Libs 29c5ff3
C++ firmware library for audio applications on 32-bit ARM Cortex-M processors
Loading...
Searching...
No Matches
wavetable.hpp
Go to the documentation of this file.
1// sbl/dsp/wavetable.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#pragma once
16
17#include <cstdint>
18
19#include <sbl/dsp/lut.hpp>
20
21namespace sbl::dsp {
22
23template<uint16_t Size>
25public:
26 /// @note All public methods are ISR-safe — bounded computation, no I/O.
27
28 /** @brief Set the wavetable to read from (must have Size+1 guard points for linear) */
29 void set_table(const float* table) { table_ = table; }
30
31 /** @brief Current table pointer */
32 const float* table() const { return table_; }
33
34 /**
35 * @brief Read a single sample with linear interpolation
36 * @param phase 32-bit phase (full range = one table cycle)
37 * @return Interpolated sample value in [-1.0, 1.0]
38 */
39 float read(uint32_t phase) const {
40 return lut::lookup_linear<Size>(table_, phase);
41 }
42
43 /**
44 * @brief Read a single sample with cubic Hermite interpolation
45 * @param phase 32-bit phase
46 * @return Interpolated sample value (smoother, higher cost)
47 */
48 float read_cubic(uint32_t phase) const {
49 return lut::lookup_cubic<Size>(table_, phase);
50 }
51
52 /**
53 * @brief Read a block of samples using linear interpolation
54 * @param phases Array of per-sample phase values
55 * @param out Output sample buffer
56 * @param frames Number of frames to process
57 */
58 void read_block(const uint32_t* phases, float* out, uint16_t frames) const {
59 for (uint16_t i = 0; i < frames; ++i) {
60 out[i] = lut::lookup_linear<Size>(table_, phases[i]);
61 }
62 }
63
64private:
65 const float* table_ = nullptr;
66};
67
68} // namespace sbl::dsp
float read(uint32_t phase) const
Read a single sample with linear interpolation.
Definition wavetable.hpp:39
float read_cubic(uint32_t phase) const
Read a single sample with cubic Hermite interpolation.
Definition wavetable.hpp:48
void read_block(const uint32_t *phases, float *out, uint16_t frames) const
Read a block of samples using linear interpolation.
Definition wavetable.hpp:58
void set_table(const float *table)
Set the wavetable to read from (must have Size+1 guard points for linear)
Definition wavetable.hpp:29
const float * table() const
Current table pointer.
Definition wavetable.hpp:32
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