home *** CD-ROM | disk | FTP | other *** search
/ Frozen Fish 2: PC / frozenfish_august_1995.bin / bbs / d07xx / d0741.lha / RKRM_Devices / RKRM_Devices.lha / Audio / Audio.c < prev   
C/C++ Source or Header  |  1992-09-03  |  9KB  |  190 lines

  1. /*
  2.  * Copyright (c) 1992 Commodore-Amiga, Inc.
  3.  * 
  4.  * This example is provided in electronic form by Commodore-Amiga, Inc. for 
  5.  * use with the "Amiga ROM Kernel Reference Manual: Devices", 3rd Edition, 
  6.  * published by Addison-Wesley (ISBN 0-201-56775-X).
  7.  * 
  8.  * The "Amiga ROM Kernel Reference Manual: Devices" contains additional 
  9.  * information on the correct usage of the techniques and operating system 
  10.  * functions presented in these examples.  The source and executable code 
  11.  * of these examples may only be distributed in free electronic form, via 
  12.  * bulletin board or as part of a fully non-commercial and freely 
  13.  * redistributable diskette.  Both the source and executable code (including 
  14.  * comments) must be included, without modification, in any copy.  This 
  15.  * example may not be published in printed form or distributed with any
  16.  * commercial product.  However, the programming techniques and support
  17.  * routines set forth in these examples may be used in the development
  18.  * of original executable software products for Commodore Amiga computers.
  19.  * 
  20.  * All other rights reserved.
  21.  * 
  22.  * This example is provided "as-is" and is subject to change; no
  23.  * warranties are made.  All use is at your own risk. No liability or
  24.  * responsibility is assumed.
  25.  *
  26.  ******************************************************************************
  27.  *
  28.  * Audio.c
  29.  *
  30.  * Audio example
  31.  *
  32.  * Compile with SAS C 5.10  lc -b1 -cfistq -v -y -L
  33.  *
  34.  * Run from CLI only
  35.  */
  36.  
  37. #include <exec/types.h>
  38. #include <exec/memory.h>
  39. #include <devices/audio.h>
  40. #include <dos/dos.h>
  41. #include <dos/dosextens.h>
  42. #include <graphics/gfxbase.h>
  43.  
  44. #include <clib/exec_protos.h>
  45. #include <clib/alib_protos.h>
  46. #include <clib/dos_protos.h>
  47. #include <clib/graphics_protos.h>
  48.  
  49. #include <stdlib.h>
  50. #include <stdio.h>
  51.  
  52. #ifdef LATTICE
  53. int CXBRK(void) { return(0); }     /* Disable SAS CTRL/C handling */
  54. int chkabort(void) { return(0); }  /* really */
  55. #endif
  56.  
  57. struct GfxBase *GfxBase;
  58. /*-----------------------------------------------------------*/
  59. /* The whichannel array is used when we allocate a channel.  */
  60. /* It tells the audio device which channel we want. The code */
  61. /* is 1 =channel0, 2 =channel1, 4 =channel2, 8 =channel3.    */
  62. /* If you want more than one channel, add the codes up.      */
  63. /* This array says "Give me channel 0. If it's not available */
  64. /* then try channel 1; then try channel 2 and then channel 3 */
  65. /*-----------------------------------------------------------*/
  66. UBYTE           whichannel[] = { 1,2,4,8 };
  67.  
  68. void main(int argc, char **argv)
  69. {
  70. struct IOAudio *AudioIO;      /* Pointer to the I/O block for I/O commands   */
  71. struct MsgPort *AudioMP;      /* Pointer to a port so the device can reply */
  72. struct Message *AudioMSG;     /* Pointer for the reply message             */
  73. ULONG           device;
  74. BYTE           *waveptr;                /* Pointer to the sample bytes     */
  75. LONG            frequency = 440;        /* Frequency of the tone desired   */
  76. LONG            duration  = 3;          /* Duration in seconds             */
  77. LONG            clock     = 3579545;    /* Clock constant, 3546895 for PAL */
  78. LONG            samples   = 2;          /* Number of sample bytes          */
  79. LONG            samcyc    = 1;          /* Number of cycles in the sample  */
  80. /*-------------------------------------------------------------------------*/
  81. /* Ask the system if we are PAL or NTSC and set clock constant accordingly */
  82. /*-------------------------------------------------------------------------*/
  83. GfxBase = (struct GfxBase *)OpenLibrary("graphics.library",0L);
  84. if (GfxBase == 0L)
  85.     goto killaudio;
  86. if (GfxBase->DisplayFlags & PAL)
  87.     clock = 3546895;        /* PAL clock */
  88. else
  89.     clock = 3579545;        /* NTSC clock */
  90.  
  91. if (GfxBase)
  92.     CloseLibrary((struct Library *) GfxBase);
  93. /*--------------------------------------------------------------------------*/
  94. /*  Create an audio I/O block so we can send commands to the audio device   */
  95. /*--------------------------------------------------------------------------*/
  96. AudioIO = (struct IOAudio *)
  97.            AllocMem( sizeof(struct IOAudio),MEMF_PUBLIC | MEMF_CLEAR);
  98. if (AudioIO == 0)
  99.     goto killaudio;
  100. printf("IO block created...\n");
  101. /*-------------------------------------------------------------------*/
  102. /* Create a reply port so the audio device can reply to our commands */
  103. /*-------------------------------------------------------------------*/
  104. AudioMP = CreatePort(0,0);
  105. if (AudioMP == 0)
  106.     goto killaudio;
  107. printf("Port created...\n");
  108. /*----------------------------------------------------------------------*/
  109. /* Set up the audio I/O block for channel allocation:                   */
  110. /* ioa_Request.io_Message.mn_ReplyPort is the address of a reply port.  */
  111. /* ioa_Request.io_Message.mn_Node.ln_Pri sets the precedence (priority) */
  112. /*   of our use of the audio device. Any tasks asking to use the audio  */
  113. /*   device that have a higher precedence will steal the channel from us.*/
  114. /* ioa_Request.io_Command is the command field for I/O.                  */
  115. /* ioa_Request.io_Flags is used for the I/O flags.                       */
  116. /* ioa_AllocKey will be filled in by the audio device if the allocation */
  117. /*   succeeds. We must use the key it gives for all other commands sent.*/
  118. /* ioa_Data is a pointer to the array listing the channels we want.     */
  119. /* ioa_Length tells how long our list of channels is.                   */
  120. /*----------------------------------------------------------------------*/
  121. AudioIO->ioa_Request.io_Message.mn_ReplyPort   = AudioMP;
  122. AudioIO->ioa_Request.io_Message.mn_Node.ln_Pri = 0;
  123. AudioIO->ioa_Request.io_Command                = ADCMD_ALLOCATE;
  124. AudioIO->ioa_Request.io_Flags                  = ADIOF_NOWAIT;
  125. AudioIO->ioa_AllocKey                          = 0;
  126. AudioIO->ioa_Data                              = whichannel;
  127. AudioIO->ioa_Length                            = sizeof(whichannel);
  128. printf("I/O block initialized for channel allocation...\n");
  129. /*-----------------------------------------------*/
  130. /* Open the audio device and allocate a channel  */
  131. /*-----------------------------------------------*/
  132. device = OpenDevice("audio.device",0L, (struct IORequest *) AudioIO ,0L);
  133. if (device != 0)
  134.     goto killaudio;
  135. printf("Audio device opened, channel allocated...\n");
  136. /*----------------------------------------------*/
  137. /* Create a very simple audio sample in memory. */
  138. /* The sample must be CHIP RAM                  */
  139. /*----------------------------------------------*/
  140. waveptr = (BYTE *)AllocMem( samples , MEMF_CHIP|MEMF_PUBLIC);
  141. if (waveptr == 0)
  142.     goto killaudio;
  143. waveptr[0] =  127;
  144. waveptr[1] = -127;
  145. printf("Wave data ready...\n");
  146.  
  147. /*------------------------------------------------------------*/
  148. /* Set up audio I/O block to play a sample using CMD_WRITE.   */
  149. /* The io_Flags are set to ADIOF_PERVOL so we can set the     */
  150. /*    period (speed) and volume with the our sample;          */
  151. /* ioa_Data points to the sample; ioa_Length gives the length */
  152. /* ioa_Cycles tells how many times to repeat the sample       */
  153. /* If you want to play the sample at a given sampling rate,   */
  154. /* set ioa_Period = clock/(given sampling rate)               */
  155. /*------------------------------------------------------------*/
  156. AudioIO->ioa_Request.io_Message.mn_ReplyPort =AudioMP;
  157. AudioIO->ioa_Request.io_Command              =CMD_WRITE;
  158. AudioIO->ioa_Request.io_Flags                =ADIOF_PERVOL;
  159. AudioIO->ioa_Data                            =(BYTE *)waveptr;
  160. AudioIO->ioa_Length                          =samples;
  161. AudioIO->ioa_Period                          =clock*samcyc/(samples*frequency);
  162. AudioIO->ioa_Volume                          =64;
  163. AudioIO->ioa_Cycles                          =frequency*duration/samcyc;
  164. printf("I/O block initialized to play tone...\n");
  165.  
  166. /*-------------------------------------------------------*/
  167. /* Send the command to start a sound using BeginIO()     */
  168. /* Go to sleep and wait for the sound to finish with     */
  169. /* WaitPort().  When we wake-up we have to get the reply */
  170. /*-------------------------------------------------------*/
  171. printf("Starting tone now...\n");
  172. BeginIO((struct IORequest *) AudioIO );
  173. WaitPort(AudioMP);
  174. AudioMSG = GetMsg(AudioMP);
  175.  
  176. printf("Sound finished...\n");
  177.  
  178. killaudio:
  179.  
  180. printf("Killing audio device...\n");
  181. if (waveptr != 0)
  182.     FreeMem(waveptr, 2);
  183. if (device == 0)
  184.     CloseDevice( (struct IORequest *) AudioIO );
  185. if (AudioMP != 0)
  186.     DeletePort(AudioMP);
  187. if (AudioIO != 0)
  188.     FreeMem( AudioIO,sizeof(struct IOAudio) );
  189. }
  190.