//
// Programmer:    Craig Stuart Sapp <craig@ccrma.stanford.edu>
// Creation Date: Thu Jun 21 13:05:20 PDT 2001
// Last Modified: Wed Jun 27 16:42:48 PDT 2001
// Filename:      ...sig/examples/sig/sigAudio/recordfile.cpp
// Syntax:        C++ 
//
// Description:   Simple example audio reading program.
//

#include "sigAudio.h"

AudioInput audioinput;

void recordFile(AudioInput& audioinput, char* filename, int framecount);

////////////////////////////////////////////////////////////////////////

int main(void) {
   audioinput.open16LSB(1, 44100);
   cout << "Recording input from microphone..." << endl;
   recordFile(audioinput, "test.wav", 100);
   cout << "Done recording input audio." << endl;
   audioinput.close();
   return 0;
}

////////////////////////////////////////////////////////////////////////

//////////////////////////////
//
// recordFile -- record from audio input into a soundfile on disk.
//     Audio data is passed though a DC blocking filter before it is saved.
//

#define AUDIO_BUFFER_SIZE 4096 

void recordFile(AudioInput& audioinput, char* filename, int framecount) {
   unsigned char audio_buffer[AUDIO_BUFFER_SIZE];
   unsigned short* ab = (unsigned short*)&audio_buffer;

   SoundHeader outheader;
   outheader.setHighMono();
   SoundFileOut outsound(filename, outheader);
   DCBlock blocker;
   Constant signalvalue;
   outsound.connect(blocker);
   blocker.connect(signalvalue);

   double sample = 0.0;
   int i, k;
   for (i=0; i<framecount; i++) {
      audioinput.read((unsigned char*)audio_buffer, AUDIO_BUFFER_SIZE);
      for (k=0; k<AUDIO_BUFFER_SIZE/2; k++) {
         sample = ((double)((short)ab[k]))/(long)0x8000;
         signalvalue.setValue(sample);
         Tick(outsound);
      }
   }
}
// md5sum: d89d3200ef5e9025de6eab93729b75b7 recordfile.cpp [20050403]