用bladedll 我就是這么干的
//---------------------------------------------------------------------------
#include <vcl.h>
#pragma hdrstop
#include "UWavToMp3.h"
#pragma package(smart_init)
String Temp;
int iCount=0;
float fProgress;
//---------------------------------------------------------------------------
// Important: Methods and properties of objects in VCL can only be
// used in a method called using Synchronize, for example:
//
// Synchronize(UpdateCaption);
//
// where UpdateCaption could look like:
//
// void __fastcall WavMp3::UpdateCaption()
// {
// Form1->Caption = "Updated in a thread";
// }
//---------------------------------------------------------------------------
void __fastcall WavMp3::begin
Mp3()
{
FMain->CGyl->Progress=0;
FMain->CGyl->Show();
DataPath=FMain->DataPath;
}
void __fastcall WavMp3:
oingMp3()
{
FMain->POnHint->Caption="淏婓揤坫﹝﹝﹝";
FMain->CGyl->Progress=fProgress;
}
void __fastcall WavMp3::FinishMp3()
{
FMain->REFileName->Lines->Delete(1);
FMain->CGyl->Hide();
DeleteFile(FMain->DataPath+"//"+Temp+".wav");
FMain->POnHint->Caption="";
}
void __fastcall WavMp3::FileCount()
{
iCount=FMain->REFileName->Lines->Count;
if(iCount>1)
{
try{
Temp=StrToDateTime(FMain->REFileName->Lines->Strings[1]).FormatString("yyyy-mm-dd hhnnss");
}
catch(...)
{
//Application->MessageBox("恅璃祥湔婓","劑豢",0);
MessageBeep(0);
}
}
}
__fastcall WavMp3::WavMp3(bool CreateSuspended)
: TThread(CreateSuspended)
{
}
//---------------------------------------------------------------------------
void __fastcall WavMp3::Execute()
{
for(;
{
/*
if(FMain->bExit)
break;
*/
Synchronize(FileCount);
while(iCount>1)
{
Synchronize(begin
Mp3);
//FMain->RE->Lines->Add(DataPath);
WavToMp3(DataPath+"//"+Temp+".wav");
//WavToMp3(FMain->DataPath+"//"+Temp+".wav");
Synchronize(FinishMp3);
Synchronize(FileCount);
}
}
}
//---------------------------------------------------------------------------
void __fastcall WavMp3::WavToMp3(AnsiString sFileName)
{
BEINITSTREAM beInitStream;
BEENCODECHUNK beEncodeChunk;
BEDEINITSTREAM beDeinitStream;
BECLOSESTREAM beCloseStream;
BEVERSION beVersion;
// Load BladeEnc.DLL
HINSTANCE hBladeDLL = LoadLibrary("BLADEENC.DLL");
if(!hBladeDLL) {
//ShowMessage("Error loading BLADEENC.DLL");
MessageBeep(0);
return
}
// Get Interface
beInitStream = (BEINITSTREAM) GetProcAddress(hBladeDLL, TEXT_BEINITSTREAM);
beEncodeChunk = (BEENCODECHUNK) GetProcAddress(hBladeDLL, TEXT_BEENCODECHUNK);
beDeinitStream = (BEDEINITSTREAM) GetProcAddress(hBladeDLL, TEXT_BEDEINITSTREAM);
beCloseStream = (BECLOSESTREAM) GetProcAddress(hBladeDLL, TEXT_BECLOSESTREAM);
beVersion = (BEVERSION) GetProcAddress(hBladeDLL, TEXT_BEVERSION);
if(!beInitStream || !beEncodeChunk || !beDeinitStream || !beCloseStream || !beVersion) {
//ShowMessage("Unable to get BladeEnc interface");
MessageBeep(0);
return
}
// Get Version
BE_VERSION Version;
beVersion(&Version);
/*
char aa[100];
wsprintf(aa, "BladeEnc.dll version %u.%02u (%u/%u/%u)/n"
"BladeEnc Engine %u.%02u/n"
"BladeEnc homepage at %s/n/n",
Version.byDLLMajorVersion, Version.byDLLMinorVersion,
Version.byDay, Version.byMonth, Version.wYear,
Version.byMajorVersion, Version.byMinorVersion,
Version.zHomepage);
ShowMessage(aa);
*/
// Open RAW file
int hIn = open(sFileName.c_str(), O_RDONLY | O_BINARY);
if(hIn == -1) {
/*
char aa[100];
wsprintf(aa,"Error opening %s", sFileName.c_str());
ShowMessage(aa);
*/
MessageBeep(0);
return
}
// Generate filename.mp3
char zOutputFilename[MAX_PATH + 1];
lstrcpy(zOutputFilename,sFileName.c_str());
int l = lstrlen(zOutputFilename);
while(l &&
zOutputFilename[l] != '.') {
l--;
}
if(!l) {
l = lstrlen(zOutputFilename) - 1;
}
zOutputFilename[l] = '/0';
lstrcat(zOutputFilename, ".mp3");
// Open MP3 file
int hOut = open(zOutputFilename, O_WRONLY | O_BINARY | O_TRUNC | O_CREAT, S_IWRITE);
if(hOut == -1) {
/*
char aa[100];
wsprintf(aa,"Error creating file %s", zOutputFilename);
ShowMessage(aa);
*/
MessageBeep(0);
return
}
// Open MP3 Encoding Stream
BE_CONFIG beConfig;
beConfig.dwConfig = BE_CONFIG_MP3;
beConfig.format.mp3.dwSampleRate = 44100;
beConfig.format.mp3.byMode = 3;
beConfig.format.mp3.wBitrate =FMain->BitRate;
beConfig.format.mp3.bCopyright = FALSE;
beConfig.format.mp3.bCRC = FALSE;
beConfig.format.mp3.bOriginal = FALSE;
beConfig.format.mp3.bPrivate = FALSE;
DWORD dwSamples, dwMP3Buffer;
HBE_STREAM hbeStream;
BE_ERR err;
err = beInitStream(&beConfig, &dwSamples, &dwMP3Buffer, &hbeStream);
if(err != BE_ERR_SUCCESSFUL) {
/*
char aa[100];
wsprintf(aa,"Error opening encoding stream (%lu)", err);
ShowMessage(aa);
*/
MessageBeep(0);
return
}
// Allocate buffers
PBYTE pMP3Buffer = new BYTE[dwMP3Buffer];
PSHORT pBuffer = new SHORT[dwSamples];
if(!pMP3Buffer || !pBuffer) {
// ShowMessage("Out of memory");
MessageBeep(0);
return
}
// Start encoding
DWORD length = filelength(hIn);
DWORD done = 0;
DWORD dwWrite;
DWORD toread;
DWORD towrite;
setbuf(stdout,NULL);
while(done < length) {
if(done + dwSamples * 2 < length) {
toread = dwSamples * 2;
}
else
{
toread = length -do
ne;
}
if(read(hIn, pBuffer, toread) == -1) {
//ShowMessage("/nRead error");
MessageBeep(0);
return
}
err = beEncodeChunk(hbeStream, toread/2, pBuffer, pMP3Buffer, &towrite);
if(err != BE_ERR_SUCCESSFUL) {
beCloseStream(hbeStream);
/*
char aa[100];
wsprintf(aa,"beEncodeChunk() failed (%lu)", err);
ShowMessage(aa);
*/
MessageBeep(0);
return
}
if(write(hOut, pMP3Buffer, towrite) == -1) {
/*
ShowMessage("/nWrite error");
*/
MessageBeep(0);
return
}
done += toread;
/*
FMain->POnHint->Caption="淏婓揤坫﹝﹝﹝";
FMain->CGyl->Progress=100*(float)done/(float)length;
*/
fProgress=100*(float)done/(float)length;
Synchronize(DoingMp3);
//printf("Done: %0.2f%% /r", 100 * (float)done/(float)length);
}
err = beDeinitStream(hbeStream, pMP3Buffer, &dwWrite);
if(err != BE_ERR_SUCCESSFUL) {
beCloseStream(hbeStream);
/*
char aa[100];
wsprintf(aa,"beExitStream failed (%lu)", err);
ShowMessage(aa);
*/
MessageBeep(0);
return
}
if(dwWrite) {
if(write(hOut, pMP3Buffer, dwWrite) == -1) {
// ShowMessage("Write error");
MessageBeep(0);
return
}
}
beCloseStream(hbeStream);
close(hIn);
close(hOut);
//ShowMessage("/n/nIt's Easy!");
//DeleteFile(sFileName);
}