home *** CD-ROM | disk | FTP | other *** search
/ AmigActive 13 / AACD13.ISO / AACD / Sound / LAME / src / main.c < prev    next >
C/C++ Source or Header  |  2000-07-17  |  5KB  |  189 lines

  1. #include <stdlib.h>
  2. #include <string.h>
  3. #include <stdio.h>
  4. #include <fcntl.h>
  5. #ifdef _WIN32
  6. /* needed to set stdout to binary */
  7. #include <io.h>
  8. #endif
  9. #include "lame.h"
  10. #include "util.h"
  11.  
  12. #ifdef HAVEGTK
  13. #include "gtkanal.h"
  14. #include <gtk/gtk.h>
  15. #endif
  16.  
  17. #ifdef __riscos__
  18. #include "asmstuff.h"
  19. #endif
  20.  
  21. /* PLL 14/04/2000 */
  22. #if macintosh
  23. #include <console.h>
  24. #endif
  25.  
  26.  
  27.  
  28.  
  29. /************************************************************************
  30. *
  31. * main
  32. *
  33. * PURPOSE:  MPEG-1,2 Layer III encoder with GPSYCHO
  34. * psychoacoustic model.
  35. *
  36. ************************************************************************/
  37.  
  38.  
  39. int main(int argc, char **argv)
  40. {
  41.  
  42.   char mp3buffer[LAME_MAXMP3BUFFER];
  43.   short int Buffer[2][1152];
  44.   int iread,imp3;
  45.   lame_global_flags gf;
  46.   FILE *outf;
  47. #ifdef __riscos__
  48.   int i;
  49. #endif
  50.  
  51. #if macintosh
  52.   argc = ccommand(&argv);
  53. #endif
  54.  
  55.  
  56.   /* initialize libmp3lame */
  57.   if (lame_init(&gf)<0) {
  58.     ERRORF("fatal error during initialization\n");
  59.     LAME_ERROR_EXIT();  
  60.   }
  61.   if(argc==1) lame_usage(&gf,argv[0]);  /* no command-line args, print usage, exit  */
  62.  
  63.   /* parse the command line arguments, setting various flags in the
  64.    * struct 'gf'.  If you want to parse your own arguments,
  65.    * or call libmp3lame from a program which uses a GUI to set arguments,
  66.    * skip this call and set the values of interest in the gf struct.
  67.    * (see lame.h for documentation about these parameters)
  68.    */
  69. #ifdef ONLYVORBIS
  70.   gf.ogg=1;
  71. #endif
  72.   lame_parse_args(&gf,argc, argv);
  73.  
  74.  
  75.  
  76.   /* open the wav/aiff/raw pcm or mp3 input file.  This call will
  77.    * open the file with name gf.inFile, try to parse the headers and
  78.    * set gf.samplerate, gf.num_channels, gf.num_samples.
  79.    * if you want to do your own file input, skip this call and set
  80.    * these values yourself.
  81.    */
  82.   lame_init_infile(&gf);
  83.  
  84.   /* Now that all the options are set, lame needs to analyze them and
  85.    * set some more options
  86.    */
  87.   if (lame_init_params(&gf)<0)  {
  88.     ERRORF("fatal error during initialization\n");
  89.     LAME_ERROR_EXIT();
  90.   }
  91.  
  92.   if (!gf.decode_only)
  93.     lame_print_config(&gf);   /* print usefull information about options being used */
  94.  
  95.  
  96.   if (!gf.gtkflag) {
  97.     /* open the output file */
  98.     if (!strcmp(gf.outPath, "-")) {
  99. #ifdef __EMX__
  100.       _fsetmode(stdout,"b");
  101. #elif (defined  __BORLANDC__)
  102.       setmode(_fileno(stdout), O_BINARY);
  103. #elif (defined  __CYGWIN__)
  104.       setmode(fileno(stdout), _O_BINARY);
  105. #elif (defined _WIN32)
  106.       _setmode(_fileno(stdout), _O_BINARY);
  107. #endif
  108.       outf = stdout;
  109.     } else {
  110.       if ((outf = fopen(gf.outPath, "wb")) == NULL) {
  111.     ERRORF("Could not create \"%s\".\n", gf.outPath);
  112.     LAME_ERROR_EXIT();
  113.       }
  114.     }
  115. #ifdef __riscos__
  116.     /* Assign correct file type */
  117.     for (i = 0; gf.outPath[i]; i++) {
  118.       if (gf.outPath[i] == '.')
  119.         gf.outPath[i] = '/';
  120.       else if (gf.outPath[i] == '/')
  121.         gf.outPath[i] = '.';
  122.     }
  123.     if (gf.decode_only)
  124.       SetFiletype(gf.outPath, 0xfb1); /* Wave */
  125.     else
  126.       SetFiletype(gf.outPath, 0x1ad); /* AMPEG */
  127. #endif
  128.   }
  129.  
  130.  
  131.   if (gf.gtkflag) {
  132.  
  133. #ifdef HAVEGTK
  134.     gtk_init (&argc, &argv);
  135.     gtkcontrol(&gf);
  136. #else
  137.     ERRORF("Error: lame not compiled with GTK support \n");
  138. #endif
  139.  
  140.   } else if (gf.decode_only) {
  141.  
  142.     /* decode an mp3 file to a .wav */
  143.     lame_decoder(&gf,outf,gf.encoder_delay);
  144.  
  145.   } else {
  146.  
  147.       lame_id3v2_tag(&gf,outf); /* add ID3 version 2 tag to mp3 file */
  148.  
  149.       /* encode until we hit eof */
  150.       do {
  151.     /* read in 'iread' samples */
  152.     iread=lame_readframe(&gf,Buffer);
  153.  
  154.  
  155.     /* encode */
  156.     imp3=lame_encode_buffer(&gf,Buffer[0],Buffer[1],iread,
  157.               mp3buffer,(int)sizeof(mp3buffer));
  158.  
  159.     /* was our output buffer big enough? */
  160.     if (imp3<0) {
  161.       if (imp3==-1) ERRORF("mp3 buffer is not big enough... \n");
  162.       else ERRORF("mp3 internal error:  error code=%i\n",imp3);
  163.       LAME_ERROR_EXIT();
  164.     }
  165.  
  166.     /* imp3 is not negative, but fwrite needs an unsigned here */
  167.     if (fwrite(mp3buffer,1,(unsigned int)imp3,outf) != imp3) {
  168.       ERRORF("Error writing mp3 output \n");
  169.       LAME_ERROR_EXIT();
  170.     }
  171.       } while (iread);
  172.       imp3=lame_encode_finish(&gf,mp3buffer,(int)sizeof(mp3buffer));   /* may return one more mp3 frame */
  173.       if (imp3<0) {
  174.     if (imp3==-1) ERRORF("mp3 buffer is not big enough... \n");
  175.     else ERRORF("mp3 internal error:  error code=%i\n",imp3);
  176.     LAME_ERROR_EXIT();
  177.       }
  178.       /* imp3 is not negative, but fwrite needs an unsigned here */
  179.       fwrite(mp3buffer,1,(unsigned int)imp3,outf);
  180.       fclose(outf);
  181.       lame_mp3_tags(&gf);       /* add ID3 version 1 or VBR tags to mp3 file */
  182.     }
  183.   lame_close_infile(&gf);            /* close the input file */
  184.   return 0;
  185. }
  186.  
  187.  
  188.  
  189.