arduino-audio-tools
CodecAACHelix.h
1 #pragma once
2 
3 //#include "Stream.h"
4 #include "AudioCodecs/AudioEncoded.h"
5 #include "AACDecoderHelix.h"
6 
7 namespace audio_tools {
8 
17 class AACDecoderHelix : public AudioDecoder {
18  public:
19 
20  AACDecoderHelix() {
21  TRACED();
22  aac = new libhelix::AACDecoderHelix();
23  if (aac!=nullptr){
24  aac->setReference(this);
25  } else {
26  LOGE("Not enough memory for libhelix");
27  }
28  }
34  AACDecoderHelix(Print &out_stream){
35  TRACED();
36  aac = new libhelix::AACDecoderHelix(out_stream);
37  if (aac!=nullptr){
38  aac->setReference(this);
39  } else {
40  LOGE("Not enough memory for libhelix");
41  }
42  }
43 
52  TRACED();
53  aac = new libhelix::AACDecoderHelix(out_stream);
54  if (aac!=nullptr){
55  aac->setReference(this);
56  } else {
57  LOGE("Not enough memory for libhelix");
58  }
60  }
61 
67  TRACED();
68  if (aac!=nullptr) delete aac;
69  }
70 
71  void setRaw(bool flag){
72  if (aac!=nullptr) aac->setRaw(flag);
73  }
74 
76  virtual void setOutput(Print &out_stream){
77  TRACED();
78  AudioDecoder::setOutput(out_stream);
79  if (aac!=nullptr) aac->setOutput(out_stream);
80  }
81 
83  bool begin() override {
84  TRACED();
85  if (aac!=nullptr) {
86  aac->setDelay(CODEC_DELAY_MS);
87  aac->setInfoCallback(infoCallback, this);
88  aac->begin();
89  }
90  return true;
91  }
92 
94  virtual void end() override {
95  TRACED();
96  if (aac!=nullptr) aac->end();
97  }
98 
99  virtual _AACFrameInfo audioInfoEx(){
100  return aac->audioInfo();
101  }
102 
103  AudioInfo audioInfo() override{
104  AudioInfo result;
105  auto i = audioInfoEx();
106  result.channels = i.nChans;
107  result.sample_rate = i.sampRateOut;
108  result.bits_per_sample = i.bitsPerSample;
109  return result;
110  }
111 
112  void setAudioInfo(AudioInfo info) override {
113  this->info = info;
114  if(info_notifications_active){
115  notifyAudioChange(info);
116  }
117  }
118 
120  size_t write(const void* aac_data, size_t len) override {
121  LOGD("AACDecoderHelix::write: %d", (int)len);
122  if (aac==nullptr) return 0;
123  int open = len;
124  int processed = 0;
125  uint8_t *data = (uint8_t*)aac_data;
126  while(open>0){
127  int act_write = aac->write(data+processed, min(open, DEFAULT_BUFFER_SIZE));
128  open -= act_write;
129  processed += act_write;
130  }
131  return processed;
132  }
133 
135  virtual operator bool() override {
136  return aac!=nullptr && (bool)*aac;
137  }
138 
139  void flush(){
140  // aac->flush();
141  }
142 
144  static void infoCallback(_AACFrameInfo &i, void* ref){
145  AACDecoderHelix *p_helix = (AACDecoderHelix *)ref;
146  if (p_helix!=nullptr){
147  TRACED();
148  AudioInfo baseInfo;
149  baseInfo.channels = i.nChans;
150  baseInfo.sample_rate = i.sampRateOut;
151  baseInfo.bits_per_sample = i.bitsPerSample;
152  //p_helix->audioChangeAACHelix->setAudioInfo(baseInfo);
153  LOGW("sample_rate: %d", i.sampRateOut);
154  p_helix->setAudioInfo(baseInfo);
155  }
156  }
157 
159  size_t maxFrameSize() {
160  return aac->maxFrameSize();
161  }
162 
164  void setMaxFrameSize(size_t len){
165  aac->setMaxFrameSize(len);
166  }
167 
168  void setAudioInfoNotifications(bool active){
169  info_notifications_active = active;
170  }
171 
172 #ifdef HELIX_PCM_CORRECTED
174  size_t maxPCMSize() {
175  return aac->maxPCMSize();
176  }
177 
179  void setMaxPCMSize(size_t len) {
180  aac->setMaxPCMSize(len);
181  }
182 #else
184  size_t maxPCMSize() {
185  return aac->maxPWMSize();
186  }
187 
189  void setMaxPCMSize(size_t len) {
190  aac->setMaxPWMSize(len);
191  }
192 #endif
193 
194  protected:
195  libhelix::AACDecoderHelix *aac=nullptr;
196  bool info_notifications_active = true;
197 
198 
199 };
200 
201 
202 } // namespace
203 
AAC Decoder using libhelix: https://github.com/pschatzmann/arduino-libhelix This is basically just a ...
Definition: CodecAACHelix.h:17
void setMaxPCMSize(size_t len)
Define your optimized maximum pwm buffer size.
Definition: CodecAACHelix.h:189
size_t write(const void *aac_data, size_t len) override
Write AAC data to decoder.
Definition: CodecAACHelix.h:120
void setMaxFrameSize(size_t len)
Define your optimized maximum frame size.
Definition: CodecAACHelix.h:164
virtual void setOutput(Print &out_stream)
Defines the output Stream.
Definition: CodecAACHelix.h:76
static void infoCallback(_AACFrameInfo &i, void *ref)
notifies the subscriber about a change
Definition: CodecAACHelix.h:144
size_t maxFrameSize()
Provides the maximum frame size - this is allocated on the heap and you can reduce the heap size my m...
Definition: CodecAACHelix.h:159
size_t maxPCMSize()
Provides the maximum pwm buffer size - this is allocated on the heap and you can reduce the heap size...
Definition: CodecAACHelix.h:184
~AACDecoderHelix()
Destroy the AACDecoderMini object.
Definition: CodecAACHelix.h:66
bool begin() override
Starts the processing.
Definition: CodecAACHelix.h:83
void setAudioInfo(AudioInfo info) override
for most decoders this is not needed
Definition: CodecAACHelix.h:112
virtual void end() override
Releases the reserved memory.
Definition: CodecAACHelix.h:94
AACDecoderHelix(Print &out_stream)
Construct a new AACDecoderMini object.
Definition: CodecAACHelix.h:34
AACDecoderHelix(Print &out_stream, AudioInfoSupport &bi)
Construct a new AACDecoderMini object. The decoded output will go to the print object.
Definition: CodecAACHelix.h:51
AudioInfo audioInfo() override
provides the actual input AudioInfo
Definition: CodecAACHelix.h:103
Docoding of encoded audio into PCM data.
Definition: AudioEncoded.h:18
virtual void setOutput(AudioStream &out_stream)
Defines where the decoded result is written to.
Definition: AudioEncoded.h:36
virtual void addNotifyAudioChange(AudioInfoSupport &bi)
Adds target to be notified about audio changes.
Definition: AudioTypes.h:158
Supports changes to the sampling rate, bits and channels.
Definition: AudioTypes.h:136
Definition: NoArduino.h:58
Generic Implementation of sound input and output for desktop environments using portaudio.
Definition: AnalogAudio.h:10
Basic Audio information which drives e.g. I2S.
Definition: AudioTypes.h:50
sample_rate_t sample_rate
Sample Rate: e.g 44100.
Definition: AudioTypes.h:53
uint16_t channels
Number of channels: 2=stereo, 1=mono.
Definition: AudioTypes.h:55
uint8_t bits_per_sample
Number of bits per sample (int16_t = 16 bits)
Definition: AudioTypes.h:57