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