home *** CD-ROM | disk | FTP | other *** search
/ Frozen Fish 2: PC / frozenfish_august_1995.bin / bbs / d01xx / d0128.lha / MRBackup / Speech.c < prev    next >
C/C++ Source or Header  |  1988-01-02  |  3KB  |  157 lines

  1.  
  2. /* Minimal-capability speech support package.
  3.  * Author:        Mark R. Rinfret
  4.  * Date:        07/30/87
  5.  * Description:
  6.  *
  7.  *        This package provides a quick and dirty means for adding
  8.  *        speech to C language programs.  In order to use it, observe
  9.  *        the following:
  10.  *
  11.  *            1. Call SpeechOn - return parameter of 0 => success.
  12.  *            2. Call Say(<your message in English>) as often as the
  13.  *               application requires.
  14.  *            3. Call SpeechOff to close libraries/ports/devices.
  15.  */
  16.  
  17. #include <exec/types.h>
  18. #include <exec/exec.h>
  19. #include <intuition/intuition.h>
  20. #include <intuition/intuitionbase.h>
  21. #include <devices/narrator.h>
  22. #include <libraries/translator.h>
  23. #include <functions.h>
  24.  
  25. /*#define DEBUG */
  26.  
  27.  
  28. #define PHONEME_MAX 1024L        /* size of phoneme buffer */
  29.  
  30. /* Which audio channels to use */
  31.  
  32. BYTE audio_chan[] = {3, 5, 10, 12};
  33.  
  34. struct Library *TranslatorBase = NULL;
  35. struct narrator_rb voice_io;    /* Narrator I/O request block */
  36. struct MsgPort *voice_port = NULL;
  37. ULONG narrator_status = -1L,translate_error;
  38.  
  39. UBYTE Phonemes[PHONEME_MAX];    /* Phoneme text buffer */
  40.  
  41. /******************
  42.  *    ROUTINES    *
  43.  ******************/
  44.  
  45. /* Enable speech capability. */
  46.  
  47. SpeechOn(on)
  48.     int on;
  49. {
  50.  
  51.    if (!(TranslatorBase = (struct Library *)
  52.         OpenLibrary("translator.library", (long) LIBRARY_VERSION))) {
  53. #ifdef DEBUG
  54.           DebugWrite("Can't open the translator library!\n");
  55. #endif
  56. fail:
  57.         SpeechOff();            /* close whatever's open */
  58.           return 1;
  59.    }
  60.  
  61.     /* Open a reply port for the narrator. */
  62.  
  63.     if (!(voice_port = CreatePort(0L, 0L))) {
  64. #ifdef DEBUG
  65.         DebugWrite("Can't create narrator reply port!\n");
  66. #endif
  67.         goto fail;
  68.     }
  69.  
  70.     voice_io.message.io_Message.mn_ReplyPort = voice_port;
  71.  
  72.    /* Open the device */
  73.    if ((narrator_status = 
  74.                OpenDevice("narrator.device", 0L, &voice_io, 0L))) {
  75. #ifdef DEBUG
  76.       DebugWrite("Can't open the Narrator device!\n");
  77. #endif
  78.       goto fail;
  79.    }
  80.  
  81.  
  82.    return 0;
  83. }
  84.  
  85. SpeechOff()
  86. {
  87.     if (voice_port) 
  88.         DeletePort(voice_port);
  89.  
  90.        if (!narrator_status)
  91.           CloseDevice(&voice_io);
  92.  
  93.        if (TranslatorBase)
  94.           CloseLibrary(TranslatorBase);
  95.  
  96.        return(0);
  97. }
  98.  
  99. Say(English)
  100.     char *English;
  101. {
  102.     if (!TranslatorBase) {
  103.         if (SpeechOn())    return 1;
  104.     }
  105.  
  106.     if (translate_error = 
  107.         Translate(English, (long) strlen(English), 
  108.                   Phonemes, PHONEME_MAX)) {
  109. #ifdef DEBUG
  110.          DebugWrite("Translator error!\n");
  111. #endif
  112.       }
  113.  
  114. /* Set up the write channel information */
  115.  
  116.     voice_io.ch_masks = (UBYTE *) (audio_chan);
  117.     voice_io.nm_masks = sizeof(audio_chan);
  118.     voice_io.mouths = 0;
  119.     voice_io.message.io_Command = CMD_WRITE;
  120.     voice_io.message.io_Offset = 0;
  121.     voice_io.message.io_Data = (APTR)Phonemes;
  122.     voice_io.message.io_Message.mn_Length = sizeof(voice_io);
  123.     voice_io.message.io_Length = strlen(Phonemes);
  124.     DoIO(&voice_io);
  125.  
  126.  
  127. #ifdef DEBUG
  128. DebugWrite(msg)
  129.     char *msg;
  130. {
  131.     puts(msg);
  132. }
  133.  
  134. main()
  135. {
  136.     short i;
  137.      char *s;
  138.  
  139. static char *text[] = {
  140.     "I am an Amiga computer.  I am currently testing my voice ability.",
  141.     "This is pretty incredible, don't you agree?  I thought you would.",
  142.     "This package is really a set of routines to be used by an application.",
  143.     "All you have to do is call Speech On first, ",
  144.     "then call Say as often as you like.",
  145.     NULL
  146.     };
  147.  
  148.     SpeechOn();
  149.     for (i = 0;s = text[i]; ++i) {
  150.         Say(s);
  151.         Delay(30L);
  152.     }
  153.     SpeechOff();
  154. }
  155. #endif
  156.