Null reference when trying to play audio using NAudio on ASIO output

407 Views Asked by At

I have 6 audio sources which I need to play on 6 separate channels using ASIO.

I managed to get this working using WaveOutEvent as the output, but when I switch to AsioOut I get a null reference error, and I can't figure out what I'm doing wrong.

I need to use ASIO since I require 6 output channels and because I have to broadcast the audio over the network using Dante protocol. The output device is a Dante Virtual Soundcard.

The error is:

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)

This is the (simplified) code that plays the audio. The buffers are filled by other methods in external classes.

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

I may have a misunderstanding of how AsioOut works but I'm not sure where to start on this or how to debug the error.

1

There are 1 best solutions below

1
On

You can use the Low-latency Multichannel Audio asset for unity to play multi-channel audio with asio. It's made especially for unity, unlike naudio, and it works without problems!