2 * Command line frontend program
4 * Copyright (c) 1999 Mark Taylor
5 * 2000 Takehiro TOMINAGA
6 * 2010-2011 Robert Hegemann
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Library General Public
10 * License as published by the Free Software Foundation; either
11 * version 2 of the License, or (at your option) any later version.
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Library General Public License for more details.
18 * You should have received a copy of the GNU Library General Public
19 * License along with this library; if not, write to the
20 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
21 * Boston, MA 02111-1307, USA.
24 /* $Id: main.c,v 1.127 2011/10/02 14:52:20 robert Exp $ */
39 # define strrchr rindex
41 char *strchr(), *strrchr();
43 # define memcpy(d, s, n) bcopy ((s), (d), (n))
44 # define memmove(d, s, n) bcopy ((s), (d), (n))
53 /* woraround for SunOS 4.x, it has SEEK_* defined here */
60 #define PRTYC_REGULAR 2
61 #define PRTYD_MINIMUM -31
62 #define PRTYD_MAXIMUM 31
71 main.c is example code for how to use libmp3lame.a. To use this library,
72 you only need the library and lame.h. All other .h files are private
90 static int c_main(int argc, char *argv[]);
91 extern int lame_main(lame_t gf, int argc, char *argv[]);
94 /************************************************************************
98 * PURPOSE: MPEG-1,2 Layer III encoder with GPSYCHO
99 * psychoacoustic model.
101 ************************************************************************/
104 #if defined( _WIN32 ) && !defined(__MINGW32__)
106 set_process_affinity()
110 the following fix seems to be a workaround for a problem in the
111 parent process calling LAME. It would be better to fix the broken
112 application => code disabled.
115 /* set affinity back to all CPUs. Fix for EAC/lame on SMP systems from
116 "Todd Richmond" <todd.richmond@openwave.com> */
117 typedef BOOL(WINAPI * SPAMFunc) (HANDLE, DWORD_PTR);
121 if ((func = (SPAMFunc) GetProcAddress(GetModuleHandleW(L"KERNEL32.DLL"),
122 "SetProcessAffinityMask")) != NULL) {
124 func(GetCurrentProcess(), si.dwActiveProcessorMask);
134 * Long Filename support for the WIN32 platform
139 dosToLongFileName(char *fn)
141 const int MSIZE = PATH_MAX + 1 - 4; /* we wanna add ".mp3" later */
142 WIN32_FIND_DATAA lpFindFileData;
143 HANDLE h = FindFirstFileA(fn, &lpFindFileData);
144 if (h != INVALID_HANDLE_VALUE) {
148 for (a = 0; a < MSIZE; a++) {
149 if ('\0' == lpFindFileData.cFileName[a])
152 if (a >= MSIZE || a == 0)
154 q = strrchr(fn, '\\');
155 p = strrchr(fn, '/');
159 q = strrchr(fn, ':');
161 strncpy(fn, lpFindFileData.cFileName, a);
166 strncpy(++q, lpFindFileData.cFileName, MSIZE - a);
172 SetPriorityClassMacro(DWORD p)
174 HANDLE op = GetCurrentProcess();
175 return SetPriorityClass(op, p);
179 setProcessPriority(int Priority)
184 SetPriorityClassMacro(IDLE_PRIORITY_CLASS);
185 console_printf("==> Priority set to Low.\n");
189 SetPriorityClassMacro(NORMAL_PRIORITY_CLASS);
190 console_printf("==> Priority set to Normal.\n");
194 SetPriorityClassMacro(HIGH_PRIORITY_CLASS);
195 console_printf("==> Priority set to High.\n");
203 /* OS/2 priority functions */
205 setProcessPriority(int Priority)
212 rc = DosSetPriority(0, /* Scope: only one process */
213 PRTYC_IDLE, /* select priority class (idle, regular, etc) */
215 0); /* Assume current process */
216 console_printf("==> Priority set to 0 (Low priority).\n");
220 rc = DosSetPriority(0, /* Scope: only one process */
221 PRTYC_IDLE, /* select priority class (idle, regular, etc) */
222 PRTYD_MAXIMUM, /* set delta */
223 0); /* Assume current process */
224 console_printf("==> Priority set to 1 (Medium priority).\n");
228 rc = DosSetPriority(0, /* Scope: only one process */
229 PRTYC_REGULAR, /* select priority class (idle, regular, etc) */
230 PRTYD_MINIMUM, /* set delta */
231 0); /* Assume current process */
232 console_printf("==> Priority set to 2 (Regular priority).\n");
236 rc = DosSetPriority(0, /* Scope: only one process */
237 PRTYC_REGULAR, /* select priority class (idle, regular, etc) */
239 0); /* Assume current process */
240 console_printf("==> Priority set to 3 (High priority).\n");
244 rc = DosSetPriority(0, /* Scope: only one process */
245 PRTYC_REGULAR, /* select priority class (idle, regular, etc) */
246 PRTYD_MAXIMUM, /* set delta */
247 0); /* Assume current process */
248 console_printf("==> Priority set to 4 (Maximum priority). I hope you enjoy it :)\n");
252 console_printf("==> Invalid priority specified! Assuming idle priority.\n");
258 /***********************************************************************
262 ***********************************************************************/
265 #if defined( _WIN32 ) && !defined(__MINGW32__)
266 /* Idea for unicode support in LAME, work in progress
267 * - map UTF-16 to UTF-8
268 * - advantage, the rest can be kept unchanged (mostly)
269 * - make sure, fprintf on console is in correct code page
270 * + normal text in source code is in ASCII anyway
271 * + ID3 tags and filenames coming from command line need attention
272 * - call wfopen with UTF-16 names where needed
274 * why not wchar_t all the way?
275 * well, that seems to be a big mess and not portable at all
278 #include <mbstring.h>
280 static wchar_t *mbsToUnicode(const char *mbstr, int code_page)
282 int n = MultiByteToWideChar(code_page, 0, mbstr, -1, NULL, 0);
283 wchar_t* wstr = malloc( n*sizeof(wstr[0]) );
285 n = MultiByteToWideChar(code_page, 0, mbstr, -1, wstr, n);
294 static char *unicodeToMbs(const wchar_t *wstr, int code_page)
296 int n = 1+WideCharToMultiByte(code_page, 0, wstr, -1, 0, 0, 0, 0);
297 char* mbstr = malloc( n*sizeof(mbstr[0]) );
299 n = WideCharToMultiByte(code_page, 0, wstr, -1, mbstr, n, 0, 0);
308 char* mbsToMbs(const char* str, int cp_from, int cp_to)
310 wchar_t* wstr = mbsToUnicode(str, cp_from);
312 char* local8bit = unicodeToMbs(wstr, cp_to);
319 enum { cp_utf8, cp_console, cp_actual };
321 wchar_t *utf8ToUnicode(const char *mbstr)
323 return mbsToUnicode(mbstr, CP_UTF8);
326 char *unicodeToUtf8(const wchar_t *wstr)
328 return unicodeToMbs(wstr, CP_UTF8);
331 char* utf8ToLocal8Bit(const char* str)
333 return mbsToMbs(str, CP_UTF8, CP_ACP);
336 char* utf8ToConsole8Bit(const char* str)
338 return mbsToMbs(str, CP_UTF8, GetConsoleOutputCP());
341 char* local8BitToUtf8(const char* str)
343 return mbsToMbs(str, CP_ACP, CP_UTF8);
346 char* console8BitToUtf8(const char* str)
348 return mbsToMbs(str, GetConsoleOutputCP(), CP_UTF8);
351 char* utf8ToLatin1(char const* str)
353 return mbsToMbs(str, CP_UTF8, 28591); /* Latin-1 is code page 28591 */
356 unsigned short* utf8ToUtf16(char const* mbstr) /* additional Byte-Order-Marker */
358 int n = MultiByteToWideChar(CP_UTF8, 0, mbstr, -1, NULL, 0);
359 wchar_t* wstr = malloc( (n+1)*sizeof(wstr[0]) );
361 wstr[0] = 0xfeff; /* BOM */
362 n = MultiByteToWideChar(CP_UTF8, 0, mbstr, -1, wstr+1, n);
373 int wmain(int argc, wchar_t* argv[])
378 utf8_argv = calloc(argc, sizeof(char*));
379 for (i = 0; i < argc; ++i) {
380 utf8_argv[i] = unicodeToUtf8(argv[i]);
382 ret = c_main(argc, utf8_argv);
383 for (i = 0; i < argc; ++i) {
384 free( utf8_argv[i] );
390 FILE* lame_fopen(char const* file, char const* mode)
393 wchar_t* wfile = utf8ToUnicode(file);
394 wchar_t* wmode = utf8ToUnicode(mode);
395 if (wfile != 0 && wmode != 0) {
396 fh = _wfopen(wfile, wmode);
399 fh = fopen(file, mode);
406 char* lame_getenv(char const* var)
409 wchar_t* wvar = utf8ToUnicode(var);
412 wstr = _wgetenv(wvar);
413 str = unicodeToUtf8(wstr);
422 FILE* lame_fopen(char const* file, char const* mode)
424 return fopen(file, mode);
427 char* lame_getenv(char const* var)
429 char* str = getenv(var);
436 int main(int argc, char *argv[])
438 return c_main(argc, argv);
447 c_main(int argc, char *argv[])
453 argc = ccommand(&argv);
456 /* This gives wildcard expansion on Non-POSIX shells with OS/2 */
457 _wildcard(&argc, &argv);
459 #if defined( _WIN32 ) && !defined(__MINGW32__)
460 set_process_affinity();
463 frontend_open_console();
464 gf = lame_init(); /* initialize libmp3lame */
466 error_printf("fatal error during initialization\n");
470 ret = lame_main(gf, argc, argv);
473 frontend_close_console();