arduino-audio-tools
Loading...
Searching...
No Matches
CodecMP3Helix.h
1#pragma once
2
3#include "AudioTools/AudioCodecs/AudioCodecsBase.h"
4#ifndef HELIX_PRINT
5# define HELIX_PRINT
6#endif
7#include "MP3DecoderHelix.h"
8
9namespace audio_tools {
10
20 public:
21
23 TRACED();
24 mp3 = new libhelix::MP3DecoderHelix();
25 if (mp3!=nullptr){
26 mp3->setReference(this);
27 } else {
28 LOGE("Not enough memory for libhelix");
29 }
30 }
36 MP3DecoderHelix(Print &out_stream){
37 TRACED();
38 mp3 = new libhelix::MP3DecoderHelix();
39 if (mp3!=nullptr){
40 mp3->setReference(this);
41 } else {
42 LOGE("Not enough memory for libhelix");
43 }
44 setOutput(out_stream);
45 }
46
55 TRACED();
56 mp3 = new libhelix::MP3DecoderHelix();
57 if (mp3!=nullptr){
58 mp3->setReference(this);
59 } else {
60 LOGE("Not enough memory for libhelix");
61 }
62 setOutput(out_stream);
64 }
65
71 if (mp3!=nullptr) delete mp3;
72 }
73
75 void setOutput(Print &outStream) override {
76 AudioDecoder::setOutput(outStream);
77 if (mp3!=nullptr) mp3->setOutput(outStream);
78 }
79
81 bool begin() override {
82 TRACEI();
83 if (mp3 == nullptr) {
84 LOGE("Not enough memory for libhelix");
85 return false;
86 }
87 mp3->begin();
88 return true;
89 }
90
92 void end() override {
93 TRACED();
94 if (mp3!=nullptr) mp3->end();
95 }
96
97 MP3FrameInfo audioInfoEx(){
98 return mp3->audioInfo();
99 }
100
101 AudioInfo audioInfo() override {
102 MP3FrameInfo i = audioInfoEx();
103 AudioInfo baseInfo;
104 baseInfo.channels = i.nChans;
105 baseInfo.sample_rate = i.samprate;
106 baseInfo.bits_per_sample = i.bitsPerSample;
107 return baseInfo;
108 }
109
111 size_t write(const uint8_t* data, size_t len) override {
112 LOGD("%s: %zu", LOG_METHOD, len);
113 if (mp3==nullptr) return 0;
114 return mp3->write((uint8_t*)data, len);
115 }
116
118 operator bool() override {
119 return mp3!=nullptr && (bool) *mp3;
120 }
121
122 libhelix::MP3DecoderHelix *driver() {
123 return mp3;
124 }
125
128 TRACED();
130 if (mp3!=nullptr) mp3->setInfoCallback(infoCallback, this);
131 }
132
134 static void infoCallback(MP3FrameInfo &i, void * ref){
135 MP3DecoderHelix* p_helix = (MP3DecoderHelix* )ref;
136 if (p_helix!=nullptr){
137 TRACED();
138 AudioInfo baseInfo;
139 baseInfo.channels = i.nChans;
140 baseInfo.sample_rate = i.samprate;
141 baseInfo.bits_per_sample = i.bitsPerSample;
142 baseInfo.logInfo("MP3DecoderHelix");
143 p_helix->notifyAudioChange(baseInfo);
144 } else {
145 LOGE("Wrong Libhelix Version");
146 }
147 }
148
150 size_t maxFrameSize() {
151 return mp3->maxFrameSize();
152 }
153
155 void setMaxFrameSize(size_t len){
156 mp3->setMaxFrameSize(len);
157 }
158
160 size_t maxPCMSize() {
161 return mp3->maxPCMSize();
162 }
163
165 void setMaxPCMSize(size_t len) {
166 mp3->setMaxPCMSize(len);
167 }
168 protected:
169 libhelix::MP3DecoderHelix *mp3=nullptr;
170
171};
172
173
174} // namespace
175
176
Decoding of encoded audio into PCM data.
Definition AudioCodecsBase.h:18
virtual void setOutput(AudioStream &out_stream)
Defines where the decoded result is written to.
Definition AudioCodecsBase.h:36
virtual void addNotifyAudioChange(AudioInfoSupport &bi)
Adds target to be notified about audio changes.
Definition AudioTypes.h:151
Supports changes to the sampling rate, bits and channels.
Definition AudioTypes.h:133
MP3 Decoder using libhelix: https://github.com/pschatzmann/arduino-libhelix This is basically just a ...
Definition CodecMP3Helix.h:19
void setMaxPCMSize(size_t len)
Define your optimized maximum pwm buffer size.
Definition CodecMP3Helix.h:165
void setMaxFrameSize(size_t len)
Define your optimized maximum frame size.
Definition CodecMP3Helix.h:155
void addNotifyAudioChange(AudioInfoSupport &bi) override
Defines the callback object to which the Audio information change is provided.
Definition CodecMP3Helix.h:127
~MP3DecoderHelix()
Destroy the MP3DecoderMini object.
Definition CodecMP3Helix.h:70
void setOutput(Print &outStream) override
Defines the output Stream.
Definition CodecMP3Helix.h:75
void end() override
Releases the reserved memory.
Definition CodecMP3Helix.h:92
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:150
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:160
size_t write(const uint8_t *data, size_t len) override
Write mp3 data to decoder.
Definition CodecMP3Helix.h:111
MP3DecoderHelix(Print &out_stream, AudioInfoSupport &bi)
Construct a new MP3DecoderMini object. The decoded output will go to the print object.
Definition CodecMP3Helix.h:54
MP3DecoderHelix(Print &out_stream)
Construct a new MP3DecoderMini object.
Definition CodecMP3Helix.h:36
static void infoCallback(MP3FrameInfo &i, void *ref)
notifies the subscriber about a change
Definition CodecMP3Helix.h:134
bool begin() override
Starts the processing.
Definition CodecMP3Helix.h:81
AudioInfo audioInfo() override
provides the actual input AudioInfo
Definition CodecMP3Helix.h:101
Definition NoArduino.h:62
Generic Implementation of sound input and output for desktop environments using portaudio.
Definition AudioCodecsBase.h:10
Basic Audio information which drives e.g. I2S.
Definition AudioTypes.h:53
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