arduino-audio-tools
CodecMP3MAD.h
1 #pragma once
2 
3 #define MINIMP3_IMPLEMENTATION
4 #define MINIMP3_NO_STDIO
5 #define LOGGING_ACTIVE true
6 
7 #include "Stream.h"
8 #include "AudioTools/AudioCodecs/AudioCodecsBase.h"
9 #include "MP3DecoderMAD.h"
10 
11 
12 namespace audio_tools {
13 
14 // forward audio changes
15 static AudioInfoSupport *audioChangeMAD=nullptr;
16 
24 class MP3DecoderMAD : public AudioDecoder {
25  public:
26 
27  MP3DecoderMAD(){
28  TRACED();
29  mad = new libmad::MP3DecoderMAD();
30  }
31 
32  MP3DecoderMAD(libmad::MP3DataCallback dataCallback, libmad::MP3InfoCallback infoCB=nullptr){
33  TRACED();
34  mad = new libmad::MP3DecoderMAD(dataCallback, infoCB);
35  }
36 
37  MP3DecoderMAD(Print &mad_output_streamput, libmad::MP3InfoCallback infoCB = nullptr){
38  TRACED();
39  mad = new libmad::MP3DecoderMAD(mad_output_streamput, infoCB);
40  }
41 
42  ~MP3DecoderMAD(){
43  TRACED();
44  delete mad;
45  }
46 
47  void setOutput(Print &out){
48  TRACED();
49  mad->setOutput(out);
50  }
51 
53  void setAudioDataCallback(libmad::MP3DataCallback cb){
54  TRACED();
55  mad->setDataCallback(cb);
56  }
57 
59  void setInfoCallback(libmad::MP3InfoCallback cb){
60  TRACED();
61  mad->setInfoCallback(cb);
62  }
63 
65  bool begin(){
66  TRACED();
67  mad->begin();
68  return true;
69  }
70 
72  void end(){
73  TRACED();
74  mad->end();
75  }
76 
78  libmad::MadAudioInfo audioInfoEx(){
79  TRACED();
80  return mad->audioInfo();
81  }
82 
83  AudioInfo audioInfo() override {
84  TRACED();
85  libmad::MadAudioInfo info = audioInfoEx();
86  AudioInfo base;
87  base.channels = info.channels;
88  base.sample_rate = info.sample_rate;
89  base.bits_per_sample = info.bits_per_sample;
90  return base;
91  }
92 
94  size_t write(const uint8_t *data, size_t len){
95  TRACED();
96  return mad->write(data,len);
97  }
98 
100  size_t write(void *data, size_t len){
101  TRACED();
102  return mad->write(data,len);
103  }
104 
106  operator bool(){
107  return (bool)*mad;
108  }
109 
110  libmad::MP3DecoderMAD *driver() {
111  return mad;
112  }
113 
114  static void audioChangeCallback(libmad::MadAudioInfo &info){
115  if (audioChangeMAD!=nullptr){
116  TRACED();
117  AudioInfo base;
118  base.channels = info.channels;
119  base.sample_rate = info.sample_rate;
120  base.bits_per_sample = info.bits_per_sample;
121  // notify audio change
122  audioChangeMAD->setAudioInfo(base);
123  }
124  }
125 
127  TRACED();
128  audioChangeMAD = &bi;
129  // register audio change handler
130  mad->setInfoCallback(audioChangeCallback);
131  }
132 
133  protected:
134  libmad::MP3DecoderMAD *mad;
135 
136 };
137 
138 } // namespace
139 
140 
Docoding of encoded audio into PCM data.
Definition: AudioCodecsBase.h:16
Supports changes to the sampling rate, bits and channels.
Definition: AudioTypes.h:139
virtual void setAudioInfo(AudioInfo info)=0
Defines the input AudioInfo.
MP3 Decoder using https://github.com/pschatzmann/arduino-libmad.
Definition: CodecMP3MAD.h:24
void setInfoCallback(libmad::MP3InfoCallback cb)
Defines the callback which receives the Info changes.
Definition: CodecMP3MAD.h:59
bool begin()
Starts the processing.
Definition: CodecMP3MAD.h:65
size_t write(void *data, size_t len)
Makes the mp3 data available for decoding: however we recommend to provide the data via a callback or...
Definition: CodecMP3MAD.h:100
virtual void addNotifyAudioChange(AudioInfoSupport &bi)
Adds target to be notified about audio changes.
Definition: CodecMP3MAD.h:126
void end()
Releases the reserved memory.
Definition: CodecMP3MAD.h:72
void setAudioDataCallback(libmad::MP3DataCallback cb)
Defines the callback which receives the decoded data.
Definition: CodecMP3MAD.h:53
void setOutput(Print &out)
Defines where the decoded result is written to.
Definition: CodecMP3MAD.h:47
AudioInfo audioInfo() override
provides the actual input AudioInfo
Definition: CodecMP3MAD.h:83
libmad::MadAudioInfo audioInfoEx()
Provides the last valid audio information.
Definition: CodecMP3MAD.h:78
size_t write(const uint8_t *data, size_t len)
Makes the mp3 data available for decoding: however we recommend to provide the data via a callback or...
Definition: CodecMP3MAD.h:94
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