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
22
23 #include <windows.h>
24 #include <stdio.h>
25 #include <io.h>
26 #include <fcntl.h>
27 #include <sys/stat.h>
28 #include "BladeMP3EncDLL.h"
29
30 BEINITSTREAM beInitStream=NULL;
31 BEENCODECHUNK beEncodeChunk=NULL;
32 BEDEINITSTREAM beDeinitStream=NULL;
33 BECLOSESTREAM beCloseStream=NULL;
34 BEVERSION beVersion=NULL;
35 BEWRITEVBRHEADER beWriteVBRHeader=NULL;
36 BEWRITEINFOTAG beWriteInfoTag=NULL;
37
38 struct Resources
39 {
40 HINSTANCE hDLL;
41 FILE* pFileIn;
42 FILE* pFileOut;
43 HBE_STREAM hbeStream;
44 PBYTE pMP3Buffer;
45 PSHORT pWAVBuffer;
46
ResourcesResources47 Resources()
48 {
49 hDLL =NULL;
50 pFileIn =NULL;
51 pFileOut =NULL;
52 hbeStream =0;
53 pMP3Buffer =NULL;
54 pWAVBuffer =NULL;
55 }
56
~ResourcesResources57 ~Resources()
58 {
59 // close the MP3 Stream
60 if (hbeStream) beCloseStream(hbeStream);
61 // Delete WAV buffer
62 if (pWAVBuffer) delete [] pWAVBuffer;
63 // Delete MP3 Buffer
64 if (pMP3Buffer) delete [] pMP3Buffer;
65 // Close input file
66 if (pFileIn) fclose(pFileIn);
67 // Close output file
68 if (pFileOut) fclose(pFileOut);
69 // Were done, return OK result
70 if (hDLL) FreeLibrary(hDLL);
71 }
72 };
73
74 // Main program
main(int argc,char * argv[])75 int main(int argc, char *argv[])
76 {
77 Resources r; // frees Resources on destruction at block scope end
78 BE_VERSION Version ={0,};
79 BE_CONFIG beConfig ={0,};
80
81 CHAR strFileIn[255] ={'0',};
82 CHAR strFileOut[255] ={'0',};
83
84 DWORD dwSamples =0;
85 DWORD dwMP3Buffer =0;
86 BE_ERR err =0;
87
88 // check number of arguments
89 if(argc != 2)
90 {
91 fprintf(stderr,"Usage: %s <filename.wav>\n", argv[0]);
92 fprintf(stderr,"Descr: Short demo to show how to use the lame_enc.dll library file\n");
93 fprintf(stderr,"Note : WAV file is assumed to to have the following parameters\n");
94 fprintf(stderr," : 44100 Hz, stereo, 16 Bits per sample\n");
95 return -1;
96 }
97
98 // Setup the file names
99 strcpy(strFileIn ,argv[1]);
100 strcpy(strFileOut,argv[1]);
101
102 // Add mp3 extention
103 strcat(strFileOut,".mp3");
104
105 // Load lame_enc.dll library (Make sure though that you set the
106 // project/settings/debug Working Directory correctly, otherwhise the DLL can't be loaded
107
108 r.hDLL = LoadLibrary("lame_enc.dll");
109
110 if ( NULL == r.hDLL )
111 {
112 r.hDLL = LoadLibrary("..\\..\\output\\lame_enc.dll");
113 }
114
115 if( NULL == r.hDLL )
116 {
117 fprintf(stderr,"Error loading lame_enc.DLL");
118 return -1;
119 }
120
121 // Get Interface functions from the DLL
122 beInitStream = (BEINITSTREAM) GetProcAddress(r.hDLL, TEXT_BEINITSTREAM);
123 beEncodeChunk = (BEENCODECHUNK) GetProcAddress(r.hDLL, TEXT_BEENCODECHUNK);
124 beDeinitStream = (BEDEINITSTREAM) GetProcAddress(r.hDLL, TEXT_BEDEINITSTREAM);
125 beCloseStream = (BECLOSESTREAM) GetProcAddress(r.hDLL, TEXT_BECLOSESTREAM);
126 beVersion = (BEVERSION) GetProcAddress(r.hDLL, TEXT_BEVERSION);
127 beWriteVBRHeader= (BEWRITEVBRHEADER) GetProcAddress(r.hDLL,TEXT_BEWRITEVBRHEADER);
128 beWriteInfoTag = (BEWRITEINFOTAG) GetProcAddress(r.hDLL,TEXT_BEWRITEINFOTAG);
129
130 // Check if all interfaces are present
131 if(!beInitStream || !beEncodeChunk || !beDeinitStream || !beCloseStream || !beVersion || !beWriteVBRHeader)
132 {
133 printf("Unable to get LAME interfaces");
134 return -1;
135 }
136
137 // Get the version number
138 beVersion( &Version );
139
140 printf(
141 "lame_enc.dll version %u.%02u (%u/%u/%u)\n"
142 "lame_enc Engine %u.%02u\n"
143 "lame_enc homepage at %s\n\n",
144 Version.byDLLMajorVersion, Version.byDLLMinorVersion,
145 Version.byDay, Version.byMonth, Version.wYear,
146 Version.byMajorVersion, Version.byMinorVersion,
147 Version.zHomepage);
148
149 // Try to open the WAV file, be sure to open it as a binary file!
150 r.pFileIn = fopen( strFileIn, "rb" );
151
152 // Check file open result
153 if(r.pFileIn == NULL)
154 {
155 fprintf(stderr,"Error opening %s", argv[1]);
156 return -1;
157 }
158
159 // Open MP3 file
160 r.pFileOut= fopen(strFileOut,"wb+");
161
162 // Check file open result
163 if(r.pFileOut == NULL)
164 {
165 fprintf(stderr,"Error creating file %s", strFileOut);
166 return -1;
167 }
168
169 memset(&beConfig,0,sizeof(beConfig)); // clear all fields
170
171 // use the LAME config structure
172 beConfig.dwConfig = BE_CONFIG_LAME;
173
174 // this are the default settings for testcase.wav
175 beConfig.format.LHV1.dwStructVersion = 1;
176 beConfig.format.LHV1.dwStructSize = sizeof(beConfig);
177 beConfig.format.LHV1.dwSampleRate = 44100; // INPUT FREQUENCY
178 beConfig.format.LHV1.dwReSampleRate = 0; // DON"T RESAMPLE
179 beConfig.format.LHV1.nMode = BE_MP3_MODE_JSTEREO; // OUTPUT IN STREO
180 beConfig.format.LHV1.dwBitrate = 128; // MINIMUM BIT RATE
181 beConfig.format.LHV1.nPreset = LQP_R3MIX; // QUALITY PRESET SETTING
182 beConfig.format.LHV1.dwMpegVersion = MPEG1; // MPEG VERSION (I or II)
183 beConfig.format.LHV1.dwPsyModel = 0; // USE DEFAULT PSYCHOACOUSTIC MODEL
184 beConfig.format.LHV1.dwEmphasis = 0; // NO EMPHASIS TURNED ON
185 beConfig.format.LHV1.bOriginal = TRUE; // SET ORIGINAL FLAG
186 beConfig.format.LHV1.bWriteVBRHeader = TRUE; // Write INFO tag
187
188 // beConfig.format.LHV1.dwMaxBitrate = 320; // MAXIMUM BIT RATE
189 // beConfig.format.LHV1.bCRC = TRUE; // INSERT CRC
190 // beConfig.format.LHV1.bCopyright = TRUE; // SET COPYRIGHT FLAG
191 // beConfig.format.LHV1.bPrivate = TRUE; // SET PRIVATE FLAG
192 // beConfig.format.LHV1.bWriteVBRHeader = TRUE; // YES, WRITE THE XING VBR HEADER
193 // beConfig.format.LHV1.bEnableVBR = TRUE; // USE VBR
194 // beConfig.format.LHV1.nVBRQuality = 5; // SET VBR QUALITY
195 beConfig.format.LHV1.bNoRes = TRUE; // No Bit resorvoir
196
197 // Preset Test
198 // beConfig.format.LHV1.nPreset = LQP_PHONE;
199
200 // Init the MP3 Stream
201 err = beInitStream(&beConfig, &dwSamples, &dwMP3Buffer, &r.hbeStream);
202
203 // Check result
204 if(err != BE_ERR_SUCCESSFUL)
205 {
206 fprintf(stderr,"Error opening encoding stream (%lu)", err);
207 return -1;
208 }
209
210
211 // Allocate MP3 buffer
212 r.pMP3Buffer = new BYTE[dwMP3Buffer];
213
214 // Allocate WAV buffer
215 r.pWAVBuffer = new SHORT[dwSamples];
216
217 // Check if Buffer are allocated properly
218 if(!r.pMP3Buffer || !r.pWAVBuffer)
219 {
220 printf("Out of memory");
221 return -1;
222 }
223
224 DWORD dwRead=0;
225 DWORD dwWrite=0;
226 DWORD dwDone=0;
227 DWORD dwFileSize=0;
228
229 // Seek to end of file
230 fseek(r.pFileIn,0,SEEK_END);
231
232 // Get the file size
233 dwFileSize=ftell(r.pFileIn);
234
235 // Seek back to start of WAV file,
236 // but skip the first 44 bytes, since that's the WAV header
237 fseek(r.pFileIn,44,SEEK_SET);
238
239
240 // Convert All PCM samples
241 while ( (dwRead=fread(r.pWAVBuffer,sizeof(SHORT),dwSamples,r.pFileIn)) >0 )
242 {
243 // Encode samples
244 err = beEncodeChunk(r.hbeStream, dwRead, r.pWAVBuffer, r.pMP3Buffer, &dwWrite);
245
246 // Check result
247 if(err != BE_ERR_SUCCESSFUL)
248 {
249 fprintf(stderr,"beEncodeChunk() failed (%lu)", err);
250 return -1;
251 }
252
253 // write dwWrite bytes that are returned in tehe pMP3Buffer to disk
254 if(fwrite(r.pMP3Buffer,1,dwWrite,r.pFileOut) != dwWrite)
255 {
256 fprintf(stderr,"Output file write error");
257 return -1;
258 }
259
260 dwDone += dwRead*sizeof(SHORT);
261
262 printf("Done: %0.2f%% \r", 100 * (float)dwDone/(float)(dwFileSize));
263 }
264
265 // Deinit the stream
266 err = beDeinitStream(r.hbeStream, r.pMP3Buffer, &dwWrite);
267
268 // Check result
269 if(err != BE_ERR_SUCCESSFUL)
270 {
271 fprintf(stderr,"beExitStream failed (%lu)", err);
272 return -1;
273 }
274
275 // Are there any bytes returned from the DeInit call?
276 // If so, write them to disk
277 if( dwWrite )
278 {
279 if( fwrite( r.pMP3Buffer, 1, dwWrite, r.pFileOut ) != dwWrite )
280 {
281 fprintf(stderr,"Output file write error");
282 return -1;
283 }
284 }
285
286 // Close output file
287 fclose( r.pFileOut );
288
289 if ( beWriteInfoTag )
290 {
291 // Write the INFO Tag
292 beWriteInfoTag( r.hbeStream, strFileOut );
293 }
294 else
295 {
296 beWriteVBRHeader( strFileOut );
297 }
298
299 // Were done, return OK result
300 return 0;
301 }
302