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
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
|
/*
* FAAC - Freeware Advanced Audio Coder
* Copyright (C) 2001 Menno Bakker
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
* $Id: maingui.c,v 1.21 2007/03/19 19:57:40 menno Exp $
*/
#include <windows.h>
#include <commdlg.h>
#include <commctrl.h>
#include <stdlib.h>
#include "input.h"
#include <faac.h>
#include "resource.h"
static HINSTANCE hInstance;
static char inputFilename[_MAX_PATH], outputFilename[_MAX_PATH];
static BOOL Encoding = FALSE;
static BOOL SelectFileName(HWND hParent, char *filename, BOOL forReading)
{
OPENFILENAME ofn;
ofn.lStructSize = sizeof(OPENFILENAME);
ofn.hwndOwner = hParent;
ofn.hInstance = hInstance;
ofn.nFilterIndex = 0;
ofn.lpstrFileTitle = NULL;
ofn.nMaxFileTitle = 31;
filename [0] = 0x00;
ofn.lpstrFile = (LPSTR)filename;
ofn.nMaxFile = _MAX_PATH;
ofn.lpstrInitialDir = NULL;
ofn.lpstrCustomFilter = NULL;
ofn.nMaxCustFilter = 0;
ofn.nFileOffset = 0;
ofn.nFileExtension = 0;
ofn.lCustData = 0;
ofn.lpfnHook = NULL;
ofn.lpTemplateName = NULL;
if (forReading)
{
char filters[] = { "Wave Files (*.wav)\0*.wav\0" \
"AIFF Files (*.aif;*.aiff;*.aifc)\0*.aif;*.aiff;*.aifc\0" \
"AU Files (*.au)\0*.au\0" \
"All Files (*.*)\0*.*\0\0" };
ofn.lpstrFilter = filters;
ofn.lpstrDefExt = "wav";
ofn.Flags = OFN_EXPLORER | OFN_FILEMUSTEXIST | OFN_HIDEREADONLY;
ofn.lpstrTitle = "Select Source File";
return GetOpenFileName (&ofn);
} else {
char filters [] = { "AAC Files (*.aac)\0*.aac\0" \
"All Files (*.*)\0*.*\0\0" };
ofn.lpstrFilter = filters;
ofn.lpstrDefExt = "aac";
ofn.Flags = OFN_EXPLORER | OFN_PATHMUSTEXIST | OFN_OVERWRITEPROMPT | OFN_HIDEREADONLY;
ofn.lpstrTitle = "Select Output File";
return GetSaveFileName(&ofn);
}
}
static void AwakeDialogControls(HWND hWnd)
{
char szTemp[64];
pcmfile_t *infile = NULL;
unsigned int sampleRate, numChannels;
char *pExt;
if ((infile = wav_open_read(inputFilename, 0)) == NULL)
return;
/* determine input file parameters */
sampleRate = infile->samplerate;
numChannels = infile->channels;
wav_close(infile);
SetDlgItemText (hWnd, IDC_INPUTFILENAME, inputFilename);
strncpy(outputFilename, inputFilename, sizeof(outputFilename) - 5);
pExt = strrchr(outputFilename, '.');
if (pExt == NULL) lstrcat(outputFilename, ".aac");
else lstrcpy(pExt, ".aac");
EnableWindow(GetDlgItem(hWnd, IDC_OUTPUTFILENAME), TRUE);
EnableWindow(GetDlgItem(hWnd, IDC_SELECT_OUTPUTFILE), TRUE);
SetDlgItemText(hWnd, IDC_OUTPUTFILENAME, outputFilename);
wsprintf(szTemp, "%iHz %ich", sampleRate, numChannels);
SetDlgItemText(hWnd, IDC_INPUTPARAMS, szTemp);
EnableWindow(GetDlgItem(hWnd, IDOK), TRUE);
}
static DWORD WINAPI EncodeFile(LPVOID pParam)
{
HWND hWnd = (HWND) pParam;
pcmfile_t *infile = NULL;
GetDlgItemText(hWnd, IDC_INPUTFILENAME, inputFilename, sizeof(inputFilename));
GetDlgItemText(hWnd, IDC_OUTPUTFILENAME, outputFilename, sizeof(outputFilename));
/* open the input file */
if ((infile = wav_open_read(inputFilename, 0)) != NULL)
{
/* determine input file parameters */
unsigned int sampleRate = infile->samplerate;
unsigned int numChannels = infile->channels;
unsigned long inputSamples;
unsigned long maxOutputBytes;
/* open and setup the encoder */
faacEncHandle hEncoder = faacEncOpen(sampleRate, numChannels,
&inputSamples, &maxOutputBytes);
if (hEncoder)
{
HANDLE hOutfile;
char szTemp[256];
/* set encoder configuration */
faacEncConfigurationPtr config = faacEncGetCurrentConfiguration(hEncoder);
config->allowMidside = IsDlgButtonChecked(hWnd, IDC_ALLOWMIDSIDE) == BST_CHECKED ? 1 : 0;
config->useTns = IsDlgButtonChecked(hWnd, IDC_USETNS) == BST_CHECKED ? 1 : 0;
config->useLfe = IsDlgButtonChecked(hWnd, IDC_USELFE) == BST_CHECKED ? 1 : 0;
config->outputFormat = IsDlgButtonChecked(hWnd, IDC_USERAW) == BST_CHECKED ? 0 : 1;
config->mpegVersion = SendMessage(GetDlgItem(hWnd, IDC_MPEGVERSION), CB_GETCURSEL, 0, 0);
config->aacObjectType = SendMessage(GetDlgItem(hWnd, IDC_OBJECTTYPE), CB_GETCURSEL, 0, 0);
if (config->aacObjectType == SSR) /* Set to LTP */
config->aacObjectType = LTP;
GetDlgItemText(hWnd, IDC_QUALITY, szTemp, sizeof(szTemp));
config->quantqual = atoi(szTemp);
if (IsDlgButtonChecked(hWnd, IDC_BWCTL) == BST_CHECKED)
{
GetDlgItemText(hWnd, IDC_BANDWIDTH, szTemp, sizeof(szTemp));
config->bandWidth = atoi(szTemp);
}
else
config->bandWidth = 0;
if (!faacEncSetConfiguration(hEncoder, config))
{
faacEncClose(hEncoder);
wav_close(infile);
MessageBox (hWnd, "faacEncSetConfiguration failed!", "Error", MB_OK | MB_ICONSTOP);
SendMessage(hWnd,WM_SETTEXT,0,(long)"FAAC GUI");
Encoding = FALSE;
SetDlgItemText(hWnd, IDOK, "Encode");
return 0;
}
sprintf(szTemp, "%ld", config->quantqual);
SetDlgItemText(hWnd, IDC_QUALITY, szTemp);
sprintf(szTemp, "%d", config->bandWidth);
SetDlgItemText(hWnd, IDC_BANDWIDTH, szTemp);
/* open the output file */
hOutfile = CreateFile(outputFilename, GENERIC_WRITE, 0, NULL,
CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
if (hOutfile != INVALID_HANDLE_VALUE)
{
UINT startTime = GetTickCount(), lastUpdated = 50;
DWORD totalBytesRead = 0;
unsigned int bytesInput = 0;
DWORD numberOfBytesWritten = 0;
int *pcmbuf;
unsigned char *bitbuf;
char HeaderText[50];
char Percentage[5];
pcmbuf = (short*)LocalAlloc(0, inputSamples*sizeof(int));
bitbuf = (unsigned char*)LocalAlloc(0, maxOutputBytes*sizeof(unsigned char));
SendDlgItemMessage(hWnd, IDC_PROGRESS, PBM_SETRANGE, 0, MAKELPARAM(0, 1024));
SendDlgItemMessage(hWnd, IDC_PROGRESS, PBM_SETPOS, 0, 0);
for ( ;; )
{
int bytesWritten;
UINT timeElapsed, timeEncoded;
bytesInput = wav_read_int24(infile, pcmbuf, inputSamples, NULL) * sizeof(int);
SendDlgItemMessage (hWnd, IDC_PROGRESS, PBM_SETPOS, (unsigned long)((float)totalBytesRead * 1024.0f / (infile->samples*sizeof(int)*numChannels)), 0);
/* Percentage for Dialog Output */
_itoa((int)((float)totalBytesRead * 100.0f / (infile->samples*sizeof(int)*numChannels)),Percentage,10);
lstrcpy(HeaderText,"FAAC GUI: ");
lstrcat(HeaderText,Percentage);
lstrcat(HeaderText,"%");
SendMessage(hWnd,WM_SETTEXT,0,(long)HeaderText);
totalBytesRead += bytesInput;
timeElapsed = (GetTickCount () - startTime) / 10;
timeEncoded = 100.0 * totalBytesRead / (sampleRate * numChannels * sizeof (int));
if (timeElapsed > (lastUpdated + 20))
{
float factor;
unsigned timeLeft;
lastUpdated = timeElapsed;
factor = (float) timeEncoded / (float) (timeElapsed ? timeElapsed : 1);
timeLeft = 10.0 * infile->samples / sampleRate / factor - 0.1 * timeElapsed;
sprintf(szTemp, "Playing time: %2.2i:%04.1f\tEncoding time: %2.2i:%04.1f\n"
"Play/enc factor: %.2f\tEstimated time left: %2.2i:%04.1f",
timeEncoded / 6000, 0.01 * (timeEncoded % 6000),
timeElapsed / 6000, 0.01 * (timeElapsed % 6000),
factor,
timeLeft / 600, 0.1 * (timeLeft % 600)
);
SetDlgItemText(hWnd, IDC_TIME, szTemp);
}
/* call the actual encoding routine */
bytesWritten = faacEncEncode(hEncoder,
pcmbuf,
bytesInput/sizeof(int),
bitbuf,
maxOutputBytes);
/* Stop Pressed */
if ( !Encoding )
break;
/* all done, bail out */
if (!bytesInput && !bytesWritten)
break;
if (bytesWritten < 0)
{
MessageBox (hWnd, "faacEncEncodeFrame failed!", "Error", MB_OK | MB_ICONSTOP);
break;
}
WriteFile(hOutfile, bitbuf, bytesWritten, &numberOfBytesWritten, NULL);
}
CloseHandle(hOutfile);
if (pcmbuf) LocalFree(pcmbuf);
if (bitbuf) LocalFree(bitbuf);
}
faacEncClose(hEncoder);
}
wav_close(infile);
MessageBeep(1);
SendDlgItemMessage(hWnd, IDC_PROGRESS, PBM_SETPOS, 0, 0);
} else {
MessageBox(hWnd, "Couldn't open input file!", "Error", MB_OK | MB_ICONSTOP);
}
SendMessage(hWnd,WM_SETTEXT,0,(long)"FAAC GUI");
Encoding = FALSE;
SetDlgItemText(hWnd, IDOK, "Encode");
return 0;
}
static BOOL WINAPI DialogProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
switch (msg)
{
case WM_INITDIALOG:
{
unsigned long samplesInput, maxBytesOutput;
faacEncHandle hEncoder =
faacEncOpen(44100, 2, &samplesInput, &maxBytesOutput);
faacEncConfigurationPtr myFormat =
faacEncGetCurrentConfiguration(hEncoder);
if (myFormat->version == FAAC_CFG_VERSION)
{
char txt[100];
sprintf(txt, "libfaac version %s", myFormat->name);
SetDlgItemText(hWnd, IDC_COMPILEDATE, txt);
}
else
{
MessageBox(hWnd, "wrong libfaac version", "FAAC",
MB_OK | MB_ICONERROR);
PostMessage(hWnd, WM_CLOSE, 0, 0);
}
faacEncClose(hEncoder);
}
inputFilename[0] = 0x00;
SendMessage(GetDlgItem(hWnd, IDC_MPEGVERSION), CB_ADDSTRING, 0, (LPARAM)(LPCTSTR)"MPEG4");
SendMessage(GetDlgItem(hWnd, IDC_MPEGVERSION), CB_ADDSTRING, 0, (LPARAM)(LPCTSTR)"MPEG2");
SendMessage(GetDlgItem(hWnd, IDC_MPEGVERSION), CB_SETCURSEL, 0, 0);
// SendMessage(GetDlgItem(hWnd, IDC_OBJECTTYPE), CB_ADDSTRING, 0, (LPARAM)(LPCTSTR)"Main");
SendMessage(GetDlgItem(hWnd, IDC_OBJECTTYPE), CB_ADDSTRING, 0, (LPARAM)(LPCTSTR)"Low Complexity");
// SendMessage(GetDlgItem(hWnd, IDC_OBJECTTYPE), CB_ADDSTRING, 0, (LPARAM)(LPCTSTR)"LTP");
SendMessage(GetDlgItem(hWnd, IDC_OBJECTTYPE), CB_SETCURSEL, 0, 0);
CheckDlgButton(hWnd, IDC_ALLOWMIDSIDE, TRUE);
CheckDlgButton(hWnd, IDC_USELFE, FALSE);
CheckDlgButton(hWnd, IDC_USERAW, FALSE);
CheckDlgButton(hWnd, IDC_USETNS, TRUE);
SetDlgItemText(hWnd, IDC_QUALITY, "100");
SetDlgItemText(hWnd, IDC_BANDWIDTH, "0");
DragAcceptFiles(hWnd, TRUE);
return TRUE;
case WM_DROPFILES:
if (DragQueryFile((HDROP) wParam, 0, (LPSTR) inputFilename, _MAX_PATH - 1))
AwakeDialogControls(hWnd);
DragFinish((HDROP) wParam);
return FALSE;
case WM_COMMAND:
switch (wParam)
{
case IDOK:
if ( !Encoding )
{
DWORD retval;
CreateThread(NULL,0,EncodeFile,hWnd,0,&retval);
Encoding = TRUE;
SetDlgItemText(hWnd, IDOK, "Stop");
}
else
{
Encoding = FALSE;
SetDlgItemText(hWnd, IDOK, "Encode");
}
return TRUE;
case IDCANCEL:
EndDialog(hWnd, TRUE);
return TRUE;
case IDC_SELECT_INPUTFILE:
if (SelectFileName(hWnd, inputFilename, TRUE))
AwakeDialogControls(hWnd);
break;
case IDC_SELECT_OUTPUTFILE:
if (SelectFileName(hWnd, outputFilename, FALSE))
{
SetDlgItemText(hWnd, IDC_OUTPUTFILENAME, outputFilename);
}
break;
case IDC_BWCTL:
switch (IsDlgButtonChecked(hWnd, IDC_BWCTL))
{
case BST_CHECKED:
EnableWindow(GetDlgItem(hWnd, IDC_BANDWIDTH), TRUE);
//SetDlgItemText(hWnd, IDC_BANDWIDTH, "0");
break;
case BST_UNCHECKED:
EnableWindow(GetDlgItem(hWnd, IDC_BANDWIDTH), FALSE);
//SetDlgItemText(hWnd, IDC_BANDWIDTH, "");
break;
}
break;
}
break;
}
return FALSE;
}
int WINAPI WinMain (HINSTANCE hInst, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
{
hInstance = hInst;
return DialogBox(hInstance, MAKEINTRESOURCE (IDD_MAINDIALOG), NULL, (DLGPROC) DialogProc);
}
|