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

Transfer voice from mic by UDP

Jan 3, 2011 at 8:46 AM
Hello, I'm trying to transfer voice from mic by udp socket. It worked to transfer bytes data and playback, but the sound heared like with echo effect.
Could anyone tell me how to fix it?
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Net.Sockets;
using System.Net;
using System.Threading;
using NAudio.Wave;
using NAudio.CoreAudioApi;
using System.IO;

namespace 廣播系統
{
    public partial class Form1 : Form
    {
        private Thread mListenThread;
        private IPEndPoint sIpEnd;
        private Socket server;
        private WaveIn wavein;
        private WaveOut waveout;
        private static BufferedWaveProvider wavProv;
        public Form1()
        {
            InitializeComponent();

            wavein = new WaveIn();
            wavein.WaveFormat = new WaveFormat(8000, 16, 1);

            wavein.DataAvailable += new EventHandler<WaveInEventArgs>(Recorded);
            waveout = new WaveOut();
            wavProv = new BufferedWaveProvider(new WaveFormat(8000, 16, 1));
            
            waveout.Init(wavProv);
            
            mListenThread = new Thread(new ParameterizedThreadStart(startListen));
            mListenThread.Start();
            server = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
        }

        private void button1_Click(object sender, EventArgs e)
        {
            // Start Record
            
            wavein.StartRecording();
            
        }
        private void Recorded(object sender, WaveInEventArgs e)
        {
            try
            {
                
                IPEndPoint cIpEnd = new IPEndPoint(IPAddress.Parse("127.0.0.1"), 12131);
                server.SendTo(e.Buffer, cIpEnd);
                
                
            }
            catch (Exception err)
            {
                //statusBar1.Text = err.Message;
            }
        }
        private void startListen(object sender)
        {
            //MessageBox.Show("Thread Start");
            // UDP Start
            IPEndPoint sIpEnd = new IPEndPoint(IPAddress.Any, 12131);
            Socket sock = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
            sock.Bind(sIpEnd);

            IPEndPoint remoteIpEnd = new IPEndPoint(IPAddress.Any, 0);
            EndPoint Remote = (EndPoint)remoteIpEnd;
            
            
            waveout.Play();
            int offset = 0;
            while (true)
            {
                byte[] data = new byte[65535];
                int recv = sock.ReceiveFrom(data, ref Remote);
                wavProv.AddSamples(data, offset, recv);
                
            }
        }
    }
}

Coordinator
Jan 5, 2011 at 10:36 AM

there will inevitably be latency with anything like this. you are probably hearing the direct sound (check your windows mixer settings) as well as the sound received over the network.