home *** CD-ROM | disk | FTP | other *** search
/ Otherware / Otherware_1_SB_Development.iso / amiga / utility / misc / yak.lha / Yak / src / beep.c next >
Encoding:
C/C++ Source or Header  |  1992-09-17  |  2.8 KB  |  108 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.  
  40. long clock = 3579545;    /* default to PAL if no GfxBase */
  41. extern struct GfxBase    *GfxBase;
  42.  
  43. void
  44. FreeAudio ()            /* free allocated audio resources */
  45. {
  46.     if (AIOptr)
  47.     {
  48.         FreeMem (AIOptr, sizeof (struct IOAudio));
  49.         AIOptr = NULL;
  50.     }
  51.     if (port)
  52.     {
  53.         DeleteMsgPort (port);
  54.         port = NULL;
  55.     }
  56. }
  57.  
  58. BOOL 
  59. AllocAudio ()            /* allocate audio resources */
  60. {
  61.     if (GfxBase = (void *)OpenLibrary("graphics.library", 0L))
  62.     {
  63.         if (GfxBase->DisplayFlags & PAL)
  64.             clock = 3456895;
  65.         CloseLibrary(GfxBase);
  66.     }
  67.  
  68.     if ((AIOptr = (void *)AllocMem (sizeof (struct IOAudio), MEMF_PUBLIC | MEMF_CLEAR)) &&
  69.         (port = CreateMsgPort ()))
  70.     {
  71.         AIOptr->ioa_Request.io_Message.mn_ReplyPort = port;
  72.         AIOptr->ioa_Request.io_Message.mn_Node.ln_Pri = 0;
  73.         return TRUE;
  74.     }
  75.     FreeAudio ();
  76.     return FALSE;
  77. }
  78.  
  79. void
  80. beep (long volume)
  81. {
  82.     static struct Message *msg;    /* Pointer for the reply message             */
  83.  
  84.     AIOptr->ioa_Request.io_Command = ADCMD_ALLOCATE;
  85.     AIOptr->ioa_Request.io_Flags = ADIOF_NOWAIT;
  86.     AIOptr->ioa_AllocKey = 85;
  87.     AIOptr->ioa_Data = whichannel;
  88.     AIOptr->ioa_Length = sizeof (whichannel);
  89.  
  90.     /** Open the audio device and allocate a channel **/
  91.     if (OpenDevice ("audio.device", 0L, (struct IORequest *) AIOptr, 0L))
  92.         return;
  93.  
  94.     AIOptr->ioa_Request.io_Command = CMD_WRITE;
  95.     AIOptr->ioa_Request.io_Flags = ADIOF_PERVOL;
  96.     AIOptr->ioa_Data = (BYTE *)data;
  97.     AIOptr->ioa_Length = SAMPLES;
  98.     AIOptr->ioa_Period = clock / (SAMPLES * FREQUENCY);
  99.     AIOptr->ioa_Cycles = (FREQUENCY * DURATION) / 1000;
  100.     AIOptr->ioa_Volume = volume;
  101.  
  102.     BeginIO ((struct IORequest *) AIOptr);
  103.     Wait (1L << port->mp_SigBit);
  104.     msg = GetMsg (port);
  105.  
  106.     CloseDevice ((struct IORequest *) AIOptr);
  107. }
  108.