2023-07-16 23:36:25 +00:00
|
|
|
#include "flactomp3.h"
|
|
|
|
|
2023-07-23 12:04:26 +00:00
|
|
|
#include <cmath>
|
|
|
|
|
2023-07-18 23:05:32 +00:00
|
|
|
constexpr uint16_t flacDefaultMaxBlockSize = 4096;
|
2023-07-23 12:04:26 +00:00
|
|
|
constexpr uint16_t id3v1TagSize = 128;
|
|
|
|
// constexpr std::wstring_view BOM ({(0xFEFF}); //one day...
|
|
|
|
|
|
|
|
constexpr std::string_view title ("TITLE=");
|
|
|
|
constexpr std::string_view artist ("ARTIST=");
|
|
|
|
constexpr std::string_view album ("ALBUM=");
|
|
|
|
constexpr std::string_view comment ("COMMENT=");
|
|
|
|
constexpr std::string_view genre ("GENRE=");
|
|
|
|
constexpr std::string_view track ("TRACKNUMBER=");
|
|
|
|
constexpr std::string_view date ("DATE=");
|
2023-07-16 23:36:25 +00:00
|
|
|
|
2023-07-23 12:04:26 +00:00
|
|
|
constexpr std::string_view jpeg ("image/jpeg");
|
2023-07-17 21:29:59 +00:00
|
|
|
|
2023-07-18 23:05:32 +00:00
|
|
|
|
2023-07-17 21:29:59 +00:00
|
|
|
static const std::map<std::string, const std::string> knownTags {
|
|
|
|
{"ALBUMARTIST=", "TPE2="},
|
|
|
|
{"PUBLISHER=", "TPUB="},
|
|
|
|
{"LENGTH=", "TLEN="},
|
|
|
|
{"ISRC=", "TSRC="},
|
|
|
|
{"DISCNUMBER=", "TPOS="},
|
2023-07-23 12:04:26 +00:00
|
|
|
{"BPM=", "TBPM="},
|
|
|
|
{"LYRICS=", "USLT="} //but it's not supported in LAME
|
2023-07-17 21:29:59 +00:00
|
|
|
};
|
|
|
|
|
2023-07-23 12:04:26 +00:00
|
|
|
FLACtoMP3::FLACtoMP3(Severity severity, uint8_t size) :
|
|
|
|
Loggable(severity),
|
2023-07-16 23:36:25 +00:00
|
|
|
inPath(),
|
|
|
|
outPath(),
|
|
|
|
decoder(FLAC__stream_decoder_new()),
|
|
|
|
encoder(lame_init()),
|
|
|
|
statusFLAC(),
|
2023-07-17 21:29:59 +00:00
|
|
|
output(nullptr),
|
2023-07-18 23:05:32 +00:00
|
|
|
bufferMultiplier(size),
|
|
|
|
flacMaxBlockSize(0),
|
2023-07-16 23:36:25 +00:00
|
|
|
pcmCounter(0),
|
2023-07-18 23:05:32 +00:00
|
|
|
pcmSize(0),
|
|
|
|
pcm(nullptr),
|
|
|
|
outputBuffer(nullptr),
|
|
|
|
outputBufferSize(0),
|
|
|
|
outputInitilized(false),
|
2023-07-23 12:04:26 +00:00
|
|
|
downscaleAlbumArt(false),
|
|
|
|
usc2convertor()
|
2023-07-16 23:36:25 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
FLACtoMP3::~FLACtoMP3() {
|
|
|
|
lame_close(encoder);
|
|
|
|
FLAC__stream_decoder_delete(decoder);
|
|
|
|
}
|
|
|
|
|
2023-07-19 22:12:18 +00:00
|
|
|
bool FLACtoMP3::run() {
|
2023-07-16 23:36:25 +00:00
|
|
|
FLAC__bool ok = FLAC__stream_decoder_process_until_end_of_stream(decoder);
|
2023-07-23 12:04:26 +00:00
|
|
|
uint32_t fileSize;
|
2023-07-17 21:29:59 +00:00
|
|
|
if (ok) {
|
2023-07-19 22:12:18 +00:00
|
|
|
if (pcmCounter > 0)
|
|
|
|
flush();
|
2023-07-17 21:29:59 +00:00
|
|
|
|
|
|
|
int nwrite = lame_encode_flush(encoder, outputBuffer, pcmSize * 2);
|
2023-07-18 23:05:32 +00:00
|
|
|
fwrite((char*)outputBuffer, nwrite, 1, output);
|
|
|
|
|
|
|
|
if (downscaleAlbumArt) {
|
2023-07-23 12:04:26 +00:00
|
|
|
fileSize = ftell(output);
|
2023-07-18 23:05:32 +00:00
|
|
|
lame_mp3_tags_fid(encoder, output);
|
|
|
|
} else {
|
2023-07-23 12:04:26 +00:00
|
|
|
int tag1Size = lame_get_id3v1_tag(encoder, outputBuffer, id3v1TagSize);
|
|
|
|
if (tag1Size > id3v1TagSize)
|
|
|
|
log(warning, "couldn't write id3v1 tag");
|
2023-07-18 23:05:32 +00:00
|
|
|
else
|
|
|
|
fwrite((char*)outputBuffer, tag1Size, 1, output);
|
|
|
|
|
2023-07-23 12:04:26 +00:00
|
|
|
fileSize = ftell(output);
|
2023-07-18 23:05:32 +00:00
|
|
|
fseek(output, 0, SEEK_SET);
|
|
|
|
int tag2Size = lame_get_id3v2_tag(encoder, outputBuffer, outputBufferSize);
|
|
|
|
if (tag2Size > outputBufferSize) {
|
2023-07-23 12:04:26 +00:00
|
|
|
log(Loggable::error, "couldn't write id3v2 tag");
|
2023-07-18 23:05:32 +00:00
|
|
|
} else
|
|
|
|
fwrite((char*)outputBuffer, tag2Size, 1, output);
|
|
|
|
|
|
|
|
int vbrTagSize = lame_get_lametag_frame(encoder, outputBuffer, outputBufferSize);
|
|
|
|
if (vbrTagSize > outputBufferSize)
|
2023-07-23 12:04:26 +00:00
|
|
|
log(Loggable::error, "couldn't write vbr tag");
|
2023-07-18 23:05:32 +00:00
|
|
|
|
|
|
|
fwrite((char*)outputBuffer, vbrTagSize, 1, output);
|
|
|
|
}
|
2023-07-17 21:29:59 +00:00
|
|
|
}
|
2023-07-16 23:36:25 +00:00
|
|
|
|
2023-07-19 22:12:18 +00:00
|
|
|
// std::cout << " state: " << FLAC__StreamDecoderStateString[FLAC__stream_decoder_get_state(decoder)] << std::endl;
|
2023-07-16 23:36:25 +00:00
|
|
|
|
2023-07-17 21:29:59 +00:00
|
|
|
if (outputInitilized) {
|
|
|
|
fclose(output);
|
|
|
|
output = nullptr;
|
2023-07-18 23:05:32 +00:00
|
|
|
|
|
|
|
delete[] pcm;
|
|
|
|
delete[] outputBuffer;
|
|
|
|
|
|
|
|
pcm = nullptr;
|
|
|
|
outputBuffer = nullptr;
|
|
|
|
pcmSize = 0;
|
|
|
|
flacMaxBlockSize = 0;
|
|
|
|
outputBufferSize = 0;
|
2023-07-23 12:04:26 +00:00
|
|
|
if (ok) {
|
|
|
|
float MBytes = (float)fileSize / 1024 / 1024;
|
|
|
|
std::string strMBytes = std::to_string(MBytes);
|
|
|
|
strMBytes = strMBytes.substr(0, strMBytes.find(".") + 3) + " MiB";
|
|
|
|
log(info, "resulting file size: " + strMBytes);
|
|
|
|
}
|
2023-07-19 22:12:18 +00:00
|
|
|
|
|
|
|
return ok;
|
2023-07-17 21:29:59 +00:00
|
|
|
}
|
2023-07-19 22:12:18 +00:00
|
|
|
|
|
|
|
return false;
|
2023-07-16 23:36:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void FLACtoMP3::setInputFile(const std::string& path) {
|
|
|
|
if (inPath.size() > 0)
|
|
|
|
throw 1;
|
|
|
|
|
|
|
|
inPath = path;
|
|
|
|
|
|
|
|
FLAC__stream_decoder_set_md5_checking(decoder, true);
|
2023-07-17 21:29:59 +00:00
|
|
|
FLAC__stream_decoder_set_metadata_respond_all(decoder);
|
2023-07-16 23:36:25 +00:00
|
|
|
statusFLAC = FLAC__stream_decoder_init_file(decoder, path.c_str(), write, metadata, error, this);
|
|
|
|
}
|
|
|
|
|
|
|
|
void FLACtoMP3::setOutputFile(const std::string& path) {
|
|
|
|
if (outPath.size() > 0)
|
|
|
|
throw 2;
|
|
|
|
|
|
|
|
outPath = path;
|
|
|
|
|
|
|
|
lame_set_VBR(encoder, vbr_default);
|
2023-07-17 21:29:59 +00:00
|
|
|
lame_set_VBR_quality(encoder, 0);
|
|
|
|
lame_set_quality(encoder, 0);
|
2023-07-18 23:05:32 +00:00
|
|
|
if (!downscaleAlbumArt)
|
|
|
|
lame_set_write_id3tag_automatic(encoder, 0);
|
2023-07-17 21:29:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool FLACtoMP3::initializeOutput() {
|
|
|
|
if (outputInitilized)
|
|
|
|
throw 5;
|
|
|
|
|
|
|
|
output = fopen(outPath.c_str(), "w+b");
|
|
|
|
if (output == 0) {
|
|
|
|
output = nullptr;
|
2023-07-23 12:04:26 +00:00
|
|
|
log(fatal, "Error opening file " + outPath);
|
2023-07-17 21:29:59 +00:00
|
|
|
return false;
|
|
|
|
}
|
2023-07-16 23:36:25 +00:00
|
|
|
|
|
|
|
int ret = lame_init_params(encoder);
|
|
|
|
if (ret < 0) {
|
2023-07-23 12:04:26 +00:00
|
|
|
log(fatal, "Error initializing LAME parameters. Code = " + std::to_string(ret));
|
2023-07-17 21:29:59 +00:00
|
|
|
fclose(output);
|
|
|
|
output = nullptr;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2023-07-18 23:05:32 +00:00
|
|
|
if (flacMaxBlockSize == 0)
|
|
|
|
flacMaxBlockSize = flacDefaultMaxBlockSize;
|
|
|
|
|
|
|
|
pcmSize = lame_get_num_channels(encoder) * flacMaxBlockSize * bufferMultiplier;
|
|
|
|
outputBufferSize = pcmSize / 2;
|
|
|
|
|
|
|
|
if (!downscaleAlbumArt) {
|
|
|
|
int tag2Size = lame_get_id3v2_tag(encoder, nullptr, 0);
|
|
|
|
int vbrTagSize = lame_get_lametag_frame(encoder, nullptr, 0);
|
|
|
|
|
|
|
|
fseek(output, tag2Size + vbrTagSize, SEEK_SET); //reserve place for tags
|
|
|
|
if (tag2Size > outputBufferSize)
|
|
|
|
outputBufferSize = tag2Size;
|
|
|
|
}
|
|
|
|
|
|
|
|
pcm = new int16_t[pcmSize];
|
|
|
|
outputBuffer = new uint8_t[outputBufferSize];
|
|
|
|
|
2023-07-17 21:29:59 +00:00
|
|
|
outputInitilized = true;
|
2023-07-18 23:05:32 +00:00
|
|
|
|
2023-07-17 21:29:59 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void FLACtoMP3::processInfo(const FLAC__StreamMetadata_StreamInfo& info) {
|
|
|
|
lame_set_in_samplerate(encoder, info.sample_rate);
|
|
|
|
lame_set_num_channels(encoder, info.channels);
|
2023-07-18 23:05:32 +00:00
|
|
|
flacMaxBlockSize = info.max_blocksize;
|
2023-07-23 12:04:26 +00:00
|
|
|
log(Loggable::info, "sample rate: " + std::to_string(info.sample_rate));
|
|
|
|
log(Loggable::info, "channels: " + std::to_string(info.channels));
|
|
|
|
log(Loggable::info, "bits per sample: " + std::to_string(info.bits_per_sample));
|
2023-07-17 21:29:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void FLACtoMP3::processTags(const FLAC__StreamMetadata_VorbisComment& tags) {
|
2023-07-21 21:32:24 +00:00
|
|
|
bool artistSet = false; //in vorbis comment there could be more then 1 artist tag
|
|
|
|
for (FLAC__uint32 i = 0; i < tags.num_comments; ++i) { //usualy it's about guested and fetured artists
|
|
|
|
const FLAC__StreamMetadata_VorbisComment_Entry& entry = tags.comments[i]; //gonna keep only the first one for now
|
2023-07-17 21:29:59 +00:00
|
|
|
std::string_view comm((const char*)entry.entry);
|
|
|
|
if (comm.find(title) == 0) {
|
2023-07-23 12:04:26 +00:00
|
|
|
setTagTitle(comm.substr(title.size()));
|
2023-07-17 21:29:59 +00:00
|
|
|
} else if (comm.find(artist) == 0) {
|
2023-07-21 21:32:24 +00:00
|
|
|
if (!artistSet) {
|
2023-07-23 12:04:26 +00:00
|
|
|
setTagArtist(comm.substr(artist.size()));
|
2023-07-21 21:32:24 +00:00
|
|
|
artistSet = true;
|
2023-07-23 12:04:26 +00:00
|
|
|
} else {
|
|
|
|
log(minor, "more than one artist tag, ignoring " + std::string(comm.substr(artist.size())));
|
2023-07-21 21:32:24 +00:00
|
|
|
}
|
2023-07-17 21:29:59 +00:00
|
|
|
} else if (comm.find(album) == 0) {
|
2023-07-23 12:04:26 +00:00
|
|
|
setTagAlbum(comm.substr(album.size()));
|
2023-07-17 21:29:59 +00:00
|
|
|
} else if (comm.find(comment) == 0) {
|
|
|
|
id3tag_set_comment(encoder, comm.substr(comment.size()).data());
|
|
|
|
} else if (comm.find(genre) == 0) {
|
|
|
|
id3tag_set_genre(encoder, comm.substr(genre.size()).data());
|
|
|
|
} else if (comm.find(track) == 0) {
|
|
|
|
id3tag_set_track(encoder, comm.substr(track.size()).data());
|
|
|
|
} else if (comm.find(date) == 0) {
|
|
|
|
std::string_view fullDate = comm.substr(date.size());
|
2023-07-23 12:04:26 +00:00
|
|
|
if (fullDate.size() == 10) { //1999-11-11 kind of string
|
2023-07-17 21:29:59 +00:00
|
|
|
std::string_view month = fullDate.substr(5, std::size_t(2));
|
|
|
|
std::string_view day = fullDate.substr(8);
|
2023-07-23 12:04:26 +00:00
|
|
|
std::string md = "TDAT=" + std::string(day) + std::string(month);
|
2023-07-17 21:29:59 +00:00
|
|
|
int res = id3tag_set_fieldvalue(encoder, md.c_str());
|
|
|
|
if (res != 0)
|
2023-07-23 12:04:26 +00:00
|
|
|
log(warning, "wasn't able to set the date tag (" + md + ")");
|
2023-07-17 21:29:59 +00:00
|
|
|
|
|
|
|
fullDate = fullDate.substr(0, 4); //year;
|
|
|
|
}
|
|
|
|
id3tag_set_year(encoder, std::string(fullDate).data());
|
|
|
|
} else if (!tryKnownTag(comm)) {
|
|
|
|
std::string tag = "TXXX=" + std::string(comm);
|
|
|
|
int res = id3tag_set_fieldvalue(encoder, tag.c_str());
|
|
|
|
if (res != 0)
|
2023-07-23 12:04:26 +00:00
|
|
|
log(warning, "wasn't able to set user tag (" + tag + ")");
|
2023-07-17 21:29:59 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-07-18 23:05:32 +00:00
|
|
|
void FLACtoMP3::processPicture(const FLAC__StreamMetadata_Picture& picture) {
|
|
|
|
if (downscaleAlbumArt && picture.data_length > LAME_MAXALBUMART) {
|
2023-07-23 12:04:26 +00:00
|
|
|
log(info, "embeded album art is too big (" + std::to_string(picture.data_length) + " bytes), rescaling");
|
|
|
|
log(debug, "mime type is " + std::string(picture.mime_type));
|
2023-07-18 23:05:32 +00:00
|
|
|
if (picture.mime_type == jpeg) {
|
2023-07-23 12:04:26 +00:00
|
|
|
if (scaleJPEG(picture))
|
|
|
|
log(debug, "successfully rescaled album art");
|
|
|
|
else
|
|
|
|
log(warning, "failed to rescale album art");
|
2023-07-18 23:05:32 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
int result = id3tag_set_albumart(encoder, (const char*)picture.data, picture.data_length);
|
2023-07-23 12:04:26 +00:00
|
|
|
if (result != 0)
|
|
|
|
log(warning, "couldn't set album art tag, errcode: " + std::to_string(result));
|
2023-07-18 23:05:32 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool FLACtoMP3::scaleJPEG(const FLAC__StreamMetadata_Picture& picture) {
|
|
|
|
struct jpeg_decompress_struct dinfo;
|
|
|
|
struct jpeg_error_mgr derr;
|
|
|
|
|
|
|
|
dinfo.err = jpeg_std_error(&derr);
|
|
|
|
jpeg_create_decompress(&dinfo);
|
|
|
|
jpeg_mem_src(&dinfo, picture.data, picture.data_length);
|
|
|
|
int rc = jpeg_read_header(&dinfo, TRUE);
|
|
|
|
|
|
|
|
if (rc != 1) {
|
2023-07-23 12:04:26 +00:00
|
|
|
log(Loggable::error, "error reading jpeg header");
|
2023-07-18 23:05:32 +00:00
|
|
|
return false;
|
|
|
|
}
|
2023-07-23 12:04:26 +00:00
|
|
|
uint64_t mem_size = 0;
|
|
|
|
uint8_t *mem = new uint8_t[LAME_MAXALBUMART + 1024 * 4]; //I allocate a little bit more not to corrupt someone else's the memory
|
2023-07-18 23:05:32 +00:00
|
|
|
|
|
|
|
dinfo.scale_num = 2; //need to tune it, feels like 500 by 500 is a good size
|
|
|
|
dinfo.scale_denom = 3;
|
|
|
|
jpeg_start_decompress(&dinfo);
|
|
|
|
|
|
|
|
struct jpeg_compress_struct cinfo;
|
|
|
|
struct jpeg_error_mgr cerr;
|
|
|
|
cinfo.err = jpeg_std_error(&cerr);
|
|
|
|
jpeg_create_compress(&cinfo);
|
|
|
|
jpeg_mem_dest(&cinfo, &mem, &mem_size);
|
|
|
|
|
|
|
|
cinfo.image_width = dinfo.output_width;
|
|
|
|
cinfo.image_height = dinfo.output_height;
|
|
|
|
cinfo.input_components = dinfo.output_components;
|
|
|
|
cinfo.in_color_space = dinfo.out_color_space;
|
|
|
|
jpeg_set_defaults(&cinfo);
|
|
|
|
|
|
|
|
jpeg_start_compress(&cinfo, TRUE);
|
|
|
|
|
|
|
|
uint32_t rowSize = dinfo.image_width * dinfo.output_components;
|
|
|
|
uint8_t* row = new uint8_t[rowSize];
|
|
|
|
|
2023-07-19 22:12:18 +00:00
|
|
|
while (dinfo.output_scanline < dinfo.output_height) {
|
|
|
|
jpeg_read_scanlines(&dinfo, &row, 1);
|
2023-07-18 23:05:32 +00:00
|
|
|
jpeg_write_scanlines(&cinfo, &row, 1);
|
2023-07-23 12:04:26 +00:00
|
|
|
|
|
|
|
if (mem_size > LAME_MAXALBUMART) {
|
|
|
|
delete[] mem;
|
|
|
|
log(Loggable::error, "resized album art exceeded reserved buffer size, stopping before memory corrution");
|
|
|
|
return false;
|
|
|
|
}
|
2023-07-19 22:12:18 +00:00
|
|
|
}
|
|
|
|
jpeg_finish_decompress(&dinfo);
|
|
|
|
jpeg_destroy_decompress(&dinfo);
|
2023-07-18 23:05:32 +00:00
|
|
|
|
|
|
|
jpeg_finish_compress(&cinfo);
|
|
|
|
jpeg_destroy_compress(&cinfo);
|
|
|
|
|
2023-07-19 22:12:18 +00:00
|
|
|
delete[] row;
|
2023-07-18 23:05:32 +00:00
|
|
|
|
|
|
|
int result = id3tag_set_albumart(encoder, (const char*)mem, mem_size);
|
|
|
|
delete[] mem;
|
|
|
|
|
|
|
|
return result == 0;
|
|
|
|
}
|
|
|
|
|
2023-07-17 21:29:59 +00:00
|
|
|
bool FLACtoMP3::tryKnownTag(std::string_view source) {
|
|
|
|
for (const std::pair<const std::string, const std::string>& pair : knownTags) {
|
|
|
|
if (source.find(pair.first) == 0) {
|
2023-07-23 12:04:26 +00:00
|
|
|
if (pair.first == "LYRICS=") {
|
|
|
|
log(warning, "lyrics tag was not transfered, it is not supported currently");
|
|
|
|
} else if (pair.first == "BPM=") {
|
|
|
|
float bpm = std::atof(source.substr(pair.first.size()).data());
|
|
|
|
bpm = std::round(bpm); //id3 bpm must be integer
|
|
|
|
int result = id3tag_set_fieldvalue(encoder, std::string(pair.second + std::to_string((int)bpm)).c_str());
|
|
|
|
if (result != 0)
|
|
|
|
log(warning, "wasn't able to set BPM tag. Code = " + std::to_string(result));
|
|
|
|
} else if (pair.first == "ALBUMARTIST=") {
|
|
|
|
int res = setTagUSC2("TPE2", source.substr(pair.first.size()));
|
|
|
|
if (res != 0)
|
|
|
|
log(warning, "wasn't able to album artist tag. Code = " + std::to_string(res));
|
|
|
|
} else {
|
|
|
|
std::string tag = pair.second + std::string(source.substr(pair.first.size()));
|
|
|
|
int res = id3tag_set_fieldvalue(encoder, tag.c_str());
|
|
|
|
if (res != 0)
|
|
|
|
log(warning, "wasn't able to set tag (" + std::string(source) + "). Code = " + std::to_string(res));
|
|
|
|
}
|
2023-07-17 21:29:59 +00:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
2023-07-16 23:36:25 +00:00
|
|
|
}
|
|
|
|
|
2023-07-17 21:29:59 +00:00
|
|
|
return false;
|
2023-07-16 23:36:25 +00:00
|
|
|
}
|
|
|
|
|
2023-07-17 21:29:59 +00:00
|
|
|
|
|
|
|
bool FLACtoMP3::decodeFrame(const int32_t * const buffer[], uint32_t size) {
|
|
|
|
if (!outputInitilized) {
|
|
|
|
bool success = initializeOutput();
|
|
|
|
if (!success)
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2023-07-16 23:36:25 +00:00
|
|
|
for (size_t i = 0; i < size; ++i) {
|
2023-07-18 23:05:32 +00:00
|
|
|
pcm[pcmCounter++] = (int16_t)buffer[0][i];
|
|
|
|
pcm[pcmCounter++] = (int16_t)buffer[1][i];
|
2023-07-16 23:36:25 +00:00
|
|
|
|
2023-07-18 23:05:32 +00:00
|
|
|
if (pcmCounter == pcmSize)
|
|
|
|
return flush();
|
|
|
|
}
|
2023-07-17 21:29:59 +00:00
|
|
|
|
|
|
|
return true;
|
2023-07-16 23:36:25 +00:00
|
|
|
}
|
|
|
|
|
2023-07-17 21:29:59 +00:00
|
|
|
bool FLACtoMP3::flush() {
|
2023-07-16 23:36:25 +00:00
|
|
|
int nwrite = lame_encode_buffer_interleaved(
|
|
|
|
encoder,
|
|
|
|
pcm,
|
2023-07-18 23:05:32 +00:00
|
|
|
pcmCounter / 2,
|
2023-07-17 21:29:59 +00:00
|
|
|
outputBuffer,
|
2023-07-18 23:05:32 +00:00
|
|
|
outputBufferSize
|
2023-07-16 23:36:25 +00:00
|
|
|
);
|
2023-07-18 23:05:32 +00:00
|
|
|
while (nwrite == -1) { //-1 is returned when there was not enough space in the given buffer
|
2023-07-23 12:04:26 +00:00
|
|
|
log(major, std::to_string(outputBufferSize) + " bytes in the output buffer wasn't enough");;
|
2023-07-18 23:05:32 +00:00
|
|
|
outputBufferSize = outputBufferSize * 2;
|
|
|
|
delete[] outputBuffer;
|
|
|
|
outputBuffer = new uint8_t[outputBufferSize];
|
2023-07-23 12:04:26 +00:00
|
|
|
log(major, "allocating " + std::to_string(outputBufferSize) + " bytes");
|
2023-07-18 23:05:32 +00:00
|
|
|
|
|
|
|
nwrite = lame_encode_buffer_interleaved(
|
|
|
|
encoder,
|
|
|
|
pcm,
|
|
|
|
pcmCounter,
|
|
|
|
outputBuffer,
|
|
|
|
outputBufferSize
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (nwrite > 0) {
|
|
|
|
int actuallyWritten = fwrite((char*)outputBuffer, nwrite, 1, output);
|
|
|
|
pcmCounter = 0;
|
|
|
|
return actuallyWritten == 1;
|
|
|
|
} else {
|
|
|
|
if (nwrite == 0) {
|
2023-07-23 12:04:26 +00:00
|
|
|
log(minor, "encoding flush encoded 0 bytes, skipping write");
|
2023-07-18 23:05:32 +00:00
|
|
|
return true;
|
|
|
|
} else {
|
2023-07-23 12:04:26 +00:00
|
|
|
log(fatal, "encoding flush failed. Code = : " + std::to_string(nwrite));
|
2023-07-18 23:05:32 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
2023-07-16 23:36:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void FLACtoMP3::metadata(const FLAC__StreamDecoder* decoder, const FLAC__StreamMetadata* metadata, void* client_data) {
|
2023-07-17 21:29:59 +00:00
|
|
|
(void)(decoder);
|
|
|
|
FLACtoMP3* self = static_cast<FLACtoMP3*>(client_data);
|
2023-07-16 23:36:25 +00:00
|
|
|
|
2023-07-17 21:29:59 +00:00
|
|
|
switch (metadata->type) {
|
|
|
|
case FLAC__METADATA_TYPE_STREAMINFO:
|
|
|
|
self->processInfo(metadata->data.stream_info);
|
|
|
|
break;
|
|
|
|
case FLAC__METADATA_TYPE_VORBIS_COMMENT:
|
|
|
|
self->processTags(metadata->data.vorbis_comment);
|
|
|
|
break;
|
2023-07-18 23:05:32 +00:00
|
|
|
case FLAC__METADATA_TYPE_PICTURE:
|
|
|
|
self->processPicture(metadata->data.picture);
|
|
|
|
break;
|
2023-07-17 21:29:59 +00:00
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
2023-07-16 23:36:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
FLAC__StreamDecoderWriteStatus FLACtoMP3::write(
|
|
|
|
const FLAC__StreamDecoder* decoder,
|
|
|
|
const FLAC__Frame* frame,
|
|
|
|
const FLAC__int32 * const buffer[],
|
|
|
|
void* client_data
|
2023-07-17 21:29:59 +00:00
|
|
|
) {
|
|
|
|
(void)(decoder);
|
2023-07-16 23:36:25 +00:00
|
|
|
// if (decoded->channels != 2 || decoded->depth != 16) {
|
|
|
|
// std::cout << "ERROR: this example only supports 16bit stereo streams" << std::endl;
|
|
|
|
// return FLAC__STREAM_DECODER_WRITE_STATUS_ABORT;
|
|
|
|
// }
|
2023-07-23 12:04:26 +00:00
|
|
|
FLACtoMP3* self = static_cast<FLACtoMP3*>(client_data);
|
2023-07-16 23:36:25 +00:00
|
|
|
if (frame->header.channels != 2) {
|
2023-07-23 12:04:26 +00:00
|
|
|
self->log(fatal, "ERROR: This frame contains " + std::to_string(frame->header.channels) + " channels (should be 2)");
|
2023-07-16 23:36:25 +00:00
|
|
|
return FLAC__STREAM_DECODER_WRITE_STATUS_ABORT;
|
|
|
|
}
|
|
|
|
if (buffer[0] == NULL) {
|
2023-07-23 12:04:26 +00:00
|
|
|
self->log(fatal, "ERROR: buffer [0] is NULL");
|
2023-07-16 23:36:25 +00:00
|
|
|
return FLAC__STREAM_DECODER_WRITE_STATUS_ABORT;
|
|
|
|
}
|
|
|
|
if (buffer[1] == NULL) {
|
2023-07-23 12:04:26 +00:00
|
|
|
self->log(fatal, "ERROR: buffer [1] is NULL");
|
2023-07-16 23:36:25 +00:00
|
|
|
return FLAC__STREAM_DECODER_WRITE_STATUS_ABORT;
|
|
|
|
}
|
|
|
|
|
2023-07-17 21:29:59 +00:00
|
|
|
bool result = self->decodeFrame(buffer, frame->header.blocksize);
|
2023-07-16 23:36:25 +00:00
|
|
|
|
2023-07-17 21:29:59 +00:00
|
|
|
if (result)
|
|
|
|
return FLAC__STREAM_DECODER_WRITE_STATUS_CONTINUE;
|
|
|
|
else
|
|
|
|
return FLAC__STREAM_DECODER_WRITE_STATUS_ABORT;
|
2023-07-16 23:36:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void FLACtoMP3::error(const FLAC__StreamDecoder* decoder, FLAC__StreamDecoderErrorStatus status, void* client_data) {
|
2023-07-23 12:04:26 +00:00
|
|
|
(void)decoder;
|
|
|
|
FLACtoMP3* self = static_cast<FLACtoMP3*>(client_data);
|
|
|
|
std::string errText(FLAC__StreamDecoderErrorStatusString[status]);
|
|
|
|
self->log(Loggable::error, "Got error callback: " + errText);
|
|
|
|
}
|
|
|
|
|
|
|
|
void FLACtoMP3::setTagTitle(const std::string_view& title) {
|
|
|
|
id3tag_set_title(encoder, title.data()); //to set at least some possibly encoding broken id3v1 tag
|
|
|
|
int res = setTagUSC2("TIT2", title);
|
|
|
|
if (res != 0)
|
|
|
|
log(warning, "Couldn't set Title tag. Code = " + std::to_string(res));
|
|
|
|
}
|
|
|
|
|
|
|
|
void FLACtoMP3::setTagAlbum(const std::string_view& album) {
|
|
|
|
id3tag_set_album(encoder, album.data()); //to set at least some possibly encoding broken id3v1 tag
|
|
|
|
int res = setTagUSC2("TALB", album);
|
|
|
|
if (res != 0)
|
|
|
|
log(warning, "Couldn't set Album tag. Code = " + std::to_string(res));
|
|
|
|
}
|
|
|
|
|
|
|
|
void FLACtoMP3::setTagArtist(const std::string_view& artist) {
|
|
|
|
id3tag_set_artist(encoder, artist.data()); //to set at least some possibly encoding broken id3v1 tag
|
|
|
|
int res = setTagUSC2("TPE1", artist);
|
|
|
|
if (res != 0)
|
|
|
|
log(warning, "Couldn't set Artist tag. Code = " + std::to_string(res));
|
|
|
|
}
|
|
|
|
|
|
|
|
int FLACtoMP3::setTagUSC2(const std::string_view& field, const std::string_view& value) {
|
|
|
|
std::u16string utf16 = std::u16string({0xFEFF}) + usc2convertor.from_bytes(value.data());
|
|
|
|
return id3tag_set_textinfo_utf16(encoder, field.data(), (unsigned short*)utf16.c_str());
|
2023-07-16 23:36:25 +00:00
|
|
|
}
|