This project has moved and is read-only. For the latest updates, please go here.

Filtering silence and soundplayer gets choppy!

Dec 16, 2010 at 3:18 PM


I have a voice-chat program that streams recorded data between clients.

Here's the relevant code snippets:


// Audio
        private WaveIn waveIn;
        private IWavePlayer waveOut;
        private BufferedWaveProvider waveProvider;

               this.waveIn = new WaveIn();
                this.waveIn.WaveFormat = new WaveFormat(16000, 2);
                this.waveIn.BufferMillisconds = 50;
                this.waveIn.DataAvailable += new    EventHandler<WaveInEventArgs>(waveIn_DataAvailable);
                this.waveOut = new DirectSoundOut();
                this.waveProvider = new BufferedWaveProvider(this.waveIn.WaveFormat);

 private void waveIn_DataAvailable(object sender, WaveInEventArgs e)
                byte[] sendArr = new Byte[e.BytesRecorded];
                sendArr = e.Buffer.ToArray();
                this.audioSock.BeginSend(sendArr, sendArr.Length, this.serverEP, new AsyncCallback(audioSendCallback), this.audioSock);
               // this.textbox_display.Text = this.textbox_display.Text + " sent bytes: " + sendArr.Length;
            catch (Exception ez)

 private void audioReceive(IAsyncResult asyn)
                byte[] temp = audioSock.EndReceive(asyn, ref this.serverEP);
                this.waveProvider.AddSamples(temp, 0, temp.Length);
                this.textbox_display.Text = this.textbox_display.Text + " got bytes: " + temp.Length;
                audioSock.BeginReceive(new AsyncCallback(audioReceive), null);
            catch (Exception ez)
                MessageBox.Show("audioReceive: " + this.textbox_nick.Text + "        " +ez.ToString());


I have two questions now;

1. First of all, how do I filter out silence? I have used float f = (float)(Math.Abs((BitConverter.ToInt16(sendArr, i) / 1000.0))); to get an approximate noise threshold,
which can be used to filter for each byte, but the problem is (I think) the computations take too long time and the event will fire again before it's complete. Basically any additional computations seems to bog down
the eventhandler... can't really describe it better O_O. I don't want to send useless silent packets over the network.

2. Using the code above, all noise gets sent, and it works great for like 30 seconds, then for some reason the soundplayer seems to clog up over time and the voice gets distorted and eventually you can't hear what's being said.
What is the reason for this?

Love this library, it is totally awesome!




Dec 18, 2010 at 4:39 PM

I'd like to add, while streaming voice between two clients, the sender client basically hangs up due to all data processing with dataavailable event. Is there a way around this?

Sep 14, 2011 at 10:19 AM

did you find a solution to filter noise ?