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
convert.hpp
Go to the documentation of this file.
1/**
2 * @file convert.hpp
3 * @brief Conversion at the DMA boundary: interleaved codec words <-> float L/R buffers
4 *
5 * The only int <-> float conversion in the audio path. DMA buffers hold
6 * interleaved stereo samples in S24RjI32; processing uses separate float
7 * L/R buffers in [-1.0, 1.0].
8 */
9
10#ifndef SBL_DSP_TYPES_CONVERT_HPP_
11#define SBL_DSP_TYPES_CONVERT_HPP_
12
13#include <cstddef>
14#include <cstdint>
15
16/**
17 * @brief Count a NaN replaced by interleave_from_float()
18 *
19 * Increments nan_detect_count() when SBL_PROFILING_ENABLED is defined, and
20 * compiles to nothing otherwise.
21 */
22#ifdef SBL_PROFILING_ENABLED
23#define SBL_NAN_DETECTED() (++sbl::dsp::types::nan_detect_count())
24#else
25#define SBL_NAN_DETECTED() ((void)0)
26#endif
27
28namespace sbl::dsp::types {
29
30/**
31 * @brief One sample in the codec's DMA word format, `s24_rj_i32`
32 *
33 * 24-bit two's complement, right-aligned in 32 bits. The SAI leaves the
34 * upper byte zero; deinterleave_to_float() sign-extends from bit 23, so a
35 * sign-extended word reads the same.
36 */
37using S24RjI32 = int32_t;
38
39inline constexpr float SAMPLE_SCALE_F = 8388607.0f; /**< 24-bit full scale: +/-8,388,607 */
40inline constexpr float SAMPLE_SCALE_INV_F = 1.0f / 8388607.0f; /**< 1 / SAMPLE_SCALE_F */
41
42/**
43 * @brief Deinterleave stereo codec words into float L/R buffers
44 *
45 * @param interleaved [L0, R0, L1, R1, ...], two words per frame
46 * @param[out] left Output, one float per frame; full scale is +/-1.0
47 * @param[out] right Output, one float per frame; full scale is +/-1.0
48 * @param frames Frame count
49 * @context Audio thread; real-time safe.
50 */
51inline void deinterleave_to_float(const S24RjI32* interleaved, float* left, float* right, uint16_t frames) {
52 constexpr uint32_t PAD_BITS = 8; // the 32-bit word above the 24-bit sample
53 for (size_t i = 0; i < frames; ++i) {
54 // Sign-extend from bit 23. The left shift is unsigned: shifting a
55 // negative int32_t left is undefined in C++17. The conversion back to
56 // int32_t and the right shift are implementation-defined; GCC and
57 // Clang define them as two's complement and an arithmetic shift.
58 const int32_t l = static_cast<int32_t>(static_cast<uint32_t>(interleaved[i * 2]) << PAD_BITS) >> PAD_BITS;
59 const int32_t r = static_cast<int32_t>(static_cast<uint32_t>(interleaved[i * 2 + 1]) << PAD_BITS) >> PAD_BITS;
60 left[i] = static_cast<float>(l) * SAMPLE_SCALE_INV_F;
61 right[i] = static_cast<float>(r) * SAMPLE_SCALE_INV_F;
62 }
63}
64
65#ifdef SBL_PROFILING_ENABLED
66/**
67 * @brief NaNs replaced with silence by interleave_from_float()
68 *
69 * NaN in the audio path indicates a bug upstream (uninitialized buffer,
70 * division by zero, corrupted filter state).
71 */
72inline uint32_t& nan_detect_count() {
73 static uint32_t count = 0;
74 return count;
75}
76#endif
77
78/**
79 * @brief Interleave float L/R buffers into stereo codec words
80 *
81 * Clamps each sample to [-1.0, 1.0] before conversion.
82 *
83 * NaN guard: IEEE 754 NaN fails all comparisons, so the clamp would pass NaN
84 * through to static_cast<int32_t>(NaN * SCALE), which is undefined behavior.
85 * A NaN sample is replaced with 0 first.
86 *
87 * @param left Input, one float per frame, nominally [-1.0, 1.0]
88 * @param right Input, one float per frame, nominally [-1.0, 1.0]
89 * @param[out] interleaved Output [L0, R0, L1, R1, ...], two words per frame
90 * @param frames Frame count
91 * @context Audio thread; real-time safe.
92 */
93inline void interleave_from_float(const float* left, const float* right, S24RjI32* interleaved, uint16_t frames) {
94 for (size_t i = 0; i < frames; ++i) {
95 float l = left[i];
96 float r = right[i];
97 // NaN check: NaN != NaN is true per IEEE 754
98 if (l != l) {
99 l = 0.0f;
101 }
102 if (r != r) {
103 r = 0.0f;
105 }
106 if (l > 1.0f) {
107 l = 1.0f;
108 }
109 if (l < -1.0f) {
110 l = -1.0f;
111 }
112 if (r > 1.0f) {
113 r = 1.0f;
114 }
115 if (r < -1.0f) {
116 r = -1.0f;
117 }
118 interleaved[i * 2] = static_cast<S24RjI32>(l * SAMPLE_SCALE_F);
119 interleaved[i * 2 + 1] = static_cast<S24RjI32>(r * SAMPLE_SCALE_F);
120 }
121}
122
123} // namespace sbl::dsp::types
124
125#endif // SBL_DSP_TYPES_CONVERT_HPP_
#define SBL_NAN_DETECTED()
Count a NaN replaced by interleave_from_float()
Definition convert.hpp:25
Conversion at the DMA boundary.
Definition convert.hpp:28
void interleave_from_float(const float *left, const float *right, S24RjI32 *interleaved, uint16_t frames)
Interleave float L/R buffers into stereo codec words.
Definition convert.hpp:93
int32_t S24RjI32
One sample in the codec's DMA word format, s24_rj_i32
Definition convert.hpp:37
void deinterleave_to_float(const S24RjI32 *interleaved, float *left, float *right, uint16_t frames)
Deinterleave stereo codec words into float L/R buffers.
Definition convert.hpp:51
constexpr float SAMPLE_SCALE_INV_F
Definition convert.hpp:40
constexpr float SAMPLE_SCALE_F
Definition convert.hpp:39