home *** CD-ROM | disk | FTP | other *** search
/ 17 Bit Software 4: Phase Four / 17Bit_Phase_Four.iso / files / 2801.dms / 2801.adf / Chess / src.lha / src / amiga / beep.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-09-03  |  2.2 KB  |  85 lines

  1.  
  2. /*
  3.  * beep.c
  4.  *
  5.  * Simple duration-beep.
  6.  * MWS, 4/92.
  7.  *
  8.  * Hacked from audio1.c (RKM companion disks)...
  9.  * ...Copyright (c) 1990 Commodore-Amiga, Inc.
  10.  */
  11. #include <exec/types.h>        /* Some header files for system calls */
  12. #include <exec/memory.h>
  13. #include <devices/audio.h>
  14. #include <graphics/gfxbase.h>
  15. #include <proto/exec.h>
  16. #include <proto/dos.h>
  17.  
  18. void FreeAudio(void);
  19. BOOL AllocAudio(void);
  20. void dobeep(LONG);
  21.  
  22. static UBYTE whichannel[] = {1, 2, 4, 8};
  23. static UBYTE __chip waveptr[] = {  0, 45, 90, 127, 90, 45,
  24.                        0,-45,-90,-127,-90,-45  };
  25. #define FREQUENCY 1500
  26. #define SAMPLES (sizeof(waveptr))
  27.  
  28. static struct IOAudio *AIOptr;    /* Pointer to the IO block for IO commands   */
  29. static struct MsgPort *port;    /* Pointer to a port so the device can reply */
  30.  
  31. void 
  32. FreeAudio ()            /* free allocated audio resources */
  33. {
  34.     if (port) DeleteMsgPort (port);
  35.     if (AIOptr) FreeMem (AIOptr, sizeof (struct IOAudio));
  36. }
  37.  
  38. BOOL 
  39. AllocAudio ()            /* allocate audio resources */
  40. {
  41.     if ((AIOptr = (void *) AllocMem (sizeof (struct IOAudio), MEMF_PUBLIC | MEMF_CLEAR)) &&
  42.         (port = CreateMsgPort ()))
  43.       {
  44.           return TRUE;
  45.       }
  46.     FreeAudio ();
  47.     return FALSE;
  48. }
  49.  
  50. void
  51. dobeep (long duration)        /* duration in milliseconds */
  52. {
  53.     static struct Message *msg;    /* Pointer for the reply message             */
  54.     if (!AllocAudio())
  55.         return;
  56.  
  57.     /** open audio.device **/
  58.     AIOptr->ioa_Request.io_Message.mn_ReplyPort = port;
  59.     AIOptr->ioa_Request.io_Message.mn_Node.ln_Pri = 0;
  60.     AIOptr->ioa_Request.io_Command = ADCMD_ALLOCATE;
  61.     AIOptr->ioa_Request.io_Flags = ADIOF_NOWAIT;
  62.     AIOptr->ioa_AllocKey = 90;
  63.     AIOptr->ioa_Data = whichannel;
  64.     AIOptr->ioa_Length = sizeof (whichannel);
  65.  
  66.     /** Open the audio device and allocate a channel **/
  67.     if (OpenDevice ("audio.device", 0L, (struct IORequest *) AIOptr, 0L))
  68.         return;
  69.     AIOptr->ioa_Request.io_Message.mn_ReplyPort = port;
  70.     AIOptr->ioa_Request.io_Command = CMD_WRITE;
  71.     AIOptr->ioa_Request.io_Flags = ADIOF_PERVOL;
  72.     AIOptr->ioa_Data = (BYTE *) waveptr;
  73.     AIOptr->ioa_Length = SAMPLES;
  74.     AIOptr->ioa_Period = 3579545 / (SAMPLES * FREQUENCY);
  75.     AIOptr->ioa_Cycles = (FREQUENCY * duration) / 1000;
  76.     AIOptr->ioa_Volume = 64;
  77.  
  78.     BeginIO ((struct IORequest *) AIOptr);
  79.     Wait (1L << port->mp_SigBit);
  80.     msg = GetMsg (port);
  81.     CloseDevice ((struct IORequest *) AIOptr);
  82.  
  83.     FreeAudio();
  84. }
  85.