arduino-audio-tools
Loading...
Searching...
No Matches
CodecMP3Mini.h
Go to the documentation of this file.
1#pragma once
2
3#define MINIMP3_NO_STDIO
4//#define MINIMP3_NO_SIMD
5//#define MINIMP3_IMPLEMENTATION
6//#define MINIMP3_ONLY_MP3
7//#define MINIMP3_FLOAT_OUTPUT
8
9#ifndef MINIMP3_MAX_SAMPLE_RATE
10#define MINIMP3_MAX_SAMPLE_RATE 44100
11#endif
12
14#include "minimp3.h"
15
16
17namespace audio_tools {
18
29 public:
30 MP3DecoderMini() = default;
31
34 if (active) {
35 end();
36 }
37 }
38
39 const char *mime() override { return "audio/mpeg"; }
40
41 void setBufferLength(int len) { buffer_size = len; }
42
44 bool begin() {
45 TRACED();
46 //esp_task_wdt_delete(nullptr);
47 ::mp3dec_init(&mp3d);
49 pcm.resize(MINIMP3_MAX_SAMPLES_PER_FRAME);
50 buffer_pos = 0;
51 active = true;
52 return true;
53 }
54
56 void end() {
57 TRACED();
58 flush();
59 active = false;
60 }
61
63 void setOutput(Print &outStream) { this->out = &outStream; }
64
66 size_t write(const uint8_t *data, size_t len) {
67 LOGD("write: %zu", len);
68 if (active) {
69 if (buffer_pos+len>=buffer.size()){
70 decode(len);
71 }
73 memcpy(buffer.data()+buffer_pos, data, len);
74 buffer_pos += len;
75 }
76 return len;
77 }
78
80 void flush() {
81 // decode the full buffer
82 decode(0);
83 buffer_pos = 0;
84 }
85
87 virtual operator bool() { return active; }
88
89 void setSampleRateLimit(int limit){
90 sample_rate_limit = limit;
91 }
92
93 protected:
94 Print *out = nullptr;
95 mp3dec_t mp3d;
96 mp3dec_frame_info_t mp3dec_info;
97 size_t buffer_size = 5 * 1024;
98 size_t buffer_pos = 0;
101 #ifdef MINIMP3_FLOAT_OUTPUT
102 Vector<int16_t> pcm16;
103 #endif
104 bool active;
106
108 void decode(int write_len) {
109 LOGD("decode: %zd ", buffer_pos);
110 int open = buffer_pos;
111 int processed = 0;
112 int samples;
113 do {
114 // decode data
115 samples = ::mp3dec_decode_frame(&mp3d, buffer.data()+processed, open,
116 pcm.data(), &mp3dec_info);
117 LOGD("frame_offset: %d - frame_bytes: %d -> samples %d", mp3dec_info.frame_offset, mp3dec_info.frame_bytes, samples);
118 open -= mp3dec_info.frame_bytes;
119 processed += mp3dec_info.frame_bytes;
120 // output decoding result
121 if (samples > 0) {
122 provideResult(samples);
123 }
124 // process until we have space for the next write
125 } while(processed < write_len);
126
127 // save unprocessed data
128 buffer_pos = open;
129 memmove(buffer.data(),buffer.data()+processed, open);
130 }
131
133 void provideResult(int samples) {
134 LOGD("provideResult: %d samples", samples);
135 AudioInfo tmp;
137 tmp.channels = mp3dec_info.channels;
138 tmp.bits_per_sample = 16;
139
140 // notify about audio changes
141 if (tmp != info) {
142 tmp.logInfo();
144 }
145 // store last info so that we can detect any changes
146 info = info;
147
148 // provide result pwm data
149 if (out != nullptr) {
150 #ifdef MINIMP3_FLOAT_OUTPUT
151 pcm16.resize(samples);
152 f32_to_s16(pcm.data(), pcm16.data(), samples);
153 out->write((uint8_t *)pcm16.data(), samples * sizeof(int16_t));
154 #else
155 out->write((uint8_t *)pcm.data(), samples * sizeof(mp3d_sample_t));
156 #endif
157 }
158 }
159
160 void f32_to_s16(float *in, int16_t *out, int num_samples) {
161 int i = 0;
162 for(; i < num_samples; i++){
163 float sample = in[i] * 32768.0f;
164 if (sample >= 32766.5f)
165 out[i] = (int16_t) 32767;
166 else if (sample <= -32767.5f)
167 out[i] = (int16_t)-32768;
168 else {
169 int16_t s = (int16_t)(sample + .5f);
170 s -= (s < 0); /* away from zero, to be compliant */
171 out[i] = s;
172 }
173 }
174 }
175
176
177};
178
179} // namespace audio_tools
#define TRACED()
Definition AudioLoggerIDF.h:31
#define LOGD(...)
Definition AudioLoggerIDF.h:27
#define MINIMP3_MAX_SAMPLE_RATE
Definition CodecMP3Mini.h:10
#define assert(T)
Definition avr.h:10
Definition Arduino.h:56
virtual size_t write(const uint8_t *data, size_t len)
Definition Arduino.h:120
Decoding of encoded audio into PCM data.
Definition AudioCodecsBase.h:19
AudioInfo info
Definition AudioCodecsBase.h:80
void notifyAudioChange(AudioInfo info)
Definition AudioTypes.h:175
MP3 Decoder using https://github.com/pschatzmann/minimp3. This decoder does not provide any good resu...
Definition CodecMP3Mini.h:28
bool active
Definition CodecMP3Mini.h:104
void setSampleRateLimit(int limit)
Definition CodecMP3Mini.h:89
mp3dec_frame_info_t mp3dec_info
Definition CodecMP3Mini.h:96
Vector< mp3d_sample_t > pcm
Definition CodecMP3Mini.h:100
void setOutput(Print &outStream)
Defines the output Stream.
Definition CodecMP3Mini.h:63
bool begin()
Starts the processing.
Definition CodecMP3Mini.h:44
size_t buffer_size
Definition CodecMP3Mini.h:97
void f32_to_s16(float *in, int16_t *out, int num_samples)
Definition CodecMP3Mini.h:160
void setBufferLength(int len)
Definition CodecMP3Mini.h:41
~MP3DecoderMini()
Destroy the MP3DecoderMini object.
Definition CodecMP3Mini.h:33
void provideResult(int samples)
Provides Metadata and PCM data.
Definition CodecMP3Mini.h:133
mp3dec_t mp3d
Definition CodecMP3Mini.h:95
void end()
Releases the reserved memory.
Definition CodecMP3Mini.h:56
Print * out
Definition CodecMP3Mini.h:94
const char * mime() override
Provides the mime type of the data that is expected by this decoder.
Definition CodecMP3Mini.h:39
size_t buffer_pos
Definition CodecMP3Mini.h:98
void flush()
Decodes the last outstanding data.
Definition CodecMP3Mini.h:80
int sample_rate_limit
Definition CodecMP3Mini.h:105
void decode(int write_len)
Process single bytes so that we can decode a full frame when it is available.
Definition CodecMP3Mini.h:108
Vector< uint8_t > buffer
Definition CodecMP3Mini.h:99
size_t write(const uint8_t *data, size_t len)
Write mp3 data to decoder.
Definition CodecMP3Mini.h:66
Vector implementation which provides the most important methods as defined by std::vector....
Definition Vector.h:21
bool resize(size_t newSize, T value)
Definition Vector.h:266
T * data()
Definition Vector.h:316
int size()
Definition Vector.h:178
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