home *** CD-ROM | disk | FTP | other *** search
/ The Fred Fish Collection 1.5 / ffcollection-1-5-1992-11.iso / ff_disks / 200-299 / ff262.lzh / Lotto / beep.c < prev    next >
C/C++ Source or Header  |  1989-10-31  |  2KB  |  100 lines

  1. /*************************************/
  2. /*                                   */
  3. /* AUoH Progressive Door Prize Lotto */
  4. /*                                   */
  5. /*     Bleeps and bloops routines    */
  6. /*                                   */
  7. /*  Michael D. Groshart - 16 Sep 89  */
  8. /*                                   */
  9. /*************************************/
  10.  
  11. #include <exec/memory.h>
  12. #include <devices/audio.h>
  13. #include <functions.h>
  14.  
  15. #define WAVELENGTH 2L
  16. #define AUDIO_PRI 127
  17. #define VOLUME 64
  18. #define HERTZ (3579545 / WAVELENGTH)
  19. #define CLOCK 50
  20.  
  21. struct IOAudio *ioaudio;
  22. struct MsgPort *audio_port;
  23.  
  24. UBYTE allocation [] = { 1,8,2,4 };
  25. UBYTE *waveform, audio_open;
  26.  
  27. init_beep()
  28. {
  29.     if (!(ioaudio = AllocMem((long)sizeof(struct IOAudio),MEMF_PUBLIC|MEMF_CLEAR)))
  30.         quit();
  31.  
  32.     if (!(audio_port = CreatePort("beep.port",NULL)))
  33.         quit();
  34.  
  35.     if (!(waveform = AllocMem(WAVELENGTH,MEMF_CHIP)))
  36.         quit();
  37.  
  38.     waveform[0] =  127;
  39.     waveform[1] = -127;
  40.  
  41.     ioaudio->ioa_Request.io_Message.mn_Node.ln_Pri = AUDIO_PRI;
  42.     ioaudio->ioa_Request.io_Message.mn_ReplyPort = audio_port;
  43.     ioaudio->ioa_Data = allocation;
  44.     ioaudio->ioa_Length = (long)sizeof(allocation);
  45.  
  46.     if (OpenDevice("audio.device",NULL,ioaudio,NULL))
  47.         quit();
  48.  
  49.     ioaudio->ioa_Request.io_Command = CMD_WRITE;
  50.     ioaudio->ioa_Request.io_Flags = ADIOF_PERVOL;
  51.     ioaudio->ioa_Data = waveform;
  52.     ioaudio->ioa_Length = WAVELENGTH;
  53.     ioaudio->ioa_Period = HERTZ / 4096;
  54.     ioaudio->ioa_Volume = 64;
  55.     ioaudio->ioa_Cycles = 5L * 4096 / CLOCK;
  56.     BeginIO(ioaudio);
  57.  
  58.     return (audio_open = 1);
  59. }
  60.  
  61. beep(freq,ticks)
  62. int freq,ticks;
  63. {
  64.     if (freq < 128)
  65.         freq = 128;
  66.     else if (freq > 8192)
  67.         freq = 8192;
  68.  
  69.     if (!CheckIO(ioaudio))
  70.     {
  71.         AbortIO(ioaudio);
  72.     }
  73.     Remove(ioaudio);
  74.  
  75.     if (ioaudio->ioa_Request.io_Error == 0)
  76.     {
  77.         ioaudio->ioa_Period = HERTZ / freq;
  78.         ioaudio->ioa_Cycles = (long) ticks * freq / CLOCK;
  79.  
  80.         BeginIO(ioaudio);
  81.     }
  82. }
  83.  
  84. exit_beep()
  85. {
  86.     if (!CheckIO(ioaudio))
  87.     {
  88.         AbortIO(ioaudio);
  89.         Remove(ioaudio);
  90.     }
  91.     if (audio_open)
  92.         CloseDevice(ioaudio);
  93.     if (waveform)
  94.         FreeMem(waveform,WAVELENGTH);
  95.     if (audio_port)
  96.         DeletePort(audio_port);
  97.     if (ioaudio)
  98.         FreeMem(ioaudio,(long)sizeof(struct IOAudio));
  99. }
  100.