# Measuring Decibels from Microphone

### Measuring Decibels from Microphone

Warning: preg_replace(): The /e modifier is no longer supported, use preg_replace_callback instead in /www/wp-content/plugins/latex/latex.php on line 47

Warning: preg_replace(): The /e modifier is no longer supported, use preg_replace_callback instead in /www/wp-content/plugins/latex/latex.php on line 49

Warning: preg_replace(): The /e modifier is no longer supported, use preg_replace_callback instead in /www/wp-content/plugins/latex/latex.php on line 47

Warning: preg_replace(): The /e modifier is no longer supported, use preg_replace_callback instead in /www/wp-content/plugins/latex/latex.php on line 49

Warning: preg_replace(): The /e modifier is no longer supported, use preg_replace_callback instead in /www/wp-content/plugins/latex/latex.php on line 47

Warning: preg_replace(): The /e modifier is no longer supported, use preg_replace_callback instead in /www/wp-content/plugins/latex/latex.php on line 49

Warning: preg_replace(): The /e modifier is no longer supported, use preg_replace_callback instead in /www/wp-content/plugins/latex/latex.php on line 47

Warning: preg_replace(): The /e modifier is no longer supported, use preg_replace_callback instead in /www/wp-content/plugins/latex/latex.php on line 49

Warning: preg_replace(): The /e modifier is no longer supported, use preg_replace_callback instead in /www/wp-content/plugins/latex/latex.php on line 47

Warning: preg_replace(): The /e modifier is no longer supported, use preg_replace_callback instead in /www/wp-content/plugins/latex/latex.php on line 49

Warning: preg_replace(): The /e modifier is no longer supported, use preg_replace_callback instead in /www/wp-content/plugins/latex/latex.php on line 47

Warning: preg_replace(): The /e modifier is no longer supported, use preg_replace_callback instead in /www/wp-content/plugins/latex/latex.php on line 49

Warning: preg_replace(): The /e modifier is no longer supported, use preg_replace_callback instead in /www/wp-content/plugins/latex/latex.php on line 47

Warning: preg_replace(): The /e modifier is no longer supported, use preg_replace_callback instead in /www/wp-content/plugins/latex/latex.php on line 49

Warning: preg_replace(): The /e modifier is no longer supported, use preg_replace_callback instead in /www/wp-content/plugins/latex/latex.php on line 47

Warning: preg_replace(): The /e modifier is no longer supported, use preg_replace_callback instead in /www/wp-content/plugins/latex/latex.php on line 49

Warning: preg_replace(): The /e modifier is no longer supported, use preg_replace_callback instead in /www/wp-content/plugins/latex/latex.php on line 47

Warning: preg_replace(): The /e modifier is no longer supported, use preg_replace_callback instead in /www/wp-content/plugins/latex/latex.php on line 49

This year was my third year as a mentor at a summer computer camp. We all had lots of fun, the only problem was that the participants - mostly teenage kids at the beginning of the high school - were quite talkative. Loud. To solve this problem a colleague and I came up with a nice solution for the next year.The idea is quite simple. A simple device with a microphone that is measuring decibels in a room. If the sound reaches certain threshold, a very loud music/sound, something very annoying is played through speakers connected to the device.

The first part of measuring decibels was proven to be a little harder than it seems at a first glance. The formula resides based on a power output from a microphone, an information which is usually hard or near impossible to get from an everyday microphone. Therefore, I ignored the correct decibel range and focused only on the correct scale. OpenAL library was used for sound capture as it provides cross platform sound wrappers and it's quite easy to use.

If you're developing a mobile app, there are usually some requirements given by the operating system. For example, Google's requirements for Android devices are 90 dB SPL at 1000 Hz, should yield RMS of 2500 for 16-bit samples. This information should get you a correct dB value.

For more information on how to connect and capture audio with OpenAL take a look in a source code here at GitHub.

Decibels were measured using RMS (Root Mean Square). The formula is quite simple:



 is frequency intensity at specified index,  is the number of samples and  is the maximum value the microphone can output. As we're capturing at 16-bit, the maximum value can be .

OpenAL outputs captured audio in arrays. In the code linked above 2500 samples each time. This is our  array. So the first part was to calculate RMS :

double rms = 0;
for (int i = 0; i < val; i++)
rms += data[i] * data[i];

rms = sqrt(rms / val);

And the second part, the logarithm :

rmsSmooth = rmsSmooth * alpha + (1 - alpha) * rms;
auto rmsdB = 20.0 * log10(rmsSmooth / MAX_AMPLITUDE);
cout << rmsdB << "dB" << endl;

you might notice that I added the smoothing factor  (0.3 in my case) which eases transition between previous and current value so that there isn't too much jumping around. The above example prints dB on my PC in silence at around -70dB, but jumps correctly to around -30dB if talking near the microphone.