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 mp3->begin();
90 return true;
91 }
92
94 void end() override {
95 TRACEI();
96 if (mp3 != nullptr) mp3->end();
97 }
98
99 MP3FrameInfo audioInfoEx() { return mp3->audioInfo(); }
100
101 AudioInfo audioInfo() override {
102 AudioInfo baseInfo;
103 MP3FrameInfo i = audioInfoEx();
104 if (i.nChans != 0 && i.samprate != 0 && i.bitsPerSample != 0) {
105 baseInfo.channels = i.nChans;
106 baseInfo.sample_rate = i.samprate;
107 baseInfo.bits_per_sample = i.bitsPerSample;
108 }
109 return baseInfo;
110 }
111
113 size_t write(const uint8_t *data, size_t len) override {
114 LOGD("%s: %zu", LOG_METHOD, len);
115 if (mp3 == nullptr) return 0;
116 return mp3->write((uint8_t *)data, len);
117 }
118
120 operator bool() override { return mp3 != nullptr && (bool)*mp3; }
121
122 libhelix::MP3DecoderHelix *driver() { return mp3; }
123
127 TRACED();
129 if (mp3 != nullptr) mp3->setInfoCallback(infoCallback, this);
130 }
131
133 static void infoCallback(MP3FrameInfo &i, void *ref) {
134 MP3DecoderHelix *p_helix = (MP3DecoderHelix *)ref;
135 if (p_helix != nullptr) {
136 TRACED();
137 AudioInfo baseInfo;
138 baseInfo.channels = i.nChans;
139 baseInfo.sample_rate = i.samprate;
140 baseInfo.bits_per_sample = i.bitsPerSample;
141 baseInfo.logInfo("MP3DecoderHelix");
142 p_helix->notifyAudioChange(baseInfo);
143 } else {
144 LOGE("Wrong Libhelix Version");
145 }
146 }
147
150 size_t maxFrameSize() { return mp3->maxFrameSize(); }
151
153 void setMaxFrameSize(size_t len) { mp3->setMaxFrameSize(len); }
154
157 size_t maxPCMSize() { return mp3->maxPCMSize(); }
158
160 void setMaxPCMSize(size_t len) { mp3->setMaxPCMSize(len); }
161
162 protected:
163 libhelix::MP3DecoderHelix *mp3 = nullptr;
164};
165
166} // 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:175
virtual void addNotifyAudioChange(AudioInfoSupport &bi)
Adds target to be notified about audio changes.
Definition AudioTypes.h:150
Supports changes to the sampling rate, bits and channels.
Definition AudioTypes.h:132
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:160
void setMaxFrameSize(size_t len)
Define your optimized maximum frame size.
Definition CodecMP3Helix.h:153
void addNotifyAudioChange(AudioInfoSupport &bi) override
Definition CodecMP3Helix.h:126
libhelix::MP3DecoderHelix * driver()
Definition CodecMP3Helix.h:122
~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:94
size_t maxFrameSize()
Definition CodecMP3Helix.h:150
size_t maxPCMSize()
Definition CodecMP3Helix.h:157
MP3FrameInfo audioInfoEx()
Definition CodecMP3Helix.h:99
size_t write(const uint8_t *data, size_t len) override
Write mp3 data to decoder.
Definition CodecMP3Helix.h:113
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:133
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:163
AudioInfo audioInfo() override
provides the actual input AudioInfo
Definition CodecMP3Helix.h:101
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:51
sample_rate_t sample_rate
Sample Rate: e.g 44100.
Definition AudioTypes.h:53
uint16_t channels
Number of channels: 2=stereo, 1=mono.
Definition AudioTypes.h:55
uint8_t bits_per_sample
Number of bits per sample (int16_t = 16 bits)
Definition AudioTypes.h:57
virtual void logInfo(const char *source="")
Definition AudioTypes.h:122