3 #include "CommonHelix.h"
4 #include "libhelix-aac/aacdec.h"
8 typedef void (*AACInfoCallback)(_AACFrameInfo &info,
void *ref);
9 typedef void (*AACDataCallback)(_AACFrameInfo &info,
short *pcm_buffer,
10 size_t len,
void *ref);
25 #if defined(ARDUINO) || defined(HELIX_PRINT)
34 this->pcmCallback = dataCallback;
39 void setInfoCallback(AACInfoCallback cb,
void *caller =
nullptr) {
40 this->infoCallback = cb;
42 p_caller_ref = caller;
45 void setDataCallback(AACDataCallback cb) { this->pcmCallback = cb; }
51 virtual void end()
override {
53 if (decoder !=
nullptr) {
55 AACFreeDecoder(decoder);
59 memset(&aacFrameInfo, 0,
sizeof(_AACFrameInfo));
63 return max_frame_size == 0 ? AAC_MAX_FRAME_SIZE : max_frame_size;
67 return max_pcm_size == 0 ? AAC_MAX_OUTPUT_SIZE : max_pcm_size;
72 memset(&aacFrameInfo, 0,
sizeof(AACFrameInfo));
73 aacFrameInfo.nChans = channels;
75 aacFrameInfo.sampRateCore = samplerate;
76 aacFrameInfo.profile = AAC_PROFILE_LC;
77 AACSetRawBlockParams(decoder, 0, &aacFrameInfo);
81 HAACDecoder decoder =
nullptr;
82 AACDataCallback pcmCallback =
nullptr;
83 AACInfoCallback infoCallback =
nullptr;
84 _AACFrameInfo aacFrameInfo;
85 void *p_caller_data =
nullptr;
89 if (decoder ==
nullptr) {
90 decoder = AACInitDecoder();
92 memset(&aacFrameInfo, 0,
sizeof(_AACFrameInfo));
93 return decoder !=
nullptr;
98 if (offset > frame_buffer.available())
return -1;
99 int result = AACFindSyncWord(frame_buffer.data() + offset,
100 frame_buffer.available() - offset);
101 if (result < 0)
return result;
102 return offset == 0 ? result : result - offset;
107 LOGD_HELIX(
"decode");
109 int available = frame_buffer.available();
110 int bytes_left = frame_buffer.available();
111 uint8_t *data = frame_buffer.data();
112 int rc = AACDecode(decoder, &data, &bytes_left, (
short *)pcm_buffer.data());
114 processed = data - frame_buffer.data();
117 AACGetLastFrameInfo(decoder, &info);
124 void provideResult(_AACFrameInfo &info) {
126 int sampleSize = info.bitsPerSample / 8;
127 assert(info.outputSamps * sampleSize <=
maxPCMSize());
129 LOGD_HELIX(
"==> provideResult: %d samples", info.outputSamps);
130 if (info.outputSamps > 0) {
132 if (pcmCallback !=
nullptr) {
134 pcmCallback(info, (
short *)pcm_buffer.data(), info.outputSamps,
138 if (info.sampRateOut != aacFrameInfo.sampRateOut &&
139 infoCallback !=
nullptr) {
140 infoCallback(info, p_caller_ref);
142 #if defined(ARDUINO) || defined(HELIX_PRINT)
144 size_t to_write = info.outputSamps * sampleSize;
145 size_t written = out->write((uint8_t *)pcm_buffer.data(), to_write);
146 assert(written == to_write);
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:48
size_t maxPCMSize() override
Definition: AACDecoderHelix.h:66
virtual void end() override
Releases the reserved memory.
Definition: AACDecoderHelix.h:51
int decode() override
decods the data and removes the decoded frame from the buffer
Definition: AACDecoderHelix.h:106
void setAudioInfo(int channels, int samplerate)
Used by M3A format.
Definition: AACDecoderHelix.h:71
virtual bool allocateDecoder() override
Allocate the decoder.
Definition: AACDecoderHelix.h:88
size_t maxFrameSize() override
Definition: AACDecoderHelix.h:62
int findSynchWord(int offset=0) override
finds the sync word in the buffer
Definition: AACDecoderHelix.h:97
Common Simple Arduino API.
Definition: CommonHelix.h:33
void flush()
Decode all open packets.
Definition: CommonHelix.h:104
virtual void end()
Releases the reserved memory.
Definition: CommonHelix.h:63
virtual void setMinFrameBufferSize(int size)
Defines the minimum frame buffer size which is required before starting the decoding.
Definition: CommonHelix.h:251