3 #define MINIMP3_NO_STDIO
9 #ifndef MINIMP3_MAX_SAMPLE_RATE
10 #define MINIMP3_MAX_SAMPLE_RATE 44100
13 #include "AudioTools/AudioCodecs/AudioCodecsBase.h"
39 void setBufferLength(
int len) { buffer_size = len; }
46 buffer.resize(buffer_size);
47 pcm.resize(MINIMP3_MAX_SAMPLES_PER_FRAME);
64 size_t write(
const uint8_t *data,
size_t len) {
65 LOGD(
"write: %zu", len);
67 if (buffer_pos+len>=buffer.size()){
70 assert(buffer_pos+len<buffer.size());
71 memcpy(buffer.data()+buffer_pos, data, len);
85 virtual operator bool() {
return active; }
87 void setSampleRateLimit(
int limit){
88 sample_rate_limit = limit;
94 mp3dec_frame_info_t mp3dec_info;
95 size_t buffer_size = 5 * 1024;
96 size_t buffer_pos = 0;
97 Vector<uint8_t> buffer;
98 Vector<mp3d_sample_t> pcm;
99 #ifdef MINIMP3_FLOAT_OUTPUT
100 Vector<int16_t> pcm16;
103 int sample_rate_limit = MINIMP3_MAX_SAMPLE_RATE;
107 LOGD(
"decode: %zd ", buffer_pos);
108 int open = buffer_pos;
113 samples = ::mp3dec_decode_frame(&mp3d, buffer.data()+processed, open,
114 pcm.data(), &mp3dec_info);
115 LOGD(
"frame_offset: %d - frame_bytes: %d -> samples %d", mp3dec_info.frame_offset, mp3dec_info.frame_bytes, samples);
116 open -= mp3dec_info.frame_bytes;
117 processed += mp3dec_info.frame_bytes;
123 }
while(processed < write_len);
127 memmove(buffer.data(),buffer.data()+processed, open);
132 LOGD(
"provideResult: %d samples", samples);
134 tmp.
sample_rate = mp3dec_info.hz>sample_rate_limit ? sample_rate_limit : mp3dec_info.hz;
135 tmp.
channels = mp3dec_info.channels;
141 notifyAudioChange(tmp);
147 if (out !=
nullptr) {
148 #ifdef MINIMP3_FLOAT_OUTPUT
149 pcm16.resize(samples);
150 f32_to_s16(pcm.data(), pcm16.data(), samples);
151 out->write((uint8_t *)pcm16.data(), samples *
sizeof(int16_t));
153 out->write((uint8_t *)pcm.data(), samples *
sizeof(mp3d_sample_t));
158 void f32_to_s16(
float *in, int16_t *out,
int num_samples) {
160 for(; i < num_samples; i++){
161 float sample = in[i] * 32768.0f;
162 if (sample >= 32766.5f)
163 out[i] = (int16_t) 32767;
164 else if (sample <= -32767.5f)
165 out[i] = (int16_t)-32768;
167 int16_t s = (int16_t)(sample + .5f);