我想使用 naudio 通过网络与 csharp 进行语音对话。然而,回声正在发生。我使用 libspeexdsp.dll 来避免回声,但回声仍在继续,几乎没用。我想知道我在哪里做错了。我的声音回到我身边。我计算了任何延迟。我想知道我是否应该考虑 speex 数据的其他替代方案。
class EchoFilter {
[DllImport("libspeexdsp", EntryPoint = "speex_echo_state_init", CallingConvention = CallingConvention.Cdecl)]
static extern IntPtr speex_echo_state_init(int frame_size, int filter_length);
[DllImport("libspeexdsp", EntryPoint = "speex_echo_cancellation", CallingConvention = CallingConvention.Cdecl)]
static extern void speex_echo_cancellation(IntPtr state, short[] inputFrame, short[] echoFrame, short[] outputFrame);
[DllImport("libspeexdsp", EntryPoint = "speex_echo_ctl", CallingConvention = CallingConvention.Cdecl)]
public static extern int speex_echo_ctl(IntPtr st, int id, ref int sampleRate);
[DllImport("libspeexdsp", EntryPoint = "speex_preprocess_state_init", CallingConvention = CallingConvention.Cdecl)]
static extern IntPtr speex_preprocess_state_init(int frame_size, int sampleRate);
[DllImport("libspeexdsp", EntryPoint = "speex_preprocess_ctl", CallingConvention = CallingConvention.Cdecl)]
public static extern int speex_preprocess_ctl(IntPtr state, int id, IntPtr val);
[DllImport("libspeexdsp", EntryPoint = "speex_preprocess_run", CallingConvention = CallingConvention.Cdecl)]
public static extern int speex_preprocess_run(IntPtr st, short[] outputFrame);
[DllImport("libspeexdsp", EntryPoint = "speex_preprocess_state_destroy", CallingConvention = CallingConvention.Cdecl)]
public static extern void speex_preprocess_state_destroy(IntPtr st);
[DllImport("libspeexdsp", EntryPoint = "speex_echo_state_destroy", CallingConvention = CallingConvention.Cdecl)]
static extern void speex_echo_state_destroy(IntPtr state);
IntPtr st;
IntPtr den;
int SPEEX_ECHO_SET_SAMPLING_RATE = 24;
int SPEEX_PREPROCESS_SET_ECHO_STATE = 24;
/// <param name="frameSize">frameSize is the amount of data (in samples) you want to process at once.</param>
/// <param name="filterLength">filterLength is the length (in samples) of the echo cancelling filter you want to use (also known as tail length).</param>
public EchoFilter(int frameSize, int filterLength, int sampleRate) {
st = speex_echo_state_init(frameSize, filterLength);
den = speex_preprocess_state_init(frameSize, sampleRate);
speex_echo_ctl(st, SPEEX_ECHO_SET_SAMPLING_RATE, ref sampleRate);
speex_preprocess_ctl(den, SPEEX_PREPROCESS_SET_ECHO_STATE, st);
}
/// <summary>
/// Method for echo cancellation
/// </summary>
/// <param name="inputFrame">Frame obtained from local microphone (Signal that contains echo)</param>
/// <param name="echoFrame">Frame obtained from remote source (Source of echo)</param>
/// <param name="outputFrame">Filtered output</param>
public void Filter(short[] inputFrame, short[] echoFrame, short[] outputFrame) {
speex_echo_cancellation(st, inputFrame, echoFrame, outputFrame);
speex_preprocess_run(den, outputFrame);
}
public void Dispose() {
Dispose(true);
GC.SuppressFinalize(this);
}
void Dispose(bool disposing) {
if (st != IntPtr.Zero) {
speex_echo_state_destroy(st);
st = IntPtr.Zero;
}
if (den != IntPtr.Zero) {
speex_preprocess_state_destroy(den);
den = IntPtr.Zero;
}
}
~EchoFilter() {
Dispose(false);
}
}
class IMicrophone : IMMNotificationClient {
MMDeviceEnumerator deviceEnum;
ComboBox cbbDeviceList;
int bufferMilliseconds = 20;
WaveIn waveIn = null;
BufferedWaveProvider bufferedWaveProvider;
WaveFormat waveFormat = null;
EventHandler<WaveInEventArgs> dataAvailable;
public MMDevice defaultSpeaker = null;
public MMDevice defaultMicrophone = null;
public bool noDevice() {
return WaveIn.DeviceCount == 0 ? true : false;
}
public EchoFilter filterSpeex = null;
Queue<byte[]> playedQueue = new Queue<byte[]>();
public IMicrophone() {
deviceEnum = new MMDeviceEnumerator();
deviceEnum.RegisterEndpointNotificationCallback(this);
waveFormat = new WaveFormat(ISetting.MIC_SAMPLE_RATE, 1);
TimeSpan frameSizeTime = TimeSpan.FromMilliseconds(bufferMilliseconds);
int frameSize = (int)Math.Ceiling(frameSizeTime.TotalSeconds * waveFormat.SampleRate);
int filterLength = frameSize * 25;
filterSpeex = new EchoFilter(frameSize, filterLength, ISetting.MIC_SAMPLE_RATE);
}
public void echoCancellation(byte[] buffer, Action<byte[]> completed) {
short[] bufferOut = new short[buffer.Length / 2];
lock (playedQueue) {
if (playedQueue.Count == 0) {
completed(buffer);
} else {
filterSpeex.Filter(BytesToShorts(buffer), BytesToShorts(playedQueue.First()), bufferOut);
completed(ShortsToBytes(bufferOut));
}
}
}
public void load(ComboBox cbbDeviceList, EventHandler<WaveInEventArgs> dataAvailable) {
defaultSpeaker = deviceEnum.GetDefaultAudioEndpoint(DataFlow.Render, Role.Multimedia);
defaultMicrophone = deviceEnum.GetDefaultAudioEndpoint(DataFlow.Capture, Role.Multimedia);
this.cbbDeviceList = cbbDeviceList;
this.dataAvailable = dataAvailable;
List<WaveInCapabilities> waveIns = new List<WaveInCapabilities>();
for (int i = 0; i < WaveIn.DeviceCount; i++) {
cbbDeviceList.Items.Add(WaveIn.GetCapabilities(i).ProductName);
}
if (WaveIn.DeviceCount > 0) {
cbbDeviceList.SelectedIndex = 0;
}
setMicrophoneMasterVolumeLevel(80);
}
public void addSamples(AudioInfo audioInfo) {
if (!ISetting.ipAddress.Equals(audioInfo.ip)) {
lock (playedQueue) {
playedQueue.Enqueue(audioInfo.buffer);
bufferedWaveProvider.AddSamples(audioInfo.buffer, 0, audioInfo.buffer.Length);
}
}
}
public void connect() {
stop();
if (WaveIn.DeviceCount > 0) {
waveIn = new WaveIn();
waveIn.DeviceNumber = cbbDeviceList.SelectedIndex;
waveIn.DataAvailable += new EventHandler<WaveInEventArgs>(dataAvailable);
waveIn.WaveFormat = waveFormat;
waveIn.BufferMilliseconds = bufferMilliseconds; // Biriktirme MS
start();
// Mic Play
WaveOut waveOut = new WaveOut();
bufferedWaveProvider = new BufferedWaveProvider(waveFormat) { DiscardOnBufferOverflow = true };
waveOut.DesiredLatency = 100;
waveOut.Volume = 1.0f;
waveOut.Init(bufferedWaveProvider);
waveOut.Play();
}
}
public void start() {
waveIn.StartRecording();
}
public void stop() {
if (waveIn != null) {
waveIn.Dispose();
waveIn = null;
}
}
}
[Serializable]
class AudioInfo {
public string ip;
public byte[] buffer;
public AudioInfo(string ip, byte[] buffer) {
this.ip = ip;
this.buffer = buffer;
}
}}
void wave_DataAvailable(object sender, WaveInEventArgs e) {
microphone.echoCancellation(e.Buffer, (bufferOut) => {
if (MYInfo.info.isSpeaker && MYInfo.info.startedMic) {
byte[] bytesStream = Helper.serializeToStream(new AudioInfo(ISetting.ipAddress, bufferOut));
foreach (UserInfo userInfo in UsersInfo.infos) { // SADECE SERVER
ITCP.sendDataAsync(userInfo.ipAddress, ISetting.PORT_MIC, bytesStream);
}
}
});
}
private void micStartListening(IAsyncResult ar) {
ITCP.getListenData(listenerMic, ar, micStartListening, (buffer) => {
AudioInfo audioInfo = (AudioInfo)Helper.desserialize(buffer);
microphone.addSamples(audioInfo);
if (MYInfo.info.isServer) {
foreach (UserInfo userInfo in UsersInfo.infos.Where(info => info.isClient && !info.ipAddress.Equals(audioInfo.ip))) {
//ITCP.sendDataAsync(userInfo.ipAddress, ISetting.PORT_MIC, buffer);
}
}
});
}