24#include "WrappedAudioOutputBase.h"
25#include "BackgroundAudioGain.h"
26#include "BackgroundAudioBuffers.h"
27#include "libmad/config.h"
28#include "libmad/mad.h"
35template<
class DataBuffer>
78 _gain = (int32_t)(scale * (1 << 16));
90 if (_playing || !_out) {
94#ifdef ARDUINO_ARCH_RP2040
95 _workIRQ = user_irq_claim_unused(
true);
97 irq_set_exclusive_handler(_workIRQ, _irqStub);
98 irq_set_priority(_workIRQ, 0xc0);
99 irq_set_enabled(_workIRQ,
true);
103 mad_stream_init(&_stream);
104 mad_frame_init(&_frame);
105 mad_synth_init(&_synth);
106 mad_stream_options(&_stream, 0);
109 _out->setBuffers(5, framelen);
110 _out->onTransmit(&_cb, (
void *)
this);
111 _out->setBitsPerSample(16);
112 _out->setStereo(
true);
113 _out->setFrequency(44100);
117 uint16_t zeros[32] __attribute__((aligned(4))) = {};
118 while (_out->availableForWrite() > 32) {
119 _out->write((uint8_t *)zeros,
sizeof(zeros));
132#ifdef ARDUINO_ARCH_RP2040
133 irq_set_enabled(_workIRQ,
false);
134 user_irq_unclaim(_workIRQ);
168 size_t write(
const void *data,
size_t len) {
169 return _ib.write((
const uint8_t *)data, len);
184 return _ib.availableForWrite();
193 return _ib.available() - _accumShift;
202 return available() <= MAD_BUFFER_GUARD * 2;
289#ifdef ARDUINO_ARCH_RP2040
290 static void _irqStub() {
294 static void _cb(
void *ptr) {
299 static void _cb(
void *ptr) {
304 void generateOneFrame() {
311 if (_stream.buffer) {
312 if (_stream.next_frame && _stream.next_frame != _stream.this_frame) {
313 _accumShift += _stream.next_frame - _stream.this_frame;
316 _accumShift += maxFrameSize;
322 int pend = _ib.available() - 1 - _accumShift;
323 const uint8_t *b = _ib.buffer() + _accumShift;
325 for (ptr = 0; ptr < pend; ptr++) {
326 if ((b[ptr] == 0xff) && ((b[ptr + 1] & 0xe0) == 0xe0)) {
335 _ib.shiftUp(_ib.available());
340 if (_accumShift > _ib.size() / 2) {
341 _ib.shiftUp(_accumShift);
347 if (_ib.available() - _accumShift < MAD_BUFFER_GUARD) {
348 _ib.write0(MAD_BUFFER_GUARD);
353 mad_stream_buffer(&_stream, _ib.buffer() + _accumShift, _ib.available() - _accumShift);
356 if (mad_frame_decode(&_frame, &_stream)) {
357 mad_frame_mute(&_frame);
360 mad_synth_frame(&_synth, &_frame);
364 if (_synth.pcm.channels == 1) {
365 for (
size_t i = 0; i < framelen; i++) {
366 _synth.pcm.samplesX[i][1] = _synth.pcm.samplesX[i][0];
370 ApplyGain((int16_t*)_synth.pcm.samplesX, framelen * 2, _gain);
373#ifdef ARDUINO_ARCH_RP2040
377 while (_out->availableForWrite() >= (
int)(framelen * 4)) {
379 bzero(_synth.pcm.samplesX, _synth.pcm.length * 4);
382 if (_synth.pcm.samplerate) {
383 _out->setFrequency(_synth.pcm.samplerate);
386 assert(_out->write((uint8_t *)_synth.pcm.samplesX, _synth.pcm.length * 4) == _synth.pcm.length * 4);
388#ifdef ARDUINO_ARCH_RP2040
393#ifdef ARDUINO_ARCH_RP2040
394 static uint8_t _workIRQ;
399 AudioOutputBase *_out;
400 bool _playing =
false;
401 bool _paused =
false;
403 static const size_t framelen = 1152;
404 static const size_t maxFrameSize = 2881;
406 struct mad_stream _stream;
407 struct mad_frame _frame;
408 struct mad_synth _synth;
409 int32_t _gain = 1 << 16;
410 uint32_t _accumShift = 0;
413 uint32_t _frames = 0;
414 uint32_t _shifts = 0;
415 uint32_t _underflows = 0;
416 uint32_t _errors = 0;
420#ifdef ARDUINO_ARCH_RP2040
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:183
uint32_t underflows()
Get the number of times the MP3 decoder has underflowed waiting on raw data since begin
Definition BackgroundAudioMP3.h:228
uint32_t dumps()
Get the number of full buffer dumps (catastrophic data error) since begin
Definition BackgroundAudioMP3.h:246
void end()
Stops the MP3 decoder process and the calls the output device's end to shut it down,...
Definition BackgroundAudioMP3.h:130
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:281
uint32_t shifts()
Get the number of input data shifts processed by decoder since begin
Definition BackgroundAudioMP3.h:219
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:201
void unpause()
Unpause previously paused playback. Will start processing input data again.
Definition BackgroundAudioMP3.h:270
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:89
size_t available()
Gets number of bytes already in the raw buffer.
Definition BackgroundAudioMP3.h:192
uint32_t errors()
Get the number of decoder errors since begin
Definition BackgroundAudioMP3.h:237
size_t write(const void *data, size_t len)
Writes a block of raw data to the decoder's buffer.
Definition BackgroundAudioMP3.h:168
void pause()
Pause the decoder. Won't process raw input data and will transmit silence.
Definition BackgroundAudioMP3.h:253
bool playing()
Determines if the MP3 decoder has been started.
Definition BackgroundAudioMP3.h:146
uint32_t frames()
Get number of "frames" (1152 stereo samples) processed by decoder.
Definition BackgroundAudioMP3.h:210
bool paused()
Determine if the playback is paused.
Definition BackgroundAudioMP3.h:262