arduino-audio-tools
CodecAACFDK.h
1 #pragma once
2 
3 #include "AudioTools/AudioCodecs/AudioCodecsBase.h"
4 #include "AACDecoderFDK.h"
5 #include "AACEncoderFDK.h"
6 
7 
8 namespace audio_tools {
9 
10 // audio change notification target
11 AudioInfoSupport *audioChangeFDK = nullptr;
12 
22 class AACDecoderFDK : public AudioDecoder {
23  public:
24  AACDecoderFDK(int output_buffer_size = FDK_OUT_BUFFER_DEFAULT_SIZE){
25  TRACED();
26  dec = new aac_fdk::AACDecoderFDK(output_buffer_size);
27  }
28 
29  AACDecoderFDK(Print &out_stream, int output_buffer_size = 2048){
30  TRACED();
31  dec = new aac_fdk::AACDecoderFDK(out_stream, output_buffer_size);
32  }
33 
34  virtual ~AACDecoderFDK(){
35  delete dec;
36  }
37 
39  void setOutput(Print &out_stream){
40  dec->setOutput(out_stream);
41  }
42 
43  bool begin(){
44  return dec->begin(TT_MP4_ADTS, 1);
45  }
46 
47  // opens the decoder
48  bool begin(TRANSPORT_TYPE transportType, UINT nrOfLayers){
49  return dec->begin(transportType, nrOfLayers);
50  }
51 
59  AAC_DECODER_ERROR configure(uint8_t *conf, const uint32_t &length) {
60  return dec->configure(conf, length);
61  }
62 
63  // write AAC data to be converted to PCM data
64  virtual size_t write(const uint8_t *data, size_t len) {
65  return dec->write(data, len);
66  }
67 
68  // provides detailed information about the stream
69  CStreamInfo audioInfoEx(){
70  return dec->audioInfo();
71  }
72 
73  // provides common information
74  AudioInfo audioInfo() override {
75  AudioInfo result;
76  CStreamInfo i = audioInfoEx();
77  result.channels = i.numChannels;
78  result.sample_rate = i.sampleRate;
79  result.bits_per_sample = 16;
80  return result;
81  }
82 
83  // release the resources
84  void end(){
85  TRACED();
86  dec->end();
87  }
88 
89  virtual operator bool() {
90  return (bool)*dec;
91  }
92 
93  aac_fdk::AACDecoderFDK *driver() {
94  return dec;
95  }
96 
97  static void audioChangeCallback(CStreamInfo &info){
98  if (audioChangeFDK!=nullptr){
99  AudioInfo base;
100  base.channels = info.numChannels;
101  base.sample_rate = info.sampleRate;
102  base.bits_per_sample = 16;
103  // notify audio change
104  audioChangeFDK->setAudioInfo(base);
105  }
106  }
107 
109  audioChangeFDK = &bi;
110  // register audio change handler
111  dec->setInfoCallback(audioChangeCallback);
112  }
113 
114  protected:
115  aac_fdk::AACDecoderFDK *dec=nullptr;
116 };
117 
118 
127 class AACEncoderFDK : public AudioEncoder {
128 
129 public:
130 
131  AACEncoderFDK(){
132  enc = new aac_fdk::AACEncoderFDK();
133  }
134 
135  AACEncoderFDK(Print &out_stream){
136  enc = new aac_fdk::AACEncoderFDK();
137  enc->setOutput(out_stream);
138  }
139 
140  ~AACEncoderFDK(){
141  delete enc;
142  }
143 
145  void setOutput(Print &out_stream){
146  enc->setOutput(out_stream);
147  }
148 
154  virtual void setBitrate(int bitrate){
155  enc->setBitrate(bitrate);
156  }
157 
179  virtual void setAudioObjectType(int aot){
180  enc->setAudioObjectType(aot);
181  }
182 
194  virtual void setAfterburner(bool afterburner){
195  enc->setAfterburner(afterburner);
196  }
197 
204  virtual void setSpectralBandReplication(int eld_sbr){
205  enc->setSpectralBandReplication(eld_sbr);
206  }
207 
226  virtual void setVariableBitrateMode(int vbr){
227  enc->setVariableBitrateMode(vbr);
228  }
229 
235  virtual void setOutputBufferSize(int outbuf_size){
236  enc->setOutputBufferSize(outbuf_size);
237  }
238 
240  void setAudioInfo(AudioInfo from) override {
241  TRACED();
243  aac_fdk::AudioInfo info;
244  info.channels = from.channels;
245  info.sample_rate = from.sample_rate;
246  info.bits_per_sample = from.bits_per_sample;
247  enc->setAudioInfo(info);
248  }
249 
256  virtual bool begin(AudioInfo info) {
257  TRACED();
258  return enc->begin(info.channels,info.sample_rate, info.bits_per_sample);
259  }
260 
269  virtual bool begin(int input_channels=2, int input_sample_rate=44100, int input_bits_per_sample=16) {
270  TRACED();
271  return enc->begin(input_channels,input_sample_rate, input_bits_per_sample);
272  }
273 
274  // starts the processing
275  bool begin() {
276  enc->begin();
277  return true;
278  }
279 
280  // convert PCM data to AAC
281  size_t write(const uint8_t *data, size_t len){
282  LOGD("write %d bytes", (int)len);
283  return enc->write((uint8_t*)data, len);
284  }
285 
286  // release resources
287  void end(){
288  TRACED();
289  enc->end();
290  }
291 
292  UINT getParameter(const AACENC_PARAM param) {
293  return enc->getParameter(param);
294  }
295 
296  int setParameter(AACENC_PARAM param, uint32_t value){
297  return enc->setParameter(param, value);
298  }
299 
300  aac_fdk::AACEncoderFDK *driver() {
301  return enc;
302  }
303 
304  const char *mime() {
305  return "audio/aac";
306  }
307 
308  operator bool(){
309  return (bool) *enc;
310  }
311 
312 
313 protected:
314  aac_fdk::AACEncoderFDK *enc=nullptr;
315 
316 };
317 
318 }
319 
Audio Decoder which decodes AAC into a PCM stream This is basically just a wrapper using https://gith...
Definition: CodecAACFDK.h:22
void addNotifyAudioChange(AudioInfoSupport &bi) override
Adds target to be notified about audio changes.
Definition: CodecAACFDK.h:108
void setOutput(Print &out_stream)
Defines the output stream.
Definition: CodecAACFDK.h:39
AAC_DECODER_ERROR configure(uint8_t *conf, const uint32_t &length)
Explicitly configure the decoder by passing a raw AudioSpecificConfig (ASC) or a StreamMuxConfig (SMC...
Definition: CodecAACFDK.h:59
AudioInfo audioInfo() override
provides the actual input AudioInfo
Definition: CodecAACFDK.h:74
Encodes PCM data to the AAC format and writes the result to a stream This is basically just a wrapper...
Definition: CodecAACFDK.h:127
const char * mime()
Provides the mime type of the encoded result.
Definition: CodecAACFDK.h:304
virtual void setOutputBufferSize(int outbuf_size)
Set the Output Buffer Size object.
Definition: CodecAACFDK.h:235
virtual bool begin(AudioInfo info)
Opens the encoder
Definition: CodecAACFDK.h:256
virtual void setAudioObjectType(int aot)
Audio object type. See ::AUDIO_OBJECT_TYPE in FDK_audio.h.
Definition: CodecAACFDK.h:179
void setAudioInfo(AudioInfo from) override
Defines the Audio Info.
Definition: CodecAACFDK.h:240
virtual void setAfterburner(bool afterburner)
This parameter controls the use of the afterburner feature. The afterburner is a type of analysis by ...
Definition: CodecAACFDK.h:194
virtual void setBitrate(int bitrate)
Total encoder bitrate. This parameter is mandatory and interacts with ::AACENC_BITRATEMODE.
Definition: CodecAACFDK.h:154
void setOutput(Print &out_stream)
Defines the output.
Definition: CodecAACFDK.h:145
virtual bool begin(int input_channels=2, int input_sample_rate=44100, int input_bits_per_sample=16)
Opens the encoder
Definition: CodecAACFDK.h:269
virtual void setVariableBitrateMode(int vbr)
Bitrate mode. Configuration can be different kind of bitrate configurations:
Definition: CodecAACFDK.h:226
virtual void setSpectralBandReplication(int eld_sbr)
Configure SBR independently of the chosen Audio Object Type ::AUDIO_OBJECT_TYPE. This parameter is fo...
Definition: CodecAACFDK.h:204
Docoding of encoded audio into PCM data.
Definition: AudioCodecsBase.h:16
Encoding of PCM data.
Definition: AudioCodecsBase.h:84
void setAudioInfo(AudioInfo from) override
Defines the sample rate, number of channels and bits per sample.
Definition: AudioCodecsBase.h:93
Supports changes to the sampling rate, bits and channels.
Definition: AudioTypes.h:139
virtual void setAudioInfo(AudioInfo info)=0
Defines the input AudioInfo.
Definition: NoArduino.h:58
Generic Implementation of sound input and output for desktop environments using portaudio.
Definition: AudioConfig.h:868
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