1: 2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20: 21: 22: 23: 24: 25: 26: 27: 28: 29: 30: 31: 32: 33: 34: 35: 36: 37: 38: 39: 40: 41: 42: 43: 44: 45: 46: 47: 48: 49: 50: 51: 52: 53: 54: 55: 56: 57: 58: 59: 60: 61: 62: 63: 64: 65: 66: 67: 68: 69: 70: 71: 72: 73: 74: 75: 76: 77: 78: 79: 80: 81: 82: 83: 84: 85: 86: 87: 88: 89: 90: 91: 92: 93: 94: 95: 96: 97: 98: 99: 100: 101: 102: 103: 104: 105: 106: 107: 108: 109: 110: 111: 112: 113: 114: 115: 116: 117: 118: 119: 120: 121: 122: 123: 124: 125: 126: 127: 128: 129: 130: 131: 132: 133: 134: 135: 136: 137: 138: 139: 140: 141: 142: 143: 144: 145: 146: 147: 148: 149: 150: 151: 152: 153: 154: 155: 156: 157: 158: 159: 160: 161: 162: 163: 164: 165: 166: 167: 168: 169: 170: 171: 172: 173: 174: 175: 176: 177: 178: 179: 180: 181: 182: 183: 184: 185: 186: 187: 188: 189: 190: 191: 192: 193: 194: 195: 196: 197: 198: 199: 200: 201: 202: 203: 204: 205: 206: 207: 208: 209: 210: 211: 212: 213: 214: 215: 216: 217: 218: 219: 220: 221: 222: 223: 224: 225: 226: 227: 228: 229: 230: 231: 232: 233: 234: 235: 236: 237: 238: 239: 240: 241: 242: 243: 244: 245: 246: 247: 248: 249: 250: 251: 252: 253: 254: 255: 256: 257: 258: 259: 260: 261: 262: 263: 264: 265: 266: 267: 268: 269: 270: 271: 272: 273: 274: 275: 276: 277: 278: 279: 280: 281: 282: 283: 284: 285: 286: 287: 288: 289: 290: 291: 292: 293: 294: 295: 296: 297: 298: 299: 300: 301: 302: 303:
| unit mp3export;
interface
Uses SysUtils, WinTypes, WinProcs, Messages, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls;
type THBE_STREAM = LongWord; PHBE_STREAM = ^PHBE_STREAM; BE_ERR = LongWord;
const BE_CONFIG_MP3 = 0; BE_CONFIG_LAME = 256;
BE_ERR_SUCCESSFUL: LongWord = 0; BE_ERR_INVALID_FORMAT: LongWord = 1; BE_ERR_INVALID_FORMAT_PARAMETERS: LongWord = 2; BE_ERR_NO_MORE_HANDLES: LongWord = 3; BE_ERR_INVALID_HANDLE: LongWord = 4;
BE_MAX_HOMEPAGE = 256;
BE_MP3_MODE_STEREO = 0; BE_MP3_MODE_DUALCHANNEL = 2; BE_MP3_MODE_MONO = 3;
type
TMP3 = packed record dwSampleRate : LongWord; byMode : Byte; wBitRate : Word; bPrivate : LongWord; bCRC : LongWord; bCopyright : LongWord; bOriginal : LongWord; end;
TLHV1 = packed record dwStructVersion: DWORD; dwStructSize: DWORD;
dwSampleRate: DWORD; dwReSampleRate: DWORD; nMode: Integer; dwBitrate: DWORD; dwMaxBitrate: DWORD; nQuality: Integer; dwMpegVersion: DWORD; dwPsyModel: DWORD; dwEmphasis: DWORD; bPrivate: LONGBOOL; bCRC: LONGBOOL; bCopyright: LONGBOOL; bOriginal: LONGBOOL; bWriteVBRHeader: LONGBOOL; bEnableVBR: LONGBOOL; nVBRQuality: Integer; btReserved: array[0..255] of Byte; end;
TAAC = packed record dwSampleRate : LongWord; byMode : Byte; wBitRate : Word; byEncodingMethod : Byte; end;
TFormat = packed record case byte of 1 : (mp3 : TMP3); 2 : (lhv1 : TLHV1); 3 : (aac : TAAC); end;
TBE_Config = packed record dwConfig : LongWord; format : TFormat; end;
PBE_Config = ^TBE_Config;
TBE_Version = record byDLLMajorVersion : Byte; byDLLMinorVersion : Byte;
byMajorVersion : Byte; byMinorVersion : Byte;
byDay : Byte; byMonth : Byte; wYear : Word;
zHomePage : Array[0..BE_MAX_HOMEPAGE + 1] of Char; end;
PBE_Version = ^TBE_Version;
Function beInitStream(var pbeConfig: TBE_CONFIG; var dwSample: LongWord; var dwBufferSize: LongWord; var phbeStream: THBE_STREAM ): BE_Err; cdecl; external 'Lame_enc.dll'; Function beEncodeChunk(hbeStream: THBE_STREAM; nSamples: LongWord; var pSample;var pOutput; var pdwOutput: LongWord): BE_Err; cdecl; external 'Lame_enc.dll'; Function beDeinitStream(hbeStream: THBE_STREAM; var pOutput; var pdwOutput: LongWord): BE_Err; cdecl; external 'Lame_enc.dll'; Function beCloseStream(hbeStream: THBE_STREAM): BE_Err; cdecl; external 'Lame_enc.dll'; Procedure beVersion(var pbeVersion: TBE_VERSION); cdecl; external 'Lame_enc.dll';
Procedure EncodeWavToMP3(fs, fd: Integer); implementation
Uses InternetSnd, TraiteWav;
Procedure EncodeWavToMP3(fs, fd: Integer); var err: Integer; beConfig: TBE_Config; dwSamples, dwSamplesMP3 : LongWord; hbeStream : THBE_STREAM; error: BE_ERR; pBuffer: PSmallInt; pMP3Buffer: PByte; Marque:PChar;
done: LongWord; dwWrite: LongWord; ToRead: LongWord; ToWrite: LongWord; i:Integer;
begin beConfig.dwConfig := BE_CONFIG_LAME;
beConfig.Format.lhv1.dwStructVersion := 1; beConfig.Format.lhv1.dwStructSize := SizeOf(beConfig); beConfig.Format.lhv1.dwSampleRate := WavInfo.SamplesPerSec; beConfig.Format.lhv1.dwReSampleRate := 44100; beConfig.Format.lhv1.nMode := BE_MP3_MODE_STEREO; beConfig.Format.lhv1.dwBitrate := strToInt(MainFrm.Mp3BitRate.Text); beConfig.Format.lhv1.dwMaxBitrate := strToInt(MainFrm.Mp3BitRate.Text); beConfig.Format.lhv1.nQuality := 2; beConfig.Format.lhv1.dwMPegVersion := 1; beConfig.Format.lhv1.dwPsyModel := 0; beConfig.Format.lhv1.dwEmphasis := 0; beConfig.Format.lhv1.bPrivate := False; beConfig.Format.lhv1.bCRC := False; beConfig.Format.lhv1.bCopyright := True; beConfig.Format.lhv1.bOriginal := True; beConfig.Format.lhv1.bWriteVBRHeader := false; beConfig.Format.lhv1.bEnableVBR := false; beConfig.Format.lhv1.nVBRQuality := 0;
i := 0; error := beInitStream(beConfig, dwSamples, dwSamplesMP3, hbeStream); if error = BE_ERR_SUCCESSFUL then begin pBuffer := AllocMem(dwSamples*2); pMP3Buffer := AllocMem(dwSamplesMP3); try done := 0;
error := FileSeek(fs, 0, 0); While (done < TotalSize) do begin if (done + dwSamples*2 < TotalSize) then ToRead := dwSamples*2 else begin ToRead := TotalSize-done; FillChar(pbuffer^,dwSamples,0); end;
if FileRead(fs, pbuffer^, toread) = -1 then raise Exception.Create('Erreur de lecture');
error := beEncodeChunk(hbeStream, toRead div 2, pBuffer^, pMP3Buffer^, toWrite);
if error <> BE_ERR_SUCCESSFUL then begin beCloseStream(hbeStream); raise Exception.Create('Echec de l''encodage'); end;
if FileWrite(fd, pMP3Buffer^, toWrite) = -1 then raise Exception.Create('Erreur d''écriture');
done := done + toread; inc(i); if i mod 64 = 0 then begin MainFrm.ProgressBar1.Position := round(100*done/Totalsize); Application.ProcessMessages; end; end;
error := beDeInitStream(hbeStream, pMP3Buffer^, dwWrite); if error <> BE_ERR_SUCCESSFUL then begin beCloseStream(hbeStream); raise Exception.Create('Echec à la sortie'); end;
if dwWrite <> 0 then begin if FileWrite(fd, pMP3Buffer^, dwWrite) = -1 then raise Exception.Create('Erreur à la dernière écriture'); end;
beCloseStream(hbeStream); finally FreeMem(pBuffer); FreeMem(pMP3Buffer); end; end else begin
end; end;
end. |