MP3export.pas 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  1. unit MP3export;
  2. interface
  3. Uses SysUtils, WinTypes, WinProcs, Messages, Classes, Graphics, Controls,
  4. Forms, Dialogs, StdCtrls;
  5. type
  6. //type definitions
  7. //typedef unsigned long HBE_STREAM;
  8. //typedef HBE_STREAM *PHBE_STREAM;
  9. //typedef unsigned long BE_ERR;
  10. THBE_STREAM = LongWord;
  11. PHBE_STREAM = ^PHBE_STREAM;
  12. BE_ERR = LongWord;
  13. const
  14. // encoding formats
  15. //#define BE_CONFIG_MP3 0
  16. //#define BE_CONFIG_LAME 256
  17. BE_CONFIG_MP3 = 0;
  18. BE_CONFIG_LAME = 256;
  19. // error codes
  20. //#define BE_ERR_SUCCESSFUL 0x00000000
  21. //#define BE_ERR_INVALID_FORMAT 0x00000001
  22. //#define BE_ERR_INVALID_FORMAT_PARAMETERS 0x00000002
  23. //#define BE_ERR_NO_MORE_HANDLES 0x00000003
  24. //#define BE_ERR_INVALID_HANDLE 0x00000004
  25. BE_ERR_SUCCESSFUL: LongWord = 0;
  26. BE_ERR_INVALID_FORMAT: LongWord = 1;
  27. BE_ERR_INVALID_FORMAT_PARAMETERS: LongWord = 2;
  28. BE_ERR_NO_MORE_HANDLES: LongWord = 3;
  29. BE_ERR_INVALID_HANDLE: LongWord = 4;
  30. // other constants
  31. BE_MAX_HOMEPAGE = 256;
  32. // format specific variables
  33. BE_MP3_MODE_STEREO = 0;
  34. BE_MP3_MODE_DUALCHANNEL = 2;
  35. BE_MP3_MODE_MONO = 3;
  36. type
  37. TMP3 = packed record
  38. dwSampleRate : LongWord;
  39. byMode : Byte;
  40. wBitRate : Word;
  41. bPrivate : LongWord;
  42. bCRC : LongWord;
  43. bCopyright : LongWord;
  44. bOriginal : LongWord;
  45. end;
  46. TLHV1 = packed record
  47. // STRUCTURE INFORMATION
  48. dwStructVersion: DWORD;
  49. dwStructSize: DWORD;
  50. // BASIC ENCODER SETTINGS
  51. dwSampleRate: DWORD; // ALLOWED SAMPLERATE VALUES DEPENDS ON dwMPEGVersion
  52. dwReSampleRate: DWORD; // DOWNSAMPLERATE, 0=ENCODER DECIDES
  53. nMode: Integer; // BE_MP3_MODE_STEREO, BE_MP3_MODE_DUALCHANNEL, BE_MP3_MODE_MONO
  54. dwBitrate: DWORD; // CBR bitrate, VBR min bitrate
  55. dwMaxBitrate: DWORD; // CBR ignored, VBR Max bitrate
  56. nQuality: Integer; // Quality setting (NORMAL,HIGH,LOW,VOICE)
  57. dwMpegVersion: DWORD; // MPEG-1 OR MPEG-2
  58. dwPsyModel: DWORD; // FUTURE USE, SET TO 0
  59. dwEmphasis: DWORD; // FUTURE USE, SET TO 0
  60. // BIT STREAM SETTINGS
  61. bPrivate: LONGBOOL; // Set Private Bit (TRUE/FALSE)
  62. bCRC: LONGBOOL; // Insert CRC (TRUE/FALSE)
  63. bCopyright: LONGBOOL; // Set Copyright Bit (TRUE/FALSE)
  64. bOriginal: LONGBOOL; // Set Original Bit (TRUE/FALSE_
  65. // VBR STUFF
  66. bWriteVBRHeader: LONGBOOL; // WRITE XING VBR HEADER (TRUE/FALSE)
  67. bEnableVBR: LONGBOOL; // USE VBR ENCODING (TRUE/FALSE)
  68. nVBRQuality: Integer; // VBR QUALITY 0..9
  69. btReserved: array[0..255] of Byte; // FUTURE USE, SET TO 0
  70. end;
  71. TAAC = packed record
  72. dwSampleRate : LongWord;
  73. byMode : Byte;
  74. wBitRate : Word;
  75. byEncodingMethod : Byte;
  76. end;
  77. TFormat = packed record
  78. case byte of
  79. 1 : (mp3 : TMP3);
  80. 2 : (lhv1 : TLHV1);
  81. 3 : (aac : TAAC);
  82. end;
  83. TBE_Config = packed record
  84. dwConfig : LongWord;
  85. format : TFormat;
  86. end;
  87. PBE_Config = ^TBE_Config;
  88. //typedef struct {
  89. // // BladeEnc DLL Version number
  90. //
  91. // BYTE byDLLMajorVersion;
  92. // BYTE byDLLMinorVersion;
  93. //
  94. // // BladeEnc Engine Version Number
  95. //
  96. // BYTE byMajorVersion;
  97. // BYTE byMinorVersion;
  98. //
  99. // // DLL Release date
  100. //
  101. // BYTE byDay;
  102. // BYTE byMonth;
  103. // WORD wYear;
  104. //
  105. // // BladeEnc Homepage URL
  106. //
  107. // CHAR zHomepage[BE_MAX_HOMEPAGE + 1];
  108. //
  109. //} BE_VERSION, *PBE_VERSION;
  110. TBE_Version = record
  111. byDLLMajorVersion : Byte;
  112. byDLLMinorVersion : Byte;
  113. byMajorVersion : Byte;
  114. byMinorVersion : Byte;
  115. byDay : Byte;
  116. byMonth : Byte;
  117. wYear : Word;
  118. zHomePage : Array[0..BE_MAX_HOMEPAGE + 1] of Char;
  119. end;
  120. PBE_Version = ^TBE_Version;
  121. //__declspec(dllexport) BE_ERR beInitStream(PBE_CONFIG pbeConfig, PDWORD dwSamples, PDWORD dwBufferSize, PHBE_STREAM phbeStream);
  122. //__declspec(dllexport) BE_ERR beEncodeChunk(HBE_STREAM hbeStream, DWORD nSamples, PSHORT pSamples, PBYTE pOutput, PDWORD pdwOutput);
  123. //__declspec(dllexport) BE_ERR beDeinitStream(HBE_STREAM hbeStream, PBYTE pOutput, PDWORD pdwOutput);
  124. //__declspec(dllexport) BE_ERR beCloseStream(HBE_STREAM hbeStream);
  125. //__declspec(dllexport) VOID beVersion(PBE_VERSION pbeVersion);
  126. {
  127. Function beInitStream(var pbeConfig: TBE_CONFIG; var dwSample: LongWord; var dwBufferSize: LongWord; var phbeStream: THBE_STREAM ): BE_Err; cdecl; external 'Bladeenc.dll';
  128. //Function beEncodeChunk(hbeStream: THBE_STREAM; nSamples: LongWord; pSample: PSmallInt;pOutput: PByte; var pdwOutput: LongWord): BE_Err; cdecl; external 'Bladeenc.dll';
  129. Function beEncodeChunk(hbeStream: THBE_STREAM; nSamples: LongWord; var pSample;var pOutput; var pdwOutput: LongWord): BE_Err; stdcall; cdecl 'Bladeenc.dll';
  130. Function beDeinitStream(hbeStream: THBE_STREAM; var pOutput; var pdwOutput: LongWord): BE_Err; cdecl; external 'Bladeenc.dll';
  131. Function beCloseStream(hbeStream: THBE_STREAM): BE_Err; cdecl; external 'Bladeenc.dll';
  132. Procedure beVersion(var pbeVersion: TBE_VERSION); cdecl; external 'Bladeenc.dll';
  133. }
  134. Function beInitStream(var pbeConfig: TBE_CONFIG; var dwSample: LongWord; var dwBufferSize: LongWord; var phbeStream: THBE_STREAM ): BE_Err; cdecl; external 'Lame_enc.dll';
  135. //Function beEncodeChunk(hbeStream: THBE_STREAM; nSamples: LongWord; pSample: PSmallInt;pOutput: PByte; var pdwOutput: LongWord): BE_Err; cdecl; external 'Lame_enc.dll';
  136. Function beEncodeChunk(hbeStream: THBE_STREAM; nSamples: LongWord; var pSample;var pOutput; var pdwOutput: LongWord): BE_Err; cdecl; external 'Lame_enc.dll';
  137. Function beDeinitStream(hbeStream: THBE_STREAM; var pOutput; var pdwOutput: LongWord): BE_Err; cdecl; external 'Lame_enc.dll';
  138. Function beCloseStream(hbeStream: THBE_STREAM): BE_Err; cdecl; external 'Lame_enc.dll';
  139. Procedure beVersion(var pbeVersion: TBE_VERSION); cdecl; external 'Lame_enc.dll';
  140. Procedure EncodeWavToMP3(fs, fd: Integer);
  141. implementation
  142. Uses InternetSnd, TraiteWav;
  143. {----------------------------------------}
  144. Procedure EncodeWavToMP3(fs, fd: Integer);
  145. var
  146. err: Integer;
  147. beConfig: TBE_Config;
  148. dwSamples, dwSamplesMP3 : LongWord;
  149. hbeStream : THBE_STREAM;
  150. error: BE_ERR;
  151. pBuffer: PSmallInt;
  152. pMP3Buffer: PByte;
  153. Marque:PChar;
  154. done: LongWord;
  155. dwWrite: LongWord;
  156. ToRead: LongWord;
  157. ToWrite: LongWord;
  158. i:Integer;
  159. begin
  160. beConfig.dwConfig := BE_CONFIG_LAME;
  161. {
  162. beConfig.Format.mp3.dwSampleRate := WavInfo.SamplesPerSec;
  163. beConfig.Format.mp3.byMode := BE_MP3_MODE_STEREO;
  164. beConfig.Format.mp3.wBitrate := strToInt(MainFrm.Mp3BitRate.Text);
  165. beConfig.Format.mp3.bCopyright := 0;
  166. beConfig.Format.mp3.bCRC := $00000000;
  167. beConfig.Format.mp3.bOriginal := 0;
  168. beConfig.Format.mp3.bPrivate := 0;
  169. }
  170. //Structure information
  171. beConfig.Format.lhv1.dwStructVersion := 1;
  172. beConfig.Format.lhv1.dwStructSize := SizeOf(beConfig);
  173. //Basic encoder setting
  174. beConfig.Format.lhv1.dwSampleRate := WavInfo.SamplesPerSec;
  175. beConfig.Format.lhv1.dwReSampleRate := 44100;
  176. beConfig.Format.lhv1.nMode := BE_MP3_MODE_STEREO;
  177. beConfig.Format.lhv1.dwBitrate := strToInt(MainFrm.Mp3BitRate.Text);
  178. beConfig.Format.lhv1.dwMaxBitrate := strToInt(MainFrm.Mp3BitRate.Text);
  179. beConfig.Format.lhv1.nQuality := 2;
  180. beConfig.Format.lhv1.dwMPegVersion := 1; //MPEG1
  181. beConfig.Format.lhv1.dwPsyModel := 0;
  182. beConfig.Format.lhv1.dwEmphasis := 0;
  183. //Bit Stream Settings
  184. beConfig.Format.lhv1.bPrivate := False;
  185. beConfig.Format.lhv1.bCRC := False;
  186. beConfig.Format.lhv1.bCopyright := True;
  187. beConfig.Format.lhv1.bOriginal := True;
  188. //VBR Stuff
  189. beConfig.Format.lhv1.bWriteVBRHeader := false;
  190. beConfig.Format.lhv1.bEnableVBR := false;
  191. beConfig.Format.lhv1.nVBRQuality := 0;
  192. i := 0;
  193. error := beInitStream(beConfig, dwSamples, dwSamplesMP3, hbeStream);
  194. if error = BE_ERR_SUCCESSFUL
  195. then begin
  196. pBuffer := AllocMem(dwSamples*2);
  197. pMP3Buffer := AllocMem(dwSamplesMP3);
  198. try
  199. done := 0;
  200. error := FileSeek(fs, 0, 0);
  201. While (done < TotalSize) do
  202. begin
  203. if (done + dwSamples*2 < TotalSize)
  204. then ToRead := dwSamples*2
  205. else begin
  206. ToRead := TotalSize-done;
  207. //FillChar(buf[0],dwSamples*2,0);
  208. FillChar(pbuffer^,dwSamples,0);
  209. end;
  210. //if FileRead(fs, buf[0], toread) = -1
  211. if FileRead(fs, pbuffer^, toread) = -1
  212. then raise Exception.Create('Erreur de lecture');
  213. //error := beEncodeChunk(hbeStream, toRead div 2, Buf[0], TmpBuf[0], toWrite);
  214. error := beEncodeChunk(hbeStream, toRead div 2, pBuffer^, pMP3Buffer^, toWrite);
  215. if error <> BE_ERR_SUCCESSFUL
  216. then begin
  217. beCloseStream(hbeStream);
  218. raise Exception.Create('Echec de l''encodage');
  219. end;
  220. //if FileWrite(fd, TmpBuf[0], toWrite) = -1
  221. if FileWrite(fd, pMP3Buffer^, toWrite) = -1
  222. then raise Exception.Create('Erreur d''écriture');
  223. done := done + toread;
  224. inc(i);
  225. if i mod 64 = 0
  226. then begin
  227. MainFrm.ProgressBar1.Position := round(100*done/Totalsize);
  228. Application.ProcessMessages;
  229. end;
  230. end;
  231. error := beDeInitStream(hbeStream, pMP3Buffer^, dwWrite);
  232. //error := beDeInitStream(hbeStream, TmpBuf[0], dwWrite);
  233. if error <> BE_ERR_SUCCESSFUL
  234. then begin
  235. beCloseStream(hbeStream);
  236. raise Exception.Create('Echec à la sortie');
  237. end;
  238. if dwWrite <> 0
  239. then begin
  240. //if FileWrite(fd, TmpBuf[0], dwWrite) = -1
  241. if FileWrite(fd, pMP3Buffer^, dwWrite) = -1
  242. then raise Exception.Create('Erreur à la dernière écriture');
  243. end;
  244. beCloseStream(hbeStream);
  245. finally
  246. FreeMem(pBuffer);
  247. FreeMem(pMP3Buffer);
  248. end;
  249. end
  250. else begin
  251. end;
  252. end;
  253. end.