Skip to content

Commit

Permalink
Fix wasapi initialisation device target
Browse files Browse the repository at this point in the history
  • Loading branch information
peppy committed Dec 25, 2023
1 parent 7537080 commit 24828e3
Showing 1 changed file with 30 additions and 13 deletions.
43 changes: 30 additions & 13 deletions osu.Framework/Threading/AudioThread.cs
Original file line number Diff line number Diff line change
Expand Up @@ -124,32 +124,49 @@ internal static bool InitDevice(int deviceId)
// Try to initialise the device, or request a re-initialise.
if (Bass.Init(deviceId, Flags: (DeviceInitFlags)128)) // 128 == BASS_DEVICE_REINIT
{
if (WasapiMixer == 0)
{
wasapiProcedure = (buffer, length, user) => Bass.ChannelGetData(WasapiMixer, buffer, length);
int wasapiDevice = -1;

usingWasapi = BassWasapi.Init(-1, Procedure: wasapiProcedure, Buffer: 0.02f, Period: 0.005f);
if (Bass.CurrentDevice > 0)
{
string driver = Bass.GetDeviceInfo(Bass.CurrentDevice).Driver;

if (usingWasapi)
if (!string.IsNullOrEmpty(driver))
{
BassWasapi.GetInfo(out var wasapiInfo);
WasapiMixer = BassMix.CreateMixerStream(wasapiInfo.Frequency, wasapiInfo.Channels, BassFlags.MixerNonStop | BassFlags.Decode | BassFlags.Float);
BassWasapi.Start();

Bass.ChannelSetAttribute(WasapiMixer, ChannelAttribute.Buffer, 0);
while (true)
{
if (!BassWasapi.GetDeviceInfo(++wasapiDevice, out WasapiDeviceInfo info))
break;

if (info.ID == driver)
break;
}
}
}

if (WasapiMixer != 0)
{
Bass.StreamFree(WasapiMixer);
BassWasapi.Free();
WasapiMixer = 0;
}

wasapiProcedure = (buffer, length, _) => Bass.ChannelGetData(WasapiMixer, buffer, length);
usingWasapi = BassWasapi.Init(wasapiDevice, Procedure: wasapiProcedure, Buffer: 0.02f, Period: 0.005f);

if (usingWasapi)
{
BassWasapi.GetInfo(out var wasapiInfo);
WasapiMixer = BassMix.CreateMixerStream(wasapiInfo.Frequency, wasapiInfo.Channels, BassFlags.MixerNonStop | BassFlags.Decode | BassFlags.Float);
BassWasapi.Start();
}

initialised_devices.Add(deviceId);
return true;
}

return false;
}

private static int wasapiProc(IntPtr buffer, int length, IntPtr user) =>
Bass.ChannelGetData(WasapiMixer, buffer, length);

internal static void FreeDevice(int deviceId)
{
Debug.Assert(ThreadSafety.IsAudioThread);
Expand Down

0 comments on commit 24828e3

Please sign in to comment.