home *** CD-ROM | disk | FTP | other *** search
/ AmigActive 24 / AACD 24.iso / AACD / Sound / LAME / WarpOS / src / frontend / parse.c < prev    next >
Encoding:
C/C++ Source or Header  |  2001-07-01  |  57.6 KB  |  1,432 lines

  1. /*
  2.  *      Command line parsing related functions
  3.  *
  4.  *      Copyright (c) 1999 Mark Taylor
  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. /* $Id: parse.c,v 1.91 2001/07/01 20:04:32 markt Exp $ */
  23.  
  24. #ifdef HAVE_CONFIG_H
  25. # include <config.h>
  26. #endif
  27.  
  28. #include <assert.h>
  29. #include <ctype.h>
  30.  
  31. #ifdef STDC_HEADERS
  32. # include <stdlib.h>
  33. # include <string.h>
  34. #else
  35. # ifndef HAVE_STRCHR
  36. #  define strchr index
  37. #  define strrchr rindex
  38. # endif
  39. char *strchr (), *strrchr ();
  40. # ifndef HAVE_MEMCPY
  41. #  define memcpy(d, s, n) bcopy ((s), (d), (n))
  42. #  define memmove(d, s, n) bcopy ((s), (d), (n))
  43. # endif
  44. #endif
  45.  
  46. #include "lame.h"
  47.  
  48. #include "brhist.h"
  49. #include "parse.h"
  50. #include "main.h"
  51. #include "get_audio.h"
  52.  
  53. #ifdef WITH_DMALLOC
  54. #include <dmalloc.h>
  55. #endif
  56.  
  57.  
  58. /* GLOBAL VARIABLES.  set by parse_args() */
  59. /* we need to clean this up */
  60. sound_file_format input_format;   
  61. int swapbytes;              /* force byte swapping   default=0*/
  62. int silent;
  63. int brhist;
  64. float update_interval;      /* to use Frank's time status display */
  65. int mp3_delay;              /* to adjust the number of samples truncated
  66.                                during decode */
  67. int mp3_delay_set;          /* user specified the value of the mp3 encoder 
  68.                                delay to assume for decoding */
  69. int disable_wav_header;
  70. mp3data_struct mp3input_data; /* used by Ogg and MP3 */
  71.  
  72. int in_signed=1;
  73. int in_unsigned=0;
  74. int in_endian=order_littleEndian;
  75. int in_bitwidth=16;
  76.  
  77.  
  78. /**  
  79.  *  Long Filename support for the WIN32 platform
  80.  *  
  81.  */
  82. #ifdef WIN32
  83. #include <winbase.h>
  84. static void  
  85. dosToLongFileName( char *fn )
  86. {
  87.     const int MSIZE = MAX_NAME_SIZE-4;  //  we wanna add ".mp3" later
  88.     WIN32_FIND_DATAA lpFindFileData;
  89.     HANDLE h = FindFirstFileA( fn, &lpFindFileData );
  90.     if ( h != INVALID_HANDLE_VALUE ) {
  91.         int   a;
  92.         char *q;
  93.         FindClose( h );
  94.         for ( a = 0; a < MSIZE; a++ ) {
  95.             if ( '\0' == lpFindFileData.cFileName[a] ) break;
  96.         }
  97.         if ( a >= MSIZE || a == 0 ) return;
  98.         q = strrchr( fn, '\\' );
  99.         if ( q == NULL ) q = strrchr(fn,':');
  100.         if ( q == NULL ) strncpy( fn, lpFindFileData.cFileName, a );
  101.         else {
  102.             a += q-fn +1;
  103.             if ( a >= MSIZE ) return;
  104.             strncpy( ++q, lpFindFileData.cFileName, MSIZE-a );
  105.         }
  106.     }
  107. }
  108. #endif
  109.  
  110. /************************************************************************
  111. *
  112. * license
  113. *
  114. * PURPOSE:  Writes version and license to the file specified by fp
  115. *
  116. ************************************************************************/
  117.  
  118. static int  lame_version_print ( FILE* const fp )
  119. {
  120.     const char * v = get_lame_version ();
  121.     const char * u = get_lame_url ();
  122.     const int lenv = strlen (v);
  123.     const int lenu = strlen (u);
  124.     const int lw = 80; /* line width of terminal in characters */
  125.     const int sw = 16; /* static width of text */
  126.     
  127.     if (lw >= lenv+lenu+sw || lw < lenu+2)                
  128.         /* text fits in 80 chars per line, or line even too small for url */
  129.         fprintf ( fp, "LAME version %s (%s)\n\n", v, u );
  130.     else            
  131.         /* text too long, wrap url into next line, right aligned */
  132.         fprintf ( fp, "LAME version %s\n%*s(%s)\n\n", v, lw-2-lenu, "", u );
  133.     
  134.     return 0;
  135. }
  136.  
  137. int  print_license ( const lame_global_flags* gfp, FILE* const fp, const char* ProgramName )  /* print version & license */
  138. {
  139.     lame_version_print ( fp );
  140.     fprintf ( fp, 
  141.               "Can I use LAME in my commercial program?\n"
  142.               "\n"
  143.               "Yes, you can, under the restrictions of the LGPL.  In particular, you\n"
  144.               "can include a compiled version of the LAME library (for example,\n"
  145.               "lame.dll) with a commercial program.  Some notable requirements of\n"
  146.               "the LGPL:\n"
  147.               "\n"
  148.               "1. In your program, you cannot include any source code from LAME, with\n"
  149.               "   the exception of files whose only purpose is to describe the library\n"
  150.               "   interface (such as lame.h).\n"
  151.               "\n"
  152.               "2. Any modifications of LAME must be released under the LGPL.\n"
  153.               "   The LAME project (www.mp3dev.org) would appreciate being\n"
  154.               "   notified of any modifications.\n"
  155.               "\n"
  156.               "3. You must give prominent notice that your program is:\n"
  157.               "      A. using LAME (including version number)\n"
  158.               "      B. LAME is under the LGPL\n"
  159.               "      C. Provide a copy of the LGPL.  (the file COPYING contains the LGPL)\n"
  160.               "      D. Provide a copy of LAME source, or a pointer where the LAME\n"
  161.               "         source can be obtained (such as www.mp3dev.org)\n"
  162.               "   An example of prominent notice would be an \"About the LAME encoding engine\"\n"
  163.               "   button in some pull down menu within the executable of your program.\n"
  164.               "\n"
  165.               "4. If you determine that distribution of LAME requires a patent license,\n"
  166.               "   you must obtain such license.\n"
  167.               "\n"
  168.               "\n"
  169.               "*** IMPORTANT NOTE ***\n"
  170.               "\n"
  171.               "The decoding functions provided in LAME use the mpglib decoding engine which\n"
  172.               "is under the GPL.  They may not be used by any program not released under the\n"
  173.               "GPL unless you obtain such permission from the MPG123 project (www.mpg123.de).\n"
  174.               "\n" );
  175.     return 0;
  176. }
  177.  
  178.  
  179. /************************************************************************
  180. *
  181. * usage
  182. *
  183. * PURPOSE:  Writes command line syntax to the file specified by fp
  184. *
  185. ************************************************************************/
  186.  
  187. int  usage ( const lame_global_flags* gfp, FILE* const fp, const char* ProgramName )  /* print general syntax */
  188. {
  189.     lame_version_print ( fp );
  190.     fprintf ( fp,
  191.               "usage: %s [options] <infile> [outfile]\n"
  192.               "\n"
  193.               "    <infile> and/or <outfile> can be \"-\", which means stdin/stdout.\n"
  194.               "\n"
  195.               "Try  \"%s --help\"     for more information\n" 
  196.               "  or \"%s --longhelp\"\n"
  197.               "  or \"%s -?\"         for a complete options list\n\n",
  198.               ProgramName, ProgramName, ProgramName, ProgramName ); 
  199.     return 0;
  200. }
  201.  
  202.  
  203. /************************************************************************
  204. *
  205. * usage
  206. *
  207. * PURPOSE:  Writes command line syntax to the file specified by fp
  208. *           but only the most important ones, to fit on a vt100 terminal
  209. *
  210. ************************************************************************/
  211.  
  212. int  short_help ( const lame_global_flags* gfp, FILE* const fp, const char* ProgramName )  /* print short syntax help */
  213. {
  214.     lame_version_print ( fp );
  215.     fprintf ( fp,
  216.               "usage: %s [options] <infile> [outfile]\n"
  217.               "\n"
  218.               "    <infile> and/or <outfile> can be \"-\", which means stdin/stdout.\n"
  219.               "\n"
  220.               "RECOMMENDED:\n"
  221.               "    lame -h input.wav output.mp3\n"
  222.               "\n"
  223.               "OPTIONS:\n"
  224.               "    -b bitrate      set the bitrate, default 128 kbps\n"
  225.               "    -f              fast mode (lower quality)\n"
  226.               "    -h              higher quality, but a little slower.  Recommended.\n"
  227.               "    -k              keep ALL frequencies (disables all filters)\n"
  228.               "                    Can cause ringing and twinkling\n"
  229.               "    -m mode         (s)tereo, (j)oint, (m)ono or (a)uto\n"
  230.               "                    default is (j) or (s) depending on bitrate\n"
  231.               "    -V n            quality setting for VBR.  default n=%i\n"
  232.               "\n"
  233.               "    --preset type   type must be phone, voice, fm, tape, hifi, cd or studio\n"
  234.               "                    \"--preset help\" gives some more infos on these\n"
  235.               "\n"
  236.               "    --longhelp      full list of options\n"
  237.               "\n",
  238.               ProgramName, lame_get_VBR_q(gfp) );
  239.  
  240.     return 0;
  241. }
  242.  
  243. /************************************************************************
  244. *
  245. * usage
  246. *
  247. * PURPOSE:  Writes command line syntax to the file specified by fp
  248. *
  249. ************************************************************************/
  250.  
  251. static void  wait_for ( FILE* const fp, int lessmode )
  252. {
  253.     if ( lessmode ) {
  254.         fflush ( fp ); 
  255.         getchar (); 
  256.     } else {
  257.         fprintf ( fp, "\n" );
  258.     }
  259.     fprintf ( fp, "\n" );
  260. }
  261.  
  262. int  long_help ( const lame_global_flags* gfp, FILE* const fp, const char* ProgramName, int lessmode )  /* print long syntax help */
  263. {
  264.     lame_version_print ( fp );
  265.     fprintf ( fp,
  266.               "usage: %s [options] <infile> [outfile]\n"
  267.               "\n"
  268.               "    <infile> and/or <outfile> can be \"-\", which means stdin/stdout.\n"
  269.               "\n"
  270.               "RECOMMENDED:\n"
  271.               "    lame -h input.wav output.mp3\n"
  272.               "\n"
  273.               "OPTIONS:\n"
  274.               "  Input options:\n"
  275.               "    -r              input is raw pcm\n"
  276.               "    -x              force byte-swapping of input\n"
  277.               "    -s sfreq        sampling frequency of input file (kHz) - default 44.1 kHz\n"
  278. #ifdef LIBSNDFILE
  279.               "    --bitwidth w    input bit width is w (default 16)\n"
  280.               "    --signed        input is signed (default)\n"
  281.               "    --unsigned      input is unsigned\n"
  282.               "    --little-endian input is little-endian (default from host)\n"
  283.               "    --big-endian    input is big-endian (default from host)\n"
  284. #endif
  285.               "    --mp1input      input file is a MPEG Layer I   file\n"
  286.               "    --mp2input      input file is a MPEG Layer II  file\n"
  287.               "    --mp3input      input file is a MPEG Layer III file\n"
  288.               "    --ogginput      input file is a Ogg Vorbis file",
  289.               ProgramName );
  290.  
  291.     wait_for ( fp, lessmode );
  292.     fprintf ( fp,
  293.               "  Operational options:\n"
  294.               "    -m <mode>       (s)tereo, (j)oint, (f)orce, (m)ono or (a)auto  \n"
  295.               "                    default is (s) or (j) depending on bitrate\n"
  296.               "                    force = force ms_stereo on all frames.\n"
  297.               "                    auto = jstereo, with varialbe mid/side threshold\n"
  298.               "    -a              downmix from stereo to mono file for mono encoding\n"
  299.               "    -d              allow channels to have different blocktypes\n"
  300.               "    --disptime <arg>print progress report every arg seconds\n"
  301.               "    --ogg           encode to Ogg Vorbis instead of MP3\n"
  302.               "    --freeformat    produce a free format bitstream\n"
  303.               "    --decode        input=mp3 file, output=wav\n"
  304.               "    -t              disable writing wav header when using --decode\n"
  305.               "    --comp  <arg>   choose bitrate to achive a compression ratio of <arg>\n"
  306.               "    --scale <arg>   scale input (multiply PCM data) by <arg>\n"
  307.               "    --athonly       only use the ATH for masking\n"
  308.               "    --noath         disable the ATH for masking\n"
  309.               "    --athlower x    lower the ATH x dB\n"
  310.               "    --notemp        disable temporal masking effect\n"
  311.               "    --short         use short blocks\n"
  312.               "    --noshort       do not use short blocks\n"
  313.               "    --voice         experimental voice mode\n"
  314.               "    --preset type   type must be phone, voice, fm, tape, hifi, cd or studio\n"
  315.               "                    \"--preset help\" gives some more infos on these" );
  316.  
  317.     wait_for ( fp, lessmode );
  318.     fprintf ( fp,
  319.               "  Verbosity:\n"
  320.               "    -S              don't print progress report, VBR histograms\n"
  321.               "    --silent        don't print anything on screen\n"
  322.               "    --quiet         don't print anything on screen\n"
  323.               "    --verbose       print a lot of useful information\n"
  324.               "\n"
  325.               "  Noise shaping & psycho acoustic algorithms:\n"
  326.               "    -q <arg>        <arg> = 0...9.  Default  -q 5 \n"
  327.               "                    -q 0:  Highest quality, very slow \n"
  328.               "                    -q 9:  Poor quality, but fast \n"
  329.               "    -h              Same as -q 2.   Recommended.\n"
  330.               "    -f              Same as -q 7.   Fast, ok quality\n" );
  331.  
  332.     wait_for ( fp, lessmode );
  333.     fprintf ( fp,
  334.               "  CBR (constant bitrate, the default) options:\n"
  335.               "    -b <bitrate>    set the bitrate in kbps, default 128 kbps\n"
  336.               "\n"
  337.               "  ABR options:\n"
  338.               "    --abr <bitrate> specify average bitrate desired (instead of quality)\n"
  339.               "\n"
  340.               "  VBR options:\n"
  341.               "    -v              use variable bitrate (VBR) (--vbr-old)\n"
  342.               "    --vbr-old       use old variable bitrate (VBR) routine\n"
  343.               "    --vbr-new       use new variable bitrate (VBR) routine\n"
  344.               "    --vbr-mtrh      a merger of old and new (VBR) routine\n"
  345.               "    -V n            quality setting for VBR.  default n=%i\n"
  346.               "                    0=high quality,bigger files. 9=smaller files\n"
  347.               "    -b <bitrate>    specify minimum allowed bitrate, default  32 kbps\n"
  348.               "    -B <bitrate>    specify maximum allowed bitrate, default 320 kbps\n"
  349.               "    -F              strictly enforce the -b option, for use with players that\n"
  350.               "                    do not support low bitrate mp3 (Apex AD600-A DVD/mp3 player)\n"
  351.               "    -t              disable writing Xing VBR informational tag\n"
  352.               "    --nohist        disable VBR histogram display", 
  353.                                                      lame_get_VBR_q(gfp) );
  354.   
  355.     wait_for ( fp, lessmode );  
  356.     fprintf ( fp,
  357.               "  MP3 header/stream options:\n"
  358.               "    -e <emp>        de-emphasis n/5/c  (obsolete)\n"
  359.               "    -c              mark as copyright\n"
  360.               "    -o              mark as non-original\n"
  361.               "    -p              error protection.  adds 16 bit checksum to every frame\n"
  362.               "                    (the checksum is computed correctly)\n"
  363.               "    --nores         disable the bit reservoir\n"
  364.               "    --strictly-enforce-ISO   comply as much as possible to ISO MPEG spec\n"
  365.               "\n"
  366.               "  Filter options:\n"
  367.               "    -k              keep ALL frequencies (disables all filters),\n"
  368.               "                    Can cause ringing and twinkling\n"
  369.               "  --lowpass <freq>        frequency(kHz), lowpass filter cutoff above freq\n"
  370.               "  --lowpass-width <freq>  frequency(kHz) - default 15%% of lowpass freq\n"
  371.               "  --highpass <freq>       frequency(kHz), highpass filter cutoff below freq\n"
  372.               "  --highpass-width <freq> frequency(kHz) - default 15%% of highpass freq\n"
  373.               "  --resample <sfreq>  sampling frequency of output file(kHz)- default=automatic\n"
  374.               "  --cwlimit <freq>    compute tonality up to freq (in kHz) default 8.8717" );
  375.   
  376.     wait_for ( fp, lessmode );
  377.     fprintf ( fp,
  378.               "  ID3 tag options:\n"
  379.               "    --tt <title>    audio/song title (max 30 chars for version 1 tag)\n"
  380.               "    --ta <artist>   audio/song artist (max 30 chars for version 1 tag)\n"
  381.               "    --tl <album>    audio/song album (max 30 chars for version 1 tag)\n"
  382.               "    --ty <year>     audio/song year of issue (1 to 9999)\n"
  383.               "    --tc <comment>  user-defined text (max 30 chars for v1 tag, 28 for v1.1)\n"
  384.               "    --tn <track>    audio/song track number (1 to 255, creates v1.1 tag)\n"
  385.               "    --tg <genre>    audio/song genre (name or number in list)\n"
  386.               "    --add-id3v2     force addition of version 2 tag\n"
  387.               "    --id3v1-only    add only a version 1 tag\n"
  388.               "    --id3v2-only    add only a version 2 tag\n"
  389.               "    --space-id3v1   pad version 1 tag with spaces instead of nulls\n"
  390.               "    --pad-id3v2     pad version 2 tag with extra 128 bytes\n"
  391.               "    --genre-list    print alphabetically sorted ID3 genre list and exit\n"
  392.               "\n"
  393.               "    Note: A version 2 tag will NOT be added unless one of the input fields\n"
  394.               "    won't fit in a version 1 tag (e.g. the title string is longer than 30\n"
  395.               "    characters), or the '--add-id3v2' or '--id3v2-only' options are used,\n"
  396.               "    or output is redirected to stdout."
  397. #if defined(HAVE_VORBIS)
  398.               "\n\n"
  399.               "    Note: All '--t*' options (except those for track and genre) work for Ogg\n"
  400.               "    Vorbis output, but other ID3-specific options are ignored."
  401. #endif              
  402. #if defined(HAVE_GTK)
  403.               "\n\n"
  404.               "    -g              run graphical analysis on <infile>"
  405. #endif
  406.               );
  407.  
  408.     wait_for ( fp, lessmode );  
  409.     display_bitrates ( fp );
  410.  
  411.     return 0;
  412. }
  413.  
  414. int  extra_help ( const lame_global_flags* gfp, FILE* const fp, const char* ProgramName, int lessmode )  /* print long syntax help */
  415. {
  416.     lame_version_print ( fp );
  417.     fprintf ( fp,
  418.               "Expert options (if you really know what you are doing)\n"
  419.               "\nATH related:\n"
  420.               "--noath               turns ATH down to a flat noise floor\n"
  421.               "--athshort            ignore GPSYCHO for short blocks, use ATH only\n"
  422.               "--athonly             ignore GPSYCHO completely, use ATH only\n"
  423.               "--athtype n           selects between different ATH types [0-5]\n"
  424.               "--athlower x          lowers ATH by x dB\n"
  425.               "--ath-adjust n        ATH auto adjust types 1-3, else no adjustment\n"
  426.               "--adapt-thres-type n  n=1 total energy or n=2 approximated loudness\n"
  427.               "--adapt-thres-level x ??\n"
  428.               "-Y                    different ATH adjust method\n"
  429.               "\nPSY related:\n"
  430.               "--temporal-masking n  use temporal masking effect n=0:no n=1:yes\n"
  431.               "--notemp              disable temporal masking effect\n"
  432.               "--nspsytune           experimental PSY tunings by Naoki Shibata\n"
  433.               "--nssafejoint         M/S switching criterion\n"
  434.               "--ns-bass x           used by above\n"
  435.               "--ns-alto x           used by above\n"         
  436.               "--ns-treble x         used by above\n"
  437.             );
  438.  
  439.     wait_for ( fp, lessmode );  
  440.  
  441.     fprintf ( fp,
  442.               "\nmisc:\n"
  443.               "--nores               disables the use of the bitreservoir\n" 
  444.             );
  445.  
  446.     wait_for ( fp, lessmode );  
  447.  
  448.     return 0;
  449. }
  450.  
  451. static void  display_bitrate ( FILE* const fp, const char* const version, const int div, const int index )
  452. {
  453.     int  i;
  454.     
  455.     fprintf ( fp,
  456.               "\nMPEG-%-3s layer III sample frequencies (kHz):  %2d  %2d  %g\n"
  457.               "bitrates (kbps):", 
  458.               version, 32/div, 48/div, 44.1/div );
  459.     for (i = 1; i <= 14; i++ )          // 14 numbers are printed, not 15, and it was a bug of me
  460.         fprintf ( fp, " %2i", bitrate_table [index] [i] );
  461.     fprintf ( fp, "\n" );
  462. }
  463.  
  464. int  display_bitrates ( FILE* const fp )
  465. {
  466.     display_bitrate ( fp, "1"  , 1, 1 );
  467.     display_bitrate ( fp, "2"  , 2, 0 );
  468.     display_bitrate ( fp, "2.5", 4, 0 );
  469.     fprintf ( fp, "\n" );
  470.     fflush  ( fp );
  471.     return 0;
  472. }
  473.  
  474.  
  475. /************************************************************************
  476. *
  477. * usage
  478. *
  479. * PURPOSE:  Writes presetting info to #stdout#
  480. *
  481. ************************************************************************/
  482.  
  483.  
  484. typedef struct {
  485.     const char* name;                   // name of preset
  486.     long        resample;               // resample frequency in Hz, or -1 for no resampling
  487.     short       highpass_freq;          // highpass frequency in Hz, or -1 for no highpass filtering
  488.     short       lowpass_freq;           // lowpass frequency in Hz, or -1 for no lowpass filtering
  489.     short       lowpass_width;          // lowpass width in Hz
  490.     signed char no_short_blocks;        // use of short blocks, 1: no, 0: yes
  491.     signed char quality;                // quality, the same as -f or -h
  492.     MPEG_mode   mode;                   // channel mode (mono, stereo, joint)
  493.     short       cbr;                    // CBR data rate in kbps (8...320)
  494.     signed char vbr_mode;               // VBR mode (0...9)
  495.     short       vbr_min;                // minimum VBR rate in kbps ( 8...256)
  496.     short       vbr_max;                // maximum VBR rate in kbps (16...320)
  497. } preset_t;
  498.  
  499. const preset_t Presets [] = {
  500.    // name       fs     fu    fo    dfo shrt qual  mode              cbr vbr_mode/min/max
  501.     { "phone" ,  8000, 125,  3400,    0,  1,  5, MONO        ,  16,  6,   8,  24 },  // phone standard 300-3400
  502.     { "phon+" , 11025, 100,  4000,    0,  1,  5, MONO        ,  24,  4,  16,  32 },  // phone theoretical limits
  503.     { "lw"    , 11025,  -1,  4000,    0,  0,  5, MONO        ,  24,  3,  16,  56 },  // LW
  504.     { "mw-eu" , 11025,  -1,  4000,    0,  0,  5, MONO        ,  24,  3,  16,  56 },  // MW in europe
  505.     { "mw-us" , 16000,  -1,  7500,    0,  0,  5, MONO        ,  40,  3,  24, 112 },  // MW in U.S.A.
  506.     { "sw"    , 11025,  -1,  4000,    0,  0,  5, MONO        ,  24,  3,  16,  56 },  // SW
  507.     { "fm"    , 32000,  -1, 15000,    0,  0,  3, JOINT_STEREO, 112,  3,  80, 256 },
  508.     { "voice" , 24000,  -1, 12000,    0,  1,  5, MONO        ,  56,  4,  40, 112 },
  509.     { "radio" ,    -1,  -1, 15000,    0,  0,  3, JOINT_STEREO, 128,  3,  96, 256 },
  510.     { "tape"  ,    -1,  -1, 18000,  900,  0,  3, JOINT_STEREO, 128,  3,  96, 256 },
  511.     { "hifi"  ,    -1,  -1, 18000,  900,  0, -1, JOINT_STEREO, 160,  2, 112, 320 },
  512.     { "cd"    ,    -1,  -1,    -1,   -1,  0, -1, STEREO      , 192,  1, 128, 320 },
  513.     { "studio",    -1,  -1,    -1,   -1,  0, -1, STEREO      , 256,  0, 160, 320 },
  514. };
  515.  
  516.  
  517. static int  presets_info ( const lame_global_flags* gfp, FILE* const fp, const char* ProgramName )  /* print possible combination */
  518. {
  519.     int i;
  520.  
  521.     fprintf ( fp, "\n");    
  522.     lame_version_print ( fp );
  523.     
  524.     fprintf ( fp, "Presets are some shortcuts for common settings.\n");
  525.     fprintf ( fp, "They can be combined with -v if you want VBR MP3s.\n");
  526.     
  527.     fprintf ( fp, "\n                ");
  528.     for ( i = 0; i < sizeof(Presets)/sizeof(*Presets); i++)
  529.         fprintf ( fp,  strlen(Presets[i].name) <= 4 ? "%5s " : " %-5s", Presets[i].name );
  530.     fprintf ( fp, "\n=================");
  531.     for ( i = 0; i < sizeof(Presets)/sizeof(*Presets); i++)
  532.         fprintf ( fp,  "======" );
  533.     fprintf ( fp, "\n--resample      ");
  534.     for ( i = 0; i < sizeof(Presets)/sizeof(*Presets); i++)
  535.         if ( Presets[i].resample < 0 )
  536.             fprintf ( fp,  "      " );
  537.         else
  538.             fprintf ( fp,  "%6.3g",  Presets[i].resample*1.e-3 );
  539.     fprintf ( fp, "\n--highpass      ");
  540.     for ( i = 0; i < sizeof(Presets)/sizeof(*Presets); i++)
  541.         if ( Presets[i].highpass_freq < 0 )
  542.             fprintf ( fp,  "      " );
  543.         else
  544.             fprintf ( fp,  "%6.3g",  Presets[i].highpass_freq*1.e-3 );
  545.     fprintf ( fp, "\n--lowpass       ");
  546.     for ( i = 0; i < sizeof(Presets)/sizeof(*Presets); i++)
  547.         if ( Presets[i].lowpass_freq < 0 )
  548.             fprintf ( fp,  "      " );
  549.         else
  550.             fprintf ( fp,  "%6.3g",  Presets[i].lowpass_freq*1.e-3 );
  551.     fprintf ( fp, "\n--lowpass-width ");
  552.     for ( i = 0; i < sizeof(Presets)/sizeof(*Presets); i++)
  553.         if ( Presets[i].lowpass_width < 0 )
  554.             fprintf ( fp,  "      " );
  555.         else
  556.             fprintf ( fp,  "%6.3g",  Presets[i].lowpass_width*1.e-3 );
  557.     fprintf ( fp, "\n--noshort       ");
  558.     for ( i = 0; i < sizeof(Presets)/sizeof(*Presets); i++)
  559.         switch ( Presets[i].no_short_blocks ) {
  560.         case  1: fprintf ( fp,  "   yes" ); break;
  561.         case  0: fprintf ( fp,  "    no" ); break;
  562.         case -1: fprintf ( fp,  "      " ); break;
  563.         default: assert (0);                break;
  564.         }
  565.     fprintf ( fp, "\n                ");
  566.     for ( i = 0; i < sizeof(Presets)/sizeof(*Presets); i++)
  567.         switch ( Presets[i].mode ) {
  568.         case MONO:         fprintf ( fp, "   -mm"); break;
  569.         case JOINT_STEREO: fprintf ( fp, "   -mj"); break;
  570.         case STEREO:       fprintf ( fp, "   -ms"); break;
  571.         case -1:                  fprintf ( fp, "      "); break;
  572.         default:                  assert (0);              break;
  573.         }
  574.     fprintf ( fp, "\n                ");
  575.     for ( i = 0; i < sizeof(Presets)/sizeof(*Presets); i++)
  576.         switch ( Presets[i].quality ) {
  577.         case -1: fprintf ( fp, "      "); break;
  578.         case  2: fprintf ( fp, "    -h"); break;
  579.         case  3: fprintf ( fp, "   -q3"); break;
  580.         case  5: fprintf ( fp, "      "); break;
  581.         case  7: fprintf ( fp, "    -f"); break;
  582.         default: assert (0);              break;
  583.     }
  584.     fprintf ( fp, "\n-b              ");
  585.     for ( i = 0; i < sizeof(Presets)/sizeof(*Presets); i++)
  586.         fprintf ( fp,  "%6u", Presets[i].cbr );
  587.     fprintf ( fp, "\n-- PLUS WITH -v ");
  588.     for ( i = 0; i < sizeof(Presets)/sizeof(*Presets); i++)
  589.         fprintf ( fp,  "------" );
  590.     fprintf ( fp, "-\n-V              ");
  591.     for ( i = 0; i < sizeof(Presets)/sizeof(*Presets); i++)
  592.         fprintf ( fp,  "%6u", Presets[i].vbr_mode );
  593.     fprintf ( fp, "\n-b              ");
  594.     for ( i = 0; i < sizeof(Presets)/sizeof(*Presets); i++)
  595.         fprintf ( fp,  "%6u", Presets[i].vbr_min );
  596.     fprintf ( fp, "\n-B              ");
  597.     for ( i = 0; i < sizeof(Presets)/sizeof(*Presets); i++)
  598.         fprintf ( fp,  "%6u", Presets[i].vbr_max );
  599.     fprintf ( fp, "\n----------------");
  600.     for ( i = 0; i < sizeof(Presets)/sizeof(*Presets); i++)
  601.         fprintf ( fp,  "------" );
  602.   
  603.     fprintf ( fp, "-\nEXAMPLES:\n");
  604.     fprintf ( fp, " a) --preset fm\n");
  605.     fprintf ( fp, "    equal to: -mj -b112 --resample 32 --lowpass 15 --lowpass-width 0\n");
  606.     fprintf ( fp, " b) -v --preset studio\n");
  607.     fprintf ( fp, "    equals to: -h -ms -V0 -b160 -B320\n");
  608.  
  609.     return 0;
  610. }
  611.  
  612.  
  613. static int  presets_setup ( lame_global_flags* gfp, const char* preset_name, const char* ProgramName )
  614. {
  615.     int i;
  616.  
  617.     for ( i = 0; i < sizeof(Presets)/sizeof(*Presets); i++ )
  618.         if ( 0 == strncmp (preset_name, Presets[i].name, strlen (preset_name) ) ) {
  619.             if ( Presets[i].resample >= 0 )
  620.                 (void) lame_set_out_samplerate( gfp, Presets[i].resample );
  621.             if ( Presets[i].highpass_freq >= 0 )
  622.                 lame_set_highpassfreq(gfp,Presets[i].highpass_freq),
  623.                 lame_set_highpasswidth(gfp,0);
  624.             lame_set_lowpassfreq(gfp,Presets[i].lowpass_freq);
  625.             lame_set_lowpasswidth(gfp,Presets[i].lowpass_width);
  626.             lame_set_no_short_blocks( gfp, Presets[i].no_short_blocks );
  627.             lame_set_quality        ( gfp, Presets[i].quality         );
  628.             lame_set_mode           ( gfp, Presets[i].mode            );
  629.             lame_set_brate(gfp,Presets[i].cbr);
  630.             lame_set_VBR_q(gfp,Presets[i].vbr_mode);
  631.             lame_set_VBR_min_bitrate_kbps(gfp,Presets[i].vbr_min);
  632.             lame_set_VBR_max_bitrate_kbps(gfp,Presets[i].vbr_max);
  633.             return 0;
  634.         }
  635.  
  636.     presets_info ( gfp, stderr, ProgramName );
  637.     return -1;
  638. }
  639.  
  640.  
  641. static void genre_list_handler (int num,const char *name,void *cookie)
  642. {
  643.     printf ("%3d %s\n", num, name);
  644. }
  645.  
  646.  
  647. /************************************************************************
  648. *
  649. * parse_args
  650. *
  651. * PURPOSE:  Sets encoding parameters to the specifications of the
  652. * command line.  Default settings are used for parameters
  653. * not specified in the command line.
  654. *
  655. * If the input file is in WAVE or AIFF format, the sampling frequency is read
  656. * from the AIFF header.
  657. *
  658. * The input and output filenames are read into #inpath# and #outpath#.
  659. *
  660. ************************************************************************/
  661.  
  662. /* would use real "strcasecmp" but it isn't portable */
  663. static int local_strcasecmp ( const char* s1, const char* s2 )
  664. {
  665.     unsigned char c1;
  666.     unsigned char c2;
  667.     
  668.     do {
  669.         c1 = tolower(*s1);
  670.         c2 = tolower(*s2);
  671.         if (!c1) {
  672.             break;
  673.         }
  674.         ++s1;
  675.         ++s2;
  676.     } while (c1 == c2);
  677.     return c1 - c2;
  678. }
  679.  
  680.  
  681. // LAME is a simple frontend which just uses the file extension
  682. // to determine the file type.  Trying to analyze the file
  683. // contents is well beyond the scope of LAME and should not be added.
  684. static int filename_to_type ( const char* FileName )
  685. {
  686.     int len = strlen (FileName);
  687.     
  688.     if ( len < 4 ) return sf_unknown;
  689.  
  690.     FileName += len-4;
  691.     if ( 0 == local_strcasecmp ( FileName, ".mpg" ) ) return sf_mp1;
  692.     if ( 0 == local_strcasecmp ( FileName, ".mp1" ) ) return sf_mp1;
  693.     if ( 0 == local_strcasecmp ( FileName, ".mp2" ) ) return sf_mp2;
  694.     if ( 0 == local_strcasecmp ( FileName, ".mp3" ) ) return sf_mp3;
  695.     if ( 0 == local_strcasecmp ( FileName, ".ogg" ) ) return sf_ogg;
  696.     if ( 0 == local_strcasecmp ( FileName, ".wav" ) ) return sf_wave;
  697.     if ( 0 == local_strcasecmp ( FileName, ".aif" ) ) return sf_aiff;
  698.     if ( 0 == local_strcasecmp ( FileName, ".raw" ) ) return sf_raw;
  699.     return sf_unknown;
  700. }
  701.  
  702. static int resample_rate ( double freq )
  703. {
  704.     if (freq >= 1.e3) freq *= 1.e-3;
  705.  
  706.     switch ( (int)freq ) {
  707.     case  8: return  8000;
  708.     case 11: return 11025;
  709.     case 12: return 12000;
  710.     case 16: return 16000;
  711.     case 22: return 22050;
  712.     case 24: return 24000;
  713.     case 32: return 32000;
  714.     case 44: return 44100;
  715.     case 48: return 48000;
  716.     default: fprintf(stderr,"Illegal resample frequency: %.3f kHz\n", freq );
  717.              return 0;
  718.     }
  719. }
  720.  
  721. /* Ugly, NOT final version */
  722.  
  723. #define T_IF(str)          if ( 0 == local_strcasecmp (token,str) ) {
  724. #define T_ELIF(str)        } else if ( 0 == local_strcasecmp (token,str) ) {
  725. #define T_ELIF2(str1,str2) } else if ( 0 == local_strcasecmp (token,str1)  ||  0 == local_strcasecmp (token,str2) ) {
  726. #define T_ELSE             } else {
  727. #define T_END              }
  728.  
  729. int  parse_args ( lame_global_flags* gfp, int argc, char** argv, 
  730. char* const inPath, char* const outPath, char **nogap_inPath, int *num_nogap)
  731. {
  732.     int         err;
  733.     int         input_file=0;  // set to 1 if we parse an input file name 
  734.     int         i;
  735.     int         autoconvert  = 0;
  736.     double      val;
  737.     int         nogap=0;
  738.     const char* ProgramName  = argv[0]; 
  739.     int count_nogap=0;
  740.  
  741.     inPath [0] = '\0';   
  742.     outPath[0] = '\0';
  743.     /* turn on display options. user settings may turn them off below */
  744.     silent   = 0;
  745.     brhist   = 1;
  746.     mp3_delay = 0;   
  747.     mp3_delay_set=0;
  748.     disable_wav_header=0;
  749.     id3tag_init (gfp);
  750.  
  751.     /* process args */
  752.     for ( i = 0, err = 0; ++i < argc  &&  !err; ) {
  753.         char   c;
  754.         char*  token;
  755.         char*  arg;
  756.         char*  nextArg;
  757.         int    argUsed;
  758.     
  759.         token = argv[i];
  760.         if ( *token++ == '-' ) {
  761.             argUsed = 0;
  762.             nextArg = i+1 < argc  ?  argv[i+1]  :  "";
  763.             
  764.             if (! *token) { /* The user wants to use stdin and/or stdout. */
  765.                 input_file = 1;
  766.                 if (inPath [0] == '\0')
  767.                     strncpy (inPath, argv[i],MAX_NAME_SIZE);
  768.                 else 
  769.                 if (outPath[0] == '\0') 
  770.                     strncpy (outPath, argv[i],MAX_NAME_SIZE);
  771.             } 
  772.             if (*token == '-') { /* GNU style */
  773.                 token++;
  774.  
  775.                 T_IF ("resample")
  776.                     argUsed = 1;
  777.                     (void) lame_set_out_samplerate( gfp,
  778.                         resample_rate ( atof (nextArg) ) );
  779.                 
  780.                 T_ELIF ("vbr-old")
  781.                     lame_set_VBR(gfp,vbr_rh); 
  782.                 
  783.                 T_ELIF ("vbr-new")
  784.                     lame_set_VBR(gfp,vbr_mt); 
  785.                 
  786.                 T_ELIF ("vbr-mtrh")
  787.                     lame_set_VBR(gfp,vbr_mtrh); 
  788.  
  789.                 T_ELIF ("r3mix")
  790.                     lame_set_VBR(gfp,vbr_rh); 
  791.                     lame_set_VBR_q(gfp,1);
  792.                     lame_set_quality( gfp, 2 );
  793.                     lame_set_lowpassfreq(gfp,19500);
  794.                     lame_set_mode( gfp, JOINT_STEREO );
  795.                     lame_set_ATHtype( gfp, 3 );
  796.                     lame_set_VBR_min_bitrate_kbps(gfp,112);
  797.                     
  798.                 
  799.                 /**
  800.                  *  please, do *not* DOCUMENT this one
  801.                  *  it is a developers only switch (rh)
  802.                  */
  803.                 T_ELIF ("remix")
  804.                     lame_set_VBR(gfp,vbr_mtrh); 
  805.                     lame_set_VBR_q(gfp,3);
  806.                     lame_set_quality( gfp, 0 );
  807.                     lame_set_lowpassfreq(gfp,19500);
  808.                     lame_set_mode( gfp, JOINT_STEREO );
  809.                     lame_set_ATHtype( gfp, 3 );
  810.                     lame_set_VBR_min_bitrate_kbps(gfp,32);
  811.                     
  812.                     
  813.                 T_ELIF ("abr")
  814.                     argUsed=1;
  815.                     lame_set_VBR(gfp,vbr_abr); 
  816.                     lame_set_VBR_mean_bitrate_kbps(gfp,atoi(nextArg));
  817.                     /* values larger than 8000 are bps (like Fraunhofer), so it's strange to get 320000 bps MP3 when specifying 8000 bps MP3 */
  818.                     if ( lame_get_VBR_mean_bitrate_kbps(gfp) >= 8000 )
  819.                         lame_set_VBR_mean_bitrate_kbps(gfp,( lame_get_VBR_mean_bitrate_kbps(gfp) + 500 ) / 1000);
  820.  
  821.                     lame_set_VBR_mean_bitrate_kbps(gfp,Min(lame_get_VBR_mean_bitrate_kbps(gfp), 320)); 
  822.                     lame_set_VBR_mean_bitrate_kbps(gfp,Max(lame_get_VBR_mean_bitrate_kbps(gfp),8)); 
  823.                 
  824.                 T_ELIF ("bitwidth")
  825.                     argUsed=1;
  826.                     in_bitwidth=atoi(nextArg);
  827.  
  828.                 T_ELIF ("signed")
  829.             in_signed=1;
  830.  
  831.                 T_ELIF ("unsigned")
  832.             in_signed=0;
  833.  
  834.                 T_ELIF ("little-endian")
  835.             in_endian=order_littleEndian;
  836.  
  837.                 T_ELIF ("big-endian")
  838.             in_endian=order_bigEndian;
  839.  
  840.                 T_ELIF ("mp1input")
  841.                     input_format=sf_mp1;
  842.                 
  843.                 T_ELIF ("mp2input")
  844.                     input_format=sf_mp2;
  845.                 
  846.                 T_ELIF ("mp3input")
  847.                     input_format=sf_mp3;
  848.                 
  849.                 T_ELIF ("ogginput")
  850. #if defined(HAVE_VORBIS)
  851.                     input_format=sf_ogg;
  852. #else
  853.                     fprintf(stderr,"Error: LAME not compiled with Vorbis support\n");
  854.                     return -1;
  855. #endif
  856.                 T_ELIF ("ogg")
  857. #if defined(HAVE_VORBIS)
  858.                     (void) lame_set_ogg( gfp, 1 );
  859. #else
  860.                     fprintf(stderr,"Error: LAME not compiled with Vorbis support\n");
  861.                     return -1;
  862. #endif
  863.                 T_ELIF ("phone")
  864.                     if (presets_setup ( gfp, token, ProgramName ) < 0)
  865.                         return -1;
  866.                     
  867.                 T_ELIF ("voice")
  868.                     if (presets_setup ( gfp, token, ProgramName ) < 0)
  869.                         return -1;
  870.                     
  871.                 T_ELIF ("radio")
  872.                     if (presets_setup ( gfp, token, ProgramName ) < 0)
  873.                         return -1;
  874.                     
  875.                 T_ELIF ("tape")
  876.                     if (presets_setup ( gfp, token, ProgramName ) < 0)
  877.                         return -1;
  878.                     
  879.                 T_ELIF ("cd")
  880.                     if (presets_setup ( gfp, token, ProgramName ) < 0)
  881.                         return -1;
  882.                     
  883.                 T_ELIF ("studio")
  884.                     if (presets_setup ( gfp, token, ProgramName ) < 0)
  885.                         return -1;
  886.                     
  887.                 T_ELIF ("noshort")
  888.                     (void) lame_set_no_short_blocks( gfp, 1 );
  889.                 
  890.                 T_ELIF ("short")
  891.                     (void) lame_set_no_short_blocks( gfp, 0 );
  892.                 
  893.                 T_ELIF ("decode")
  894.                     (void) lame_set_decode_only( gfp, 1 );
  895.  
  896.                 T_ELIF ("decode-mp3delay")
  897.                     mp3_delay = atoi( nextArg );
  898.                     mp3_delay_set=1;
  899.                     argUsed=1;
  900.                 
  901.                 T_ELIF ("noath")
  902.                     (void) lame_set_noATH( gfp, 1 );
  903.                 
  904.                 T_ELIF ("nores")
  905.                     lame_set_disable_reservoir(gfp,1);
  906.                     lame_set_padding_type(gfp,0);
  907.                 
  908.                 T_ELIF ("strictly-enforce-ISO")
  909.                     lame_set_strict_ISO(gfp,1);
  910.                 
  911.                 T_ELIF ("athonly")
  912.                     (void) lame_set_ATHonly( gfp, 1 );
  913.                 
  914.                 T_ELIF ("athlower")
  915.                     argUsed=1;
  916.                     (void) lame_set_ATHlower( gfp, atof( nextArg ) );
  917.                 
  918.                 T_ELIF ("athtype")
  919.                     argUsed=1;
  920.                     (void) lame_set_ATHtype( gfp, atoi( nextArg ) );
  921.  
  922.                 T_ELIF ("ath-adjust")   //  switch for developing, no DOCU
  923.                     argUsed=1;          //  1:Gaby, 2:Robert, 3:Jon, else:off
  924.                     lame_set_adjust_type( gfp, atoi(nextArg) );
  925.  
  926.                 T_ELIF ("adapt-thres-type")
  927.                     argUsed=1;
  928.                     lame_set_adapt_thres_type( gfp, atoi(nextArg) );
  929.  
  930.                 T_ELIF ("adapt-thres-level")
  931.                     argUsed=1;
  932.                     lame_set_adapt_thres_level( gfp, atof(nextArg) );
  933.  
  934.                 T_ELIF ("scale")
  935.                     argUsed=1;
  936.                     (void) lame_set_scale( gfp, atof(nextArg) );
  937.                 
  938.                 T_ELIF ("freeformat")
  939.                     lame_set_free_format(gfp,1);
  940.                 
  941.                 T_ELIF ("athshort")
  942.                     (void) lame_set_ATHshort( gfp, 1 );
  943.                 
  944.                 T_ELIF ("nohist")
  945.                     brhist = 0;
  946.                 
  947.                 /* options for ID3 tag */
  948.                 T_ELIF ("tt")
  949.                     argUsed=1;
  950.                     id3tag_set_title(gfp, nextArg);
  951.                 
  952.                 T_ELIF ("ta")
  953.                     argUsed=1;
  954.                     id3tag_set_artist(gfp, nextArg);
  955.                 
  956.                 T_ELIF ("tl")
  957.                     argUsed=1;
  958.                     id3tag_set_album(gfp, nextArg);
  959.                 
  960.                 T_ELIF ("ty")
  961.                     argUsed=1;
  962.                     id3tag_set_year(gfp, nextArg);
  963.                 
  964.                 T_ELIF ("tc")
  965.                     argUsed=1;
  966.                     id3tag_set_comment(gfp, nextArg);
  967.                 
  968.                 T_ELIF ("tn")
  969.                     argUsed=1;
  970.                     id3tag_set_track(gfp, nextArg);
  971.                 
  972.                 T_ELIF ("tg")
  973.                     argUsed=1;
  974.                     if (id3tag_set_genre(gfp, nextArg)) {
  975.                         fprintf(stderr,"Unknown genre: %s.  Specify genre name or number\n", nextArg);
  976.                         return -1;
  977.                     }
  978.                 
  979.                 T_ELIF ("add-id3v2")
  980.                     id3tag_add_v2(gfp);
  981.                 
  982.                 T_ELIF ("id3v1-only")
  983.                     id3tag_v1_only(gfp);
  984.                 
  985.                 T_ELIF ("id3v2-only")
  986.                     id3tag_v2_only(gfp);
  987.                 
  988.                 T_ELIF ("space-id3v1")
  989.                     id3tag_space_v1(gfp);
  990.                 
  991.                 T_ELIF ("pad-id3v2")
  992.                     id3tag_pad_v2(gfp);
  993.                 
  994.                 T_ELIF ("genre-list")
  995.                     id3tag_genre_list(genre_list_handler, NULL);
  996.                     return -2;
  997.                 
  998.                 T_ELIF ("lowpass")
  999.                     val     = atof( nextArg );
  1000.                     argUsed = 1;
  1001.                     /* useful are 0.001 kHz...50 kHz, 50 Hz...50000 Hz */
  1002.                     if ( val < 0.001 || val > 50000. ) {
  1003.                         fprintf(stderr,"Must specify lowpass with --lowpass freq, freq >= 0.001 kHz\n");
  1004.                         return -1;
  1005.                     }
  1006.                     lame_set_lowpassfreq(gfp,val * (val < 50. ? 1.e3 : 1.e0 ) + 0.5);
  1007.                 
  1008.                 T_ELIF ("lowpass-width")
  1009.                     argUsed=1;
  1010.                     val = 1000.0 * atof( nextArg ) + 0.5;
  1011.                     if (val  < 0) {
  1012.                         fprintf(stderr,"Must specify lowpass width with --lowpass-width freq, freq >= 0 kHz\n");
  1013.                         return -1;
  1014.                     }
  1015.                     lame_set_lowpasswidth(gfp,val); 
  1016.                 
  1017.                 T_ELIF ("highpass")
  1018.                     val = atof( nextArg );
  1019.                     argUsed=1;
  1020.                     /* useful are 0.001 kHz...16 kHz, 16 Hz...50000 Hz */
  1021.                     if ( val < 0.001 || val > 50000. ) {
  1022.                         fprintf(stderr,"Must specify highpass with --highpass freq, freq >= 0.001 kHz\n");
  1023.                         return -1;
  1024.                     }
  1025.                     lame_set_highpassfreq(gfp, val * (val < 16. ? 1.e3 : 1.e0 ) + 0.5);
  1026.                 
  1027.                 T_ELIF ("highpass-width")
  1028.                     argUsed=1;
  1029.                     val = 1000.0 * atof( nextArg ) + 0.5;
  1030.                     if (val   < 0) {
  1031.                         fprintf(stderr,"Must specify highpass width with --highpass-width freq, freq >= 0 kHz\n");
  1032.                         return -1;
  1033.                     }
  1034.                     lame_set_highpasswidth(gfp,val);
  1035.                 
  1036.                 T_ELIF ("cwlimit")
  1037.                     val = atof (nextArg);
  1038.                     argUsed=1;
  1039.                     /* useful are 0.001 kHz...50 kHz, 50 Hz...50000 Hz */
  1040.                     {
  1041.                         int my_cwlimit = val * ( val <= 50. ? 1.e3 : 1.e0 );
  1042.                         lame_set_cwlimit( gfp, my_cwlimit );
  1043.                         if ( my_cwlimit <= 0 ) {
  1044.                             fprintf( stderr,
  1045.               "Must specify cwlimit with --cwlimit freq, freq >= 0.001 kHz\n" );
  1046.                             return -1;
  1047.                         }
  1048.                     }
  1049.                  
  1050.                 T_ELIF ("comp")
  1051.                     argUsed=1;
  1052.                     val = atof( nextArg );
  1053.                     if (val  < 1.0 ) {
  1054.                         fprintf(stderr,"Must specify compression ratio >= 1.0\n");
  1055.                         return -1;
  1056.                     }
  1057.                     lame_set_compression_ratio(gfp,val);
  1058.                 
  1059.                 T_ELIF ("notemp")
  1060.                     (void) lame_set_useTemporal( gfp, 0 );
  1061.  
  1062.                 T_ELIF ("temporal-masking")
  1063.                     argUsed = 1;
  1064.                     (void) lame_set_useTemporal( gfp, atoi(nextArg)?1:0 );
  1065.  
  1066.                 T_ELIF ("nspsytune")
  1067.                     lame_set_exp_nspsytune(gfp, lame_get_exp_nspsytune(gfp) | 1);
  1068.                     lame_set_experimentalZ(gfp,1);
  1069.                     lame_set_experimentalX(gfp,1);
  1070.                 
  1071.                 T_ELIF ("nssafejoint")
  1072.                     lame_set_exp_nspsytune(gfp,lame_get_exp_nspsytune(gfp) | 2);
  1073.                 
  1074.                 T_ELIF ("ns-bass")
  1075.                     argUsed=1;
  1076.                     {
  1077.                       double d;
  1078.                       int k;
  1079.                       d = atof( nextArg );
  1080.                       k = (int)(d * 4);
  1081.                       if (k < -32) k = -32;
  1082.                       if (k >  31) k =  31;
  1083.                       if (k < 0) k += 64;
  1084.                       lame_set_exp_nspsytune(gfp, lame_get_exp_nspsytune(gfp) | (k << 2));
  1085.                     }
  1086.                 
  1087.                 T_ELIF ("ns-alto")
  1088.                     argUsed=1;
  1089.                     {
  1090.                       double d;
  1091.                       int k;
  1092.                       d = atof( nextArg );
  1093.                       k = (int)(d * 4);
  1094.                       if (k < -32) k = -32;
  1095.                       if (k >  31) k =  31;
  1096.                       if (k < 0) k += 64;
  1097.                       lame_set_exp_nspsytune(gfp, lame_get_exp_nspsytune(gfp) | (k << 8));
  1098.                     }
  1099.                 
  1100.                 T_ELIF ("ns-treble")
  1101.                     argUsed=1;
  1102.                     {
  1103.                       double d;
  1104.                       int k;
  1105.                       d = atof( nextArg );
  1106.                       k = (int)(d * 4);
  1107.                       if (k < -32) k = -32;
  1108.                       if (k >  31) k =  31;
  1109.                       if (k < 0) k += 64;
  1110.                       lame_set_exp_nspsytune(gfp, lame_get_exp_nspsytune(gfp) | (k << 14));
  1111.                     }
  1112.                 
  1113.                 /* some more GNU-ish options could be added
  1114.                  * brief         => few messages on screen (name, status report)
  1115.                  * o/output file => specifies output filename
  1116.                  * O             => stdout
  1117.                  * i/input file  => specifies input filename
  1118.                  * I             => stdin
  1119.                  */
  1120.                 T_ELIF2 ("quiet", "silent")
  1121.                     silent = 10;    /* on a scale from 1 to 10 be very silent */
  1122.                 
  1123.                 T_ELIF ("verbose")
  1124.                     silent = -10;    /* print a lot on screen, the default */
  1125.                     
  1126.                 T_ELIF2 ("version", "license")
  1127.                     print_license ( gfp, stdout, ProgramName );
  1128.                     return -2;
  1129.                 
  1130.                 T_ELIF2 ("help", "usage")
  1131.                     short_help ( gfp, stdout, ProgramName );
  1132.                     return -2;
  1133.                 
  1134.                 T_ELIF ("longhelp")
  1135.                     long_help ( gfp, stdout, ProgramName, 0 /* lessmode=NO */ );
  1136.                     return -2;
  1137.                     
  1138.                 T_ELIF ("extrahelp")
  1139.                     extra_help ( gfp, stdout, ProgramName, 0 /* lessmode=NO */ );
  1140.                     return -2;
  1141.                     
  1142.                 T_ELIF ("?")
  1143. #ifdef __unix__
  1144.                     FILE* fp = popen ("less -Mqc", "w");
  1145.                     long_help ( gfp, fp, ProgramName, 0 /* lessmode=NO */ );
  1146.                     pclose (fp);
  1147. #else           
  1148.                     long_help ( gfp, stdout, ProgramName, 1 /* lessmode=YES */ );
  1149. #endif              
  1150.                     return -2;
  1151.                 
  1152.                 T_ELIF ("preset")
  1153.                     argUsed = 1;
  1154.                     if (presets_setup ( gfp, nextArg, ProgramName ) < 0)
  1155.                         return -1;
  1156.                     
  1157.                 T_ELIF ("disptime")
  1158.                     argUsed = 1;
  1159.                     update_interval = atof (nextArg);
  1160.  
  1161.                 T_ELIF ("nogap")
  1162.                     nogap=1;
  1163.                     
  1164.                 T_ELSE
  1165.                     fprintf(stderr,"%s: unrec option --%s\n", ProgramName, token);
  1166.                     
  1167.                 T_END
  1168.                 
  1169.                 i += argUsed;
  1170.                 
  1171.             } else {
  1172.                 while ( (c = *token++) != '\0' ) {
  1173.                     arg = *token ? token : nextArg;
  1174.                     switch (c) {
  1175.                     case 'm':        
  1176.                         argUsed           = 1;   
  1177.                         
  1178.                         switch ( *arg ) {
  1179.                         case 's': (void) lame_set_mode( gfp, STEREO       );
  1180.                                   break;
  1181.                         case 'd': (void) lame_set_mode( gfp, DUAL_CHANNEL );
  1182.                                   fprintf( stderr,
  1183.                                            "%s: dual channel is not supported yet, the result (perhaps stereo)\n"
  1184.                                            "  may not be what you expect\n",
  1185.                                            ProgramName );
  1186.                                   break;
  1187.                         case 'f': lame_set_force_ms(gfp,1);
  1188.                                  /* FALLTHROUGH */
  1189.                         case 'j': (void) lame_set_mode( gfp, JOINT_STEREO );
  1190.                                   break;
  1191.                         case 'm': (void) lame_set_mode( gfp, MONO         );
  1192.                                   break;
  1193.                         case 'a': (void) lame_set_mode_automs( gfp, 1 );
  1194.                                   /* lame picks mode and uses variable MS
  1195.                                      threshold */
  1196.                                   break; 
  1197.                         default : fprintf(stderr,"%s: -m mode must be s/d/j/f/m not %s\n", ProgramName, arg);
  1198.                             err = 1;
  1199.                             break;
  1200.                         }
  1201.                         break;
  1202.             
  1203.                     case 'V':        argUsed = 1;
  1204.                         /* to change VBR default look in lame.h */
  1205.                         if (lame_get_VBR(gfp) == vbr_off) lame_set_VBR(gfp,vbr_default);  
  1206.                         lame_set_VBR_q(gfp,atoi(arg));
  1207.                         if (lame_get_VBR_q(gfp) <0) lame_set_VBR_q(gfp,0);
  1208.                         if (lame_get_VBR_q(gfp) >9) lame_set_VBR_q(gfp,9);
  1209.                         break;
  1210.                     case 'v': 
  1211.                         /* to change VBR default look in lame.h */
  1212.                         if (lame_get_VBR(gfp) == vbr_off)
  1213.                             lame_set_VBR(gfp,vbr_default); 
  1214.                         break;
  1215.  
  1216.                     case 'q':        argUsed = 1;
  1217.                         {
  1218.                             int tmp_quality = atoi( arg );
  1219.  
  1220.                             /* XXX should we move this into lame_set_quality()? */
  1221.                             if( tmp_quality < 0 ) tmp_quality = 0;
  1222.                             if( tmp_quality > 9 ) tmp_quality = 9;
  1223.  
  1224.                             (void) lame_set_quality( gfp, tmp_quality );
  1225.                         }
  1226.                         break;
  1227.                     case 'f': 
  1228.                         (void) lame_set_quality( gfp, 7 );
  1229.                         break;
  1230.                     case 'h': 
  1231.                         (void) lame_set_quality( gfp, 2 );
  1232.                         break;
  1233.  
  1234.                     case 's':
  1235.                         argUsed = 1;
  1236.                         val = atof( arg );
  1237.                         (void) lame_set_in_samplerate( gfp,
  1238.                             val * ( val <= 192 ? 1.e3 : 1.e0 ) + 0.5 );
  1239.                         break;
  1240.                     case 'b':        
  1241.                         argUsed = 1;
  1242.                         lame_set_brate(gfp,atoi(arg)); 
  1243.                         lame_set_VBR_min_bitrate_kbps(gfp,lame_get_brate(gfp));
  1244.                         break;
  1245.                     case 'B':        
  1246.                         argUsed = 1;
  1247.                         lame_set_VBR_max_bitrate_kbps(gfp,atoi(arg)); 
  1248.                         break;  
  1249.                     case 'F':        
  1250.                         lame_set_VBR_hard_min(gfp,1);
  1251.                         break;  
  1252.                     case 't':  /* dont write VBR tag */
  1253.                         (void) lame_set_bWriteVbrTag( gfp, 0 );
  1254.                         disable_wav_header=1;
  1255.                         break;
  1256.                     case 'r':  /* force raw pcm input file */
  1257. #if defined(LIBSNDFILE)
  1258.                         fprintf(stderr,"WARNING: libsndfile may ignore -r and perform fseek's on the input.\n"
  1259.                                "Compile without libsndfile if this is a problem.\n");
  1260. #endif
  1261.                         input_format=sf_raw;
  1262.                         break;
  1263.                     case 'x':  /* force byte swapping */
  1264.                         swapbytes=1;
  1265.                         break;
  1266.                     case 'p': /* (jo) error_protection: add crc16 information to stream */
  1267.                         lame_set_error_protection(gfp,1);
  1268.                         break;
  1269.                     case 'a': /* autoconvert input file from stereo to mono - for mono mp3 encoding */
  1270.                         autoconvert=1;
  1271.                         (void) lame_set_mode( gfp, MONO );
  1272.                         break;
  1273.                     case 'k': 
  1274.                         lame_set_lowpassfreq(gfp,-1);
  1275.                         lame_set_highpassfreq(gfp,-1);
  1276.                         break;
  1277.                     case 'd': 
  1278.                         (void) lame_set_allow_diff_short( gfp, 1 );
  1279.                         break;
  1280.                     case 'S': 
  1281.                         silent = 1;
  1282.                         break;
  1283.                     case 'X':        
  1284.                         argUsed = 1;   
  1285.                         lame_set_experimentalX(gfp,atoi(arg)); 
  1286.                         break;
  1287.                     case 'Y': 
  1288.                         lame_set_experimentalY(gfp,1);
  1289.                         break;
  1290.                     case 'Z': 
  1291.                         lame_set_experimentalZ(gfp,1);
  1292.                         break;
  1293. #if defined(HAVE_GTK)
  1294.                     case 'g': /* turn on gtk analysis */
  1295.                         (void) lame_set_analysis( gfp, 1 );
  1296.                         break;
  1297. #endif                  
  1298.                     case 'e':        
  1299.                         argUsed = 1;
  1300.                         
  1301.                         switch (*arg) {
  1302.                         case 'n': lame_set_emphasis( gfp, 0 ); break;
  1303.                         case '5': lame_set_emphasis( gfp, 1 ); break;
  1304.                         case 'c': lame_set_emphasis( gfp, 3 ); break;
  1305.                         default : fprintf(stderr,"%s: -e emp must be n/5/c not %s\n", ProgramName, arg );
  1306.                             err = 1;
  1307.                             break;
  1308.                         }
  1309.                         break;
  1310.                     case 'c':   
  1311.                         lame_set_copyright(gfp,1);
  1312.                         break;
  1313.                     case 'o':   
  1314.                         lame_set_original(gfp,0);
  1315.                         break;
  1316.                         
  1317.                     case '?':   
  1318.                         long_help ( gfp, stderr, ProgramName, 0 /* LESSMODE=NO */);
  1319.                         return -1;
  1320.                         
  1321.                     default:    
  1322.                         fprintf(stderr,"%s: unrec option %c\n", ProgramName, c);
  1323.                         err = 1; 
  1324.                         break;
  1325.                     }
  1326.                     if (argUsed) {
  1327.                         if(arg == token)    token = "";   /* no more from token */
  1328.                         else                ++i;          /* skip arg we used */
  1329.                         arg = ""; argUsed = 0;
  1330.                     }
  1331.                 }
  1332.             }   
  1333.         } else {
  1334.             if (nogap) {
  1335.                 if ((num_nogap != NULL) && (count_nogap < *num_nogap)) {
  1336.                     strncpy(nogap_inPath[count_nogap++],argv[i],MAX_NAME_SIZE);
  1337.                     input_file=1;
  1338.                 } else {
  1339.                     /* sorry, calling program did not allocate enough space */
  1340.                     fprintf(stderr,"Error: 'nogap option'.  Calling program does not allow nogap option, or\nyou have exceeded maximum number of input files for the nogap option\n");
  1341.                     *num_nogap=-1;
  1342.                     return -1;
  1343.                 }
  1344.             }else{
  1345.                 /* normal options:   inputfile  [outputfile], and
  1346.                    either one can be a '-' for stdin/stdout */
  1347.                 if (inPath [0] == '\0') {     
  1348.                     strncpy(inPath , argv[i], MAX_NAME_SIZE);
  1349.                     input_file=1;
  1350.                 } else {
  1351.                     if (outPath[0] == '\0') 
  1352.                         strncpy(outPath, argv[i], MAX_NAME_SIZE);
  1353.                     else {
  1354.                         fprintf(stderr,"%s: excess arg %s\n", ProgramName, argv[i]);
  1355.                         err = 1;
  1356.                     }
  1357.                 }
  1358.             }
  1359.         }
  1360.     }  /* loop over args */
  1361.     
  1362.     if ( err  ||  !input_file ) {
  1363.         usage ( gfp, stderr, ProgramName );
  1364.         return -1;
  1365.     }
  1366.         
  1367.     if ( inPath[0] == '-' ) 
  1368.         silent = 1;  /* turn off status - it's broken for stdin */
  1369. #ifdef WIN32
  1370.     else
  1371.         dosToLongFileName( inPath );
  1372. #endif
  1373.     
  1374.     if ( outPath[0] == '\0' ) {
  1375.         if ( inPath[0] == '-' ) {
  1376.             /* if input is stdin, default output is stdout */
  1377.             strcpy(outPath,"-");
  1378.         } else {
  1379.             strncpy(outPath, inPath, MAX_NAME_SIZE - 4);
  1380.             if ( lame_get_decode_only( gfp ) ) {
  1381.                 strncat (outPath, ".wav", 4 );
  1382.             } else if( lame_get_ogg( gfp ) ) {
  1383.                 strncat (outPath, ".ogg", 4 );
  1384.             } else {
  1385.                 strncat (outPath, ".mp3", 4 );
  1386.             }
  1387.         }
  1388.     }
  1389.     /* some file options not allowed with stdout */
  1390.     if (outPath[0]=='-') {
  1391.         (void) lame_set_bWriteVbrTag( gfp, 0 ); /* turn off VBR tag */
  1392.     }
  1393.     
  1394.     /* if user did not explicitly specify input is mp3, check file name */
  1395.     if (input_format == sf_unknown)
  1396.         input_format = filename_to_type ( inPath );
  1397.     
  1398. #if !(defined HAVE_MPGLIB || defined AMIGA_MPEGA)
  1399.     if ( input_format == sf_mp1 ||
  1400.          input_format == sf_mp2 ||
  1401.          input_format == sf_mp3) {
  1402.         fprintf(stderr,"Error: libmp3lame not compiled with mpg123 *decoding* support \n");
  1403.         return -1;
  1404.     }
  1405. #endif
  1406.  
  1407. #if !(defined HAVE_VORBIS)
  1408.     if ( input_format == sf_ogg ) {
  1409.         fprintf(stderr,"Error: LAME not compiled with Vorbis support\n");
  1410.         return -1;
  1411.     }
  1412. #endif
  1413.     /* default guess for number of channels */
  1414.     if (autoconvert) 
  1415.         (void) lame_set_num_channels( gfp, 2 ); 
  1416.     else if( MONO == lame_get_mode( gfp ) ) 
  1417.         (void) lame_set_num_channels( gfp, 1 );
  1418.     else 
  1419.         (void) lame_set_num_channels( gfp, 2 );
  1420.     
  1421.     if ( lame_get_free_format(gfp) ) {
  1422.         if ( lame_get_brate(gfp) < 8  ||  lame_get_brate(gfp) > 640 ) {
  1423.             fprintf(stderr,"For free format, specify a bitrate between 8 and 640 kbps\n");
  1424.             return -1;
  1425.         }
  1426.     }
  1427.     if (num_nogap!=NULL) *num_nogap=count_nogap;
  1428.     return 0;
  1429. }
  1430.  
  1431. /* end of parse.c */
  1432.