4#include "liblame/lame.h"
10typedef void (*MP3CallbackFDK)(uint8_t *mp3_data,
size_t len);
19 int sample_rate = 16000;
21 int bits_per_sample = 16;
41 LOG_LAME(Debug, __FUNCTION__);
47 LOG_LAME(Debug, __FUNCTION__);
48 this->MP3Callback = cb;
56 LOG_LAME(Debug, __FUNCTION__);
57 this->out = &out_stream;
62 LOG_LAME(Debug, __FUNCTION__);
63 this->out = &out_stream;
69 LOG_LAME(Debug, __FUNCTION__);
72 if (convert_buffer !=
nullptr)
73 delete[] convert_buffer;
74 if (mp3_buffer !=
nullptr)
82 LOG_LAME(Debug, __FUNCTION__);
93 LOG_LAME(Debug, __FUNCTION__);
106 void begin(
int input_channels,
int input_sample_rate,
107 int input_bits_per_sample) {
108 LOG_LAME(Debug, __FUNCTION__);
110 ai.channels = input_channels;
111 ai.sample_rate = input_sample_rate;
112 ai.bits_per_sample = input_bits_per_sample;
124 int32_t
write(
void *pcm_samples,
int bytes) {
127 LOG_LAME(Debug,
"write %d bytes", bytes);
130 short *buffer = convertToShort(pcm_samples, bytes);
131 if (buffer ==
nullptr) {
136 int nsamples = bytes / (info.bits_per_sample / 8) / info.channels;
137 int outbuf_size = 7200 + (1.25 * nsamples);
138 if (!setupOutputBuffer(outbuf_size)) {
144 if (info.channels == 1) {
146 lame_encode_buffer(lame, buffer, NULL, nsamples, mp3_buffer, 0);
148 mp3_len = lame_encode_buffer_interleaved(lame, buffer, nsamples,
162 LOG_LAME(Debug, __FUNCTION__);
167 operator boolean() {
return active; }
171 MP3CallbackFDK MP3Callback =
nullptr;
174 lame_t lame =
nullptr;
176 uint8_t *mp3_buffer =
nullptr;
177 int mp3_buffer_size = 0;
179 short *convert_buffer =
nullptr;
180 int convert_buffer_size = 0;
186 bool setupOutputBuffer(
int size) {
187 if (size > mp3_buffer_size) {
188 LOG_LAME(Debug, __FUNCTION__);
189 if (mp3_buffer !=
nullptr)
191 mp3_buffer =
new uint8_t[size];
192 mp3_buffer_size = size;
194 return mp3_buffer !=
nullptr;
198 LOG_LAME(Debug, __FUNCTION__);
202 if (lame ==
nullptr) {
203 LOG_LAME(Error,
"lame_init failed");
207 lame_set_VBR(lame, vbr_default);
208 lame_set_num_channels(lame, info.channels);
209 if (info.channels == 1) {
210 lame_set_mode(lame, MONO);
212 lame_set_in_samplerate(lame, info.sample_rate);
213 lame_set_quality(lame, info.quality);
215 int initRet = lame_init_params(lame);
217 LOG_LAME(Error,
"lame_init_params\n");
221 info.frame_size = lame_get_framesize(lame);
222 LOG_LAME(Info,
"Framesize = %d\n", info.frame_size);
226 short *convertToShort(
void *pcm_samples,
int bytes) {
227 int bytes_per_sample = info.bits_per_sample / 8;
229 if (
sizeof(
short) == bytes_per_sample) {
230 return (
short *)pcm_samples;
234 LOG_LAME(Debug, __FUNCTION__);
237 int samples = bytes / bytes_per_sample;
238 if (convert_buffer ==
nullptr || samples > convert_buffer_size) {
239 if (convert_buffer !=
nullptr) {
240 delete[] convert_buffer;
242 convert_buffer =
new short[samples];
243 convert_buffer_size = samples;
246 if (convert_buffer ==
nullptr) {
247 LOG_LAME(Error,
"not enough memory to allocate conversion buffer - decrise "
248 "the size of written bytes!")
254 switch (info.bits_per_sample) {
256 int8_t *ptr = (int8_t *)pcm_samples;
257 for (
int j = 0; j < samples; j++) {
258 convert_buffer[j] = ptr[j];
260 return convert_buffer;
263 int16_t *ptr = (int16_t *)pcm_samples;
264 for (
int j = 0; j < samples; j++) {
265 convert_buffer[j] = ptr[j];
267 return convert_buffer;
270 int32_t *ptr = (int32_t *)pcm_samples;
271 for (
int j = 0; j < samples; j++) {
272 convert_buffer[j] = ptr[j];
274 return convert_buffer;
277 LOG_LAME(Error,
"Unsupported bits_per_sample: %d", info.bits_per_sample);
285 LOG_LAME(Debug,
"provideResult: %zu samples", bytes);
287 if (MP3Callback !=
nullptr) {
289 MP3Callback(data, bytes);
292 if (out !=
nullptr) {
293 out->write((uint8_t *)data, bytes);
Encodes PCM data to the MP3 format and writes the result to a stream or provides it via a callback.
Definition: MP3EncoderLAME.h:32
void begin()
Opens the encoder.
Definition: MP3EncoderLAME.h:81
void setOutput(Print &out_stream)
Defines the output stream.
Definition: MP3EncoderLAME.h:61
MP3EncoderLAME(Print &out_stream)
Definition: MP3EncoderLAME.h:55
void begin(int input_channels, int input_sample_rate, int input_bits_per_sample)
Opens the encoder.
Definition: MP3EncoderLAME.h:106
void begin(AudioInfo in)
Opens the encoder.
Definition: MP3EncoderLAME.h:92
MP3EncoderLAME(MP3CallbackFDK cb)
Constructor which provides the decoded result in a callback.
Definition: MP3EncoderLAME.h:40
int32_t write(void *pcm_samples, int bytes)
write PCM data to be converted to MP3 - The size is in bytes
Definition: MP3EncoderLAME.h:124
AudioInfo audioInfo()
Provides the audio information.
Definition: MP3EncoderLAME.h:121
void provideResult(uint8_t *data, size_t bytes)
return the result PWM data
Definition: MP3EncoderLAME.h:283
MP3EncoderLAME()
Definition: MP3EncoderLAME.h:37
void setDataCallback(MP3CallbackFDK cb)
Defines the callback which receives the encded MP3 data.
Definition: MP3EncoderLAME.h:46
void setAudioInfo(AudioInfo in)
Defines the audio information.
Definition: MP3EncoderLAME.h:118
void end()
closes the processing and release resources
Definition: MP3EncoderLAME.h:161
LAME parameters.
Definition: MP3EncoderLAME.h:16