/* $Revision Header * Header built automatically - do not edit! *************
 *
 *	(C) Copyright 1990 by Olaf 'Olsen' Barthel & MXM
 *
 *	Name .....: termSpeech.c
 *	Created ..: Thursday 27-Jun-91 18:29
 *	Revision .: 0
 *
 *	Date            Author          Comment
 *	=========       ========        ====================
 *	27-Jun-91       Olsen           Created this file!
 *
 * $Revision Header ********************************************************/

#include "TermGlobal.h"

	/* Local symbols. */

STATIC struct MsgPort		*NarratorPort;
STATIC struct narrator_rb	*NarratorRequest;
STATIC UBYTE			 SpeechString[512],FormatString[512];
struct Library			*TranslatorBase;

	/* DeleteSpeech():
	 *
	 *	Free the data associated with the speech function.
	 */

VOID
DeleteSpeech()
{
	if(TranslatorBase)
	{
		CloseLibrary(TranslatorBase);

		TranslatorBase = NULL;
	}

	if(NarratorRequest)
	{
		if(NarratorRequest -> message . io_Device)
			CloseDevice(NarratorRequest);

		DeleteIORequest(NarratorRequest);

		NarratorRequest = NULL;
	}

	if(NarratorPort)
	{
		DeleteMsgPort(NarratorPort);

		NarratorPort = NULL;
	}
}

	/* CreateSpeech():
	 *
	 *	Open translator.library and narrator.device, perform
	 *	the necessary setups for the speech function.
	 */

BYTE
CreateSpeech()
{
	if(TranslatorBase = OpenLibrary("translator.library",0))
	{
		if(NarratorPort = CreateMsgPort())
		{
			if(NarratorRequest = (struct narrator_rb *)CreateIORequest(NarratorPort,sizeof(struct narrator_rb)))
			{
					/* Any channel will do. */

				NarratorRequest -> ch_masks		= &AnyChannel[0];
				NarratorRequest -> nm_masks		= 4;

					/* This is a write request. */

				NarratorRequest -> message . io_Command	= CMD_WRITE;
				NarratorRequest -> message . io_Data	= (APTR)SpeechString;

				if(!OpenDevice("narrator.device",0,NarratorRequest,0))
				{
					SpeechSetup();

					return(TRUE);
				}
			}
		}
	}

	DeleteSpeech();

	return(FALSE);
}

	/* Say(UBYTE *String,...):
	 *
	 *	Translate a string into phonemes and speak it.
	 */

VOID
Say(UBYTE *String,...)
{
	if(TranslatorBase && SpeechConfig . Enabled)
	{
		va_list VarArgs;

		va_start(VarArgs,String);
		VSPrintf(FormatString,String,VarArgs);
		va_end(VarArgs);

		if(!Translate(FormatString,strlen(FormatString),SpeechString,511))
		{
			NarratorRequest -> message . io_Length = strlen(SpeechString);

			SendIO(NarratorRequest);
			WaitIO(NarratorRequest);
		}
	}
}

	/* SpeechSetup():
	 *
	 *	Transfer the configuration data into the setup.
	 */

VOID
SpeechSetup()
{
	if(NarratorRequest)
	{
		NarratorRequest -> rate		= SpeechConfig . Rate;
		NarratorRequest -> pitch	= SpeechConfig . Pitch;
		NarratorRequest -> sex		= SpeechConfig . Sex;
		NarratorRequest -> volume	= SpeechConfig . Volume;
		NarratorRequest -> sampfreq	= SpeechConfig . Frequency;
	}
}
