/*
 *  Program: SDK_EX3.C
 *   Author: Philip VanBaren
 *  Purpose: This program demonstrates how to use the Media Vision SDK routines
 *           to do input a single block of data.
 */

#include <math.h>
#include <stdio.h>
#include <conio.h>

#include <pcmio.h>

extern int DMARunning;     /* SDK flag that indicates when DMA has been started */

#define IRQ -1             /* -1 means use the default value */
#define DMA -1             /* -1 means use the default value */
#define DMASize 16         /* 16k DMA buffer */
#define DMADivisions 4     /* 4 divisions in this buffer */

#define SamplingRate 22050
#define StereoFlag 0       /* If this flag is 1, data is interleaved left/right */
#define CompressionFlag 0
#define SampleSize 16      /* Size of samples, 8 or 16 bits */

/*
 *  Large buffers are allowed (length is a long integer), but will need to
 *  be allocated using farmalloc.  For buffers in static memory, we can't
 *  go any higher than 64k (minus whatever is used by other data).
 */
#define BUF_LEN 10000

int out_buf[BUF_LEN];

int Done=0;

/*
 *  This routine is called when the input buffer has been filled.
 */
void far callback(void)
{
   Done=1;
}

void main(void)
{
   /*
    *  Initialize ProAudio stuff
    */
   if(OpenPCMBuffering(DMA,IRQ,DMASize,DMADivisions)!=0)
      puts("Error trying to open PCM buffering.");
   else if(PCMState(SamplingRate,StereoFlag,CompressionFlag,SampleSize)!=0)
      puts("Error setting sample rate.");
   else
   {
      if(RecordThisBlock((char *)out_buf,(long)BUF_LEN*2,callback)==0)
      {
         ClosePCMBuffering();
         puts("Error recording block.");
      }
      else
      {
         /*
          *  Wait for the callback routine to be called (which means record
          *  is complete.)
          */
         while(!kbhit() && !Done);
      }
   }
   /*
    *  You MUST call this function before ending the program, else things won't
    *  work right the next time you try to run the program.  This is critical to
    *  remember when debugging the program, because in debugging you often quit the
    *  program before it runs to completion.  The only way I know of to recover
    *  if this function is NOT called is to reboot the computer!
    */
   ClosePCMBuffering();

   /*
    *  You can process the data here
    */
}

