NAudio icon indicating copy to clipboard operation
NAudio copied to clipboard

System.AccessViolationException with ASIO for specific driver

Open BlackCoyote opened this issue 2 years ago • 0 comments

Hello,

I am experiencing a System.AccessViolationException when trying to initialize an ASIO driver named "ASIO UX2" inside a larger WPF project.

Fatal error. System.AccessViolationException: Attempted to read or write protected memory. This is often an indication that other memory is corrupt. at NAudio.Wave.Asio.AsioDriver.CreateBuffers(IntPtr, Int32, Int32, NAudio.Wave.Asio.AsioCallbacks ByRef) at NAudio.Wave.Asio.AsioDriverExt.CreateBuffers(Int32, Int32, Boolean)

I am able to initialize and get sound out of different ASIO drivers like "ASIO4ALL V2" inside the same project, and I am able to initialize the problematic "ASIO UX2" driver and also get sound out of it inside other console app projects.

The relevant bit of code looks like this:

        private static void StartAudio(int sampleRate, int bytesPerSample, double blockDuration)
        {
            MainWindow.Instance.Dispatcher.Invoke(() =>
            {
                    OutputBuffer = new BufferedWaveProvider(new WaveFormat(44100, 32, 2));
                    OutputBuffer.BufferDuration = new TimeSpan(0, 0, 0, 0, 200);
                    OutputBuffer.DiscardOnBufferOverflow = true;
                    OutputDriver = new AsioOut("ASIO UX2");
                    OutputDriver.Init(OutputBuffer);
                    OutputDriver.Play();
            });
        }

It is also worth mentioning that placing Console.WriteLine(Thread.CurrentThread.GetApartmentState()); in that block will print "STA" as expected.

I was hoping you might be able to tell me what I'm doing wrong. Thanks!

BlackCoyote avatar Oct 30 '22 01:10 BlackCoyote