home *** CD-ROM | disk | FTP | other *** search
/ Amiga Developer CD 1.2 / amidev_cd_12.iso / reference_library / devices / dev_examples / audio.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-08-20  |  6.9 KB  |  171 lines

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