I need to change mp3 bitrate, I'm using lame for cpp project under Windows and Linux. I think I need to convert mp3 to wav and then wav to mp3 with desired bitrate.
For wav to mp3 I already have the code (using lame) it works.
For mp3 to wav there is problem with app crashing on hip_decode.
Probably due to ID3 tags at the beginning of the file.
How to skip the ID3 tags or is this a different problem?
This is code I'm using
bool Helper::convert2Wav(QString fromFile, QString toFile) {
int read, write;
int ctn = 0;
FILE *mp3_in =fopen(fromFile.toLocal8Bit().data(),"rb");
//this should be WAV
FILE *pcm_out =fopen(toFile.toLocal8Bit().data(),"wb");
const int PCM_SIZE = 4096;
const int MP3_SIZE = 4096;
short int pcm_buffer[PCM_SIZE*2];
short int pcm_buffer1[PCM_SIZE];
short int pcm_buffer2[PCM_SIZE];
unsigned char mp3_buffer[MP3_SIZE];
memset(pcm_buffer, 0, sizeof(pcm_buffer));
memset(pcm_buffer1, 0, sizeof(pcm_buffer1));
memset(pcm_buffer2, 0, sizeof(pcm_buffer2));
memset(mp3_buffer, 0, sizeof(mp3_buffer));
lame_t lame = lame_init();
hip_t l = hip_decode_init();
lame_set_in_samplerate(lame, 44100);
//lame_set_VBR(lame, vbr_default);
//lame_set_mode(lame, MONO);
lame_init_params(lame);
qDebug()<<"start decoding";
do {
ctn += 4096;
qDebug()<<"a message:"<<ctn;
read = fread(mp3_buffer, sizeof(unsigned char), MP3_SIZE, mp3_in);
hip_decode(l, mp3_buffer, MP3_SIZE, pcm_buffer1, pcm_buffer2);
//read = hip_decode(l, mp3_buffer, MP3_SIZE, pcm_buffer1,pcm_buffer2);
//memcpy(pcm_buffer, pcm_buffer1, sizeof(pcm_buffer1));
//memcpy(&pcm_buffer[PCM_SIZE], pcm_buffer2, sizeof(pcm_buffer2));
//write = lame_encode_buffer_interleaved(lame, pcm_buffer, read,mp3_buffer, MP3_SIZE);
//fwrite(pcm_buffer, 1, sizeof(pcm_buffer), pcm_out);
} while (read != 0);
hip_decode_exit(l);
lame_close(lame);
fclose(pcm_out);
fclose(mp3_in);
return true;
}
It is crashing with an output:
start decoding
a message: 4096
a message: 8192
a message: 12288
1 ?? 0x7ffff7b8f0b5
2 hip_decode1_headers 0x7ffff7b8f34b
3 hip_decode1_headers 0x7ffff7b8f385
4 hip_decode_headers 0x7ffff7b8f42d
5 hip_decode 0x7ffff7b8f47c
6 Helper::convert2Wav helper.cpp 498 0x5555555b0fe7
7 ?? 0x4490551063006a0
MP3_SIZE 258346945 int
PCM_SIZE 234884892 int
ctn 189860957 int
l 1050198802094820962 hip_t
lame 1165886731226582744 lame_t
mp3_buffer "\005\0029lM^J\035�xo�R����(����Rj\172175r�V�����ԡCb٥���0a�ע�r�4�A��7Xj�Q0u�\033Os��L5�S$D2\002\016� UxCp\014s��08\004+7��@"�r"... (4096) unsigned char[4096]
mp3_in 0xe0b0e1e0e060d4a FILE*
pcm_buffer @0x7fffffff89a0 short[8192]
pcm_buffer1 @0x7fffffff69a0 short[4096]
pcm_buffer2 @0x7fffffff49a0 short[4096]
pcm_out 0xc460bb00bb40c82 FILE*
read 229772669 int
write <optimized out>
it seems this is not ID3 issue, I have used this code https://searchcode.com/file/31230698/ID3.cpp to read ID3 and it shows there is no ID3 tags But still crashing at the same point:
Has ID3v2 Tag: No
Tag size: 0
start decoding skip tag_size: 0
a message: 4096
a message: 8192
a message: 12288