2021年3月20日星期六

How do I properly remove Acoustic Echo Cancellation using Speex?

UPDATED: Overall code that I use

For this, you really have to look at Mark Heath article here: how to play and record at the sametime. With that, you can really directly understood the problem right? Which is, the played sound from mic, will be recorded back to mic .. in a loop. Hence, echo effect.

I start the record and play action within a btnRecord event:

private void btnRecord_Click(object sender, EventArgs e)          {                // set up the recorder              btnRecord.Enabled = false;              btnStop.Enabled = true;                              recorder = new WaveIn();              recorder.DataAvailable += RecorderOnDataAvailable;                // set up our signal chain              bufferedWaveProvider = new BufferedWaveProvider(recorder.WaveFormat);              savingWaveProvider = new SavingWaveProvider(bufferedWaveProvider, "tempx.wav");                // set up playback              player = new WaveOut();              player.Volume = 1;              player.Init(savingWaveProvider);                // begin playback & record              player.Play();              recorder.StartRecording();                          }  

And here is the code that get called when recoreded data available:

 private void RecorderOnDataAvailable(object sender, WaveInEventArgs waveInEventArgs)          {              //incoming data, process it              //TODO: AEC should be done here              IntPtr unmanagedPointer = Marshal.AllocHGlobal(waveInEventArgs.Buffer.Length);              Marshal.Copy(waveInEventArgs.Buffer, 0, unmanagedPointer, waveInEventArgs.Buffer.Length);              speex_preprocess_run(speex_state, unmanagedPointer);                            byte[] b = new byte[waveInEventArgs.Buffer.Length];              Marshal.Copy(unmanagedPointer, b, 0, waveInEventArgs.Buffer.Length);                // play it back              bufferedWaveProvider.AddSamples(b, 0, waveInEventArgs.BytesRecorded);          }  

In the last line you can see that I put the resultant buffer from speex processing to be played again in the bufferedWaveProvider. Unfortunately, it is still the same: still the same echo effect.

https://stackoverflow.com/questions/66627272/how-do-i-properly-remove-acoustic-echo-cancellation-using-speex March 15, 2021 at 01:05AM

没有评论:

发表评论