24#include "WrappedAudioOutputBase.h"
25#include "BackgroundAudioGain.h"
26#include "BackgroundAudioBuffers.h"
27#include "libhelix-aac/aacdec.h"
34template<
class DataBuffer>
77 _gain = (int32_t)(scale * (1 << 16));
90 if (_playing || !_out) {
94 _hAACDecoder = AACInitDecoderPre(_private,
sizeof(_private));
100 _out->setBuffers(5, framelen);
101 _out->onTransmit(&_cb, (
void *)
this);
102 _out->setBitsPerSample(16);
103 _out->setStereo(
true);
104 _out->setFrequency(44100);
108 uint16_t zeros[32] __attribute__((aligned(4))) = {};
109 while (_out->availableForWrite() > 32) {
110 _out->write((uint8_t *)zeros,
sizeof(zeros));
152 size_t write(
const void *data,
size_t len) {
153 return _ib.write((
const uint8_t *)data, len);
168 return _ib.availableForWrite();
177 return _ib.available() - _accumShift;
272 static void _cb(
void *ptr) {
276 void generateOneFrame() {
282 int nextFrame = AACFindSyncWord((uint8_t *)_ib.buffer() + _accumShift, _ib.available() - _accumShift);
283 if (nextFrame == -1) {
285 _ib.shiftUp(_ib.available());
287 bzero(_outSample,
sizeof(_outSample));
291 _accumShift += nextFrame;
292 const unsigned char *inBuff = _ib.buffer() + _accumShift;
293 int bytesLeft = _ib.available() - _accumShift;
294 int ret = AACDecode(_hAACDecoder, (
unsigned char **)&inBuff, &bytesLeft, (int16_t *)_outSample);
299 bzero(_outSample,
sizeof(_outSample));
302 AACGetLastFrameInfo(_hAACDecoder, &fi);
303 _sampleRate = fi.sampRateOut;
304 _outSamples = fi.outputSamps / 2;
305 _accumShift = inBuff - _ib.buffer();
307 if (fi.nChans == 1) {
308 for (
int i = 0; i < _outSamples; i++) {
309 _outSample[i][1] = _outSample[1][0];
316 if (_accumShift > _ib.size() / 2) {
317 _ib.shiftUp(_accumShift);
322 ApplyGain((int16_t *)_outSample, _outSamples * 2, _gain);
326 while (_out->availableForWrite() >= (
int)framelen) {
328 bzero((uint8_t *)_outSample, _outSamples * 2 *
sizeof(int16_t));
332 _out->setFrequency(_sampleRate);
335 _out->write((uint8_t *)_outSample, _outSamples * 2 *
sizeof(int16_t));
340 AudioOutputBase *_out =
nullptr;
341 HAACDecoder _hAACDecoder;
342 uint8_t _private[ 96 + 28752 + 50788 + 16];
343 bool _playing =
false;
344 bool _paused =
false;
345 static const size_t framelen = 2048;
346 int16_t _outSample[framelen][2] __attribute__((aligned(4)));
347 int _outSamples = 1024;
348 int _sampleRate = 44000;
350 int32_t _gain = 1 << 16;
351 uint32_t _accumShift = 0;
354 uint32_t _frames = 0;
355 uint32_t _shifts = 0;
356 uint32_t _underflows = 0;
357 uint32_t _errors = 0;
using ROMBackgroundAudioAAC =
BackgroundAudioAACClass<ROMDataBuffer>;
Interrupt-driven AAC decoder. Generates a full frame of samples each cycle and uses the RawBuffer to ...
Definition BackgroundAudioAAC.h:35
uint32_t underflows()
Get the number of times the MP3 decoder has underflowed waiting on raw data since begin
Definition BackgroundAudioAAC.h:212
void pause()
Pause the decoder. Won't process raw input data and will transmit silence.
Definition BackgroundAudioAAC.h:251
bool begin()
Starts the background AAC decoder/player. Will initialize the output device and start sending silence...
Definition BackgroundAudioAAC.h:89
void unpause()
Unpause previously paused playback. Will start processing input data again.
Definition BackgroundAudioAAC.h:267
bool setDevice(AudioOutputBase *d)
Set an output device before begin
Definition BackgroundAudioAAC.h:63
size_t write(const void *data, size_t len)
Writes a block of raw data to the decoder's buffer.
Definition BackgroundAudioAAC.h:152
uint32_t dumps()
Get the number of full buffer dumps (catastrophic data error) since begin
Definition BackgroundAudioAAC.h:230
void flush()
Flushes any existing raw data, resets the processor to start a new MP3.
Definition BackgroundAudioAAC.h:241
uint32_t frames()
Get number of "frames" (1024 or 2048 stereo samples) processed by decoder.
Definition BackgroundAudioAAC.h:194
uint32_t errors()
Get the number of decoder errors since begin
Definition BackgroundAudioAAC.h:221
BackgroundAudioAACClass(AudioOutputBase &d)
Construct an AAC decoder with a given AudioOutputBase.
Definition BackgroundAudioAAC.h:48
bool playing()
Determines if the AAC decoder has been started.
Definition BackgroundAudioAAC.h:130
void end()
Stops the AAC decoder process and the calls the output device's end to shut it down,...
Definition BackgroundAudioAAC.h:120
size_t available()
Gets number of bytes already in the raw buffer.
Definition BackgroundAudioAAC.h:176
bool paused()
Determine if the playback is paused.
Definition BackgroundAudioAAC.h:260
bool done()
Determine if no more AAC file is present in the buffer.
Definition BackgroundAudioAAC.h:185
uint32_t shifts()
Get the number of input data shifts processed by decoder since begin
Definition BackgroundAudioAAC.h:203
size_t availableForWrite()
Gets number of bytes available to write to raw buffer.
Definition BackgroundAudioAAC.h:167
void setGain(float scale)
Set the gain multiplier (volume) for the stream. Takes effect immediately.
Definition BackgroundAudioAAC.h:76