3#include "CommonHelix.h"
4#include "libhelix-mp3/mp3common.h"
5#include "libhelix-mp3/mp3dec.h"
10typedef void (*MP3InfoCallback)(MP3FrameInfo &info,
void *ref);
11typedef void (*MP3DataCallback)(MP3FrameInfo &info,
short *pcm_buffer,
12 size_t len,
void *ref);
14enum MP3Type { MP3Normal = 0, MP3SelfContaind = 1 };
26 this->mp3_type = MP3Normal;
28 memset(&mp3FrameInfo, 0,
sizeof(MP3FrameInfo));
31#if defined(ARDUINO) || defined(HELIX_PRINT)
34 this->mp3_type = mp3Type;
36 memset(&mp3FrameInfo, 0,
sizeof(MP3FrameInfo));
39 MP3DecoderHelix(MP3DataCallback dataCallback, MP3Type mp3Type = MP3Normal) {
40 this->pcmCallback = dataCallback;
41 this->mp3_type = mp3Type;
43 memset(&mp3FrameInfo, 0,
sizeof(MP3FrameInfo));
47 this->mp3_type = mp3Type;
49 memset(&mp3FrameInfo, 0,
sizeof(MP3FrameInfo));
54 void setInfoCallback(MP3InfoCallback cb,
void *caller =
nullptr) {
55 this->infoCallback = cb;
57 p_caller_ref = caller;
60 void setDataCallback(MP3DataCallback cb) { this->pcmCallback = cb; }
69 if (decoder !=
nullptr) {
71 MP3FreeDecoder(decoder);
75 memset(&mp3FrameInfo, 0,
sizeof(MP3FrameInfo));
80 return max_frame_size == 0 ? MP3_MAX_FRAME_SIZE : max_frame_size;
85 return max_pcm_size == 0 ? MP3_MAX_OUTPUT_SIZE : max_pcm_size;
91 static const uint8_t marker[END_MARKER_LEN] = {0xFF, 0xFB, 0x90, 0x00};
95 HMP3Decoder decoder =
nullptr;
96 MP3DataCallback pcmCallback =
nullptr;
97 MP3InfoCallback infoCallback =
nullptr;
99 MP3FrameInfo mp3FrameInfo;
100 void *p_caller_data =
nullptr;
104 if (decoder ==
nullptr) {
105 decoder = MP3InitDecoder();
107 memset(&mp3FrameInfo, 0,
sizeof(MP3FrameInfo));
108 return decoder !=
nullptr;
114 if (offset > frame_buffer.available())
return -1;
115 int result = MP3FindSyncWord(frame_buffer.data() + offset,
116 frame_buffer.available() - offset);
117 if (result < 0)
return result;
119 return result + offset;
126 int available = frame_buffer.available();
128 LOGI_HELIX(
"decode: %d (left:%d)", available, bytes_left);
129 uint8_t *data = frame_buffer.data();
130 int rc = MP3Decode(decoder, &data, &bytes_left, (
short *)pcm_buffer.data(),
133 int processed = data - frame_buffer.data();
136 MP3GetLastFrameInfo(decoder, &info);
140 LOGI_HELIX(
"MP3Decode rc: %d", rc);
146 void provideResult(MP3FrameInfo &info) {
148 assert(info.outputSamps *
sizeof(
short) <
maxPCMSize());
150 LOGD_HELIX(
"=> provideResult: %d", info.outputSamps);
151 if (info.outputSamps > 0) {
153 if (pcmCallback !=
nullptr) {
155 pcmCallback(info, (
short *)pcm_buffer.data(), info.outputSamps,
159 if (infoCallback !=
nullptr
160 && (info.samprate != mp3FrameInfo.samprate || info.nChans != mp3FrameInfo.nChans)) {
161 infoCallback(info, p_caller_ref);
163#if defined(ARDUINO) || defined(HELIX_PRINT)
165 int sampleSize = info.bitsPerSample / 8;
166 int toWrite = info.outputSamps * sampleSize;
167 writeToOut((uint8_t *)pcm_buffer.data(), toWrite);
Common Simple Arduino API.
Definition CommonHelix.h:33
void flush()
Decodes all open packets, including the last frame. Call it at the end of the stream....
Definition CommonHelix.h:109
virtual void end()
Releases the reserved memory.
Definition CommonHelix.h:63
virtual void setMinFrameBufferSize(int size)
Definition CommonHelix.h:328
int decodableBytes()
the bytes that the decoders may use for a frame
Definition CommonHelix.h:160
A simple Arduino API for the libhelix MP3 decoder. The data is provided with the help of write() call...
Definition MP3DecoderHelix.h:23
void end() override
Releases the reserved memory.
Definition MP3DecoderHelix.h:66
int findSynchWord(int offset=0) override
Definition MP3DecoderHelix.h:113
size_t maxFrameSize() override
determines the frame buffer size that will be allocated
Definition MP3DecoderHelix.h:79
MP3FrameInfo audioInfo()
Provides the last available MP3FrameInfo.
Definition MP3DecoderHelix.h:63
int decode() override
Definition MP3DecoderHelix.h:125
size_t maxPCMSize() override
Determines the pcm buffer size that will be allocated.
Definition MP3DecoderHelix.h:84
virtual bool allocateDecoder() override
Allocate the decoder.
Definition MP3DecoderHelix.h:103
const uint8_t * endSyncWord() override
the end marker appended by flush(): a valid MPEG-1 Layer III header
Definition MP3DecoderHelix.h:90