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.

- 68,205
- 6
- 94
- 158
-
currently i implemented looking for maximum value(s) during time frame, but this works bad nearly unusable – sss123next Feb 21 '13 at 13:26
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.

- 5,269
- 2
- 21
- 34
-
i have signed 16 bit 48khz pcm, as i understand i need to sum 48000 samples and compare it with level ? to detect level in one second ? – sss123next Feb 21 '13 at 13:24
-
sure, you can choose whatever time period you want... one second is fine. remember to sum the >absolute< values... – mark Feb 21 '13 at 13:28
-
ok, it worked better, still not as i want, but better than my initial code, you can look here http://sss.chaoslab.ru/git/?p=misc.git;a=blob;f=sound_detector/main.cpp;h=9cb67e71ebc6654c9008b135ef1aebbd18394f47;hb=2cb082769b5f2bd41bdaee0bddfc7cd58d7beb1c maybe you have more suggestions about sound level/silence detection, maybe i should apply some filter to reduce generic noise (i have few computers and other noisy tech in room) – sss123next Feb 21 '13 at 14:55
-
yeah you'll have to play with thresholds to figure out where your noise levels are... look into automatic gain control algorithms. you might also do a calculation of your zero-crossing rate (number of times samples go from positive to negative or back) as that too can be useful information... – mark Feb 21 '13 at 20:19