/* A test of voice.library from C. 
  
   This program reads a file of max_num frequency maps
   associated with max_num numbers, then allows you to 
   test recognition of the latter.
   
   Use learn.c to learn the numbers and create the
   corresponding file of frequency maps (nums). 

   To quit the program, hold the left mouse button 
   down while speaking a word. After the current 
   recognition attempt, the program will exit.

   ** link this code with voice.o

   Author: David J Benn
     Date: 24th,27th,31st December 1992   
*/

#include <voice.h>
#include <dos/dos.h>
#include <exec/memory.h>

#define max_num 9L		/* max_num numbers */

#define MOUSE 	0xbfe001

#define ABS(x) (x) >= 0 ? (x) : -(x)

struct  Library *VoiceBase;	/* must be called VoiceBase */
BYTE 	*MapBuffer;
char 	*num_name[] = { "one","two","three","four","five",
			"six","seven","eight","nine" };
char 	*err[] = { 
		  "  ",
		  "No match",
		  "Volume too high",
		  "Volume too low",
		  "Confused by noise" 
	         };

main()
{
char 	*mouse=(char *)MOUSE;
SHORT	wd;
struct 	FileHandle *fh;

 /* open the library */

 VoiceBase = (struct Library *)OpenLibrary(VoiceName,0L);
 if (VoiceBase == NULL) 
    { printf("can't open voice.library!\n"); exit(10); }


 /* specify the sampler */

 PickSampler(PERFECTSOUND3);


 /* increase sampler gain a bit */

 GainUp();


 /* allocate space for MapBuffer */

 MapBuffer = (BYTE *)AllocMem(FREQ_MAP_SIZE*max_num,MEMF_PUBLIC);
 if (MapBuffer == NULL) 
    { printf("AllocMem = NULL for MapBuffer!\n"); exit(10); }


 /* read into MapBuffer from a file created by learn.c */

 fh = (struct FileHandle *)Open("nums",MODE_OLDFILE); 
 if (fh == NULL) { puts("'nums' does not exist."); exit(10); }
 Read(fh,MapBuffer,FREQ_MAP_SIZE*max_num);
 Close(fh);


 /* recognise some words until left mouse button held down
    using the MapBuffer we've just read */

 while ((*mouse & 0x40) == 0x40)
 {
  printf("Speak a number from 1 to %ld.\n",max_num);
  wd = Recognize(MapBuffer,max_num,HIGH_RES);
  if (wd >= 0) printf("You said: %s.\n",num_name[wd]);
  else
      printf("ERROR: %s.\n",err[ABS(wd)]);
 }

 FreeMem(MapBuffer,FREQ_MAP_SIZE*max_num);
 

 /* decrease the gain so as to leave
    the sampler in the state it was in
    prior to running this program.
 */

 GainDown();


 /* close voice.library */

 CloseLibrary(VoiceBase);
}
