'NAudio: System.Runtime.InteropServices.COMException: 'Unable to instantiate ASIO. Check if STAThread is set'
I'm trying to create a WPF application which will use ASIO4ALL driver. I have created a class in which I create the AsioOut object:
public HostModel(float sampleRate)
{
Mixer = new MixingSampleProvider(WaveFormat.CreateIeeeFloatWaveFormat((int)(sampleRate * 1000), 2));
AudioDriverOnDriverResetRequest(this, EventArgs.Empty);
}
private void AudioDriverOnDriverResetRequest(object? sender, EventArgs e)
{
AudioDriver?.Dispose();
AudioDriver = new AsioOut(0);
AudioDriver.DriverResetRequest += AudioDriverOnDriverResetRequest;
AudioDriver.Init(Mixer);
AudioDriver.Play();
}
I created the event handler because I want to be able to change buffer size in ASIO control panel. When buffer size is changed for the first time (and also during the initial event handler call from constructor) everything works fine, but on the second attempt I get this:
ErrorCode -2147467262
System.Runtime.InteropServices.COMException: 'Unable to instantiate ASIO. Check if STAThread is set'
at NAudio.Wave.Asio.AsioDriver.InitFromGuid(Guid asioGuid)
at NAudio.Wave.Asio.AsioDriver.GetAsioDriverByGuid(Guid guid)
at NAudio.Wave.Asio.AsioDriver.GetAsioDriverByName(String name)
at NAudio.Wave.AsioOut.InitFromName(String driverName)
at NAudio.Wave.AsioOut..ctor(Int32 driverIndex)
at [...].HostModel.AudioDriverOnDriverResetRequest(Object sender, EventArgs e) in [...]\HostModel.cs:line 25
at NAudio.Wave.AsioOut.OnDriverResetRequest()
at NAudio.Wave.Asio.AsioDriverExt.AsioMessageCallBack(AsioMessageSelector selector, Int32 value, IntPtr message, IntPtr opt)
Why is this happening? Is there a better/more correct way to achieve what I want (ability to chang buffer size in ASIO control panel)?
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
