Example.cpp 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  1. /*
  2. * LAME DLL Sample Code.
  3. *
  4. * Copyright (c) 2000 A.L. Faber
  5. *
  6. * This library is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Library General Public
  8. * License as published by the Free Software Foundation; either
  9. * version 2 of the License, or (at your option) any later version.
  10. *
  11. * This library is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * Library General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Library General Public
  17. * License along with this library; if not, write to the
  18. * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
  19. * Boston, MA 02111-1307, USA.
  20. */
  21. #include <windows.h>
  22. #include <stdio.h>
  23. #include <io.h>
  24. #include <fcntl.h>
  25. #include <sys/stat.h>
  26. #include "BladeMP3EncDLL.h"
  27. BEINITSTREAM beInitStream=NULL;
  28. BEENCODECHUNK beEncodeChunk=NULL;
  29. BEDEINITSTREAM beDeinitStream=NULL;
  30. BECLOSESTREAM beCloseStream=NULL;
  31. BEVERSION beVersion=NULL;
  32. BEWRITEVBRHEADER beWriteVBRHeader=NULL;
  33. BEWRITEINFOTAG beWriteInfoTag=NULL;
  34. // Main program
  35. int main(int argc, char *argv[])
  36. {
  37. HINSTANCE hDLL =NULL;
  38. FILE* pFileIn =NULL;
  39. FILE* pFileOut =NULL;
  40. BE_VERSION Version ={0,};
  41. BE_CONFIG beConfig ={0,};
  42. CHAR strFileIn[255] ={'0',};
  43. CHAR strFileOut[255] ={'0',};
  44. DWORD dwSamples =0;
  45. DWORD dwMP3Buffer =0;
  46. HBE_STREAM hbeStream =0;
  47. BE_ERR err =0;
  48. PBYTE pMP3Buffer =NULL;
  49. PSHORT pWAVBuffer =NULL;
  50. // check number of arguments
  51. if(argc != 2)
  52. {
  53. fprintf(stderr,"Usage: %s <filename.wav>\n", argv[0]);
  54. fprintf(stderr,"Descr: Short demo to show how to use the lame_enc.dll library file\n");
  55. fprintf(stderr,"Note : WAV file is assumed to to have the following parameters\n");
  56. fprintf(stderr," : 44100 Hz, stereo, 16 Bits per sample\n");
  57. return -1;
  58. }
  59. // Setup the file names
  60. strcpy(strFileIn ,argv[1]);
  61. strcpy(strFileOut,argv[1]);
  62. // Add mp3 extention
  63. strcat(strFileOut,".mp3");
  64. // Load lame_enc.dll library (Make sure though that you set the
  65. // project/settings/debug Working Directory correctly, otherwhise the DLL can't be loaded
  66. hDLL = LoadLibrary("lame_enc.dll");
  67. if ( NULL == hDLL )
  68. {
  69. hDLL = LoadLibrary("..\\..\\output\\lame_enc.dll");
  70. }
  71. if( NULL == hDLL )
  72. {
  73. fprintf(stderr,"Error loading lame_enc.DLL");
  74. return -1;
  75. }
  76. // Get Interface functions from the DLL
  77. beInitStream = (BEINITSTREAM) GetProcAddress(hDLL, TEXT_BEINITSTREAM);
  78. beEncodeChunk = (BEENCODECHUNK) GetProcAddress(hDLL, TEXT_BEENCODECHUNK);
  79. beDeinitStream = (BEDEINITSTREAM) GetProcAddress(hDLL, TEXT_BEDEINITSTREAM);
  80. beCloseStream = (BECLOSESTREAM) GetProcAddress(hDLL, TEXT_BECLOSESTREAM);
  81. beVersion = (BEVERSION) GetProcAddress(hDLL, TEXT_BEVERSION);
  82. beWriteVBRHeader= (BEWRITEVBRHEADER) GetProcAddress(hDLL,TEXT_BEWRITEVBRHEADER);
  83. beWriteInfoTag = (BEWRITEINFOTAG) GetProcAddress(hDLL,TEXT_BEWRITEINFOTAG);
  84. // Check if all interfaces are present
  85. if(!beInitStream || !beEncodeChunk || !beDeinitStream || !beCloseStream || !beVersion || !beWriteVBRHeader)
  86. {
  87. printf("Unable to get LAME interfaces");
  88. return -1;
  89. }
  90. // Get the version number
  91. beVersion( &Version );
  92. printf(
  93. "lame_enc.dll version %u.%02u (%u/%u/%u)\n"
  94. "lame_enc Engine %u.%02u\n"
  95. "lame_enc homepage at %s\n\n",
  96. Version.byDLLMajorVersion, Version.byDLLMinorVersion,
  97. Version.byDay, Version.byMonth, Version.wYear,
  98. Version.byMajorVersion, Version.byMinorVersion,
  99. Version.zHomepage);
  100. // Try to open the WAV file, be sure to open it as a binary file!
  101. pFileIn = fopen( strFileIn, "rb" );
  102. // Check file open result
  103. if(pFileIn == NULL)
  104. {
  105. fprintf(stderr,"Error opening %s", argv[1]);
  106. return -1;
  107. }
  108. // Open MP3 file
  109. pFileOut= fopen(strFileOut,"wb+");
  110. // Check file open result
  111. if(pFileOut == NULL)
  112. {
  113. fprintf(stderr,"Error creating file %s", strFileOut);
  114. fclose(pFileIn);
  115. return -1;
  116. }
  117. memset(&beConfig,0,sizeof(beConfig)); // clear all fields
  118. // use the LAME config structure
  119. beConfig.dwConfig = BE_CONFIG_LAME;
  120. // this are the default settings for testcase.wav
  121. beConfig.format.LHV1.dwStructVersion = 1;
  122. beConfig.format.LHV1.dwStructSize = sizeof(beConfig);
  123. beConfig.format.LHV1.dwSampleRate = 44100; // INPUT FREQUENCY
  124. beConfig.format.LHV1.dwReSampleRate = 0; // DON"T RESAMPLE
  125. beConfig.format.LHV1.nMode = BE_MP3_MODE_JSTEREO; // OUTPUT IN STREO
  126. beConfig.format.LHV1.dwBitrate = 128; // MINIMUM BIT RATE
  127. beConfig.format.LHV1.nPreset = LQP_R3MIX; // QUALITY PRESET SETTING
  128. beConfig.format.LHV1.dwMpegVersion = MPEG1; // MPEG VERSION (I or II)
  129. beConfig.format.LHV1.dwPsyModel = 0; // USE DEFAULT PSYCHOACOUSTIC MODEL
  130. beConfig.format.LHV1.dwEmphasis = 0; // NO EMPHASIS TURNED ON
  131. beConfig.format.LHV1.bOriginal = TRUE; // SET ORIGINAL FLAG
  132. beConfig.format.LHV1.bWriteVBRHeader = TRUE; // Write INFO tag
  133. // beConfig.format.LHV1.dwMaxBitrate = 320; // MAXIMUM BIT RATE
  134. // beConfig.format.LHV1.bCRC = TRUE; // INSERT CRC
  135. // beConfig.format.LHV1.bCopyright = TRUE; // SET COPYRIGHT FLAG
  136. // beConfig.format.LHV1.bPrivate = TRUE; // SET PRIVATE FLAG
  137. // beConfig.format.LHV1.bWriteVBRHeader = TRUE; // YES, WRITE THE XING VBR HEADER
  138. // beConfig.format.LHV1.bEnableVBR = TRUE; // USE VBR
  139. // beConfig.format.LHV1.nVBRQuality = 5; // SET VBR QUALITY
  140. beConfig.format.LHV1.bNoRes = TRUE; // No Bit resorvoir
  141. // Preset Test
  142. // beConfig.format.LHV1.nPreset = LQP_PHONE;
  143. // Init the MP3 Stream
  144. err = beInitStream(&beConfig, &dwSamples, &dwMP3Buffer, &hbeStream);
  145. // Check result
  146. if(err != BE_ERR_SUCCESSFUL)
  147. {
  148. fprintf(stderr,"Error opening encoding stream (%lu)", err);
  149. fclose(pFileIn);
  150. fclose(pFileOut);
  151. return -1;
  152. }
  153. // Allocate MP3 buffer
  154. pMP3Buffer = new BYTE[dwMP3Buffer];
  155. // Allocate WAV buffer
  156. pWAVBuffer = new SHORT[dwSamples];
  157. // Check if Buffer are allocated properly
  158. if(!pMP3Buffer || !pWAVBuffer)
  159. {
  160. printf("Out of memory");
  161. fclose(pFileIn);
  162. fclose(pFileOut);
  163. return -1;
  164. }
  165. DWORD dwRead=0;
  166. DWORD dwWrite=0;
  167. DWORD dwDone=0;
  168. DWORD dwFileSize=0;
  169. // Seek to end of file
  170. fseek(pFileIn,0,SEEK_END);
  171. // Get the file size
  172. dwFileSize=ftell(pFileIn);
  173. // Seek back to start of WAV file,
  174. // but skip the first 44 bytes, since that's the WAV header
  175. fseek(pFileIn,44,SEEK_SET);
  176. // Convert All PCM samples
  177. while ( (dwRead=fread(pWAVBuffer,sizeof(SHORT),dwSamples,pFileIn)) >0 )
  178. {
  179. // Encode samples
  180. err = beEncodeChunk(hbeStream, dwRead, pWAVBuffer, pMP3Buffer, &dwWrite);
  181. // Check result
  182. if(err != BE_ERR_SUCCESSFUL)
  183. {
  184. beCloseStream(hbeStream);
  185. fprintf(stderr,"beEncodeChunk() failed (%lu)", err);
  186. return -1;
  187. }
  188. // write dwWrite bytes that are returned in tehe pMP3Buffer to disk
  189. if(fwrite(pMP3Buffer,1,dwWrite,pFileOut) != dwWrite)
  190. {
  191. fprintf(stderr,"Output file write error");
  192. return -1;
  193. }
  194. dwDone += dwRead*sizeof(SHORT);
  195. printf("Done: %0.2f%% \r", 100 * (float)dwDone/(float)(dwFileSize));
  196. }
  197. // Deinit the stream
  198. err = beDeinitStream(hbeStream, pMP3Buffer, &dwWrite);
  199. // Check result
  200. if(err != BE_ERR_SUCCESSFUL)
  201. {
  202. beCloseStream(hbeStream);
  203. fprintf(stderr,"beExitStream failed (%lu)", err);
  204. return -1;
  205. }
  206. // Are there any bytes returned from the DeInit call?
  207. // If so, write them to disk
  208. if( dwWrite )
  209. {
  210. if( fwrite( pMP3Buffer, 1, dwWrite, pFileOut ) != dwWrite )
  211. {
  212. fprintf(stderr,"Output file write error");
  213. return -1;
  214. }
  215. }
  216. // close the MP3 Stream
  217. beCloseStream( hbeStream );
  218. // Delete WAV buffer
  219. delete [] pWAVBuffer;
  220. // Delete MP3 Buffer
  221. delete [] pMP3Buffer;
  222. // Close input file
  223. fclose( pFileIn );
  224. // Close output file
  225. fclose( pFileOut );
  226. if ( beWriteInfoTag )
  227. {
  228. // Write the INFO Tag
  229. beWriteInfoTag( hbeStream, strFileOut );
  230. }
  231. else
  232. {
  233. beWriteVBRHeader( strFileOut );
  234. }
  235. // Were done, return OK result
  236. return 0;
  237. }