3#include "CommonHelix.h"
4#include "libhelix-aac/aacdec.h"
8typedef void (*AACInfoCallback)(_AACFrameInfo &info,
void *ref);
9typedef void (*AACDataCallback)(_AACFrameInfo &info,
short *pcm_buffer,
10 size_t len,
void *ref);
23 memset(&aacFrameInfo, 0,
sizeof(_AACFrameInfo));
26#if defined(ARDUINO) || defined(HELIX_PRINT)
30 memset(&aacFrameInfo, 0,
sizeof(_AACFrameInfo));
36 this->pcmCallback = dataCallback;
37 memset(&aacFrameInfo, 0,
sizeof(_AACFrameInfo));
42 void setInfoCallback(AACInfoCallback cb,
void *caller =
nullptr) {
43 this->infoCallback = cb;
45 p_caller_ref = caller;
48 void setDataCallback(AACDataCallback cb) { this->pcmCallback = cb; }
54 virtual void end()
override {
56 if (decoder !=
nullptr) {
58 AACFreeDecoder(decoder);
62 memset(&aacFrameInfo, 0,
sizeof(_AACFrameInfo));
66 return max_frame_size == 0 ? AAC_MAX_FRAME_SIZE : max_frame_size;
70 return max_pcm_size == 0 ? AAC_MAX_OUTPUT_SIZE : max_pcm_size;
75 memset(&aacFrameInfo, 0,
sizeof(AACFrameInfo));
76 aacFrameInfo.nChans = channels;
78 aacFrameInfo.sampRateCore = samplerate;
79 aacFrameInfo.profile = AAC_PROFILE_LC;
80 AACSetRawBlockParams(decoder, 0, &aacFrameInfo);
84 HAACDecoder decoder =
nullptr;
85 AACDataCallback pcmCallback =
nullptr;
86 AACInfoCallback infoCallback =
nullptr;
87 _AACFrameInfo aacFrameInfo;
88 void *p_caller_data =
nullptr;
92 if (decoder ==
nullptr) {
93 decoder = AACInitDecoder();
95 memset(&aacFrameInfo, 0,
sizeof(_AACFrameInfo));
96 return decoder !=
nullptr;
101 if (offset > frame_buffer.available())
return -1;
102 int result = AACFindSyncWord(frame_buffer.data() + offset,
103 frame_buffer.available() - offset);
104 if (result < 0)
return result;
106 return result + offset;
111 LOGD_HELIX(
"decode");
113 int available = frame_buffer.available();
115 uint8_t *data = frame_buffer.data();
116 int rc = AACDecode(decoder, &data, &bytes_left, (
short *)pcm_buffer.data());
118 int processed = data - frame_buffer.data();
121 AACGetLastFrameInfo(decoder, &info);
129 void provideResult(_AACFrameInfo &info) {
131 int sampleSize = info.bitsPerSample / 8;
132 assert(info.outputSamps * sampleSize <=
maxPCMSize());
134 LOGD_HELIX(
"==> provideResult: %d samples", info.outputSamps);
135 if (info.outputSamps > 0) {
137 if (pcmCallback !=
nullptr) {
139 pcmCallback(info, (
short *)pcm_buffer.data(), info.outputSamps,
143 if (infoCallback !=
nullptr
144 && (info.sampRateOut != aacFrameInfo.sampRateOut || info.nChans != aacFrameInfo.nChans)) {
145 infoCallback(info, p_caller_ref);
147#if defined(ARDUINO) || defined(HELIX_PRINT)
149 size_t toWrite = info.outputSamps * sampleSize;
150 writeToOut((uint8_t *)pcm_buffer.data(), toWrite);
A simple Arduino API for the libhelix AAC decoder. The data us provided with the help of write() call...
Definition AACDecoderHelix.h:19
_AACFrameInfo audioInfo()
Provides the last available _AACFrameInfo_t.
Definition AACDecoderHelix.h:51
size_t maxPCMSize() override
Definition AACDecoderHelix.h:69
virtual void end() override
Releases the reserved memory.
Definition AACDecoderHelix.h:54
int decode() override
decods the data and removes the decoded frame from the buffer
Definition AACDecoderHelix.h:110
void setAudioInfo(int channels, int samplerate)
Used by M3A format.
Definition AACDecoderHelix.h:74
virtual bool allocateDecoder() override
Allocate the decoder.
Definition AACDecoderHelix.h:91
size_t maxFrameSize() override
Definition AACDecoderHelix.h:65
int findSynchWord(int offset=0) override
finds the sync word in the buffer
Definition AACDecoderHelix.h:100
Common Simple Arduino API.
Definition CommonHelix.h:33
void flush()
Decodes all open packets, including the last frame. Call it at the end of the stream....
Definition CommonHelix.h:109
virtual void end()
Releases the reserved memory.
Definition CommonHelix.h:63
virtual void setMinFrameBufferSize(int size)
Definition CommonHelix.h:328
int decodableBytes()
the bytes that the decoders may use for a frame
Definition CommonHelix.h:160