arduino-audio-tools
CodecHelix.h
1 #pragma once
2 
3 #include "AudioTools/AudioCodecs/AudioCodecsBase.h"
4 #include "AudioTools/AudioCodecs/CodecWAV.h"
5 #include "AudioTools/AudioCodecs/CodecMP3Helix.h"
6 #include "AudioTools/AudioCodecs/CodecAACHelix.h"
7 
8 namespace audio_tools {
9 
19 class DecoderHelix : public AudioDecoder {
20 public:
21  DecoderHelix() { TRACED(); }
22 
23  DecoderHelix(Print &out_stream) {
24  TRACED();
25  p_out_stream = &out_stream;
26  }
27 
28  DecoderHelix(Print &out_stream, AudioInfoSupport &bi) {
29  TRACED();
30  p_out_stream = &out_stream;
31  p_bi = &bi;
32  }
33 
34  ~DecoderHelix() { resetDecoder(); }
35 
37  virtual void setOutput(Print &outStream) { p_out_stream = &outStream; }
38 
40  bool begin() {
41  TRACED();
42  // reset actual decoder so that we start a new determination
43  resetDecoder();
44  return true;
45  }
46 
48  void end() {
49  TRACED();
50  if (p_decoder!=nullptr){
51  p_decoder->end();
52  }
53  resetDecoder();
54  }
55 
56  AudioInfo audioInfo() override {
57  return p_decoder != nullptr ? p_decoder->audioInfo() : noInfo;
58  }
59 
61  size_t write(const uint8_t *data, size_t len) {
62  LOGD("%s: %zu", LOG_METHOD, len);
63  if (p_decoder == nullptr) {
64  setupDecoder((const byte *)data);
65  p_decoder->begin();
66  }
67  return p_decoder->write(data, len);
68  }
69 
71  operator bool() { return p_decoder == nullptr ? false : *p_decoder; }
72 
73 protected:
74  AudioDecoder *p_decoder = nullptr;
75  Print *p_out_stream = nullptr;
76  AudioInfoSupport *p_bi = nullptr;
77  AudioInfo noInfo;
78 
80  void setupDecoder(const byte *start) {
81  if (start[0] == 0xFF && start[1] == 0xF1) {
82  p_decoder = new AACDecoderHelix();
83  LOGI("using AACDecoderHelix");
84  } else if (start[0] == 0xFF || start[0] == 0xFE || strncmp("ID3", (const char*)start, 3)==0) {
85  p_decoder = new MP3DecoderHelix();
86  LOGI("using MP3DecoderHelix");
87  } else if (strncmp("RIFF", (const char*)start, 4)==0) {
88  p_decoder = new WAVDecoder();
89  LOGI("using WAVDecoder");
90  }
91  // if we do not have a decoder yet we use a dummy to prevent NPE
92  if (p_decoder == nullptr) {
93  LOGW("Unknown Data Format: Content will be ignored...")
94  p_decoder = CodecNOP::instance();
95  }
96  p_decoder->setOutput(*p_out_stream);
97  p_decoder->addNotifyAudioChange(*p_bi);
98  }
99 
101  void resetDecoder() {
102  if (p_decoder != nullptr) {
103  delete p_decoder;
104  }
105  p_decoder = nullptr;
106  }
107 };
108 
109 } // namespace audio_tools
110 
AAC Decoder using libhelix: https://github.com/pschatzmann/arduino-libhelix This is basically just a ...
Definition: CodecAACHelix.h:16
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 AudioInfo audioInfo()
provides the actual input AudioInfo
Definition: AudioCodecsBase.h:23
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
MP3 and AAC Decoder using libhelix: https://github.com/pschatzmann/arduino-libhelix....
Definition: CodecHelix.h:19
virtual void setOutput(Print &outStream)
Defines the output Stream.
Definition: CodecHelix.h:37
bool begin()
Starts the processing.
Definition: CodecHelix.h:40
void setupDecoder(const byte *start)
Defines the decoder based on the audio format.
Definition: CodecHelix.h:80
void end()
Releases the reserved memory.
Definition: CodecHelix.h:48
AudioInfo audioInfo() override
provides the actual input AudioInfo
Definition: CodecHelix.h:56
size_t write(const uint8_t *data, size_t len)
Write mp3 data to decoder.
Definition: CodecHelix.h:61
void resetDecoder()
Deletes the decoder.
Definition: CodecHelix.h:101
MP3 Decoder using libhelix: https://github.com/pschatzmann/arduino-libhelix This is basically just a ...
Definition: CodecMP3Helix.h:17
Definition: NoArduino.h:58
A simple WAVDecoder: We parse the header data on the first record to determine the format....
Definition: CodecWAV.h:269
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