home *** CD-ROM | disk | FTP | other *** search
/ Club Amiga de Montreal - CAM / CAM_CD_1.iso / files / 377b.lha / devices / audio / audio1.c next >
Encoding:
C/C++ Source or Header  |  1980-02-03  |  8.0 KB  |  175 lines

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