Мне удалось заставить это работать, используя 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 :0)
NAudio.Wave.Asio.AsioDriverExt.BufferSwitchCallBack (System.Int32 doubleBufferIndex, System.Boolean directProcess) (at :0)
UnityEngine.c:b__0_0(Object, UnhandledExceptionEventArgs)
Код: Выделить всё
using NAudio.Wave;
using System;
using System.Collections.Generic;
public class AudioMultiplexer
{
MultiplexingWaveProvider multiplexer;
AsioOut asioOut;
public List buffers;
public int outputChannels = 6;
public int waveFormatSampleRate = 48000;
public int waveFormatBitDepth = 24;
public int waveFormatChannels = 2;
public void Start()
{
buffers = new List();
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;
}
}
Подробнее здесь: https://stackoverflow.com/questions/667 ... sio-output