C# 将麦克风输入定向到扬声器,并使用CSCore库编写自定义DSP函数

C# 将麦克风输入定向到扬声器,并使用CSCore库编写自定义DSP函数,c#,audio,wasapi,cscore,C#,Audio,Wasapi,Cscore,CSCore()似乎是一个非常好的C#音频库,但它缺乏文档和良好的示例 我已经使用Bass.Net很长一段时间了,CSCore的体系结构与Bass库不同,因此很难找到正确的方法来完成一些常见任务 我正在尝试从WasapiCapture设备捕获麦克风输入,并将录制的数据输出到WasapiOut设备,但未能成功 下面是我在谷歌搜索后可以找到的代码,但它不起作用 MMDeviceEnumerator deviceEnum = new MMDeviceEnumerator(); MMDeviceColl

CSCore()似乎是一个非常好的C#音频库,但它缺乏文档和良好的示例

我已经使用Bass.Net很长一段时间了,CSCore的体系结构与Bass库不同,因此很难找到正确的方法来完成一些常见任务

我正在尝试从WasapiCapture设备捕获麦克风输入,并将录制的数据输出到WasapiOut设备,但未能成功

下面是我在谷歌搜索后可以找到的代码,但它不起作用

MMDeviceEnumerator deviceEnum = new MMDeviceEnumerator();
MMDeviceCollection devices = deviceEnum.EnumAudioEndpoints(DataFlow.Capture, DeviceState.Active);

using (var capture = new WasapiCapture())
{
    capture.Device = deviceEnum.GetDefaultAudioEndpoint(DataFlow.Capture, Role.Multimedia);
    capture.Initialize();

    using (var source = new SoundInSource(capture))
    {
        using (var soundOut = new WasapiOut())
        {
            capture.Start();

            soundOut.Device = deviceEnum.GetDefaultAudioEndpoint(DataFlow.Render, Role.Multimedia);
            soundOut.Initialize(source);
            soundOut.Play();
        }
    }
}
我想做的是编写一个像这样的应用程序:

我有自己的DSP功能,我想应用于记录的数据

有没有人有将wasapiapture引导到WasapiOut并编写定制DSP的例子

编辑:

我在CSCore库创建者Florian Rosmann(filoe)的帮助下找到了解决方案

下面是一个示例DSP类,用于放大提供的音频数据

class DSPGain: ISampleSource
{
    ISampleSource _source;
    public DSPGain(ISampleSource source)
    {
        if (source == null)
            throw new ArgumentNullException("source");
        _source = source;
    }
    public int Read(float[] buffer, int offset, int count)
    {
        float gainAmplification = (float)(Math.Pow(10.0, GainDB / 20.0));
        int samples = _source.Read(buffer, offset, count);
        for (int i = offset; i < offset + samples; i++)
        {
            buffer[i] = Math.Max(Math.Min(buffer[i] * gainAmplification, 1), -1);
        }
        return samples;
    }

    public float GainDB { get; set; }

    public bool CanSeek
    {
        get { return _source.CanSeek; }
    }

    public WaveFormat WaveFormat
    {
        get { return _source.WaveFormat; }
    }

    public long Position
    {
        get
        {
            return _source.Position;
        }
        set
        {
            _source.Position = value;
        }
    }

    public long Length
    {
        get { return _source.Length; }
    }

    public void Dispose()
    {
    }
}
WasapiCapture waveIn;
WasapiOut soundOut;
DSPGain gain;
private void StartFullDuplex()
{
    try
    {
        MMDeviceEnumerator deviceEnum = new MMDeviceEnumerator();
        MMDeviceCollection devices = deviceEnum.EnumAudioEndpoints(DataFlow.Capture, DeviceState.Active);

        waveIn = new WasapiCapture(false, AudioClientShareMode.Exclusive, 5);
        waveIn.Device = deviceEnum.GetDefaultAudioEndpoint(DataFlow.Capture, Role.Multimedia);
        waveIn.Initialize();
        waveIn.Start();


        var source = new SoundInSource(waveIn) { FillWithZeros = true };
        soundOut = new WasapiOut(false, AudioClientShareMode.Exclusive, 5);
        soundOut.Device = deviceEnum.GetDefaultAudioEndpoint(DataFlow.Render, Role.Multimedia);

        gain = new DSPGain(source.ToSampleSource());
        gain.GainDB = 5;

        soundOut.Initialize(gain.ToWaveSource(16));
        soundOut.Play();
    }
    catch (Exception ex)
    {
        Debug.WriteLine("Exception in StartFullDuplex: " + ex.Message);
    }

}

private void StopFullDuplex()
{
    if (soundOut != null) soundOut.Dispose();
    if (waveIn != null) waveIn.Dispose();
}