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
21 public:
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() { return mp3->audioInfo(); }
98
99 AudioInfo audioInfo() override {
100 AudioInfo baseInfo;
101 MP3FrameInfo i = audioInfoEx();
102 if (i.nChans != 0 && i.samprate != 0 && i.bitsPerSample != 0) {
103 baseInfo.channels = i.nChans;
104 baseInfo.sample_rate = i.samprate;
105 baseInfo.bits_per_sample = i.bitsPerSample;
106 }
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 { return mp3 != nullptr && (bool)*mp3; }
119
120 libhelix::MP3DecoderHelix *driver() { return mp3; }
121
125 TRACED();
127 if (mp3 != nullptr) mp3->setInfoCallback(infoCallback, this);
128 }
129
131 static void infoCallback(MP3FrameInfo &i, void *ref) {
132 MP3DecoderHelix *p_helix = (MP3DecoderHelix *)ref;
133 if (p_helix != nullptr) {
134 TRACED();
135 AudioInfo baseInfo;
136 baseInfo.channels = i.nChans;
137 baseInfo.sample_rate = i.samprate;
138 baseInfo.bits_per_sample = i.bitsPerSample;
139 baseInfo.logInfo("MP3DecoderHelix");
140 p_helix->notifyAudioChange(baseInfo);
141 } else {
142 LOGE("Wrong Libhelix Version");
143 }
144 }
145
148 size_t maxFrameSize() { return mp3->maxFrameSize(); }
149
151 void setMaxFrameSize(size_t len) { mp3->setMaxFrameSize(len); }
152
155 size_t maxPCMSize() { return mp3->maxPCMSize(); }
156
158 void setMaxPCMSize(size_t len) { mp3->setMaxPCMSize(len); }
159
160 protected:
161 libhelix::MP3DecoderHelix *mp3 = nullptr;
162};
163
164} // namespace audio_tools
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:153
Supports changes to the sampling rate, bits and channels.
Definition AudioTypes.h:135
MP3 Decoder using libhelix: https://github.com/pschatzmann/arduino-libhelix This is basically just a ...
Definition CodecMP3Helix.h:20
void setMaxPCMSize(size_t len)
Define your optimized maximum pwm buffer size.
Definition CodecMP3Helix.h:158
void setMaxFrameSize(size_t len)
Define your optimized maximum frame size.
Definition CodecMP3Helix.h:151
void addNotifyAudioChange(AudioInfoSupport &bi) override
Definition CodecMP3Helix.h:124
~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()
Definition CodecMP3Helix.h:148
size_t maxPCMSize()
Definition CodecMP3Helix.h:155
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:131
bool begin() override
Starts the processing.
Definition CodecMP3Helix.h:81
AudioInfo audioInfo() override
provides the actual input AudioInfo
Definition CodecMP3Helix.h:99
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:55
sample_rate_t sample_rate
Sample Rate: e.g 44100.
Definition AudioTypes.h:57
uint16_t channels
Number of channels: 2=stereo, 1=mono.
Definition AudioTypes.h:59
uint8_t bits_per_sample
Number of bits per sample (int16_t = 16 bits)
Definition AudioTypes.h:61