0
101 Sep 25, 2009 at 07:41

hi
I used DirectSound for recording sound. I want to record only when someone
speeches not when she’s silent. So I checked average of capture buffer. If it
is between 123 and 132, sounds aren’t recorded:

for( cT = 0; cT < nSizeToWrite; cT++ )
{
avg = avg + *((BYTE*)pbSrcData+cT);
}
avg = avg / nSizeToWrite;

if(avg > 132 || avg < 123 ){
//record in wave file.
...


Unfortunately it doesn’t work properly. Silences are recorded mostly. is
that because of environment noise ? how can i suppression it?

#### 3 Replies

0
165 Sep 25, 2009 at 16:30

Where are you getting the numbers 123 and 132 from? Those make no sense.

To properly analyze your sound data, you first need to figure out whether it’s 8-bit or 16-bit (most likely 16-bit nowadays) and look at it using the appropriate data type (char for 8-bit, short for 16-bit). Note that the data is signed, not unsigned. You can check if the absolute value of the data is bigger than some threshold (maybe this is what you tried to do with your 123-132 thing)? You’ll have to experiment to find the right threshold value. Also note that in any sound you’ll get a wave that oscillates between positive and negative values. So just a pure threshold check will not work well, as you’ll clip out the part of the wave below the threshold and it will distort the sound. Rather, you should do something like keep track of how long it’s been below the threshold and stop recording if it’s been below the threshold for half a second. Or, calculate the average power over the last half a second and stop recording if it’s below a threshold. Again, you’ll have to experiment to find the most effective way to do it.

0
101 Sep 25, 2009 at 17:51

i see that i use 16bit wave sample !
but my code give me one byte …
i think that first byte is high byte and second byte is low byte of 16 bit sample and …
and i think that 16bit wave file store sample in 2’s complement …
now every thing change …

now how can i check silence ? can you show me in simple code (like my code) what’s your offer algorithm ?

0
165 Sep 25, 2009 at 18:19

You should just cast your BYTE * to a short * and look at the data as an array of shorts rather than bytes.