home *** CD-ROM | disk | FTP | other *** search
/ Otherware / Otherware_1_SB_Development.iso / amiga / comms / comprgs / term232.lha / Source_Code / termSource / termSpeech.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-08-18  |  3.2 KB  |  173 lines

  1. /*
  2. **    $Id: termSpeech.c,v 1.6 92/08/15 20:15:16 olsen Sta Locker: olsen $
  3. **    $Revision: 1.6 $
  4. **    $Date: 92/08/15 20:15:16 $
  5. **
  6. **    Speech support routines
  7. **
  8. **    Copyright ⌐ 1990-1992 by Olaf `Olsen' Barthel & MXM
  9. **        All Rights Reserved
  10. */
  11.  
  12. #include "termGlobal.h"
  13.  
  14.     /* Local symbols. */
  15.  
  16. STATIC struct MsgPort        *NarratorPort;
  17. STATIC struct narrator_rb    *NarratorRequest;
  18. STATIC UBYTE             SpeechString[512],
  19.                  TranslatedString[512];
  20. STATIC BYTE             DidSpeak;
  21.  
  22. struct Library            *TranslatorBase;
  23.  
  24.     /* DeleteSpeech():
  25.      *
  26.      *    Free the data associated with the speech function.
  27.      */
  28.  
  29. VOID
  30. DeleteSpeech()
  31. {
  32.     if(DidSpeak)
  33.     {
  34.         if(CheckSignal(1 << NarratorRequest -> message . io_Message . mn_ReplyPort -> mp_SigBit))
  35.             WaitIO(NarratorRequest);
  36.         else
  37.         {
  38.             if(!CheckIO(NarratorRequest))
  39.                 WaitIO(NarratorRequest);
  40.         }
  41.     }
  42.  
  43.     if(TranslatorBase)
  44.     {
  45.         CloseLibrary(TranslatorBase);
  46.  
  47.         TranslatorBase = NULL;
  48.     }
  49.  
  50.     if(NarratorRequest)
  51.     {
  52.         if(NarratorRequest -> message . io_Device)
  53.             CloseDevice(NarratorRequest);
  54.  
  55.         DeleteIORequest(NarratorRequest);
  56.  
  57.         NarratorRequest = NULL;
  58.     }
  59.  
  60.     if(NarratorPort)
  61.     {
  62.         DeleteMsgPort(NarratorPort);
  63.  
  64.         NarratorPort = NULL;
  65.     }
  66.  
  67.     DidSpeak = FALSE;
  68. }
  69.  
  70.     /* CreateSpeech():
  71.      *
  72.      *    Open translator.library and narrator.device, perform
  73.      *    the necessary setups for the speech function.
  74.      */
  75.  
  76. BYTE
  77. CreateSpeech()
  78. {
  79.     if(TranslatorBase = OpenLibrary("translator.library",0))
  80.     {
  81.         if(NarratorPort = CreateMsgPort())
  82.         {
  83.             if(NarratorRequest = (struct narrator_rb *)CreateIORequest(NarratorPort,sizeof(struct narrator_rb)))
  84.             {
  85.                     /* Any channel will do. */
  86.  
  87.                 NarratorRequest -> ch_masks        = &AnyChannel[0];
  88.                 NarratorRequest -> nm_masks        = 4;
  89.  
  90.                     /* This is a write request. */
  91.  
  92.                 NarratorRequest -> message . io_Command    = CMD_WRITE;
  93.                 NarratorRequest -> message . io_Data    = (APTR)SpeechString;
  94.  
  95.                 if(!OpenDevice("narrator.device",0,NarratorRequest,0))
  96.                 {
  97.                     SpeechSetup();
  98.  
  99.                     return(TRUE);
  100.                 }
  101.             }
  102.         }
  103.     }
  104.  
  105.     DeleteSpeech();
  106.  
  107.     return(FALSE);
  108. }
  109.  
  110.     /* Say(UBYTE *String,...):
  111.      *
  112.      *    Translate a string into phonemes and speak it.
  113.      */
  114.  
  115. VOID
  116. Say(UBYTE *String,...)
  117. {
  118.     if(SpeechConfig . Enabled && English)
  119.     {
  120.         va_list VarArgs;
  121.  
  122.         if(!TranslatorBase)
  123.             CreateSpeech();
  124.  
  125.         if(TranslatorBase)
  126.         {
  127.             if(DidSpeak)
  128.             {
  129.                 if(CheckSignal(1 << NarratorRequest -> message . io_Message . mn_ReplyPort -> mp_SigBit))
  130.                     WaitIO(NarratorRequest);
  131.                 else
  132.                 {
  133.                     if(!CheckIO(NarratorRequest))
  134.                         WaitIO(NarratorRequest);
  135.                 }
  136.             }
  137.  
  138.             va_start(VarArgs,String);
  139.             VSPrintf(TranslatedString,String,VarArgs);
  140.             va_end(VarArgs);
  141.  
  142.             if(!Translate(TranslatedString,strlen(TranslatedString),SpeechString,511))
  143.             {
  144.                 NarratorRequest -> message . io_Length = strlen(SpeechString);
  145.  
  146.                 SetSignal(0,1 << NarratorRequest -> message . io_Message . mn_ReplyPort -> mp_SigBit);
  147.  
  148.                 SendIO(NarratorRequest);
  149.  
  150.                 DidSpeak = TRUE;
  151.             }
  152.         }
  153.     }
  154. }
  155.  
  156.     /* SpeechSetup():
  157.      *
  158.      *    Transfer the configuration data into the setup.
  159.      */
  160.  
  161. VOID
  162. SpeechSetup()
  163. {
  164.     if(NarratorRequest)
  165.     {
  166.         NarratorRequest -> rate        = SpeechConfig . Rate;
  167.         NarratorRequest -> pitch    = SpeechConfig . Pitch;
  168.         NarratorRequest -> sex        = SpeechConfig . Sex;
  169.         NarratorRequest -> volume    = SpeechConfig . Volume;
  170.         NarratorRequest -> sampfreq    = SpeechConfig . Frequency;
  171.     }
  172. }
  173.