home *** CD-ROM | disk | FTP | other *** search
/ Audio 4.94 - Over 11,000 Files / audio-11000.iso / msdos / sndbords / proaudio / sdk_ex / sdk_ex2.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-10-17  |  3.3 KB  |  114 lines

  1. /*
  2.  *  Program: SDK_EX2.C
  3.  *   Author: Philip VanBaren
  4.  *  Purpose: This program demonstrates how to use the Media Vision SDK routines
  5.  *           to do simple continuous data output.
  6.  */
  7.  
  8. #include <stdlib.h>
  9. #include <stdio.h>
  10. #include <conio.h>
  11. #include <math.h>
  12.  
  13. #include <pcmio.h>
  14.  
  15. extern int DMARunning;     /* SDK flag that indicates when DMA has been started */
  16.  
  17. #define IRQ -1             /* -1 means use the default value */
  18. #define DMA -1             /* -1 means use the default value */
  19. #define DMASize 16         /* 16k DMA buffer */
  20. #define DMADivisions 4     /* 4 divisions in this buffer */
  21.  
  22. #define SamplingRate 22050
  23. #define StereoFlag 0       /* When this is 1, the data is interleaved left/right */
  24. #define CompressionFlag 0
  25. #define SampleSize 16      /* Number of bits per sample, 8 or 16 */
  26.  
  27. #define BUFFERS 2
  28. #define BUF_LEN 10000
  29. int out_buf[BUFFERS][BUF_LEN];
  30. int buf_ready[BUFFERS];
  31. int current_buffer=0;
  32.  
  33. /*
  34.  *  This function is called when the PAS code is finished with a buffer.
  35.  */
  36. void far callback(void)
  37. {
  38.    buf_ready[current_buffer]=1;
  39.    if(++current_buffer>=BUFFERS)
  40.       current_buffer=0;
  41. }
  42.  
  43. void main(void)
  44. {
  45.    int play_buffer=0;
  46.    int i;
  47.    for(i=0;i<BUFFERS;i++)
  48.       buf_ready[i]=1;
  49.  
  50.    /*
  51.     *  Initialize ProAudio stuff
  52.     */
  53.    if(OpenPCMBuffering(DMA,IRQ,DMASize,DMADivisions)!=0)
  54.       puts("Error trying to open PCM buffering.");
  55.    else if(PCMState(SamplingRate,StereoFlag,CompressionFlag,SampleSize)!=0)
  56.       puts("Error setting sample rate.");
  57.    else
  58.    {
  59.       while(!kbhit())
  60.       {
  61.          /*
  62.           *  Wait for the buffer to become ready
  63.           */
  64.          while(!buf_ready[play_buffer]);
  65.  
  66.          /* 
  67.           *  Insert your code here to put meaningful data in out_buf[play_buffer]
  68.           */
  69.          for(i=0;i<BUF_LEN;i++)
  70.              out_buf[play_buffer][i]=floor(20000*sin(2*M_PI*0.1*i));
  71.  
  72.          /*
  73.           *  Flag buffer as full.  This flag will become true after the buffer
  74.           *  has been played.
  75.           */
  76.          buf_ready[play_buffer]=0;
  77.  
  78.          /*
  79.           *  If DMA is not running, we need to restart it, else we can just queue
  80.           *  up another block and it will play when the current ones are done.
  81.           */
  82.          if(!DMARunning)
  83.          {
  84.             if(PlayThisBlock((char *)out_buf[play_buffer],(long)BUF_LEN*2,callback)!=0)
  85.             {
  86.                ClosePCMBuffering();
  87.                puts("Error playing block.");
  88.                exit(1);
  89.             }
  90.          }
  91.          else
  92.          {
  93.             if(QueueThisBlock((char *)out_buf[play_buffer],(long)BUF_LEN*2,callback)!=0)
  94.             {
  95.                ClosePCMBuffering();
  96.                puts("Error playing block.");
  97.                exit(1);
  98.             }
  99.          }
  100.          if(++play_buffer>=BUFFERS)
  101.             play_buffer=0;
  102.       }
  103.    }
  104.    /*
  105.     *  You MUST call this function before ending the program, else things won't
  106.     *  work right the next time you try to run the program.  This is critical to
  107.     *  remember when debugging the program, because in debugging you often quit the
  108.     *  program before it runs to completion.  The only way I know of to recover
  109.     *  if this function is NOT called is to reboot the computer!
  110.     */
  111.    ClosePCMBuffering();
  112. }
  113.  
  114.