This project has moved. For the latest updates, please go here.

NAudio audio conference

Jun 2, 2012 at 3:55 PM
Edited Jun 2, 2012 at 4:10 PM

   I have 2 WPF applications and I'm trying to build an audio conference system between them. I followed the NetworkChat example and it works, at leas in one direction but I have 3 big problems. First, an annoying background sound is present and at first I thought it's my old laptop to blame, but then I used an i5 laptop and it's still the same. Second, I can hear the voice clearly, but on the receiving end it echoes indefinitely and third, a high pitching sound goes off at random times and I have to stop the transmission, it's unbearable. Any suggestions? I really need to solve this. I use the NarrowBand Speex  codec, as in the example. Thanks.

This is my code:

//recording:
private void StartAudioStreaming()
        {
            if (!connected)
            {
                IPEndPoint endPoint = new IPEndPoint(IPAddress.Parse("127.0.0.1"), int.Parse("7081"));
                int inputDeviceNumber = 1;
                this.codec = new NarrowBandSpeexCodec();
                Connect(endPoint, inputDeviceNumber, codec);               
            }           
        }

        private void Connect(IPEndPoint endPoint, int inputDeviceNumber, INetworkChatCodec codec)
        {
            waveIn = new WaveIn();
            waveIn.BufferMilliseconds = 50;
            waveIn.DeviceNumber = inputDeviceNumber;
            waveIn.WaveFormat = codec.RecordFormat;
            waveIn.DataAvailable += waveIn_DataAvailable;
            waveIn.StartRecording();

            udpSender = new UdpClient();                        
            udpSender.Client.Bind(endPoint);
            udpSender.Connect(endPoint);

            connected = true;
        }

void waveIn_DataAvailable(object sender, WaveInEventArgs e)
        {
            byte[] encoded = codec.Encode(e.Buffer, 0, e.BytesRecorded);
            udpSender.Send(encoded, encoded.Length);
        }     

 
   // Playback:
   public void StartAudioStreaming()
        {
            if (!connected)
            {
                IPEndPoint endPoint = new IPEndPoint(IPAddress.Parse("client.ip"), int.Parse("7081"));
                int inputDeviceNumber = 1;
                this.codec = new NarrowBandSpeexCodec();
                Connect(endPoint, inputDeviceNumber, codec);                
            }         
        }

        private void Connect(IPEndPoint endPoint, int inputDeviceNumber, INetworkChatCodec codec)
        {          
            udpListener = new UdpClient();                   
            udpListener.Client.Bind(endPoint);            

            waveOut = new WaveOut();
            waveProvider = new BufferedWaveProvider(codec.RecordFormat);
            waveOut.Init(waveProvider);
            waveOut.Play();

            connected = true;
            ListenerThreadState state = new ListenerThreadState() { Codec = codec, EndPoint = endPoint };
            ThreadPool.QueueUserWorkItem(this.ListenerThread, state);
        }


    private void ListenerThread(object state)
        {
            ListenerThreadState listenerThreadState = (ListenerThreadState)state;
            IPEndPoint endPoint = listenerThreadState.EndPoint;
            try
            {
                while (connected)
                {
                    byte[] b = this.udpListener.Receive(ref endPoint);
                    byte[] decoded = listenerThreadState.Codec.Decode(b, 0, b.Length);
                    waveProvider.AddSamples(decoded, 0, decoded.Length);
                }
            }
            catch (SocketException)
            {
                // Thrown when disconnecting
            }
        }      

     The device number 1 is ok (it appears I have 2 microphones and it doesn't work with 0)

 

 

Coordinator
Jun 11, 2012 at 10:32 AM

echo is a big problem when doing audio conferencing. you either need to listen on headphones, or to add an echo suppression algorithm (which can be tricky to write). Skype for example has built in echo-suppression for this very reason.

To track down background noise, try just recording directly to a WAV file. That will tell you if the speex or network streaming code is introducing the noise or if it is coming from the microphone.

Jul 17, 2013 at 7:00 PM
Here, this is what i want to do, but with pure PCM.

And even if i look at this code, i don´t know how to do it, anyone want to help me out?
Sep 4, 2013 at 11:48 AM
Edited Sep 4, 2013 at 11:59 AM
Yes! A slight echo at the end of the sound is always. And it is in your sample. In NAudio 1.6, NAudio demo, "WAV recording" by "waveIn". If you explicitly specify: build for 64-bit systems, there is no echo at the end.
Recording with "waveIn Event Callbacks" is correct in both cases.
Why is this happening?
Coordinator
Sep 19, 2013 at 1:08 PM
OK that's interesting. Wonder if the same buffer is getting reported back a couple of times at the end of recording. I made some changes there a while ago to fix a problem with the very last bit of recording being missed