/******************************************************************/
/*                                                                */
/*            Digitized Voice Programmer's Toolkit                */
/*            ------------------------------------                */
/*                                                                */
/*                    Recording example                           */
/*                                                                */
/*           Copyright (c) 1991, Farpoint Software                */
/*                                                                */
/******************************************************************/

#include "vrmod.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>

/*-------------------------------------------------------------------*/

#define max_buf_size  524288L

char logo[] = "Digitized Voice Recording Test Program\r\n"
 "Copyright(c) 1991, Farpoint Software\r\n";

char filename[128];        /* file to receive voice data */
char huge *buffer;         /* RAM buffer to hold voice data while recording */
int handle;                /* handle of voice data file */
short comport;             /* number of COM port attached to digitizer */

/*-------------------------------------------------------------------*/

void main(argc, argv)
int argc;
char ** argv;

{
long buf_size, byte_count;
unsigned short sizelo, sizehi;   /* used to partition file save process */
unsigned short i;
char huge *loadptr;              /* 32 bit pointer used during writing */
int delayctr;                    /* internal timing delay from RCALIBRATE */
int calflag;                     /* status word returned from RCALIBRATE */
long retval;

/* display the logo */

puts(logo);

/* make sure there are two arguments on the command line */

if ( argc != 3 )
	{
	puts("Command line must include COM port number (1 - 4) and file name.");
	exit(1);
	}

/* get the COM port number */

argv++;
comport = (short)((**argv) - '0');

/* get the file name */

argv++;
strcpy(filename, *argv);

/* open or create the file, truncating if it already exists */

handle = open(filename, O_BINARY|O_RDWR|O_CREAT|O_TRUNC, S_IREAD|S_IWRITE);
if ( handle == -1 )
	{
	puts("Error opening file.");
	exit(1);
	}

/* find out how much memory is available */

buf_size = GETMEMAVAIL();
if ( buf_size == 0L )
	{
	close(handle);
	puts("No memory available for buffer.");
	exit(1);
	}
if ( buf_size > max_buf_size )
	buf_size = max_buf_size;

printf("Available memory = %ld bytes, equivalent to %ld seconds.\n",
    buf_size, buf_size / 16572L);

/* allocate memory for the buffer */

buffer = (char huge *)halloc(buf_size, 1);
if ( buffer == NULL )
	{
	close(handle);
	puts("Unable to allocate buffer memory.");
	exit(1);
	}

/* calibrate for CPU speed */

puts("Calibrating...");
retval = RCALIBRATE();
calflag = (int)(retval & 0xFFFF);
delayctr = (int)(retval >> 16);
switch ( calflag )
	{
	case 1:
		close(handle);
		hfree(buffer);
		puts("This CPU is too slow to run this program.");
		exit(1);
	case 2:
		close(handle);
		hfree(buffer);
		puts("This program will not function properly under Enhanced-mode Microsoft Windows.");
		exit(1);
	}
printf("delay counter = %d\n", delayctr);   /* to satisfy your curiosity */

/* beep to indicate start of recording */

puts("Press any key (or digitizer \"stop\" switch) to end recording.");
puts("\x07Recording...");

/* perform the recording */

byte_count = RECORDVOICE(buffer, buf_size, comport);

/* check the return code for errors */

if ( byte_count == -1L )
	{
	close(handle);
	hfree(buffer);
	puts("Invalid COM port.");
	exit(1);
	}

/* show the number of bytes recorded */

printf("%ld bytes recorded.\n", byte_count);

/* write the data to the file */

puts("Writing file...");
sizelo = (unsigned short)(byte_count & 0x7FFFL);
sizehi = (unsigned short)(byte_count >> 15);
loadptr = buffer;
for ( i = 0 ; i < sizehi ; i++)
	{
	if ( 32768U != (unsigned)write(handle, loadptr, 32768U) )
		{
		close(handle);
		hfree(buffer);
		puts("File write error.");
		exit(1);
		}
	loadptr += 32768;
	}
if ( sizelo )
	{
	if ( sizelo != (unsigned)write(handle, loadptr, sizelo) )
		{
		close(handle);
		hfree(buffer);
		puts("File write error.");
		exit(1);
		}
	}

/* close the file and free the allocated memory */

close(handle);
hfree(buffer);
puts("Done.");
}
