/*

     This program demonstrates the skeleton of interfacing with the driver
     (CT-VOICE.DRV).

     It requires a small assembly code, CTV-ASM.ASM, for interface between
     the application and the driver.

     The program is coded for small model. Same as the CTV-ASM.ASM.
     
*/

#include <io.h>
#include <dos.h>
#include <bios.h>
#include <fcntl.h>

#define  FALSE  0
#define  TRUE   1


       char      far *voice_drv = 0 ;
       char      far *voice_buffer = 0 ;

       int       voice_status ;

load_drv()
{
       int       segment ;
       int       handle ;
       long      filesize ;
       int       byte_read ;
       int       ret_val = FALSE ;
       char      huge *fp ;

    printf("Loading CT-VOICE.DRV...") ;

    if (_dos_open("CT-VOICE.DRV",O_RDONLY,&handle))
         printf("open file error.\n") ;
    else
    {
         filesize = filelength(handle) ;

         if (_dos_allocmem((int)( (filesize + 15L) /16 ),
                 (int *)(&voice_drv)+1))
         {
              printf("memory allocation error.\n")  ;
         }
         else 
         { 
              fp = (char huge*)voice_drv ;

              do
              {
                   _dos_read(handle,fp,0x4000,&byte_read) ;
                   fp += byte_read ;

              } while (byte_read == 0x4000) ;

              ret_val = TRUE ;
              printf("\n") ;
         }

         _dos_close(handle) ;
    }

    return(ret_val) ;
}


load_voice_file(char *filename)
{
       int       segment ;
       int       handle ;
       long      filesize ;
       int       byte_read ;
       int       ret_val = FALSE ;
       char      huge *fp ;

    printf("Loading %s...",filename) ;

    if (_dos_open(filename,O_RDONLY,&handle))
         printf("open file error.\n") ;
    else
    {
         filesize = filelength(handle) ;

         if (_dos_allocmem((int)( (filesize + 15L) /16 ),
                 (int *)(&voice_buffer)+1))
         {
              printf("memory allocation error.\n")  ;
         }
         else 
         { 
              fp = (char huge*)voice_buffer ;

              do
              {
                   _dos_read(handle,fp,0x4000,&byte_read) ;
                   fp += byte_read ;

              } while (byte_read == 0x4000) ;

              ret_val = TRUE ;
              printf("\n") ;
         }

         _dos_close(handle) ;
    }

    return(ret_val) ;
}


check_driver()
{
       int    version ;
       int    ret_val ;

    version = ctv_version() ;

    printf("Drvier Version: %d.%02d\n\n",version >> 8, version % 256) ;

    if (ret_val = ctv_initial())
    {

         switch(ret_val)
         {
              case 1 : printf("Voice card error.\n") ;
                       break ;
              case 2 : printf("I/O read/write error.\n") ;
                       break ;
              case 3 : printf("DMA interrupt error.\n") ;
                       break ;
              default: printf("Unknown error to driver.\n") ;
                       break ;
         }

         ret_val = FALSE ;
    }
    else
         ret_val = TRUE ;

    return(ret_val) ;
}

main(int argc,char *argv[])
{
       char      far *ptr ;
       int       key ;

    printf("\nDemo program for CT-VOICE.DRV\n") ;
    printf("Copyright (c) Creative Technology Pte Ltd., 1990.  "
           "All rights reserved.\n\n") ;

    if (argc > 1)
    {
         if (load_drv())
         {
              if (check_driver())
              {
                   ctv_status_addx((int far*)&voice_status) ;

                   if (load_voice_file(argv[1]))
                        play_voice() ;

              }

              _dos_freemem(FP_SEG(voice_drv)) ;
         }
    }
    else
         printf("Usage:\n\tCTV-DEMO voice-filespec\n") ;
}



play_voice()
{
       char      far *ptr ;
       int       key ;
       int       temp ;

    ptr = voice_buffer + *(int far*)(voice_buffer+0x14) ;

    ctv_output(ptr) ;

    cprintf("Voice status = %04X %-40s\r",voice_status,
                         "[Esc] to abort, [Space] to pause") ;

#pragma loop_opt(off)
    while (voice_status)
    {
         if (temp != voice_status)
              cprintf("Voice status = %04X\r",temp = voice_status) ;

         if (_bios_keybrd(_KEYBRD_READY))
         {
              key = _bios_keybrd(_KEYBRD_READ) ;

              if (key == 0x11b)
                   ctv_terminate() ;
              else
              {
                   if (key == 0x3920)
                   {
                        ctv_pause() ;
                        cprintf("Voice status = %04X %-40s\r",voice_status,
                                          "[Space] to continue") ;

                        while (_bios_keybrd(_KEYBRD_READ) != 0x3920) ;

                        ctv_continue() ;

                        cprintf("Voice status = %04X %-40s\r",voice_status,
                                      "[Esc] to abort, [Space] to pause") ;
                   }
              }
         }
    }
#pragma loop_opt(on)
}
