尝试在 ASIO 输出上使用 NAudio 播放音频时为空引用

问题描述

我有 6 个音频源,需要使用 ASIO 在 6 个单独的通道上播放。

我设法使用 WaveOutEvent 作为输出使其正常工作,但是当我切换到 AsioOut 时,我收到一个空引用错误,我无法弄清楚我做错了什么。

我需要使用 ASIO,因为我需要 6 个输出通道,而且我必须使用 Dante 协议通过网络广播音频。 输出设备是 Dante Virtual Soundcard。

错误是:

NullReferenceException: Object reference not set to an instance of an object
NAudio.Wave.AsioOut.driver_BufferUpdate (System.IntPtr[] inputChannels,System.IntPtr[] outputChannels) (at <7b1c1a8badc0497bac142a81b5ef5bcf>:0)
NAudio.Wave.Asio.AsioDriverExt.BufferSwitchCallBack (System.Int32 doubleBufferIndex,System.Boolean directProcess) (at <7b1c1a8badc0497bac142a81b5ef5bcf>:0)
UnityEngine.<>c:<RegisterUECatcher>b__0_0(Object,UnhandledExceptionEventArgs)

这是播放音频的(简化)代码。缓冲区由外部类中的其他方法填充。

using NAudio.Wave;
using System;
using System.Collections.Generic;

public class AudioMultiplexer
{
    MultiplexingWaveProvider multiplexer;
    AsioOut asioOut;

    public List<BufferedWaveProvider> buffers;
    public int outputChannels = 6;
    public int waveFormatSampleRate = 48000;
    public int waveFormatBitDepth = 24;
    public int waveFormatChannels = 2;

    public void Start()
    {
        buffers = new List<BufferedWaveProvider>();
        var outputFormat = new WaveFormat(waveFormatSampleRate,waveFormatBitDepth,waveFormatChannels);

        for (int i = 0; i < outputChannels; i++)
        {
            var buffer = new BufferedWaveProvider(outputFormat);
            buffer.DiscardOnBufferOverflow = true;
            // Make sure the buffers are big enough,just in case
            buffer.BufferDuration = TimeSpan.FromMinutes(5);
            buffers.Add(buffer);
        }

        multiplexer = new MultiplexingWaveProvider(buffers,outputChannels);

        for (int i = 0; i < outputChannels; i++)
        {
            // Each input has 2 channels,left & right,take only one channel from each input source
            multiplexer.ConnectInputToOutput(i * 2,i);
        }

        var driverName = GetDanteDriverName();

        if (string.IsNullOrEmpty(driverName))
        {
            return;
        }

        asioOut = new AsioOut(driverName);
        asioOut.AudioAvailable += AsioOut_AudioAvailable;
        asioOut.Init(multiplexer);
        asioOut.Play();
    }

    private void AsioOut_AudioAvailable(object sender,AsioAudioAvailableEventArgs e)
    {
        // Do nothing for now
        Console.WriteLine("Audio available");
    }

    private string GetDanteDriverName()
    {
        foreach (var driverName in AsioOut.GetDriverNames())
        {
            if (driverName.Contains("Dante Virtual Soundcard"))
            {
                return driverName;
            }
        }
        return null;
    }

    private void OnDestroy()
    {
        asioOut.Stop();
        asioOut.Dispose();
        asioOut = null;
    }
}

我可能对 AsioOut 的工作方式有误解,但我不确定从哪里开始或如何调试错误。

解决方法

您可以使用低延迟多声道音频资产统一播放多声道音频与 asio。与 naudio 不同,它专为 Unity 而设计,而且运行没有问题!

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...