我有6个音频源,需要使用ASIO在6个单独的频道上播放。
我设法使用WaveOutEvent
作为输出来实现这一点,但当我切换到AsioOut
时,我得到了一个空引用错误,我不知道我做错了什么。
我需要使用ASIO,因为我需要6个输出通道,而且我必须使用Dante协议通过网络广播音频。输出设备是Dante虚拟声卡。
错误为:
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,它工作起来没有问题!