/* ----------------------------------------------------------
   Hello,
      this is a little demo program to show you how to
      play samples with the NAUDIO library.
      This is a little .TTP program that plays the file
      given on the command line.

      You can find a demo sample in NAUDIO\SAMPLES.
                                ("ausgebufft und abgelascht")
   --------------------------------------------------------- */
#include <naudio\naudio.h>
#include <stdlib.h>
#include <tos.h>
#include <errno.h>
#include <stdio.h>
#include <ctype.h>

#define FRQ          11000
#define DEF_ENGINE   PSG_ENGINE



main( argc, argv)
char  *argv[];
{
   int         i = 0;
   n_sample    *q;
   channel     *p;
   lword       foo;
   static long efrq = FRQ,
               frq = 0L;
   static int  type = DEF_ENGINE;
   static int  loop = 0;
   static char flag = 0;

   naudio_init();                            /* init library */
   naudio_all();                             /* load in all output devices */

   while( ++i < argc)
   {
      if( *argv[i] == '-')
      {
         if( flag)
            goto usage;

         switch( tolower( argv[i][1]))
         {
            case 'e' :
               if( ++i >= argc)
                  goto usage;
               foo = atol( argv[ i]);
               if( foo)
                  efrq = foo;
               else
                  goto usage;
               break;

            case 'f' :
               if( ++i >= argc)
                  goto usage;
               frq = atol( argv[ i]);
               break;

            case 'l' :
               loop = ! loop;
               break;

            case 'd' :
               if( ++i >= argc)
                  goto usage;
               type = (int) atol( argv[ i]);
               break;

            default  :
               goto usage;
         }
      }
      else
      {
         if( ! flag)
         {
                        /* init engine for enginefrequency 'efrq' and
                           output device 'type' */
            if( ! nsample_engine( efrq, type))
            {
               perror( "Invalid device specification");
               return( 1);
            }
            flag = 1;
         }

                        /* now load sample into dynamic memory */
         if( ! (q = nsample_load( argv[ i])))
         {
            perror( "Load of sample failed: ");
            naudio_done();
            return( 1);
         }
                        /* want to loop the sample ? Then set the
                           repeat length to length of the sample */
         if( loop)
            q->replen = q->len;

                        /* prepare sample for playing, by putting it
                           on a channel (not audible yet!) */
         if( ! (p = nsample_play( q, frq, NAUDIO_MAX_VOL, 1)))
         {
            fprintf( stderr, "No more samples can be played\n");
            continue;
         }

                        /* now start the soundsystem. Let there be NOIZE! */
         if( naudio_start( 0))
         {
            fprintf( stderr, "Soundsystem is locked\n");
            naudio_done();
            return( 1);
         }

                        /* till we tire of it, or the sample ends */
         while( ! Bconstat( 2) && p->play);

                        /* mmh, still playing ?? */
         if( p->play)
         {
            channel_stop( p);    /* and stop the channel    */
            Bconin( 2);          /* grab key from keyboard  */
         }
         naudio_halt();          /* turn off the engine     */
         channel_delete( p);     /* free that channel       */
         nsample_free( q);       /* and the loaded sample   */
      }
   }

   if( flag)            /* did we do anything then leave normally */
   {
      naudio_done();    /* ALWAYS CALL THIS BEFORE LEAVING!! */
      return( 0);
   }

usage:
   naudio_done();
   fprintf( stderr,
"Usage: s_player [flags] <samplefile>+\n\
\t-e <engine-frequency> from 48Hz to 60000Hz (def: %dHz)\n\
\t-f <sample-frequency> (0L default, -1L use engine-frq)\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 sample\n\
\t-v output conversational data\n", FRQ);
   return( 1);
}
