简体   繁体   English

如何使用NAudio和ASIO直接读取输入缓冲区和回放?

[英]How to read input buffer and playback directly using NAudio and ASIO?

I have a little problem with the following C# code using ASIO and the NAudio library. 使用ASIO和NAudio库的以下C#代码对我有一点问题。

I try to get sound from a guitar and directly play the sound of it in the speakers. 我尝试从吉他获取声音,然后直接在扬声器中播放声音。 So far it works, but the sound is very distorted. 到目前为止,它可以工作,但是声音非常失真。 I've read here that a way to slove that is by doing: 我在这里读到一种做爱的方法是:

Marshal.Copy(buf, 0, e.OutputBuffers[i], e.SamplesPerBuffer * 4);

But, if I do that, the size of the buffer is 4 times bigger, so the code won't compile. 但是,如果我这样做,缓冲区的大小将增加4倍,因此代码将无法编译。

I've tried that with asio4all and a MAUDIO card; 我已经使用asio4all和MAUDIO卡进行了尝试; it's the same problem on both of them. 他们两个都是同一个问题。

public partial class MainWindow : Window
{
    AsioOut ASIODriver;
    BufferedWaveProvider buffer;
    public MainWindow()
    {
        String[] drivernames = AsioOut.GetDriverNames();
        ASIODriver = new AsioOut(drivernames[0]);

        buffer = new BufferedWaveProvider(new WaveFormat ());
        ASIODriver.AudioAvailable += new EventHandler<AsioAudioAvailableEventArgs>(ASIODriver_AudioAvailable);
        ASIODriver.InitRecordAndPlayback(buffer,2,44100);
        //ASIODriver.InputChannelOffset = 1;
        ASIODriver.Play();       
    }

    private void ASIODriver_AudioAvailable(object sender, AsioAudioAvailableEventArgs e)
    {
        byte[] buf = new byte[e.SamplesPerBuffer];
        for (int i = 0; i < e.InputBuffers.Length; i++)
        {
           Marshal.Copy(e.InputBuffers[i], buf, 0, e.SamplesPerBuffer);
           Marshal.Copy(buf, 0, e.OutputBuffers[i], e.SamplesPerBuffer); 
        }
        e.WrittenToOutputBuffers = true;
    }
}

Solution by OP. 由OP解决。

The solution was to increase the size of the buffer. 解决方案是增加缓冲区的大小。 See the commented lines and their following one for the changes. 有关更改,请参见注释行及其后续的一行。

public partial class MainWindow : Window
{
    AsioOut ASIODriver;
    BufferedWaveProvider buffer;
    public MainWindow()
    {
        String[] drivernames = AsioOut.GetDriverNames();
        ASIODriver = new AsioOut(drivernames[0]);

        buffer = new BufferedWaveProvider(new WaveFormat ());
        ASIODriver.AudioAvailable += new EventHandler<AsioAudioAvailableEventArgs>(ASIODriver_AudioAvailable);
        ASIODriver.InitRecordAndPlayback(buffer,2,44100);
        ASIODriver.Play();       
    }

    private void ASIODriver_AudioAvailable(object sender, AsioAudioAvailableEventArgs e)
    {
        //byte[] buf = new byte[e.SamplesPerBuffer];
        byte[] buf = new byte[e.SamplesPerBuffer*4];
        for (int i = 0; i < e.InputBuffers.Length; i++)
        {
           //Marshal.Copy(e.InputBuffers[i], buf, 0, e.SamplesPerBuffer);
           //Marshal.Copy(buf, 0, e.OutputBuffers[i], e.SamplesPerBuffer);
           Marshal.Copy(e.InputBuffers[i], buf, 0, e.SamplesPerBuffer*4);
           Marshal.Copy(buf, 0, e.OutputBuffers[i], e.SamplesPerBuffer*4);
        }
        e.WrittenToOutputBuffers = true;
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM