00001 #include <stdio.h>
00002 #include <windows.h>
00003 #include "bladedll.h"
00004
00005 bool saveWaveToMp3(char *dllFile, char *mp3File, int *wave, int waveSize, int sampleRate, int bitRate, int mp3BitRate, bool mp3Stereo)
00006 {
00007 FILE *fout;
00008 BEINITSTREAM beInitStream;
00009 BEENCODECHUNK beEncodeChunk;
00010 BEDEINITSTREAM beDeinitStream;
00011 BECLOSESTREAM beCloseStream;
00012 HINSTANCE hBladeDLL;
00013 BE_CONFIG beConfig;
00014 DWORD dwSamples, dwMP3Buffer;
00015 HBE_STREAM hbeStream;
00016 PBYTE pMP3Buffer;
00017 PSHORT pBuffer;
00018 DWORD i, size, done, dwWrite, toread, towrite;
00019 int bytesShift, multipleShift;
00020
00021 if (sampleRate != 32000 && sampleRate != 44100 && sampleRate != 48000)
00022 return false;
00023
00024 if ((hBladeDLL = LoadLibrary(dllFile)) == NULL)
00025 return false;
00026
00027 beInitStream = (BEINITSTREAM)GetProcAddress(hBladeDLL, TEXT_BEINITSTREAM);
00028 beEncodeChunk = (BEENCODECHUNK)GetProcAddress(hBladeDLL, TEXT_BEENCODECHUNK);
00029 beDeinitStream = (BEDEINITSTREAM)GetProcAddress(hBladeDLL, TEXT_BEDEINITSTREAM);
00030 beCloseStream = (BECLOSESTREAM)GetProcAddress(hBladeDLL, TEXT_BECLOSESTREAM);
00031 if (!beInitStream || !beEncodeChunk || !beDeinitStream || !beCloseStream)
00032 return false;
00033
00034 if ((fout = fopen(mp3File, "wb")) == NULL)
00035 {
00036 FreeLibrary(hBladeDLL);
00037 return false;
00038 }
00039
00040 beConfig.dwConfig = BE_CONFIG_MP3;
00041 beConfig.format.mp3.dwSampleRate = sampleRate;
00042 beConfig.format.mp3.byMode = mp3Stereo? BE_MP3_MODE_STEREO : BE_MP3_MODE_MONO;
00043 beConfig.format.mp3.wBitrate = mp3BitRate;
00044 beConfig.format.mp3.bCopyright = false;
00045 beConfig.format.mp3.bCRC = false;
00046 beConfig.format.mp3.bOriginal = false;
00047 beConfig.format.mp3.bPrivate = false;
00048 beInitStream(&beConfig, &dwSamples, &dwMP3Buffer, &hbeStream);
00049
00050 pMP3Buffer = new BYTE[dwMP3Buffer];
00051 pBuffer = new SHORT[dwSamples];
00052
00053 bytesShift = mp3Stereo? 1 : 0;
00054 multipleShift = bitRate == 8? 8 : 0;
00055 size = (unsigned int)waveSize << bytesShift;
00056 done = 0;
00057 while (done < size)
00058 {
00059 if (done + dwSamples < size)
00060 toread = dwSamples;
00061 else
00062 toread = size - done;
00063 for (i = 0; i < toread; i++)
00064 pBuffer[i] = wave[(done + i) >> bytesShift] << multipleShift;
00065 beEncodeChunk(hbeStream, toread, pBuffer, pMP3Buffer, &towrite);
00066 fwrite(pMP3Buffer, sizeof(char), towrite, fout);
00067 done += toread;
00068 }
00069
00070 beDeinitStream(hbeStream, pMP3Buffer, &dwWrite);
00071 if (dwWrite)
00072 fwrite(pMP3Buffer, sizeof(char), dwWrite, fout);
00073
00074 delete[] pMP3Buffer;
00075 delete[] pBuffer;
00076
00077 beCloseStream(hbeStream);
00078 fclose(fout);
00079 FreeLibrary(hBladeDLL);
00080
00081 return true;
00082 }