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

时间:2022-09-08 23:17:42

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; it's the same problem on both of them.

我用asio4all和MAUDIO卡试过了;它们都是同样的问题。

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;
    }
}

1 个解决方案

#1


0  

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;
    }
}

#1


0  

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;
    }
}