I writing small program which need to detect sound level and write it if level higher than set in settings, i done sound capturing via portaudio, compressing via libvorbis, but one part of program has unfinished and i stuck on it, i need to detect sound level of raw pcm data, i have bad understanding of what pcm data is and does not know any audio analyzing/processing algorithm, is we have existing c/c++ library which can do it ?, or is some simple algorithm which can be implemented in c/c++ exists ?
2 Answers
It depends on how you define "sound level", which can be as simple as detecting a peak, and more complex as following industry standards/recommendation on obtaining loudness levels.
PCM data is typically a stream of signed values: 0x00..0xFF in case of 8 bit PCM, -0x8000..+0x7FFF for 16-bit PCM, or -1.0..+1.0 in case of floating point values.
Th easiest is to detect simple peak by looking for maximal absolute value for a given time frame. You can apply log10 afterwards to convert to decibels.
Look into the Speex and WebRTC libraries... they both have voice-activity-detectors in them. If you're looking for a measure of sound level, you'll need to decide on linear or logarithmic level indicator. A common format for PCM is -32768 to 32767 range (16-bit short)... one simple thing you can do is simply sum up the absolute values of the samples in a period and divide by the number of samples to get an average level for the period.