Created
December 8, 2023 07:54
-
-
Save zhuker/9f66b0029137d4dbb8fc761e4858b1b4 to your computer and use it in GitHub Desktop.
simple mp3 encoder c++
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <lame/lame.h> | |
struct Mp3Encoder { | |
lame_global_flags *lame = nullptr; | |
~Mp3Encoder() { | |
if (lame) { | |
lame_close(lame); | |
lame = nullptr; | |
} | |
} | |
bool encodeMono(const float *samples, const int num_samples, | |
std::vector<unsigned char> &mp3Buffer) { | |
mp3Buffer.resize(1.25 * num_samples + 7200); | |
int result = lame_encode_buffer_ieee_float( | |
lame, | |
samples, // Input wave frames | |
nullptr, | |
num_samples, // Number of samples in waveFrames | |
mp3Buffer.data(), // Output buffer for encoded MP3 data | |
mp3Buffer.size() // Size of the output buffer | |
); | |
if (result < 0) { | |
return false; | |
} | |
mp3Buffer.resize(result); | |
return true; | |
} | |
static Mp3Encoder mono48k() { | |
lame_global_flags *lame = lame_init(); | |
assert(lame); | |
lame_set_quality(lame, 2); | |
lame_set_in_samplerate(lame, 48000); | |
lame_set_num_channels(lame, 1); | |
lame_init_params(lame); | |
return {lame}; | |
} | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment