home *** CD-ROM | disk | FTP | other *** search
/ Windows 95 Secrets / Secrets2.iso / Audio / WAV / MaplayP / _SETUP.1 / cmdline.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  1997-07-06  |  10.0 KB  |  408 lines

  1. /* cmdline.cpp
  2.  
  3.    Command line version of maplay written by John Fehr
  4.  
  5.    Fixes for HP-UX by Earle F Philhower <earle@geocities.com>
  6.  
  7.    This can be used for unix systems as well as the Win32 console. */
  8.  
  9. #ifndef __DATE__
  10. #define __DATE__  Jul 07, 1997
  11. #endif // __DATE__
  12.  
  13. #ifndef __TIME__
  14. #define __TIME__   12:00:00
  15. #endif // __TIME__
  16.  
  17. #ifdef __WIN32__
  18. #include <windows.h>
  19. #endif // __WIN32__
  20.  
  21. #include <stdio.h>
  22. #include <string.h>
  23. #include <iostream.h>
  24.  
  25. #include "all.h"
  26. #include "crc.h"
  27. #include "args.h"
  28.  
  29. // What character marks the end of a directory entry? For DOS and
  30. // Windows, it is "\"; in UNIX it is "/".
  31. #if defined(__WIN32__) || defined(OS2) || defined(__DOS__)
  32. #define DIR_MARKER '\\'
  33. #else
  34. #define DIR_MARKER '/'
  35. #endif // __WIN32__
  36.  
  37. #ifdef IRIX
  38. #define MAPLAY_VERSION "SGI IRIX"
  39. #define MAPLAY_PORTER  "Andres Meyer (ameyer@desun1.epfl.ch)"
  40. #define MAPLAY_PORT_VERSION_NUMBER "1.1"
  41. #else
  42. #ifdef SunOS4_1_1
  43. #define MAPLAY_VERSION "SPARC SunOS 4.1.1"
  44. #define MAPLAY_PORTER  "Anonymous SPARC SunOS 4.1.1 Porter"
  45. #define MAPLAY_PORT_VERSION_NUMBER "1.0"
  46. #else
  47. #ifdef SunOS4_1_3
  48. #define MAPLAY_VERSION "SPARC SunOS 4.1.3"
  49. #define MAPLAY_PORTER  "Anonymous SPARC SunOS 4.1.3 Porter"
  50. #define MAPLAY_PORT_VERSION_NUMBER "1.0"
  51. #else
  52. #ifdef Solaris
  53. #define MAPLAY_VERSION "SPARC Solaris 2.x"
  54. #define MAPLAY_PORTER  "Anonymous SPARC Solaris 2.x Porter"
  55. #define MAPLAY_PORT_VERSION_NUMBER "1.0"
  56. #else
  57. #ifdef LINUX
  58. #ifdef PENTIUM
  59. #define MAPLAY_VERSION "Linux, Pentium CPU"
  60. #else
  61. #define MAPLAY_VERSION "Linux, 486 CPU"
  62. #endif // PENTIUM
  63. #define MAPLAY_PORTER  "Anonymous Linux Porter"
  64. #define MAPLAY_PORT_VERSION_NUMBER "1.0"
  65. #else
  66. #ifdef __FreeBSD__
  67. #ifdef PENTIUM
  68. #define MAPLAY_VERSION "FreeBSD, Pentium CPU"
  69. #else
  70. #define MAPLAY_VERSION "FreeBSD, 486 CPU"
  71. #endif // PENTIUM
  72. #define MAPLAY_PORTER  "Jeff Tsay"
  73. #define MAPLAY_PORT_VERSION_NUMBER "1.1"
  74. #else
  75. #ifdef ULTRIX
  76. #define MAPLAY_VERSION "RISC ULTRIX 4.x"
  77. #define MAPLAY_PORTER  "Anonymous ULTRIX Porter"
  78. #define MAPLAY_PORT_VERSION_NUMBER "1.0"
  79. #else
  80. #ifdef HPUX
  81. #define MAPLAY_VERSION "HP-UX 9.05 or greater"
  82. #define MAPLAY_PORTER  "Earle Philhower (earle@geocities.com)"
  83. #define MAPLAY_PORT_VERSION_NUMBER "1.1"
  84. #else
  85. #ifdef AIX
  86. #define MAPLAY_VERSION "AIX"
  87. #define MAPLAY_PORTER  "Anonymous AIX Porter"
  88. #define MAPLAY_PORT_VERSION_NUMBER "1.0"
  89. #else
  90. #ifdef NeXT
  91. #define MAPLAY_VERSION "NeXTStep 3.2"
  92. #define MAPLAY_PORTER  "Anonymous NeXTStep 3.2 Porter"
  93. #define MAPLAY_PORT_VERSION_NUMBER "1.0"
  94. #else
  95. #ifdef __WIN32__
  96. #ifdef PENTIUM
  97. #define MAPLAY_VERSION "Win32 Console, Pentium CPU"
  98. #else
  99. #define MAPLAY_VERSION "Win32 Console, 486 CPU"
  100. #endif // PENTIUM
  101. #define MAPLAY_PORTER  "Jeff Tsay"
  102. #define MAPLAY_PORT_VERSION_NUMBER "1.1"
  103. #else
  104. #ifdef BEOS
  105. #define MAPLAY_VERSION "BeOS DR8.2/DR8.3"
  106. #define MAPLAY_PORTER  "Thomas Philippe (tphilippe@sdv.fr)"
  107. #define MAPLAY_PORT_VERSION_NUMBER "1.1"
  108. #else
  109. #ifdef __DOS__
  110. #ifdef PENTIUM
  111. #define MAPLAY_VERSION "DOS, Pentium CPU"
  112. #else
  113. #define MAPLAY_VERSION "DOS, 486 CPU"
  114. #endif // PENTIUM
  115. #define MAPLAY_PORTER  "Jochen Wilhelmy (digisnap@cs.tu-berlin.de)"
  116. #define MAPLAY_PORT_VERSION_NUMBER "1.1"
  117. #else
  118. #define MAPLAY_VERSION "unknown"
  119. #define MAPLAY_PORTER  "Anonymous Porter"
  120. #define MAPLAY_PORT_VERSION_NUMBER "1.0"
  121. #endif  // __DOS__
  122. #endif  // BEOS
  123. #endif  // _WIN32__
  124. #endif  // NeXT
  125. #endif  // AIX
  126. #endif  // HPUX
  127. #endif  // ULTRIX
  128. #endif  // __FreeBSD__
  129. #endif  // LINUX
  130. #endif  // Solaris
  131. #endif  // SunOS4_1_3
  132. #endif  // SunOS4_1_1
  133. #endif  // IRIX
  134.  
  135. #ifdef __WIN32__
  136. HWAVEOUT hwo;
  137. #endif // __WIN32__
  138.  
  139. bool verbose_mode = FALSE;
  140. char filename[512] = "";
  141.  
  142. uint32 maplay(MPEG_Args *);
  143.  
  144. int ProcessArgs(int argc,char *argv[],MPEG_Args *);
  145.  
  146. int Usage(char *pname)
  147. {
  148.     cerr << "\nusage: " << pname
  149.      << " [-v] [-s] [-l] [-r] [-d] "
  150. #if defined(SPARC) || defined(HPUX)
  151.         "[-us] [-uh] "
  152. #ifndef SunOS4_1_1
  153.         "[-ul] "
  154. #endif
  155. #endif // SPARC || HPUX
  156.  
  157. #ifdef HPUX
  158.          "[-w] "
  159. #endif // HPUX
  160.  
  161.         "[-f ushort] filename\n"
  162.  
  163.         "  filename   filename of a MPEG audio stream or - for stdin\n"
  164. #ifdef VERBOSE
  165.         "  -v         verbose mode\n"
  166. #endif
  167. #ifdef ULAW
  168.         "  -s         write u-law samples at 8 kHz rate to stdout\n"
  169. #else
  170.         "  -s         write pcm samples to stdout\n"
  171. #endif
  172.         "  -l         decode only the left channel\n"
  173.         "  -r         decode only the right channel\n"
  174.         "  -d         downmix mode (layer III only)\n"
  175. #if defined(SPARC) || defined(HPUX)
  176.         "  -us        send audio signal to speaker (default)\n"
  177.         "  -uh        send audio signal to headphone\n"
  178. #ifndef SunOS4_1_1
  179.         "  -ul        send audio signal to line out\n"
  180. #endif
  181. #endif // SPARC || HPUX
  182.  
  183. #ifdef __WIN32__
  184.          "  -p name    output as a PCM wave file\n"
  185. #endif // __WIN32__
  186.  
  187. #ifdef HPUX
  188.          "  -w         wait for audio device to become available\n"
  189. #endif // HPUX
  190. #ifdef ULAW
  191.         "  -a         force maplay to treat /dev/audio as an amd device\n"
  192. #endif // ULAW
  193.         "  -f ushort  use this scalefactor instead of the default value 32768\n\n"
  194.         "@(#) MPEG Audio Player maplay 1.2+ "
  195. #ifdef ULAW
  196.         "(8 kHz u-law "
  197. #else
  198.         "("
  199. #endif // ULAW
  200.          MAPLAY_VERSION " version)\n"
  201.         "@(#) Copyright (C) 1996, 1997 Jeff Tsay (ctsay@pasteur.eecs.berkeley.edu)\n"
  202.        "@(#) Decoder engine version 1.90. Command-line code by John Fehr.\n"
  203.        "@(#) This port (version " MAPLAY_PORT_VERSION_NUMBER ") by "
  204.        MAPLAY_PORTER ".\n"
  205.        "@(#)\n"
  206.         "@(#) Original maplay 1.2:\n"
  207.         "@(#) Copyright (C) 1993, 1994 Tobias Bading (bading@cs.tu-berlin.de)\n"
  208.         "@(#) Layer III code adapted from the ISO MPEG Audio Subgroup Software\n"
  209.        "@(#) Simulation Group\n"
  210.        "@(#)\n"
  211.         "@(#) Created: " __DATE__ " " __TIME__ "\n"
  212.         "@(#) This program is free software. See the GNU General Public License\n"
  213.         "@(#) in the file COPYING for more details.\n\n";
  214.     return(0);
  215.   }
  216.  
  217. int ProcessArgs(int32 argc, char *argv[], MPEG_Args *ma)
  218. {
  219.  
  220.   Crc16 *crc = NULL;
  221.   int i;
  222.  
  223.   ma->stdout_mode  = FALSE;
  224.   ma->verbose_mode = FALSE;
  225.  
  226. #if defined(SPARC) || defined(HPUX)
  227.   ma->use_speaker   = FALSE;      
  228.   ma->use_headphone = FALSE;
  229.   ma->use_line_out  = FALSE;
  230. #endif // SPARC || HPUX
  231.  
  232. #ifdef __WIN32__
  233.   ma->phwo             = &hwo;
  234.   ma->output_mode = O_WAVEMAPPER;
  235.   lstrcpy(ma->output_filename, "");
  236. #endif
  237.  
  238. #ifdef HPUX
  239.   ma->wait_if_busy = FALSE;
  240. #endif
  241.  
  242.   if (argc < 2 || !strncmp (argv[1], "-h", 2))
  243.   {
  244.     return Usage(argv[0]);
  245.   }
  246.   // parse arguments:
  247.   i = 1;
  248.   while (i < argc) {
  249.  
  250.    if ((argv[i][0] == '-') && argv[i][1])
  251.       switch ((int32)argv[i][1]) {
  252.  
  253.         case 'v':
  254. #ifdef VERBOSE
  255.         verbose_mode = TRUE;
  256.        ma->verbose_mode = TRUE;
  257. #else
  258.         cerr << "maplay: not compiled with VERBOSE." << endl;
  259. #endif
  260.           break;
  261.  
  262.        case 's':
  263.        ma->stdout_mode = TRUE;
  264.        break;
  265.  
  266.        case 'l':
  267.        ma->which_c = left;
  268.        break;
  269.  
  270.          case 'r':
  271.        ma->which_c = right;
  272.        break;
  273.  
  274.       case 'd':
  275.        ma->which_c = downmix;
  276.       break;
  277.  
  278. #if defined(SPARC) || defined(HPUX)
  279.        case 'u':
  280.        switch (argv[i][2])
  281.        {
  282.         case 's':
  283.           ma->use_speaker = TRUE;
  284.           break;
  285.         case 'h':
  286.           ma->use_headphone = TRUE;
  287.           break;
  288. #ifndef SunOS4_1_1
  289.         case 'l':
  290.           ma->use_line_out = TRUE;
  291.           break;
  292. #endif // !SunOS4_1_1
  293.  
  294.         default:
  295.           return Usage(argv[0]);
  296.        }
  297.        break;
  298. #endif // SPARC || HPUX
  299.  
  300. #ifdef  __WIN32__
  301.       case 'p':
  302.        if (++i == argc) {
  303.           cerr << "please specify an output filename after the -p option!\n";
  304.            exit (1);
  305.       }
  306.        ma->output_mode = O_WAVEFILE;
  307.       sscanf(argv[i], "%s", &ma->output_filename);
  308.       break;
  309. #endif
  310.  
  311. #ifdef HPUX
  312.        case 'w':
  313.       ma->wait_if_busy = TRUE;
  314.       break;
  315. #endif // HPUX
  316.  
  317. #ifdef ULAW
  318.        case 'a':
  319.        ma->force_amd = TRUE;
  320.        break;
  321. #endif
  322.        case 'c':
  323.       cerr << "Sorry, this version does not support filter checking.\n" << endl;
  324.        break;
  325.  
  326.        case 'f':
  327.        if (++i == argc) {
  328.          cerr << "please specify a new scalefactor after the -f option!\n";
  329.          exit (1);
  330.        }
  331.        ma->use_own_scalefactor = TRUE;
  332.        sscanf (argv[i], "%f", &ma->scalefactor);
  333.        break;
  334.  
  335.        default:
  336.        return Usage(argv[0]);
  337.       }
  338.    else if (!filename[0])
  339.       strcpy(filename,argv[i]);
  340.    else
  341.       return Usage(argv[0]);
  342.  
  343.    i++;
  344.   } // while (i < argc)
  345.  
  346.   if (!filename[0])
  347.     return Usage(argv[0]);
  348.  
  349. #if defined(SPARC) || defined(HPUX)
  350.   if (!(ma->use_speaker || ma->use_headphone || ma->use_line_out))
  351.     ma->use_speaker = TRUE;
  352. #endif // SPARC || HPUX
  353.  
  354.   if (!strcmp (filename, "-"))
  355.     ma->stream = new Ibitstream(0);        // read from stdin
  356.   else
  357.     ma->stream = new Ibitstream(filename);        // read from file
  358.  
  359.   ma->MPEGheader = new Header;
  360.   if (!ma->MPEGheader->read_header(ma->stream, &crc)) {
  361.     cerr << endl <<  "Error : no header found!" << endl;
  362.     return(0);
  363.   }
  364.   return(1);
  365. }
  366.  
  367. int maplay_setup(MPEG_Args *ma)
  368. {
  369. #ifdef VERBOSE
  370.   if (verbose_mode)
  371.   {
  372.     // print information about the stream
  373.     char *name = strrchr (filename, DIR_MARKER);
  374.     if (name)
  375.       ++name;
  376.     else
  377.       name = filename;
  378.     cerr << name << " is a layer " << ma->MPEGheader->layer_string () << " "
  379.      << ma->MPEGheader->mode_string() << " "
  380.     << ma->MPEGheader->version_string() << " audio stream with";
  381.     if (!ma->MPEGheader->checksums())
  382.       cerr << "out";
  383.     cerr << " checksums.\nThe sample frequency is "
  384.      << ma->MPEGheader->sample_frequency_string () << " at a bitrate of "
  385.      << ma->MPEGheader->bitrate_string() << ".\n"
  386.         "This stream is ";
  387.     if (ma->MPEGheader->original())
  388.       cerr << "an original";
  389.     else
  390.       cerr << "a copy";
  391.     cerr << " and is ";
  392.     if (!ma->MPEGheader->copyright())
  393.       cerr << "not ";
  394.     cerr << "copyright protected.\n";
  395.   }
  396. #endif
  397.   return(1);
  398. }
  399.  
  400. int main(int argc,char *argv[])
  401. {
  402.     MPEG_Args ma;
  403.     if (!ProcessArgs(argc,argv,&ma)) return(0);
  404.     maplay_setup(&ma);
  405.     maplay(&ma);
  406.    return(0);
  407. }
  408.