arduino-audio-tools
Loading...
Searching...
No Matches
CodecMP3Helix.h
Go to the documentation of this file.
1#pragma once
2
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
74 const char *mime() override { return "audio/mpeg"; }
75
77 void setOutput(Print &outStream) override {
78 AudioDecoder::setOutput(outStream);
79 if (mp3 != nullptr) mp3->setOutput(outStream);
80 }
81
83 bool begin() override {
84 TRACEI();
85 if (mp3 == nullptr) {
86 LOGE("Not enough memory for libhelix");
87 return false;
88 }
89 return mp3->begin();
90 }
91
93 void end() override {
94 TRACEI();
95 if (mp3 != nullptr) mp3->end();
96 }
97
98 MP3FrameInfo audioInfoEx() {
99 if (mp3 == nullptr) return MP3FrameInfo{};
100 return mp3->audioInfo();
101 }
102
103 AudioInfo audioInfo() override {
104 AudioInfo baseInfo;
105 MP3FrameInfo i = audioInfoEx();
106 if (i.nChans != 0 && i.samprate != 0 && i.bitsPerSample != 0) {
107 baseInfo.channels = i.nChans;
108 baseInfo.sample_rate = i.samprate;
109 baseInfo.bits_per_sample = i.bitsPerSample;
110 }
111 return baseInfo;
112 }
113
115 size_t write(const uint8_t *data, size_t len) override {
116 LOGD("%s: %zu", LOG_METHOD, len);
117 if (mp3 == nullptr) return 0;
118 return mp3->write((uint8_t *)data, len);
119 }
120
122 operator bool() override { return mp3 != nullptr && (bool)*mp3; }
123
124 libhelix::MP3DecoderHelix *driver() { return mp3; }
125
129 TRACED();
131 if (mp3 != nullptr) mp3->setInfoCallback(infoCallback, this);
132 }
133
135 static void infoCallback(MP3FrameInfo &i, void *ref) {
136 MP3DecoderHelix *p_helix = (MP3DecoderHelix *)ref;
137 if (p_helix != nullptr) {
138 TRACED();
139 AudioInfo baseInfo;
140 baseInfo.channels = i.nChans;
141 baseInfo.sample_rate = i.samprate;
142 baseInfo.bits_per_sample = i.bitsPerSample;
143 baseInfo.logInfo("MP3DecoderHelix");
144 p_helix->notifyAudioChange(baseInfo);
145 } else {
146 LOGE("Wrong Libhelix Version");
147 }
148 }
149
152 size_t maxFrameSize() { return mp3 != nullptr ? mp3->maxFrameSize() : 0; }
153
155 void setMaxFrameSize(size_t len) {
156 if (mp3 != nullptr) mp3->setMaxFrameSize(len);
157 }
158
161 size_t maxPCMSize() { return mp3 != nullptr ? mp3->maxPCMSize() : 0; }
162
164 void setMaxPCMSize(size_t len) {
165 if (mp3 != nullptr) mp3->setMaxPCMSize(len);
166 }
167
168 protected:
169 libhelix::MP3DecoderHelix *mp3 = nullptr;
170};
171
172} // namespace audio_tools
#define TRACEI()
Definition AudioLoggerIDF.h:32
#define TRACED()
Definition AudioLoggerIDF.h:31
#define LOGD(...)
Definition AudioLoggerIDF.h:27
#define LOGE(...)
Definition AudioLoggerIDF.h:30
#define LOG_METHOD
Definition AudioToolsConfig.h:69
Definition Arduino.h:56
Decoding of encoded audio into PCM data.
Definition AudioCodecsBase.h:19
virtual void setOutput(AudioStream &out_stream)
Defines where the decoded result is written to.
Definition AudioCodecsBase.h:40
void notifyAudioChange(AudioInfo info)
Definition AudioTypes.h:180
virtual void addNotifyAudioChange(AudioInfoSupport &bi)
Adds target to be notified about audio changes.
Definition AudioTypes.h:155
Supports changes to the sampling rate, bits and channels.
Definition AudioTypes.h:137
MP3 Decoder using libhelix: https://github.com/pschatzmann/codec-helix This is basically just a simpl...
Definition CodecMP3Helix.h:20
void setMaxPCMSize(size_t len)
Define your optimized maximum pwm buffer size.
Definition CodecMP3Helix.h:164
void setMaxFrameSize(size_t len)
Define your optimized maximum frame size.
Definition CodecMP3Helix.h:155
void addNotifyAudioChange(AudioInfoSupport &bi) override
Definition CodecMP3Helix.h:128
libhelix::MP3DecoderHelix * driver()
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:77
void end() override
Releases the reserved memory.
Definition CodecMP3Helix.h:93
size_t maxFrameSize()
Definition CodecMP3Helix.h:152
size_t maxPCMSize()
Definition CodecMP3Helix.h:161
MP3FrameInfo audioInfoEx()
Definition CodecMP3Helix.h:98
size_t write(const uint8_t *data, size_t len) override
Write mp3 data to decoder.
Definition CodecMP3Helix.h:115
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:135
MP3DecoderHelix()
Definition CodecMP3Helix.h:22
const char * mime() override
Provides the mime type of the data that is expected by this decoder.
Definition CodecMP3Helix.h:74
bool begin() override
Starts the processing.
Definition CodecMP3Helix.h:83
libhelix::MP3DecoderHelix * mp3
Definition CodecMP3Helix.h:169
AudioInfo audioInfo() override
provides the actual input AudioInfo
Definition CodecMP3Helix.h:103
Generic Implementation of sound input and output for desktop environments using portaudio.
Definition LMSEchoCancellationStream.h:6
Basic Audio information which drives e.g. I2S.
Definition AudioTypes.h:56
sample_rate_t sample_rate
Sample Rate: e.g 44100.
Definition AudioTypes.h:58
uint16_t channels
Number of channels: 2=stereo, 1=mono.
Definition AudioTypes.h:60
uint8_t bits_per_sample
Number of bits per sample (int16_t = 16 bits)
Definition AudioTypes.h:62
virtual void logInfo(const char *source="")
Definition AudioTypes.h:127