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

Loud noise at the beginnig and at at the end of a stream with WaveOut.Play()

Apr 10, 2013 at 11:01 AM
Edited Apr 10, 2013 at 2:44 PM
I'd like to create a program witch streams sound to an another client, and replays the sound.
When I call the WaveOut.Play() method, a high pitched beep like sound is hearable (It won't show itself with zero volume, I'm sure that the Waveout.Play() produces it, and not the stream source)
In every 100ms I create a memorystream from the buffer of the microphone, and send it through network. On the another client theese memorystream objects become Enqueued into a Queue, and I call the WaveOut.Play() on each dequeued (converted into IWaveProvider) memorystream object, and call Thread.Sleep(100). It works almost flawlesly with one critical issue: that high pitched sound is present in every 100ms (I call the WaveOut.Play() in every 100ms on the freshly dequeued memorystream).

How could I make that high piched sound disappear, when WaveOut.Play() Begins to play and ends to play the 100ms long chunk? I tried to call immediately the WaveOut.volume = 0.0F and the Thread.Sleep(5) and resume the volume in every 100 ms, but it caused "choppy audio". (also timing if the sleeps is a hard thing to do.)

I am looking forward for your answers.
Apr 10, 2013 at 5:10 PM
For network streaming I strongly recommend using BufferedWaveProvider as the NetworkChatDemo project in NAudio does. Calling WaveOut.Play every 100 ms is inevitably going to cause audio artefacts.
Apr 10, 2013 at 6:48 PM
I did not know about the BufferedStreamWaveprovider. Now it is working properly. Thank you very much markheath!
Let me ask an another question. I enhanced my project with the BufferedStreamWaveprovider. Now I hear three high-pitched sound after the WaveOut.Play(). With a Thread.Sleep(500) immediately after WaveOut.Play() solves the problem, but does a proper solution exist about this problem?
Apr 10, 2013 at 7:22 PM
you should not hear high-pitched sound. Try writing the audio to a WAV file instead of playing and listen to that to see if it has the high pitched sound
Jun 3, 2013 at 7:45 AM
Hi i have similar problem .
i have 2 WaveProvider32 (sine providers) that pass to MultiplexingWaveProvider that pass to MultiplexingWaveProvider32Stereo,
the MultiplexingWaveProvider32Stereo looks like that:
    public int Read(byte[] buffer, int offset, int count)
        int sampleRead = m_MultiplexingWaveProvider.Read(buffer, offset, count);
        WaveBuffer waveBuffer = new WaveBuffer(buffer);

        if (m_WaveFileWriter != null)
            for (int i = 0; i < sampleRead / 4; i++)
        return sampleRead;            
it means he read the buffer and also writing to file.
sometimes i get in the begining of the stream beeps that take 5-10 seconds .
what i do wrong?
Jun 3, 2013 at 3:47 PM
nothing obviously wrong there, although WaveBuffer is overkill - you can just call Write on the WaveFileWriter and pass buffer in.
You say you are passing sine providers in, so a beep is exactly what you should expect to hear.