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 {
52 LOG_HELIX(LogLevelHelix::Debug,
"end");
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;
108 int available = frame_buffer.available();
109 int bytes_left = frame_buffer.available();
110 uint8_t *data = frame_buffer.data();
111 int rc = AACDecode(decoder, &data, &bytes_left, (
short *)pcm_buffer.data());
113 processed = data - frame_buffer.data();
116 AACGetLastFrameInfo(decoder, &info);
123 void provideResult(_AACFrameInfo &info) {
125 int sampleSize = info.bitsPerSample / 8;
126 assert(info.outputSamps * sampleSize <=
maxPCMSize());
128 LOG_HELIX(LogLevelHelix::Debug,
"==> provideResult: %d samples", info.outputSamps);
129 if (info.outputSamps > 0) {
131 if (pcmCallback !=
nullptr) {
133 pcmCallback(info, (
short *)pcm_buffer.data(), info.outputSamps,
137 if (info.sampRateOut != aacFrameInfo.sampRateOut &&
138 infoCallback !=
nullptr) {
139 infoCallback(info, p_caller_ref);
141 #if defined(ARDUINO) || defined(HELIX_PRINT)
142 out->write((uint8_t *)pcm_buffer.data(), info.outputSamps * sampleSize);
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:102
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:243