#include "ex.h"
#include <aes.h>
#include <string.h>
#include <naudio\naudio.h>

/* This little example is an example of BAD C-Programming, and BAD
   GEM-Programming, since it does NO error checking. On the other
   hand the NAUDIO code is done properly, what you care about any-
   way. The lack of error checking is to keep the source short,
   as to not obscure the NAUDIO functionality.
 */
#define WTYPE  (NAME | CLOSER | MOVER)


static void join( char *dst, char *path, char * file)
{
   strcpy( dst, path);
   strcpy( strrchr( dst, '\\') + 1, file);
}


main()
{
   OBJECT         *tree, *menu;
   int            id;
   unsigned int   ev;
   int            x, y, w, h, dmy;
   int            mbuf[ 8];
   auto char      path[ 128],
                  filename[ 32],
                  full[ 256];
   n_sample       *p = 0;
   channel        *q = 0;


   appl_init();
   graf_mouse( 0, NULL);
                     /* inialize NAUDIO for SAMPLE playing  */
   naudio_init();    /* the standard 3-step                 */
   naudio_some();
   if( naudio_engine( SAMPLES) < 0)
   {
      form_alert( 1, "[1][NAUDIO Initialization failed!][ EXIT ]");
      appl_exit();
      return( -1);
   }
                     /* now boring GEM stuff... */
   rsrc_load( "ex.rsc");
   rsrc_gaddr( R_TREE, T_DIAL, &tree);
   rsrc_gaddr( R_TREE, T_MENU, &menu);
   menu_bar( menu, 1);
   wind_calc( WC_BORDER, WTYPE, tree->ob_x, tree->ob_y,
                                tree->ob_width, tree->ob_height,
                                &x, &y, &w, &h);
   id = wind_create( WTYPE, x, y, w, h);
   wind_open( id, x, y, w, h);
   wind_set( id, WF_NAME, "NAUDIO-EX1");

   for( ;;)
   {
      ev = evnt_multi( MU_BUTTON | MU_MESAG, 2, 3, 1,
                       0, 0, 0, 0, 0,
                       0, 0, 0, 0, 0,
                       mbuf,
                       0, 0,
                       &x, &y, &dmy, &dmy, &dmy, &dmy);

      if( ev & MU_BUTTON)
      {
         int   no;

         if( (no = objc_find( tree, 0, 16, x, y)) >= 0 &&
             ! form_button( tree, no, 1, &dmy))
         {
            objc_change( tree, no, 0, tree->ob_x, tree->ob_y,
                                      tree->ob_width, tree->ob_height,
                                      0, SELECTED);
            switch( no)
            {
                           /* LOAD button was pressed. Ask user for
                              filename and proceed if OK was pressed
                              in the fileselector. Since we will
                              throw away the old sample (or none the
                              first time), we must also discard any
                              possibly active channel. Set q to 0 to
                              remember this. Finally load in the s
                              sample from pathname `full'.
                           */
               case D_LOAD :
                  fsel_input( path, filename, &dmy);
                  if( dmy)
                  {
                     if( q)
                     {
                        channel_delete( q);
                        q = 0;
                     }
                     if( p)
                        nsample_free( p);
                     join( full, path, filename);
                     if( ! (p = nsample_load( full)))
                        form_alert( 1, "[2][Load failed][ OK ]");
                  }
                  break;

                           /* PLAY button has been pressed. So stop the
                              engine. And then start it anew.
                              If any channel was allocated, delete it.
                              Then play sample on the new channel, if
                              one has been loaded.
                           */
               case D_PLAY :
                  naudio_stop();    /* 1st time superflous, but harmless*/
                  naudio_start( 0);
                  if( q)
                     channel_delete( q);
                  if( p)
                     q = nsample_play( p, 0L, NAUDIO_MAX_VOL, 1);
                  break;

                           /* SAVE button was pressed. So get the filename
                              from fsel_input and save the sample under
                              the `full' pathname.
                            */
               case D_SAVE :
                  if( p)
                  {
                     fsel_input( path, filename, &dmy);
                     if( dmy)
                     {
                        join( full, path, filename);
                        if( nsample_save( full, p, 'NSVX'))
                           form_alert( 1, "[2][Save failed][ OK ]");
                     }
                  }
                  break;
            }
         }
      }

         /* handle other GEM events */
      if( ev & MU_MESAG)
      {
         switch( mbuf[ 0])
         {
            case MN_SELECTED :
            case WM_CLOSED :
               goto done;

            case WM_MOVED :
               wind_set( id, WF_CURRXYWH, mbuf[4], mbuf[5], mbuf[6], mbuf[7]);
               wind_calc( WC_WORK, WTYPE, mbuf[4], mbuf[5], mbuf[6], mbuf[7],
                                          &tree->ob_x, &tree->ob_y,
                                          &tree->ob_width, &tree->ob_height);
               break;

            case WM_REDRAW :
               objc_draw( tree, 0, 16, mbuf[ 4], mbuf[ 5], mbuf[ 6], mbuf[ 7]);
         }
      }
   }
done:
   naudio_done();             /* IMPORTANT: MUST DO THIS BEFORE EXITING */
   menu_bar( menu, 0);
   wind_close( id);
   wind_delete( id);
   appl_exit();
   return( 0);
}



