Arduino LibHelix
Loading...
Searching...
No Matches
AACDecoderHelix.h
1#pragma once
2
3#include "CommonHelix.h"
4#include "libhelix-aac/aacdec.h"
5
6namespace libhelix {
7
8typedef void (*AACInfoCallback)(_AACFrameInfo &info, void *ref);
9typedef void (*AACDataCallback)(_AACFrameInfo &info, short *pcm_buffer,
10 size_t len, void *ref);
11
20 public:
22 setMinFrameBufferSize(AAC_MIN_FRAME_SIZE);
23 memset(&aacFrameInfo, 0, sizeof(_AACFrameInfo));
24 }
25
26#if defined(ARDUINO) || defined(HELIX_PRINT)
27 AACDecoderHelix(Print &output) {
28 setMinFrameBufferSize(AAC_MIN_FRAME_SIZE);
29 this->out = &output;
30 memset(&aacFrameInfo, 0, sizeof(_AACFrameInfo));
31 }
32#endif
33
34 AACDecoderHelix(AACDataCallback dataCallback) {
35 setMinFrameBufferSize(AAC_MIN_FRAME_SIZE);
36 this->pcmCallback = dataCallback;
37 memset(&aacFrameInfo, 0, sizeof(_AACFrameInfo));
38 }
39
40 virtual ~AACDecoderHelix() { end(); }
41
42 void setInfoCallback(AACInfoCallback cb, void *caller = nullptr) {
43 this->infoCallback = cb;
44 if (caller!=nullptr)
45 p_caller_ref = caller;
46 }
47
48 void setDataCallback(AACDataCallback cb) { this->pcmCallback = cb; }
49
51 _AACFrameInfo audioInfo() { return aacFrameInfo; }
52
54 virtual void end() override {
55 LOGD_HELIX( "end");
56 if (decoder != nullptr) {
57 flush();
58 AACFreeDecoder(decoder);
59 decoder = nullptr;
60 }
62 memset(&aacFrameInfo, 0, sizeof(_AACFrameInfo));
63 }
64
65 size_t maxFrameSize() override {
66 return max_frame_size == 0 ? AAC_MAX_FRAME_SIZE : max_frame_size;
67 }
68
69 size_t maxPCMSize() override {
70 return max_pcm_size == 0 ? AAC_MAX_OUTPUT_SIZE : max_pcm_size;
71 }
72
74 void setAudioInfo(int channels, int samplerate) {
75 memset(&aacFrameInfo, 0, sizeof(AACFrameInfo));
76 aacFrameInfo.nChans = channels;
77 // aacFrameInfo.bitsPerSample = bits; not used
78 aacFrameInfo.sampRateCore = samplerate;
79 aacFrameInfo.profile = AAC_PROFILE_LC;
80 AACSetRawBlockParams(decoder, 0, &aacFrameInfo);
81 }
82
83 protected:
84 HAACDecoder decoder = nullptr;
85 AACDataCallback pcmCallback = nullptr;
86 AACInfoCallback infoCallback = nullptr;
87 _AACFrameInfo aacFrameInfo;
88 void *p_caller_data = nullptr;
89
91 virtual bool allocateDecoder() override {
92 if (decoder == nullptr) {
93 decoder = AACInitDecoder();
94 }
95 memset(&aacFrameInfo, 0, sizeof(_AACFrameInfo));
96 return decoder != nullptr;
97 }
98
100 int findSynchWord(int offset = 0) override {
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;
105 // report the position relative to the start of the buffer
106 return result + offset;
107 }
108
110 int decode() override {
111 LOGD_HELIX( "decode");
112 int processed = 0;
113 int available = frame_buffer.available();
114 int bytes_left = decodableBytes();
115 uint8_t *data = frame_buffer.data();
116 int rc = AACDecode(decoder, &data, &bytes_left, (short *)pcm_buffer.data());
117 if (rc == 0) {
118 int processed = data - frame_buffer.data();
119 // return the decoded result
120 _AACFrameInfo info;
121 AACGetLastFrameInfo(decoder, &info);
122 provideResult(info);
123 rc = processed;
124 }
125 return rc;
126 }
127
128 // return the result PCM data
129 void provideResult(_AACFrameInfo &info) {
130 // increase PCM size if this fails
131 int sampleSize = info.bitsPerSample / 8;
132 assert(info.outputSamps * sampleSize <= maxPCMSize());
133
134 LOGD_HELIX( "==> provideResult: %d samples", info.outputSamps);
135 if (info.outputSamps > 0) {
136 // provide result
137 if (pcmCallback != nullptr) {
138 // output via callback
139 pcmCallback(info, (short *)pcm_buffer.data(), info.outputSamps,
140 p_caller_data);
141 } else {
142 // output to stream
143 if (infoCallback != nullptr
144 && (info.sampRateOut != aacFrameInfo.sampRateOut || info.nChans != aacFrameInfo.nChans)) {
145 infoCallback(info, p_caller_ref);
146 }
147#if defined(ARDUINO) || defined(HELIX_PRINT)
148 if (out != nullptr){
149 size_t toWrite = info.outputSamps * sampleSize;
150 writeToOut((uint8_t *)pcm_buffer.data(), toWrite);
151 }
152#endif
153 }
154 aacFrameInfo = info;
155 }
156 }
157};
158} // namespace libhelix
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