
#define ERR 1
#define SUCCESS 0
#define REV 0

#include "exec/types.h"
#include "exec/exec.h"
#include "exec/nodes.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 "libraries/translator.h"
#include "devices/audio.h"
#include "devices/narrator.h"
#include "stdio.h"

struct Library *TranslatorBase=NULL;
extern struct Library *OpenLibrary();
struct MsgPort *WPort,*RPort;
extern struct IORequest *CreateExtIO();
extern struct Library *OpenLibrary(); /* deze lijn kwam al eens voor ! */
struct narrator_rb *wmes;
struct mouth_rb *rmes;

BYTE audChanMasks[]={3,5,10,12};

main()
{
 UBYTE in_string[80],out_string[300];
 UWORD volume=55,rate=20,pitch=DEFPITCH,mode=DEFMODE,
       sex=DEFSEX,freq=DEFFREQ;


 if(set_to_talk()==ERR)
   {
    exit(FALSE);
   }

 for(;;)
  {
   puts("Typ een zin ");
   gets(in_string);

   if(!strcmp(in_string,"%halt"))
    break;

   if(!strcmp(in_string,"%set"))
   {
    set_params(&volume,&rate,&pitch,&mode,&sex,&freq);
    continue;
   }


   if(get_phoneme(in_string,strlen(in_string),out_string,300)==ERR)
    exit(FALSE);

   if(talk_to_me(out_string,volume,rate,pitch,mode,sex,freq)==ERR)
    exit(FALSE);
  };

 CloseDevice(wmes);

} /* end of main() */

get_phoneme(in,inlen,out,outlen)
UBYTE *in,*out;
SHORT inlen,outlen;
{
 TranslatorBase=(struct Library *)
                 OpenLibrary("translator.library",REV);

 if(TranslatorBase==NULL)
  return(ERR);

 if((Translate(in,inlen,out,outlen)) !=0)
  return(ERR);

 CloseLibrary(TranslatorBase);

 return(SUCCESS);
}

talk_to_me(speech,vol,rate,pitch,mode,sex,freq)

UBYTE *speech;
UWORD vol,rate,pitch,mode,sex,freq;

{
 wmes->message.io_Data=(APTR)speech; 
 wmes->message.io_Length = strlen(speech);
 wmes->ch_masks=audChanMasks;
 wmes->nm_masks=sizeof(audChanMasks);

 wmes->mouths=0;
 wmes->volume=vol;
 wmes->rate=rate;
 wmes->sex=sex;
 wmes->pitch=pitch;
 wmes->mode=mode;
 wmes->sampfreq=freq;

 SendIO(wmes);

 while(rmes->voice.message.io_Error!=ND_NoWrite)
   DoIO(rmes);
}

set_to_talk()

{
 if((WPort=(struct MsgPort *)CreatePort(0,0))==NULL)
  return(ERR);

 if((RPort=(struct MsgPort *)CreatePort(0,0))==NULL)
  return(ERR);

 if((wmes=(struct narrator_rb *)
  CreateExtIO(WPort,sizeof(struct narrator_rb)))==NULL)
  return(ERR);

 if((rmes=(struct mouth_rb *)
  CreateExtIO(WPort,sizeof(struct narrator_rb)))==NULL)
  return(ERR);

 wmes->message.io_Command=CMD_WRITE;

 if(OpenDevice("narrator.device",0,wmes,0)!=0)
  return(ERR);

 rmes->voice.message.io_Device=wmes->message.io_Device;
 rmes->voice.message.io_Unit=wmes->message.io_Unit;
 rmes->width=0;
 rmes->height=0;
 rmes->voice.message.io_Command=CMD_READ;
 rmes->voice.message.io_Error=0;

 return(SUCCESS);
}

set_params(vol,rate,pitch,mode,sex,freq)
UWORD *vol,*rate,*pitch,*mode,*sex,*freq;
{
 int x;
 char ch[80];

 puts("Volume ? : ");
 gets(ch);
 if(ch[0]=='\0')
  ;
 else
 {
  x=atoi(ch);
  *vol=x;
 }

 puts("Snelheid ? : ");
 gets(ch);
 if(ch[0]=='\0')
  ;
 else
 {
  x=atoi(ch);
  *rate=x;
 }

 puts("Pitch ? : ");
 gets(ch);
 if(ch[0]=='\0')
  ;
 else
 {
  x=atoi(ch);
  *pitch=x;
 }

 puts("Mode ? : ");
 gets(ch);
 if(ch[0]=='\0')
  ;
 else if(!strcmp(ch,"inflected"))
   *mode= NATURALF0;
 else if(!strcmp(ch,"monotome")) 
   *mode= ROBOTICF0;

 puts("Sex ? : ");
 gets(ch);
 if(ch[0]=='\0')
  ;
 else if(!strcmp(ch,"male"))
   *sex=MALE;
 else if(!strcmp(ch,"female")) 
   *sex=FEMALE;

 puts("Sample freq ? : ");
 gets(ch);
 if(ch[0]=='\0')
  ;
 else
 {
  x=atoi(ch);
  *freq=x;
 }
}



