home *** CD-ROM | disk | FTP | other *** search
/ 17 Bit Software 1: Collection A / 17Bit_Collection_A.iso / files / 1063.dms / 1063.adf / DFC / Sound.c < prev    next >
C/C++ Source or Header  |  1988-07-25  |  4KB  |  156 lines

  1. #include "DFC5.h"
  2. #include <devices/audio.h>
  3. #include <devices/narrator.h>
  4. #include <proto/translator.h>
  5.  
  6. #define PHONEMESSIZE 512
  7.  
  8. BYTE AllocTries[]={ 1,2,4,8 }; /* we try all channels */
  9.  
  10. static int SoundOpen;
  11. static struct IOAudio IOAudio;
  12. static BYTE sound[]={ 0,64,127,64,0,-64,-127,-64 }, *chipsound; /* a very little sound */
  13.  
  14. static int VoiceOpen;
  15. static char *Phonemes;
  16. static struct narrator_rb rb;
  17. struct Library *TranslatorBase;
  18.  
  19. /*
  20.  * This function produces an async beep if SoundOpen!=0, otherwise DisplayBeep()s.
  21.  */
  22.  
  23. void Beep(int Period) {
  24.     if (SoundOpen) {
  25.         WaitPort(IOAudio.ioa_Request.io_Message.mn_ReplyPort);
  26.         GetMsg(IOAudio.ioa_Request.io_Message.mn_ReplyPort);
  27.         IOAudio.ioa_Volume = 32;
  28.         IOAudio.ioa_Period = Period;
  29.         BeginIO((struct IORequest *)&IOAudio);
  30.     }
  31.     else DisplayBeep(NULL);
  32. }
  33.  
  34. /*
  35.  * Here we open the audio.device. Nothing is returned because we don't really
  36.  * care if we can use audio.device.
  37.  */
  38.  
  39. void SetUpAudio(void) {
  40.  
  41.     IOAudio.ioa_Request.io_Message.mn_Node.ln_Pri = 10;
  42.     if ((IOAudio.ioa_Request.io_Message.mn_ReplyPort = CreatePort(NULL,0)) &&
  43.         (chipsound = AllocMem(sizeof(sound), MEMF_CHIP))) {
  44.  
  45.         memcpy(chipsound, sound, sizeof(sound));
  46.         IOAudio.ioa_Data = AllocTries;
  47.         IOAudio.ioa_Length = sizeof(AllocTries);
  48.  
  49.         if (!OpenDevice(AUDIONAME,0,(struct IORequest *)&IOAudio,0)) {
  50.             SoundOpen=1;
  51.             IOAudio.ioa_Request.io_Command = CMD_WRITE;
  52.             IOAudio.ioa_Request.io_Flags = ADIOF_PERVOL;
  53.             IOAudio.ioa_Data = chipsound;
  54.             IOAudio.ioa_Length = sizeof(sound);
  55.             IOAudio.ioa_Period = 400;
  56.             IOAudio.ioa_Cycles = 8;
  57.             BeginIO((struct IORequest *)&IOAudio);
  58.             return;
  59.         }
  60.     }
  61.  
  62.     CloseAudio();
  63. }
  64.  
  65. /*
  66.  * Here we close the audio.device
  67.  */
  68.  
  69. void CloseAudio(void) {
  70.  
  71.     if (SoundOpen) {
  72.         WaitPort(IOAudio.ioa_Request.io_Message.mn_ReplyPort);
  73.         GetMsg(IOAudio.ioa_Request.io_Message.mn_ReplyPort);
  74.         CloseDevice((struct IORequest *)&IOAudio);
  75.         SoundOpen = 0;
  76.     }
  77.     if (IOAudio.ioa_Request.io_Message.mn_ReplyPort) {
  78.         DeletePort(IOAudio.ioa_Request.io_Message.mn_ReplyPort);
  79.         IOAudio.ioa_Request.io_Message.mn_ReplyPort = NULL;
  80.     }
  81.     if (chipsound) {
  82.         FreeMem(chipsound, sizeof(sound));
  83.         chipsound = NULL;
  84.     }
  85. }
  86.  
  87. /*
  88.  * Here we open the narrator.device, if someone selected Talk.
  89.  */
  90.  
  91. int OpenVoice(void) {
  92.  
  93.     if ((rb.message.io_Message.mn_ReplyPort = CreatePort(NULL,0))
  94.         && (TranslatorBase = OpenLibrary("translator.library",0))
  95.         && (Phonemes = AllocMem(PHONEMESSIZE, MEMF_CLEAR))) {
  96.  
  97.         if (!OpenDevice("narrator.device",0,(struct IORequest *)&rb,0)) {
  98.             VoiceOpen = 1;
  99.             rb.message.io_Command = CMD_WRITE;
  100.             rb.ch_masks = AllocTries;
  101.             rb.nm_masks = 4;
  102.             return(1);
  103.         }
  104.     }
  105.  
  106.     CloseVoice();
  107.     return(0);
  108. }
  109.  
  110. /*
  111.  * Here we closed the narrator.device.
  112.  */
  113.  
  114. void CloseVoice(void) {
  115.  
  116.     if (VoiceOpen) {
  117.         if (VoiceOpen == 2) WaitIO((struct IORequest *)&rb);
  118.         CloseDevice((struct IORequest *)&rb);
  119.         VoiceOpen = 0;
  120.     }
  121.  
  122.     if (rb.message.io_Message.mn_ReplyPort) {
  123.         DeletePort(rb.message.io_Message.mn_ReplyPort);
  124.         rb.message.io_Message.mn_ReplyPort = NULL;
  125.     }
  126.  
  127.     if (TranslatorBase) {
  128.         CloseLibrary(TranslatorBase);
  129.         TranslatorBase = NULL;
  130.     }
  131.  
  132.     if (Phonemes) {
  133.         FreeMem(Phonemes, PHONEMESSIZE);
  134.         Phonemes = NULL;
  135.     }
  136. }
  137.  
  138. /*
  139.  * And here we talk!
  140.  * Note that Say() is *always* called---only it won't to anything if VoiceOpen==0.
  141.  * Note also that since we use SendIO() the spoken message and the system
  142.  * requester run concurrently 8^).
  143.  */
  144.  
  145. void Say(char *s) {
  146.  
  147.     if (VoiceOpen) {
  148.         if (VoiceOpen == 1) VoiceOpen = 2;
  149.         else WaitIO((struct IORequest *)&rb);
  150.         Translate(s, strlen(s), Phonemes, PHONEMESSIZE);
  151.         rb.message.io_Data = (APTR)Phonemes;
  152.         rb.message.io_Length = strlen(Phonemes);
  153.         SendIO((struct IORequest *)&rb);
  154.     }
  155. }
  156.