#include	"sm.h"
#include	<stdio.h>
#include	<string.h>
#include	<conio.h>
#include	<io.h>
#include	<fcntl.h>
#include	<sys\stat.h>
#include	<stdlib.h>

/******************************************************************
*	Digitize speech (or other sound) and store the digitized sound
*	in a specified file.
*
*	Parameters:
*		File - file name to use for output, with no extension.
*
*	Notes:
*		1.  This version uses ADPCM 3 recording with silence suppresion
*
*		2.	Compiled with TurboC's large memory model, case sensitive link
*			turned off.
*
*		3.	Requires files sm.h and cvxtlcc.lib, both of which are
*			supplied by Covox.
*
*	Copyright:
*		Original code by William H. Roetzheim (619) 669-6970
*		Copyright 1990 by William H. Roetzheim
*		All rights reserved.
**********************************************************************/

void main (int argc, char *argv[])
{
	int		i;
	long	lLength;
	char	*lpBuffer;
	char	szFileName[15];
	int		nFile;

	if (argc != 2)
	{
		printf ("\nsyntax:  Record filename");
		exit (-1);
	}

	/* test for extension in filename */
	for (i = 0; i < strlen (argv[1]); i++)
	{
		if (argv[1][i] == '.')
		{
			printf ("\n File name should not have an extension.");
			exit (-1);
		}
	}

	strcpy (szFileName, argv[1]);
	strcat (szFileName, ".v3s");	/* ADPCM 3 compression, silence encoding */
	nFile = open (szFileName, O_BINARY | O_RDWR | O_CREAT, S_IREAD | S_IWRITE);
	if (nFile == -1)
	{
		printf ("\nError opening file.");
		exit (-1);
	}

	lpBuffer = malloc (0xFFFF);	/* Pick a number that's plenty big */
	printf ("\nPress any key to start recording.");
	getch();
	printf ("\nPress any key to stop recording.");
	lLength = record3s (lpBuffer, 0xFFFF, 0, 0, 6);
		/* lLength will be actual bytes in buffer used */
		/* 0xFFFF is the size of the buffer */
		/* The first 0 is rate, we're using the default of 132 */
		/* The second 0 is port, we're using Voice Master factory default */
		/* The 6 tells the routine to use the default for silence encoding */
		/* */
		/*	To record using a different encoding scheme, we would just */
		/* change the call to the appropriate flavor of record */

	if (kbhit()) getch();	/* flush buffer if user pressed key to stop */

	/* done, output results and clean up */
	write (nFile, lpBuffer, lLength);
	close (nFile);
	free (lpBuffer);
}