arduino-audio-tools
CodecMP3Helix.h
1 #pragma once
2 
3 #include "AudioTools/AudioCodecs/AudioCodecsBase.h"
4 #include "AudioTools/CoreAudio/AudioMetaData/MetaDataFilter.h"
5 #include "MP3DecoderHelix.h"
6 
7 namespace audio_tools {
8 
17 class MP3DecoderHelix : public AudioDecoder {
18  public:
19 
20  MP3DecoderHelix() {
21  TRACED();
22  mp3 = new libhelix::MP3DecoderHelix();
23  if (mp3!=nullptr){
24  mp3->setReference(this);
25  } else {
26  LOGE("Not enough memory for libhelix");
27  }
28  }
34  MP3DecoderHelix(Print &out_stream){
35  TRACED();
36  mp3 = new libhelix::MP3DecoderHelix();
37  if (mp3!=nullptr){
38  mp3->setReference(this);
39  } else {
40  LOGE("Not enough memory for libhelix");
41  }
42  setOutput(out_stream);
43  }
44 
53  TRACED();
54  mp3 = new libhelix::MP3DecoderHelix();
55  if (mp3!=nullptr){
56  mp3->setReference(this);
57  } else {
58  LOGE("Not enough memory for libhelix");
59  }
60  setOutput(out_stream);
62  }
63 
69  if (mp3!=nullptr) delete mp3;
70  }
71 
73  virtual void setOutput(Print &outStream){
74  if (mp3!=nullptr) mp3->setOutput(outStream);
75  }
76 
78  bool begin(){
79  TRACED();
80  if (mp3!=nullptr) {
81  //mp3->setDelay(CODEC_DELAY_MS);
82  mp3->begin();
83  }
84  return true;
85  }
86 
88  void end(){
89  TRACED();
90  if (mp3!=nullptr) mp3->end();
91  }
92 
93  MP3FrameInfo audioInfoEx(){
94  return mp3->audioInfo();
95  }
96 
97  AudioInfo audioInfo() override {
98  MP3FrameInfo i = audioInfoEx();
99  AudioInfo baseInfo;
100  baseInfo.channels = i.nChans;
101  baseInfo.sample_rate = i.samprate;
102  baseInfo.bits_per_sample = i.bitsPerSample;
103  return baseInfo;
104  }
105 
107  size_t write(const uint8_t* data, size_t len) {
108  LOGD("%s: %zu", LOG_METHOD, len);
109  if (mp3==nullptr) return 0;
110  return mp3->write((uint8_t*)data, len);
111  }
112 
114  operator bool(){
115  return mp3!=nullptr && (bool) *mp3;
116  }
117 
118  libhelix::MP3DecoderHelix *driver() {
119  return mp3;
120  }
121 
124  TRACED();
126  if (mp3!=nullptr) mp3->setInfoCallback(infoCallback, this);
127  }
128 
130  static void infoCallback(MP3FrameInfo &i, void * ref){
131  MP3DecoderHelix* p_helix = (MP3DecoderHelix* )ref;
132  if (p_helix!=nullptr){
133  TRACED();
134  AudioInfo baseInfo;
135  baseInfo.channels = i.nChans;
136  baseInfo.sample_rate = i.samprate;
137  baseInfo.bits_per_sample = i.bitsPerSample;
138  baseInfo.logInfo("MP3DecoderHelix");
139  p_helix->notifyAudioChange(baseInfo);
140  } else {
141  LOGE("Wrong Libhelix Version");
142  }
143  }
144 
146  size_t maxFrameSize() {
147  return mp3->maxFrameSize();
148  }
149 
151  void setMaxFrameSize(size_t len){
152  mp3->setMaxFrameSize(len);
153  }
154 
156  size_t maxPCMSize() {
157  return mp3->maxPCMSize();
158  }
159 
161  void setMaxPCMSize(size_t len) {
162  mp3->setMaxPCMSize(len);
163  }
164  protected:
165  libhelix::MP3DecoderHelix *mp3=nullptr;
166 
167 };
168 
169 
170 } // namespace
171 
172 
Docoding of encoded audio into PCM data.
Definition: AudioCodecsBase.h:16
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 Decoder using libhelix: https://github.com/pschatzmann/arduino-libhelix This is basically just a ...
Definition: CodecMP3Helix.h:17
void setMaxPCMSize(size_t len)
Define your optimized maximum pwm buffer size.
Definition: CodecMP3Helix.h:161
void setMaxFrameSize(size_t len)
Define your optimized maximum frame size.
Definition: CodecMP3Helix.h:151
void addNotifyAudioChange(AudioInfoSupport &bi) override
Defines the callback object to which the Audio information change is provided.
Definition: CodecMP3Helix.h:123
~MP3DecoderHelix()
Destroy the MP3DecoderMini object.
Definition: CodecMP3Helix.h:68
virtual void setOutput(Print &outStream)
Defines the output Stream.
Definition: CodecMP3Helix.h:73
bool begin()
Starts the processing.
Definition: CodecMP3Helix.h:78
size_t maxFrameSize()
Provides the maximum frame size - this is allocated on the heap and you can reduce the heap size my m...
Definition: CodecMP3Helix.h:146
size_t maxPCMSize()
Provides the maximum pwm buffer size - this is allocated on the heap and you can reduce the heap size...
Definition: CodecMP3Helix.h:156
MP3DecoderHelix(Print &out_stream, AudioInfoSupport &bi)
Construct a new MP3DecoderMini object. The decoded output will go to the print object.
Definition: CodecMP3Helix.h:52
MP3DecoderHelix(Print &out_stream)
Construct a new MP3DecoderMini object.
Definition: CodecMP3Helix.h:34
static void infoCallback(MP3FrameInfo &i, void *ref)
notifies the subscriber about a change
Definition: CodecMP3Helix.h:130
void end()
Releases the reserved memory.
Definition: CodecMP3Helix.h:88
AudioInfo audioInfo() override
provides the actual input AudioInfo
Definition: CodecMP3Helix.h:97
size_t write(const uint8_t *data, size_t len)
Write mp3 data to decoder.
Definition: CodecMP3Helix.h:107
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