/* MIT License Copyright (c) [2016] [Hsin-Wu "John" Liu] Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ #include #include /* Device name. This might be different on your computer, please run 'arecord -L' to verify valide device names. (Thanks to @Prot_EuPhobox on youtube!) */ /* static char *device = "default"; */ static char *device = "dsnoop:1,0"; /* 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. */ /* buffers for single channels or a stereo one */ short buffer_L[8*1024]; short buffer_R[8*1024]; long buffer[8*2*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_L = sizeof(buffer_L)>>1; int buffer_size_R = sizeof(buffer_R)>>1; 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_L) { int i; long int square_sum = 0.0; for(i=0; i 0 && frames < (long)buffer_size) { printf("Short read (expected %li, read %li)\n", (long)buffer_size, frames); } // Successfully read, calculate dB and update peak value Pvalue_L = rms_L(buffer_L) * k; dB = (int)20 * log10(Pvalue_L); if(dB > peak) peak = dB; show_L(dB, peak); Pvalue_R = rms_R(buffer_R) * k; dB = (int)20 * log10(Pvalue_R); if(dB > peak) peak = dB; show_R(dB, peak); } printf("\n"); snd_pcm_close(handle_capture); return 0; }