BladeEncoder.cpp

 // BladeEncoder.cpp : MP3 encoding example
// copyright © 2000 ULTiMaTuM
// Studies have shown that if you STEAL my code, you
// will die naked and alone
//

#include "stdafx.h"
#include "BladeEncoder.h"
#include "bladedll.h"

#include <windows.h>
#include <io.h>
#include <fcntl.h>
#include <sys/stat.h>

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

CWinApp theApp;

using namespace std;

int RunThisApp(void);

int _tmain(int argc, TCHAR* argv[], TCHAR* envp[])
{
int nRetCode = 0;

// initialize WFC and print and error on failure
if (!AfxWinInit(::GetModuleHandle(NULL), NULL, ::GetCommandLine(), 0))
{
// If WFC doesn't load
cerr << _T("Fatal Error: WFC initialization failed") << endl;
nRetCode = 1;
}
else
{
if ((RunThisApp()) == -1)
cout << "At least one error occurred during encoding" << endl;
else
cout << "Encoding successful" << endl;
}

return nRetCode;
}

// RunThisApp: start of main processing
int RunThisApp(void)
{
BEINITSTREAM beInitStream;
BEENCODECHUNK beEncodeChunk;
BEDEINITSTREAM beDeinitStream;
BECLOSESTREAM beCloseStream;
BEVERSION beVersion;

cout << "Blade MP3 Encoder" << endl;
cout << "created by ULTiMaTuM" << endl;
cout << "<ultimatum@ameritech.net\n" << endl;

// Load the encoding library
HINSTANCE hEncodeDLL = LoadLibrary("BLADEENC.DLL");

if(!hEncodeDLL) {

cout << "Error loading encoding library." << endl;
return -1;
}

// Get DLL Interface

beInitStream = (BEINITSTREAM) GetProcAddress(hEncodeDLL, TEXT_BEINITSTREAM);
beEncodeChunk = (BEENCODECHUNK) GetProcAddress(hEncodeDLL, TEXT_BEENCODECHUNK);
beDeinitStream = (BEDEINITSTREAM) GetProcAddress(hEncodeDLL, TEXT_BEDEINITSTREAM);
beCloseStream = (BECLOSESTREAM) GetProcAddress(hEncodeDLL, TEXT_BECLOSESTREAM);
beVersion = (BEVERSION) GetProcAddress(hEncodeDLL, TEXT_BEVERSION);

if(!beInitStream || !beEncodeChunk || !beDeinitStream || !beCloseStream || !beVersion) {

cout << "Unable to retrieve DLL interface." << endl;
return -1;
}

// Get Encoding Version

BE_VERSION Version;

beVersion(&Version);

CString szDLLVersion;

szDLLVersion.Format("Encoding DLL version %u.%02u (%u/%u/%u)\n"
"Encoding Engine %u.%02u\n\n",
Version.byDLLMajorVersion, Version.byDLLMinorVersion,
Version.byDay, Version.byMonth, Version.wYear,
Version.byMajorVersion, Version.byMinorVersion);

cout << (LPCSTR)szDLLVersion << endl;

// Get WAV file to open
char szWAVFile[51];;

cout << "WAV file: ";
cin >> szWAVFile;

// Open WAV file

int hIn = open(szWAVFile, O_RDONLY | O_BINARY);

if(hIn == -1) {

cout << "Error opening " << szWAVFile << "." << endl;
return -1;
}

// Generate filename.mp3

char szOutputFilename[MAX_PATH + 1];
lstrcpy(szOutputFilename, szWAVFile);
int l = lstrlen(szOutputFilename);
while(l && szOutputFilename[l] != '.') {

l--;
}

if(!l) {

l = lstrlen(szOutputFilename) - 1;
}

szOutputFilename[l] = '\0';

lstrcat(szOutputFilename, ".mp3");

// Open MP3 file

int hOut = open(szOutputFilename, O_WRONLY | O_BINARY | O_TRUNC | O_CREAT, S_IWRITE);

if(hOut == -1) {

cout << "Error creating file " << szOutputFilename << "." << endl;
return -1;
}

// Open MP3 Encoding Stream

BE_CONFIG beConfig;

beConfig.dwConfig = BE_CONFIG_MP3;

beConfig.format.mp3.dwSampleRate = 44100;
beConfig.format.mp3.byMode = BE_MP3_MODE_STEREO;
beConfig.format.mp3.wBitrate = 128;
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) {

cout << "Error attempting to encode. (" << err << ")" << endl;
return -1;
}

// Allocate buffers

PBYTE pMP3Buffer = new BYTE[dwMP3Buffer];

PSHORT pBuffer = new SHORT[dwSamples];

if(!pMP3Buffer || !pBuffer) {

cout << "Out of memory" << endl;
return -1;
}

// Start encoding

DWORD length = filelength(hIn);
DWORD done = 0;
DWORD dwWrite;
DWORD toread;
DWORD towrite;

setbuf(stdout,NULL);

cout << "Encoding...";
while(done < length) {

if(done + dwSamples * 2 < length) {

toread = dwSamples * 2;
}
else {

toread = length - done;
}

if(read(hIn, pBuffer, toread) == -1) {

cout << "Read error." << endl;
return -1;
}

err = beEncodeChunk(hbeStream, toread/2, pBuffer, pMP3Buffer, &towrite);

if(err != BE_ERR_SUCCESSFUL) {

beCloseStream(hbeStream);
cout << "Encoding failed (" << err << ")" << endl;
return -1;
}

if(write(hOut, pMP3Buffer, towrite) == -1) {

cout << "Write error." << endl;
return -1;
}

done += toread;

cout << ".";
}

cout << endl;

err = beDeinitStream(hbeStream, pMP3Buffer, &dwWrite);

if(err != BE_ERR_SUCCESSFUL) {

beCloseStream(hbeStream);
return -1;
}

if(dwWrite) {

if(write(hOut, pMP3Buffer, dwWrite) == -1) {

cout << "Write error" << endl;
return -1;
}
}

beCloseStream(hbeStream);

close(hIn);
close(hOut);
return 0;
}

Project Homepage: