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)