/****************************************************************/
/*                                                              */
/*             Digitized Voice Programmer's Toolkit             */
/*             ------------------------------------             */
/*                                                              */
/*                     Playback example                         */
/*                                                              */
/*            Copyright (c) 1991, Farpoint Software             */
/*                                                              */
/****************************************************************/

#include "vpmod.h"

#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 Test Program\r\n"
 "Copyright(c) 1991, Farpoint Software\r\n";

char filename[128];        /* file containing voice data */
char huge *buffer;         /* RAM buffer to hold file image */
int handle;                /* handle of voice data file */
long file_size;            /* size in bytes of voice data file */

/*-------------------------------------------------------------------*/

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 */
int delayctr;                    /* internal timing delay from PCALIBRATE */
long byte_count;                 /* count of bytes played from PLAYVOICE */
int calflag;                     /* status word returned from PCALIBRATE */
long retval;

/* 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);

/* allocate memory for the buffer */

buffer = (char huge *)halloc(file_size,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;
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);

/* calibrate for CPU speed */

puts("Calibrating...");
retval = PCALIBRATE();
calflag = (int)(retval & 0xFFFF);
delayctr = (int)(retval >> 16);
switch ( calflag )
	{
	case 1:
		hfree(buffer);
		puts("This computer is too slow to perform adequately with this program.");
		printf("Speed rating = %d    Minimum = 64\n", delayctr);
		exit(1);
	case 2:
		hfree(buffer);
		puts("This program will not function properly under Enhanced-mode Microsoft Windows.");
		exit(1);
	case 3:
		puts("Unusual speaker circuit detected; proper operation is uncertain.");
		break;
	}
printf("delay counter = %d\n", delayctr);   /* to satisfy your curiosity */

/* tell user that we're getting ready */

puts("Press any key to stop.");
puts("Starting...");

/* perform the playback */

byte_count = PLAYVOICE(buffer, file_size);

/* tell user that we're done */

puts("Done.");

/* show the number of bytes played */

printf("%ld bytes played.\n", byte_count);

/* free the allocated memory */

hfree(buffer);
}
