/* ----------------------------------------------------------
   Hello,
      this is a little demo program to show you how to
      play ProTracker modules with the NAUDIO library.
      This is a little .TTP program that plays the file
      given on the command line.

      You can find a demo module in NAUDIO\MODULES.
                                           ("we are wicked")
   --------------------------------------------------------- */
#include <naudio\naudio.h>          /* always need this file */
#include <tos.h>                    /* for Bconin            */
#include <errno.h>
#include <stdio.h>                  /* for fprintf et.al.    */
#include <ctype.h>                  /* for toupper           */
#include <stdlib.h>

#define DO_DISPLAY   1
#define FRQ 11000

static lword   rfrq     = FRQ;      /* some default values  */
static word    brk_jump = 1;
static word    pal      = 1;
static word    type     = PSG_ENGINE;    /* default ENGINE! */
static word    s_loop;

/* ----------------------------------------------------------
   If you want you can set DO_DISPLAY to 1 and see a little
   trace of the module, as its being played.
   ---------------------------------------------------------- */
void  display( n_module *song)
{
#if DO_DISPLAY
   static   oeseqno = -1,
            oepc;

   /* if current 'effective' sequence number is different or
      if the tracker has moved down to another entry in the
      pattern, then output something.
    */
   if( ntrack->eseqno != oeseqno || ntrack->epc != oepc)
   {
      oeseqno  = ntrack->eseqno;
      oepc = ntrack->epc;
      printf( "%01X: Position #%d  Pattern #%d Entry #%02X\n",
                  ntrack->on_voices, oeseqno, song->sequence[ oeseqno],
                  oepc);
   }
#endif
}


/* ----------------------------------------------------------
                     Here's where it's at
   ---------------------------------------------------------- */
main( argc, argv)
char  *argv[];
{
   int   i = 0;
   char  *modfile = 0;
   lword foo;

   if( argc == 1)
      goto usage;

   naudio_init();             /* initialize library         */
   naudio_all();              /* allow all output devices   */

   while( ++i < argc)         /* now parse command line     */
   {
      if( *argv[i] == '-')
         switch( tolower( argv[i][1]))
         {
            case 'f' :
               if( ++i >= argc)
                  goto usage;
               foo = atol( argv[ i]);
               if( foo)
                  rfrq = foo;
               else
                  goto usage;
               break;

            case 'c' :
               ntrack->cia = ! ntrack->cia;        /* set CIA compatibility   */
               break;                           /* or not ...              */

            case 'p' :
               pal = ! pal;                     /* set PAL/NTSC flag for   */
               break;                           /* enhanced Amiga comp.    */

            case 'l' :
               s_loop = ! s_loop;               /* allow looping ?         */
               break;

            case 'd' :                          /* get output device       */
               if( ++i >= argc)
                  goto usage;
               type = (int) atol( argv[ i]);
               break;

            case 'j' :
               brk_jump = ! brk_jump;           /* allow looping of module ? */
               break;

            default  :
               goto usage;
         }
      else
         if( modfile)
            goto usage;
         else
            modfile = argv[ i];
   }

   if( modfile)                              /* got something to do ?     */
   {
      n_module *song;                        /* yes, want to play a song! */

         /* init engine, with desired frequency, output device and PAL flag
            to play modules.
          */
      if( ! (rfrq = ntrack_engine( rfrq, type, pal)))
         return( 1);

#if DO_DISPLAY
      fprintf( stderr, "Actual replay frequency: %ldHz\n", rfrq);
#endif
                                              /* load in PT module from file */
      if( song = nmodule_load( modfile))
      {
                                    /* set module to play from the beginning */
         nmodule_play( song, 0, 0, 0, s_loop | (brk_jump << 1));

         if( naudio_start( 0))                     /* PLAY IT! */
         {
            fprintf( stderr, "Sound system locked\n");
            goto fail;
         }

loop:
         do
            display( song);                        /* print somethin' */
         while( ! Bconstat( 2) && ntrack->song);      /* now wait for keypress
                                                      or end of song */

         if( ntrack->song)                         /* mmm, still running ? */
            switch( (int) ((Bconin( 2) & 0xFF)))
            {
               case ' ' :
               case 'q' :
               case 'Q' :                          /* Q so quit */
                  break;

               case '1' :
                  ntrack->on_voices ^= 0x1;        /* toggle voice 1-4 */
               default  :
                  goto loop;

               case '2' :
                  ntrack->on_voices ^= 0x2;
                  goto loop;

               case '3' :
                  ntrack->on_voices ^= 0x4;
                  goto loop;

               case '4' :
                  ntrack->on_voices ^= 0x8;
                  goto loop;
            }
         ntrack_stop();                         /* turn off sound    */
         nmodule_free( song);                   /* get rid of module */
fail:
         naudio_done();                         /* destroy engine    */
         return( 0);                            /* and say goodbye   */
      }
      fprintf( stderr, "Couldn't load \"%s\"\n", modfile);
      naudio_done();
      return( 1);
   }
usage:
   fprintf( stderr,
"Usage: m_player [flags] <modfile>\n\
\t-f <replayfrequency> from 2048Hz to <= 60000Hz (def: %dHz)\n\
\t-c do not use CIA timing\n\
\t-p use NTSC timing (def: PAL)\n\
\t-d <n>, 0: slow PSG,   1: fast PSG    2:PP (StarSampler)\n\
\t        3: fast STE    4: good STE\n\
\t        7: fast FALCON 8: good FALCON 9:fastest FALCON(!)\n\
\t-l loop module (def: no)\n\
\t-j break looping position jumps (def: yes)", FRQ);
   return( 1);
}

