Arduino LibHelix
Loading...
Searching...
No Matches
MP3DecoderHelix.h
1#pragma once
2
3#include "CommonHelix.h"
4#include "libhelix-mp3/mp3common.h"
5#include "libhelix-mp3/mp3dec.h"
6
7
8namespace libhelix {
9
10typedef void (*MP3InfoCallback)(MP3FrameInfo &info, void *ref);
11typedef void (*MP3DataCallback)(MP3FrameInfo &info, short *pcm_buffer,
12 size_t len, void *ref);
13
14enum MP3Type { MP3Normal = 0, MP3SelfContaind = 1 };
15
24 public:
26 this->mp3_type = MP3Normal;
27 setMinFrameBufferSize(MP3_MIN_FRAME_SIZE);
28 memset(&mp3FrameInfo, 0, sizeof(MP3FrameInfo));
29 }
30
31#if defined(ARDUINO) || defined(HELIX_PRINT)
32 MP3DecoderHelix(Print &output, MP3Type mp3Type = MP3Normal) {
33 this->out = &output;
34 this->mp3_type = mp3Type;
35 setMinFrameBufferSize(MP3_MIN_FRAME_SIZE);
36 memset(&mp3FrameInfo, 0, sizeof(MP3FrameInfo));
37 }
38#endif
39 MP3DecoderHelix(MP3DataCallback dataCallback, MP3Type mp3Type = MP3Normal) {
40 this->pcmCallback = dataCallback;
41 this->mp3_type = mp3Type;
42 setMinFrameBufferSize(MP3_MIN_FRAME_SIZE);
43 memset(&mp3FrameInfo, 0, sizeof(MP3FrameInfo));
44 }
45
46 MP3DecoderHelix(MP3Type mp3Type) {
47 this->mp3_type = mp3Type;
48 setMinFrameBufferSize(MP3_MIN_FRAME_SIZE);
49 memset(&mp3FrameInfo, 0, sizeof(MP3FrameInfo));
50 }
51
52 virtual ~MP3DecoderHelix() { end(); }
53
54 void setInfoCallback(MP3InfoCallback cb, void *caller = nullptr) {
55 this->infoCallback = cb;
56 if (caller!=nullptr)
57 p_caller_ref = caller;
58 }
59
60 void setDataCallback(MP3DataCallback cb) { this->pcmCallback = cb; }
61
63 MP3FrameInfo audioInfo() { return mp3FrameInfo; }
64
66 void end() override {
67 LOGD_HELIX( "end");
68
69 if (decoder != nullptr) {
70 flush();
71 MP3FreeDecoder(decoder);
72 decoder = nullptr;
73 }
75 memset(&mp3FrameInfo, 0, sizeof(MP3FrameInfo));
76 }
77
79 size_t maxFrameSize() override {
80 return max_frame_size == 0 ? MP3_MAX_FRAME_SIZE : max_frame_size;
81 }
82
84 size_t maxPCMSize() override {
85 return max_pcm_size == 0 ? MP3_MAX_OUTPUT_SIZE : max_pcm_size;
86 }
87
88 protected:
90 const uint8_t *endSyncWord() override {
91 static const uint8_t marker[END_MARKER_LEN] = {0xFF, 0xFB, 0x90, 0x00};
92 return marker;
93 }
94
95 HMP3Decoder decoder = nullptr;
96 MP3DataCallback pcmCallback = nullptr;
97 MP3InfoCallback infoCallback = nullptr;
98 MP3Type mp3_type;
99 MP3FrameInfo mp3FrameInfo;
100 void *p_caller_data = nullptr;
101
103 virtual bool allocateDecoder() override {
104 if (decoder == nullptr) {
105 decoder = MP3InitDecoder();
106 }
107 memset(&mp3FrameInfo, 0, sizeof(MP3FrameInfo));
108 return decoder != nullptr;
109 }
110
113 int findSynchWord(int offset = 0) override {
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;
118 // report the position relative to the start of the buffer
119 return result + offset;
120 }
121
125 int decode() override {
126 int available = frame_buffer.available();
127 int bytes_left = decodableBytes();
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(),
131 mp3_type);
132 if (rc == 0) {
133 int processed = data - frame_buffer.data();
134 // return the decoded result
135 MP3FrameInfo info;
136 MP3GetLastFrameInfo(decoder, &info);
137 provideResult(info);
138 rc = processed;
139 } else {
140 LOGI_HELIX( "MP3Decode rc: %d", rc);
141 }
142 return rc;
143 }
144
145 // return the resulting PCM data
146 void provideResult(MP3FrameInfo &info) {
147 // increase PCM size if this fails
148 assert(info.outputSamps * sizeof(short) < maxPCMSize());
149
150 LOGD_HELIX( "=> provideResult: %d", info.outputSamps);
151 if (info.outputSamps > 0) {
152 // provide result
153 if (pcmCallback != nullptr) {
154 // output via callback
155 pcmCallback(info, (short *)pcm_buffer.data(), info.outputSamps,
156 p_caller_data);
157 } else {
158 // output to stream
159 if (infoCallback != nullptr
160 && (info.samprate != mp3FrameInfo.samprate || info.nChans != mp3FrameInfo.nChans)) {
161 infoCallback(info, p_caller_ref);
162 }
163#if defined(ARDUINO) || defined(HELIX_PRINT)
164 if (out != nullptr){
165 int sampleSize = info.bitsPerSample / 8;
166 int toWrite = info.outputSamps * sampleSize;
167 writeToOut((uint8_t *)pcm_buffer.data(), toWrite);
168 }
169#endif
170 }
171 mp3FrameInfo = info;
172 }
173 }
174};
175
176} // namespace libhelix
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