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

decrease capture latency

Dec 18, 2010 at 6:45 PM
Edited Dec 19, 2010 at 9:03 PM

Sorry, may be I've missed something but i didn't find anything  about my problem....

I'm writing a voice chat for the local network.. And i have the nessecity to allow the speaker hear his own voice

but the latency between speaking and playing is big if I do it with WaveIn, WaveOut.

how can i make it smaller..?

or may be you know the other way to speak and to listen to yourself simultaneously?

Coordinator
Dec 24, 2010 at 10:18 AM

Both WaveIn and WaveOut allow adjustment of Buffer Sizes and number of buffers. This should let you reduce latency.

Mark

Jan 11, 2011 at 7:29 PM

Big Thanks, Mark... 

Will you be so kind to answer one more question.. I'm using such parameters for  WaveIn:

waveIn.BufferMillisconds = 50;

waveIn.WaveFormat = new WaveFormat(4000,16,1);

 waveIn.NumberOfBuffers = 2;

The size of captured buffer is 400, so how can I play this buffer with WaveOut?What parameters should I set for Waveprovider?

To Play The Sound I use this class

 public class Waweprov : IWaveProvider, IDisposable
    {
        private readonly WaveFormat _wformat;
        private readonly Queue<byte[]> _input;

        public WaveFormat WaveFormat
        {
            get { return _wformat; }
        }

        public Waweprov(WaveFormat wformat)
        {
            _wformat = wformat;
            _input = new Queue<byte[]>();
        }

        public void Write(byte[] buffer)
        {
            lock (_input)
                _input.Enqueue(buffer);
        }
        public int Read(byte[] buffer, int offset, int count)
        {
            byte[] readbuffer = null;

            lock (_input)
            {
                if (0 < _input.Count)
                    readbuffer = _input.Dequeue();
            }
            if (null != readbuffer)
            {
                Buffer.BlockCopy(readbuffer, 0, buffer, 0, count);             
            }
            return null == readbuffer ? count : buffer.Length;
            
        }

        public int Read(IWaveBuffer waveBuffer)
        {
            if (_input.Count != 0)
            {
                var bu;
                bu = Read(waveBuffer.ByteBuffer, 0, waveBuffer.ByteBuffer.Length);              
                return bu;
            }
            else
            {

                using (var ms2 = new MemoryStream(waveBuffer.ByteBuffer))
                {
                    ms2.Write(new byte[waveBuffer.ByteBuffer.Length], 0, waveBuffer.ByteBuffer.Length);
                }
                return 400;
            }

        }

        public void Dispose()
        {
            lock (_input)
                _input.Clear();
        }


    
    }

Coordinator
Jan 13, 2011 at 9:37 AM

That looks like basically the right approach. Have a look at BufferedWaveProvider (I think that's the right name) in the NAudio source code

Mark