/****************************************************************/
/*                                                              */
/*             Digitized Voice Programmer's Toolkit             */
/*             ------------------------------------             */
/*                                                              */
/*             Playback example using Device Driver             */
/*                                                              */
/*            Copyright (c) 1991, Farpoint Software             */
/*                                                              */
/****************************************************************/

#include <stdlib.h>
#include <stdio.h>
#include <malloc.h>
#include <fcntl.h>
#include <io.h>
#include <sys\types.h>
#include <sys\stat.h>
#include <bios.h>
#include <string.h>

char logo[] = "Digitized Voice Playback Device Driver Test Program\r\n"
 "Copyright(c) 1991, Farpoint Software\r\n";

unsigned char devicename[] = "FSVPDD$$";
char filename[128];
char huge *buffer;
int handle;
long file_size;

/*-------------------------------------------------------------------*/

/* This a time delay of about one second. */

void one_second(void)

{
_asm
    {
            sti
            mov     ax,40h
            mov     es,ax
            mov     bx,6Ch
            mov     cx,18
    osd01:  mov     ax,es:[bx]
    osd02:  cmp     ax,es:[bx]
            je      osd02
            loop    osd01
    }
}

/*-------------------------------------------------------------------*/

/* program entry point */

void main(argc, argv)
int argc;
char **argv;

{
unsigned short sizelo, sizehi;   /* used to partition file load process */
unsigned short i;
char huge *loadptr;           /* 32 bit pointer used during loading */
long byte_count;              /* count of bytes played */
int device_handle;            /* handle obtained from opening device driver */

/* display the logo */

puts(logo);

/* check for one argument on command line */

if ( argc < 2 )
	{
	puts("Include file name on command line.");
	exit(1);
	}

argv++;
strcpy(filename, *argv);

/* open the file */

handle = open(filename, O_BINARY|O_RDONLY);
if ( handle == -1 )
	{
	puts("Error opening file.");
	exit(1);
	}

/* get the file length */

file_size = filelength(handle);
if ( file_size == 0L )
	{
	close(handle);
	puts("File has zero length.");
	exit(1);
	}

/* allocate memory for the buffer plus 4 bytes for length dword */

buffer = (char huge *)halloc(file_size + 4L, 1);
if ( buffer == NULL )
	{
	close(handle);
	puts("Unable to allocate buffer memory.");
	exit(1);
	}

/* read the file into the buffer */

sizelo = (unsigned short)(file_size & 0x7FFFL);
sizehi = (unsigned short)(file_size >> 15);
loadptr = &buffer[4L];                      /* leave room for length dword */
for ( i = 0 ; i < sizehi ; i++)
	{
	if ( 32768U != (unsigned)read(handle, loadptr, 32768U) )
		{
		close(handle);
		hfree(buffer);
		puts("File read error.");
		exit(1);
		}
	loadptr += 32768;
	}
if ( sizelo )
	{
	if ( sizelo != (unsigned)read(handle, loadptr, sizelo) )
		{
		close(handle);
		hfree(buffer);
		puts("File read error.");
		exit(1);
		}
	}

/* close the file */

close(handle);

/* copy the file size into the first 4 bytes ofthe buffer */

memcpy(buffer, &file_size, sizeof(long));

/* wait one second so that the key release won't abort playback */

one_second();

/* open the device */

device_handle = open(devicename, O_BINARY|O_RDWR);
if ( device_handle == -1 )
    {
    printf("Error - Unable to open device %s\n", devicename);
    exit(1);
    }

/* user informational messages */

puts("Press any key to stop.");
puts("Starting...");

/* perform the playback */

write(device_handle, buffer, 1);           /* always pass "1" as the length */

/* get the actual number of bytes played */

read(device_handle, &byte_count, 1);       /* always pass "1" as the length */

/* tell user that it's done */

puts("Done.");

/* close the device */

close(device_handle);

/* show the number of bytes played */

printf("%ld bytes played.\n", byte_count);

/* free the allocated memory */

hfree(buffer);
}
