#include "exec/types.h"
#include "exec/exec.h"
#include "exec/nodes.h"
#include "exec/lists.h"
#include "exec/memory.h"
#include "exec/interrupts.h"
#include "exec/ports.h"
#include "exec/libraries.h"
#include "exec/io.h"
#include "exec/tasks.h"
#include "exec/execbase.h"
#include "devices/narrator.h"
#include "libraries/translator.h"

struct MsgPort *readPort=0;
struct MsgPort *writePort=0;
 
extern struct MsgPort *CreatePort();
extern struct IORequest *CreateExtIO();
		  
struct narrator_rb *wn=0;
struct mouth_rb *rn=0;
			   
struct Library *TranslatorBase =0;
    
UBYTE *sampleInput;
UBYTE outputString[500];
SHORT rtnCode, readError, writeError, error;
     
UBYTE audChanMasks[4] = { 3,5,10,12 };
						      
extern struct Library *OpenLibrary();
#define REVISION 1
char *words[ ] = {
		"da","zee","da","zee","give","me",
		"your","an","sir","doooo" };
long pitches[] = { 300, 285, 270, 250, 260, 270, 285, 270, 285, 230 };

main()
{
  long i;
  TranslatorBase = OpenLibrary("translator.library",REVISION);
  if(TranslatorBase == NULL) exit(20);

  writePort = CreatePort(0,0);
  if(writePort == NULL) goto cleanup;
		     
  readPort = CreatePort(0,0);
  if(readPort == NULL) goto cleanup;

  wn = (struct narrator_rb *)CreateExtIO(writePort,
    	sizeof(struct narrator_rb));

  rn = (struct mouth_rb *)CreateExtIO(readPort,
	sizeof(struct mouth_rb));

  if(rn == NULL || wn == NULL) goto cleanup;

  wn->ch_masks = audChanMasks;
  wn->nm_masks = 1;   /* restrict to one channel */
  wn->message.io_Data    = (APTR)outputString;
  wn->message.io_Length  = strlen(outputString);
  wn->mouths = 0;             /* NO MOUTHS! Wont read em anyhow.*/
  wn->message.io_Command = CMD_WRITE;

  error = OpenDevice("narrator.device",0,wn,0);
  if(error != 0) goto cleanup;

  for(i=0; i<10; i++)
  {
  sampleInput = words[i];
  rtnCode = Translate(sampleInput, 
  strlen(sampleInput),outputString,500);
			       
  wn->pitch = pitches[i];
  wn->mode  = 1;          /* ROBOTIC */
						
  wn->message.io_Data    = (APTR)outputString;
  wn->message.io_Length  = strlen(outputString);
  wn->message.io_Command = CMD_WRITE;

  writeError = DoIO(wn);
  }
cleanup:
  if(wn->message.io_Device) CloseDevice(wn);
  if(wn) DeleteExtIO(wn);
  if(rn) DeleteExtIO(rn);
  if(TranslatorBase) CloseLibrary(TranslatorBase);
  if(readPort) DeletePort(readPort);
  if(writePort) DeletePort(writePort);
}
