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