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

Echo recorded voice

Jul 24, 2012 at 8:14 PM
Edited Jul 24, 2012 at 8:27 PM

Hi, I'm trying to record the voice, and play each buffer as soon as it is received, like implementing a voice-echo application.

But there is gaps in the output.

Here is the code:

 

    public partial class Form1 : Form
    {
        private WaveIn _waveIn = new WaveIn();
        private WaveOut _waveOut = new WaveOut();
        BufferedWaveProvider _waveProvider = new BufferedWaveProvider(new WaveFormat());

        public Form1()
        {
            InitializeComponent();
        }

        private void btnRec_Click(object sender, EventArgs e)
        {
            _waveIn.DataAvailable += waveIn_DataAvailable;
            _waveIn.StartRecording();

            _waveOut.Init(_waveProvider);
            _waveOut.Play();
        }

        void waveIn_DataAvailable(object sender, WaveInEventArgs e)
        {
            _waveProvider.AddSamples(e.Buffer, 0, e.BytesRecorded);
        }

        private void btnStopRec_Click(object sender, EventArgs e)
        {
            _waveIn.StopRecording();
            _waveIn.Dispose();

            _waveOut.Stop();
            _waveOut.Dispose();
        }
    }

Edited:

 

    public partial class Form1 : Form
    {
        private WaveIn _waveIn = new WaveIn();
        private WaveOut _waveOut = new WaveOut();
        BufferedWaveProvider _waveProvider = new BufferedWaveProvider(new WaveFormat(11025, 8, 1));

        public Form1()
        {
            InitializeComponent();
        }

        private void btnRec_Click(object sender, EventArgs e)
        {
            _waveIn.DataAvailable += waveIn_DataAvailable;
            _waveIn.WaveFormat = new WaveFormat(11025, 8, 1);
            _waveIn.StartRecording();

            _waveOut.Init(_waveProvider);
            _waveOut.Play();
        }

        void waveIn_DataAvailable(object sender, WaveInEventArgs e)
        {
            _waveProvider.AddSamples(e.Buffer, 0, e.BytesRecorded);
        }

        private void btnStopRec_Click(object sender, EventArgs e)
        {
            _waveIn.StopRecording();
            _waveIn.Dispose();

            _waveOut.Stop();
            _waveOut.Dispose();
        }
    }

 

After changing the format to 11025Hz, 8Bit, Mono, it plays well. It seems the problem is with high quality formats.

Anybody knows a solution for this behavior?

Thanks in advance.

Coordinator
Jul 30, 2012 at 1:57 PM

I guess it could be the garbage collector getting in the way. Also, have you tried with WaveInEvent and WaveOutEvent?