//
// Programmer:    Craig Stuart Sapp <craig@ccrma.stanford.edu>
// Creation Date: Thu Jul  5 15:07:23 PDT 2001
// Last Modified: Thu Jul  5 18:33:20 PDT 2001
// Filename:      ...sig/examples/sig/sigAudio/micinfo.cpp
// Syntax:        C++ 
//
// Description:   Simple example audio reading program that measures
//                the basic audio quality of the audio input from
//                the microphone.
//

#include "sigAudio.h"

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

int main(void) {
   AudioIn audioin;
   audioin.open();
   double sample = 0.0;
   double allsample = 0.0;
   double sqsample = 0.0;
   double rms = 0.0;
   int total = 100000;
   int timeoffset = 10000;

   cout << "Reading input from microphone." << endl;
   cout << "The input to the mic should be quiet for this test. " << endl;
   for (int i=0; i<total; i++) {
      audioin.action();
      sample = audioin.output(0);
      if (i > timeoffset) {
         allsample += sample;
         sqsample += sample * sample;
      }

   }
   cout << "Done recording input audio." << endl;
   cout << "DC offset  : " << sample/(total-timeoffset) << endl;
   rms = sqrt(sqsample/(total-timeoffset));
   cout << "RMS signal : " << rms << endl;
   cout << "SNR        : " << -20 * log10(rms) << endl;
   cout << "Linear Bits: " << (-20 * log10(rms))/6.02 << endl;
   audioin.close();
   return 0;
}

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

/* Sample analyses:
### A GOOD MICROPHONE/SOUNDCARD:
	DC offset  : -1.01725e-09
	RMS signal : 7.16089e-05
	SNR        : 82.9007
	Linear Bits: 13.7709     

### A BAD MICROPHONE/SOUNDCARD:
	DC offset  : -2.516e-07
	RMS signal : 0.0229982
	SNR        : 32.7661
	Linear Bits: 5.44288      

*/


// md5sum: 54682ff9d5110540afa2187b2531f6f1 micinfo.cpp [20050403]