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
audio_capture.hpp
Go to the documentation of this file.
1/**
2 * @file audio_capture.hpp
3 * @brief Always-on audio capture ring buffer for host-side analysis
4 */
5
6#ifndef SBL_COMMON_DIAGNOSTICS_AUDIO_CAPTURE_HPP_
7#define SBL_COMMON_DIAGNOSTICS_AUDIO_CAPTURE_HPP_
8
9#include <cstdint>
10
11#if __has_include(<ff.h>)
12#include <ff.h>
13#define SBL_HAS_FATFS 1
14#endif
15
17
18/**
19 * @brief Always-on audio capture ring buffer for host-side analysis
20 *
21 * Continuously records the last MaxFrames of audio into a circular buffer.
22 * No arming needed - the buffer always has the latest audio. To read:
23 *
24 * 1. Freeze the buffer (via SWD write or app trigger) - audio keeps playing
25 * 2. Read pos_ to know the ring head position
26 * 3. Read buf_left_/buf_right_ via sbl-debugger
27 * 4. Unfreeze to resume capture
28 * 5. Unroll the ring: [pos_..MaxFrames) then [0..pos_) = chronological order
29 *
30 * The sbl-debugger `audio_capture` tool handles all of this automatically.
31 *
32 * Example:
33 * @code
34 * static sbl::diagnostics::AudioCapture<4096> audio_capture;
35 *
36 * void audio_callback(int32_t* tx, const int32_t* rx, uint16_t frames) {
37 * render(left_buf, right_buf, frames);
38 * audio_capture.feed(left_buf, right_buf, frames);
39 * sbl::dsp::types::interleave_from_float(left_buf, right_buf, tx, frames);
40 * }
41 * @endcode
42 *
43 * When frozen, feed() is a single branch.
44 *
45 * Template parameter MaxFrames controls buffer size and capture duration:
46 * @code
47 * 2048 -> 16 KB stereo, 42.7 ms @ 48kHz
48 * 4096 -> 32 KB stereo, 85.3 ms @ 48kHz (default)
49 * 8192 -> 64 KB stereo, 170.7 ms @ 48kHz
50 * 16384 -> 128 KB stereo, 341.3 ms @ 48kHz
51 * @endcode
52 */
53template<uint16_t MaxFrames = 4096>
55public:
56 /**
57 * @brief Write one block into the ring; a no-op when frozen
58 * @param left Left channel samples
59 * @param right Right channel samples
60 * @param frames Frames in each channel
61 * @context The audio callback, every block.
62 */
63 void feed(const float* left, const float* right, uint16_t frames) {
64 if (frozen_) {
65 return;
66 }
67
68 for (uint16_t i = 0; i < frames; ++i) {
69 buf_left_[pos_] = left[i];
70 buf_right_[pos_] = right[i];
71 pos_ = (pos_ + 1 < MaxFrames) ? pos_ + 1 : 0;
72 }
73
74 // Track total writes so the reader knows the buffer is full
75 if (!ready_) {
76 written_ += frames;
77 if (written_ >= MaxFrames) {
78 ready_ = true;
79 }
80 }
81 }
82
83 /**
84 * @brief Freeze the buffer: feed() stops writing; the audio callback runs on.
85 *
86 * @context Any context; a debugger may also write frozen_ over SWD.
87 */
88 void freeze() { frozen_ = true; }
89
90 /** @brief Resume writing after a freeze */
91 void unfreeze() { frozen_ = false; }
92
93#ifdef SBL_HAS_FATFS
94 /**
95 * @brief Dump the ring buffer to a 32-bit float WAV file via FatFs
96 *
97 * Freezes capture during the write, then resumes.
98 *
99 * @param path File to write
100 * @param sample_rate The captured audio's sample rate in Hz, for the WAV header
101 * @return true on success; false if the buffer is not yet full or the file
102 * could not be written
103 */
104 bool dump_to_file(const char* path, uint32_t sample_rate) {
105 if (!ready_) {
106 return false;
107 }
108 freeze();
109
110 FIL fil;
111 if (f_open(&fil, path, FA_CREATE_ALWAYS | FA_WRITE) != FR_OK) {
112 unfreeze();
113 return false;
114 }
115
116 // WAV header - 32-bit IEEE float, stereo
117 constexpr uint16_t channels = 2;
118 constexpr uint16_t bits = 32;
119 constexpr uint16_t fmt_pcm_float = 3;
120 uint32_t data_size = MaxFrames * channels * sizeof(float);
121 uint32_t file_size = 36 + data_size;
122 uint32_t fmt_size = 16;
123 uint32_t byte_rate = sample_rate * channels * (bits / 8);
124 uint16_t block_align = channels * (bits / 8);
125 UINT bw;
126
127 f_write(&fil, "RIFF", 4, &bw);
128 f_write(&fil, &file_size, 4, &bw);
129 f_write(&fil, "WAVE", 4, &bw);
130 f_write(&fil, "fmt ", 4, &bw);
131 f_write(&fil, &fmt_size, 4, &bw);
132 f_write(&fil, &fmt_pcm_float, 2, &bw);
133 f_write(&fil, &channels, 2, &bw);
134 f_write(&fil, &sample_rate, 4, &bw);
135 f_write(&fil, &byte_rate, 4, &bw);
136 f_write(&fil, &block_align, 2, &bw);
137 f_write(&fil, &bits, 2, &bw);
138 f_write(&fil, "data", 4, &bw);
139 f_write(&fil, &data_size, 4, &bw);
140
141 // Unroll ring: [pos..MaxFrames) then [0..pos), interleaved stereo
142 uint16_t p = pos_;
143 for (uint16_t pass = 0; pass < 2; ++pass) {
144 uint16_t start = (pass == 0) ? p : 0;
145 uint16_t end = (pass == 0) ? MaxFrames : p;
146 for (uint16_t i = start; i < end; ++i) {
147 f_write(&fil, &buf_left_[i], sizeof(float), &bw);
148 f_write(&fil, &buf_right_[i], sizeof(float), &bw);
149 }
150 }
151
152 f_close(&fil);
153 unfreeze();
154 return true;
155 }
156#endif // SBL_HAS_FATFS
157
158 // State fields, readable and writable via sbl-debugger, which reads these
159 // public fields by name.
160 // NOLINTBEGIN(readability-identifier-naming)
161
162 volatile bool frozen_ = false; /**< When true, feed() is a no-op */
163 volatile bool ready_ = false; /**< True once the buffer has been fully written */
164 volatile uint16_t pos_ = 0; /**< Ring head: next write position */
165 volatile uint32_t written_ = 0; /**< Total frames written (for ready_ detection) */
166
167 // Sample buffers, readable via sbl-debugger read_memory.
168
169 float buf_left_[MaxFrames] = {}; /**< Left channel ring */
170 float buf_right_[MaxFrames] = {}; /**< Right channel ring */
171 // NOLINTEND(readability-identifier-naming)
172
173 static constexpr uint16_t MAX_FRAMES = MaxFrames; /**< Ring length in frames */
174};
175
176} // namespace sbl::diagnostics
177
178#endif // SBL_COMMON_DIAGNOSTICS_AUDIO_CAPTURE_HPP_
Always-on audio capture ring buffer for host-side analysis.
void feed(const float *left, const float *right, uint16_t frames)
Write one block into the ring; a no-op when frozen.
static constexpr uint16_t MAX_FRAMES
void freeze()
Freeze the buffer: feed() stops writing; the audio callback runs on.
void unfreeze()
Resume writing after a freeze.
Diagnostics: audio capture.