BackgroundAudio 1.3.3
Loading...
Searching...
No Matches
BackgroundAudioMP3.h
1/*
2 BackgroundAudio
3 Plays an audio file using IRQ driven decompression. Main loop() writes
4 data to the buffer but isn't blocked while playing
5
6 Copyright (c) 2024 Earle F. Philhower, III <earlephilhower@yahoo.com>
7
8 This program is free software: you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation, either version 3 of the License, or
11 (at your option) any later version.
12
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
17
18 You should have received a copy of the GNU General Public License
19 along with this program. If not, see <http://www.gnu.org/licenses/>.
20*/
21
22#pragma once
23#include <Arduino.h>
24#include "WrappedAudioOutputBase.h"
25#include "BackgroundAudioGain.h"
26#include "BackgroundAudioBuffers.h"
27#include "libmad/config.h"
28#include "libmad/mad.h"
29
35template<class DataBuffer>
37public:
39 _playing = false;
40 _out = nullptr;
41 _paused = false;
42 }
43
49 BackgroundAudioMP3Class(AudioOutputBase &d) {
50 _playing = false;
51 _paused = false;
52 setDevice(&d);
53 }
54
56
64 bool setDevice(AudioOutputBase *d) {
65 if (!_playing) {
66 _out = d;
67 return true;
68 }
69 return false;
70 }
71
77 void setGain(float scale) {
78 _gain = (int32_t)(scale * (1 << 16));
79 }
80
81
87 bool begin() {
88 // MP3 processing init
89 mad_stream_init(&_stream);
90 mad_frame_init(&_frame);
91 mad_synth_init(&_synth);
92 mad_stream_options(&_stream, 0);
93
94 // We will use natural frame size to minimize mismatch
95 _out->setBuffers(5, framelen); // Framelen is in samples, but buffers takes words, which means 2 samples per word so we're good!
96 _out->onTransmit(&_cb, (void *)this); // The pump we will use to generate our audio
97 _out->setBitsPerSample(16);
98 _out->setStereo(true);
99 _out->setFrequency(44100);
100 _out->begin();
101
102 // Stuff with silence to start
103 uint16_t zeros[32] __attribute__((aligned(4))) = {};
104 while (_out->availableForWrite() > 32) {
105 _out->write((uint8_t *)zeros, sizeof(zeros));
106 }
107
108 _playing = true;
109
110 return true;
111 }
112
116 void end() {
117 if (_playing) {
118 _out->end();
119 }
120 _playing = false;
121 }
122
128 bool playing() {
129 return _playing;
130 }
131
150 size_t write(const void *data, size_t len) {
151 return _ib.write((const uint8_t *)data, len);
152 }
153
166 return _ib.availableForWrite();
167 }
168
174 size_t available() {
175 return _ib.available() - _accumShift;
176 }
177
183 bool done() {
184 return available() <= MAD_BUFFER_GUARD * 2; // At EOF we could bounce between n and 2n guard bytes when app checks
185 }
186
192 uint32_t frames() {
193 return _frames;
194 }
195
201 uint32_t shifts() {
202 return _shifts;
203 }
204
210 uint32_t underflows() {
211 return _underflows;
212 }
213
219 uint32_t errors() {
220 return _errors;
221 }
222
228 uint32_t dumps() {
229 return _dumps;
230 }
231
235 void pause() {
236 _paused = true;
237 }
238
244 bool paused() {
245 return _paused;
246 }
247
248
252 void unpause() {
253 _paused = false;
254 }
255
263 void flush() {
264 noInterrupts();
265 _ib.flush();
266 _accumShift = 0;
267 interrupts();
268 }
269
270private:
271 static void _cb(void *ptr) {
272 ((BackgroundAudioMP3Class*)ptr)->pump();
273 }
274
275 void generateOneFrame() {
276 // Every frame requires shifting all remaining data (6K?) before processing.
277 // We're not decoding MP3s, we're shifting data! Instead, scroll down and only
278 // shift when we're > 1/2 of the total buffer size. We'll still shift to
279 // allow new data to be written, but we'll do it much less frequently.
280
281 // Shift out the used data
282 if (_stream.buffer) {
283 if (_stream.next_frame && _stream.next_frame != _stream.this_frame) {
284 _accumShift += _stream.next_frame - _stream.this_frame;
285 } else {
286 // We have only a partial frame in the buffer. We need data *now* do dump it and send zeros. Underflow case
287 _accumShift += maxFrameSize;
288 }
289 }
290
291 // Try and find a frame header to send in. Should be a single check in most cases since we should be aligned after the 1st frame
292 int ptr = 0;
293 int pend = _ib.available() - 1 - _accumShift;
294 const uint8_t *b = _ib.buffer() + _accumShift;
295 bool found = false;
296 for (ptr = 0; ptr < pend; ptr++) {
297 if ((b[ptr] == 0xff) && ((b[ptr + 1] & 0xe0) == 0xe0)) {
298 _accumShift += ptr;
299 found = true;
300 break;
301 }
302 }
303 if (!found) {
304 // Couldn't find a header at all, the entire buffer is junk so dump it
305 _accumShift = 0;
306 _ib.shiftUp(_ib.available());
307 _dumps++;
308 }
309
310 // If we accumulate too large of a shift, actually do the shift so more space for writer
311 if (_accumShift > _ib.size() / 2) {
312 _ib.shiftUp(_accumShift);
313 _accumShift = 0;
314 _shifts++;
315 }
316
317 // Ensure everything gets pumped out
318 if (_ib.available() - _accumShift < MAD_BUFFER_GUARD) {
319 _ib.write0(MAD_BUFFER_GUARD);
320 _underflows++;
321 }
322
323 // Pass the new buffer information to libmad.
324 mad_stream_buffer(&_stream, _ib.buffer() + _accumShift, _ib.available() - _accumShift);
325
326 // Decode and send next frame
327 if (mad_frame_decode(&_frame, &_stream)) {
328 mad_frame_mute(&_frame);
329 _errors++;
330 }
331 mad_synth_frame(&_synth, &_frame);
332 _frames++;
333
334 // Only stereo for sanity
335 if (_synth.pcm.channels == 1) {
336 for (size_t i = 0; i < framelen; i++) {
337 _synth.pcm.samplesX[i][1] = _synth.pcm.samplesX[i][0];
338 }
339 }
340
341 ApplyGain((int16_t*)_synth.pcm.samplesX, framelen * 2, _gain);
342 }
343
344 void pump() {
345 while (_out->availableForWrite() >= (int)framelen) {
346 if (_paused) {
347 bzero(_synth.pcm.samplesX, _synth.pcm.length * 4);
348 } else {
349 generateOneFrame();
350 if (_synth.pcm.samplerate) {
351 _out->setFrequency(_synth.pcm.samplerate);
352 }
353 }
354 _out->write((uint8_t *)_synth.pcm.samplesX, _synth.pcm.length * 4);
355 }
356 }
357
358private:
359 AudioOutputBase *_out;
360 bool _playing = false;
361 bool _paused = false;
362 static const size_t framelen = 1152;
363 static const size_t maxFrameSize = 2881;
364 DataBuffer _ib;
365 struct mad_stream _stream;
366 struct mad_frame _frame;
367 struct mad_synth _synth;
368 int32_t _gain = 1 << 16;
369 uint32_t _accumShift = 0;
370
371 // MP3 quality stats, cumulative
372 uint32_t _frames = 0;
373 uint32_t _shifts = 0;
374 uint32_t _underflows = 0;
375 uint32_t _errors = 0;
376 uint32_t _dumps = 0;
377};
378
379
384
Interrupt-driven MP3 decoder. Generates a full frame of samples each cycle and uses the RawBuffer to ...
Definition BackgroundAudioMP3.h:36
size_t availableForWrite()
Gets number of bytes available to write to raw buffer.
Definition BackgroundAudioMP3.h:165
uint32_t underflows()
Get the number of times the MP3 decoder has underflowed waiting on raw data since begin
Definition BackgroundAudioMP3.h:210
uint32_t dumps()
Get the number of full buffer dumps (catastrophic data error) since begin
Definition BackgroundAudioMP3.h:228
void end()
Stops the MP3 decoder process and the calls the output device's end to shut it down,...
Definition BackgroundAudioMP3.h:116
BackgroundAudioMP3Class(AudioOutputBase &d)
Construct an MP3 output device using the specified physical audio output.
Definition BackgroundAudioMP3.h:49
void flush()
Flushes any existing raw data, resets the processor to start a new MP3.
Definition BackgroundAudioMP3.h:263
uint32_t shifts()
Get the number of input data shifts processed by decoder since begin
Definition BackgroundAudioMP3.h:201
void setGain(float scale)
Set the gain multiplier (volume) for the stream. Takes effect immediately.
Definition BackgroundAudioMP3.h:77
bool done()
Determine if no more MP3 file is present in the buffer.
Definition BackgroundAudioMP3.h:183
void unpause()
Unpause previously paused playback. Will start processing input data again.
Definition BackgroundAudioMP3.h:252
bool setDevice(AudioOutputBase *d)
Set an output device before begin
Definition BackgroundAudioMP3.h:64
bool begin()
Starts the background MP3 decoder/player. Will initialize the output device and start sending silence...
Definition BackgroundAudioMP3.h:87
size_t available()
Gets number of bytes already in the raw buffer.
Definition BackgroundAudioMP3.h:174
uint32_t errors()
Get the number of decoder errors since begin
Definition BackgroundAudioMP3.h:219
size_t write(const void *data, size_t len)
Writes a block of raw data to the decoder's buffer.
Definition BackgroundAudioMP3.h:150
void pause()
Pause the decoder. Won't process raw input data and will transmit silence.
Definition BackgroundAudioMP3.h:235
bool playing()
Determines if the MP3 decoder has been started.
Definition BackgroundAudioMP3.h:128
uint32_t frames()
Get number of "frames" (1152 stereo samples) processed by decoder.
Definition BackgroundAudioMP3.h:192
bool paused()
Determine if the playback is paused.
Definition BackgroundAudioMP3.h:244