home *** CD-ROM | disk | FTP | other *** search
/ The CDPD Public Domain Collection for CDTV 3 / CDPDIII.bin / pd / commodities / yak / source / beep.c next >
C/C++ Source or Header  |  1993-04-21  |  3KB  |  99 lines

  1. /*
  2.  * beep.c
  3.  *
  4.  * Ultra-simple beep, for keyclick.c
  5.  * MWS, 5/92.
  6.  *
  7.  * Hacked from audio1.c (RKM companion disks)...
  8.  * ...Copyright (c) 1990 Commodore-Amiga, Inc.
  9.  */
  10. #include <exec/types.h>        /* Some header files for system calls */
  11. #include <exec/memory.h>
  12. #include <devices/audio.h>
  13. #include <graphics/gfxbase.h>
  14. #include <proto/exec.h>
  15. #include <proto/dos.h>
  16. #include "beep.h"        /* prototypes */
  17.  
  18. static UBYTE whichannel[] = {1, 2, 4, 8};
  19. static struct IOAudio *AIOptr;    /* Pointer to the IO block for IO commands   */
  20. static struct MsgPort *port;    /* Pointer to a port so the device can reply */
  21.  
  22. UBYTE __chip data[] = {
  23.     0xEF, 0xFB, 0xF7, 0xF7, 0x12, 0x0F, 0xDA, 0xCC, 
  24.     0x06, 0x20, 0x2A, 0x07, 0xC7, 0xB8, 0xD7, 0x15, 
  25.     0x50, 0x38, 0xF4, 0xB1, 0x86, 0xD0, 0x24, 0x4D, 
  26.     0x27, 0xEB, 0xBC, 0xAE, 0xD5, 0x0B, 0x1E, 0x08, 
  27.     0xDE, 0xC1, 0xC9, 0xF4, 0x28, 0x2C, 0x07, 0xDE, 
  28.     0xC3, 0xC6, 0xE4, 0x10, 0x20, 0x04, 0xE3, 0xD1, 
  29.     0xD9, 0xEE, 0x13, 0x24, 0x15, 0xEC, 0xD1, 0xCC, 
  30.     0xE4, 0x0B, 0x1E, 0x18, 0xF8, 0xDE, 0xD7, 0xE5, 
  31.     0xFD, 0x13, 0x1A, 0x06, 0xE9, 0xDA, 0xE1, 0xF4, 
  32.     0x08, 0x17, 0x16, 0xFB, 0xE1, 0xD4, 0xE4, 0xFD, 
  33.     0x0E, 0x15, 0x0C, 0xF1, 0xDE, 0xE0, 0xF0, 0x00
  34. };
  35.  
  36. #define SAMPLES        sizeof(data)
  37. #define DURATION    5
  38. #define FREQUENCY    270
  39. #define CLOCK        3579545        /* PAL clock - not crucial here */
  40.  
  41. void
  42. FreeAudio ()            /* free allocated audio resources */
  43. {
  44.     if (AIOptr)
  45.     {
  46.         FreeMem (AIOptr, sizeof (struct IOAudio));
  47.         AIOptr = NULL;
  48.     }
  49.     if (port)
  50.     {
  51.         DeleteMsgPort (port);
  52.         port = NULL;
  53.     }
  54. }
  55.  
  56. BOOL
  57. AllocAudio ()            /* allocate audio resources */
  58. {
  59.     if ((AIOptr = (void *)AllocMem (sizeof (struct IOAudio), MEMF_PUBLIC | MEMF_CLEAR)) &&
  60.         (port = CreateMsgPort ()))
  61.     {
  62.         AIOptr->ioa_Request.io_Message.mn_ReplyPort = port;
  63.         AIOptr->ioa_Request.io_Message.mn_Node.ln_Pri = -50;
  64.         return TRUE;
  65.     }
  66.     FreeAudio ();
  67.     return FALSE;
  68. }
  69.  
  70. void
  71. beep (long volume)
  72. {
  73.     static struct Message *msg;    /* Pointer for the reply message             */
  74.  
  75.     AIOptr->ioa_Request.io_Command = ADCMD_ALLOCATE;
  76.     AIOptr->ioa_Request.io_Flags = ADIOF_NOWAIT;
  77.     AIOptr->ioa_AllocKey = 0;
  78.     AIOptr->ioa_Data = whichannel;
  79.      AIOptr->ioa_Length = sizeof (whichannel);
  80.  
  81.     /** Open the audio device and allocate a channel **/
  82.     if (OpenDevice ("audio.device", 0L, (struct IORequest *) AIOptr, 0L))
  83.         return;
  84.  
  85.     AIOptr->ioa_Request.io_Command = CMD_WRITE;
  86.     AIOptr->ioa_Request.io_Flags = ADIOF_PERVOL;
  87.     AIOptr->ioa_Data = (BYTE *)data;
  88.     AIOptr->ioa_Length = SAMPLES;
  89.     AIOptr->ioa_Period = CLOCK / (SAMPLES * FREQUENCY);
  90.     AIOptr->ioa_Cycles = (FREQUENCY * DURATION) / 1000;
  91.     AIOptr->ioa_Volume = volume;
  92.  
  93.     BeginIO ((struct IORequest *) AIOptr);
  94.     WaitPort(port);
  95.     msg = GetMsg (port);
  96.  
  97.     CloseDevice ((struct IORequest *) AIOptr);
  98. }
  99.