#include #include static char *device = "default"; /* device name */ /* Each sample is a 16-bit signed integer, which means the value can vary from -32768 to 32767. Size of the sample buffer = 8*1024 = 8192, which takes 8*1024*2 = 16384 bytes. Sampling frequency = 48000Hz, in other words, sound card will record the sample value every 1/48000 second. */ short buffer[8*1024]; /* The unit of sizeof() function is byte, so we have to shift 1 bit to the right to calculate the real size of buffer. */ int buffer_size = sizeof(buffer)>>1; /* Function for calculating the Root Mean Square of sample buffer. RMS can calculate an average amplitude of buffer. */ double rms(short *buffer) { int i; long int square_sum = 0.0; for(i=0; i 0 && frames < (long)buffer_size) printf("Short read (expected %li, wrote %li)\n", (long)buffer_size, frames); Pvalue = rms(buffer) * k; dB = (int)20*log10(Pvalue); if(dB > peak) peak = dB; int j; for(j=0; j<120; j++) printf("\b"); fflush(stdout); printf("dB=%d,Peak=%d | ", dB, peak); for(j=0; j