The LAME Project LAME is a high quality MPEG Audio Layer III (MP3) encoder licensed under the LGPL.
VS2013项目(libmp3lame, 可直接编译)下载链接:http://pan.baidu.com/s/1eQnqS9c 密码:5mg3
/******************************************************************** Created: 2012/04/13 21:52 Filename: RLib_Mp3.cpp Author: rrrfff Url: http://blog.csdn.net/rrrfff *********************************************************************/ #include "RLib_File.h" #include "RLib_GlobalizeString.h" #include "support/libmp3lame/lame.h" #include <mmreg.h> #include <stdio.h> #ifdef _USE_MP3 //------------------------------------------------------------------------- #pragma pack(1) struct WAVFILE { struct RIFF_HEADER { char szRiffID[4]; // 'R','I','F','F' DWORD dwRiffSize; // file_length - sizeof(szRiffID) - sizeof(dwRiffSize) char szRiffFormat[4]; // 'W','A','V','E' }; struct FMT_BLOCK { char szFmtID[4];// 'f','m','t',' ' DWORD dwFmtSize; union WAV_FORMAT { WAVEFORMATEX wavFormatEx; //determined by dwFmtSize PCMWAVEFORMAT wavPCMFormat;//dwFmtSize == 16 }; WAV_FORMAT format; }; // struct FACT_BLOCK // { // char szFactID[4]; // 'f','a','c','t' // DWORD dwFactSize; // }; struct DATA_BLOCK { char szDataID[4]; // 'd','a','t','a' DWORD dwDataSize; }; RIFF_HEADER header; FMT_BLOCK fmt; // FACT_BLOCK fact; DATA_BLOCK data; }; #pragma pack() //------------------------------------------------------------------------- void ConvertFromWAVFile(String wav_path, String mp3_path) { auto pcm = IO::File::Open(wav_path); if (pcm == nullptr) return; //skip wav file headers #ifdef _DEBUG WAVFILE wav; pcm->Read(&wav, sizeof(wav)); #else pcm->Position = sizeof(WAVFILE); #endif // _DEBUG FILE *mp3; if (fopen_s(&mp3, GlobalizeString(IO::Path::GetDosPath(mp3_path)).toGBK(), "wb+") != 0) { delete pcm; return; } lame_t lame = lame_init(); lame_set_in_samplerate(lame, 44100); lame_set_VBR(lame, vbr_default); // lame_set_bWriteVbrTag(lame, 0); lame_init_params(lame); short int pcm_buffer[RLIB_DEFAULT_BUFFER_SIZE * sizeof(short int)]; unsigned char mp3_buffer[RLIB_DEFAULT_BUFFER_SIZE * sizeof(short int)]; //make sure... intptr_t read, write; do { read = pcm->Read(pcm_buffer, sizeof(pcm_buffer)); if (read == 0){ write = lame_encode_flush(lame, mp3_buffer, sizeof(mp3_buffer)); } else{ write = lame_encode_buffer_interleaved(lame, pcm_buffer, static_cast<int>(read / sizeof(short int)), //what should I do? mp3_buffer, sizeof(mp3_buffer)); } fwrite(mp3_buffer, write, 1, mp3); } while (read > 0); lame_mp3_tags_fid(lame, mp3); lame_close(lame); fclose(mp3); delete pcm; } #endif // _USE_MP3