Skip to content

Commit

Permalink
Isolate wasapi initialisation to windows platform
Browse files Browse the repository at this point in the history
  • Loading branch information
peppy committed Dec 25, 2023
1 parent 24828e3 commit 135c201
Showing 1 changed file with 39 additions and 33 deletions.
72 changes: 39 additions & 33 deletions osu.Framework/Threading/AudioThread.cs
Original file line number Diff line number Diff line change
Expand Up @@ -122,49 +122,55 @@ internal static bool InitDevice(int deviceId)
Trace.Assert(deviceId != -1); // The real device ID should always be used, as the -1 device has special cases which are hard to work with.

// Try to initialise the device, or request a re-initialise.
if (Bass.Init(deviceId, Flags: (DeviceInitFlags)128)) // 128 == BASS_DEVICE_REINIT
{
int wasapiDevice = -1;
if (!Bass.Init(deviceId, Flags: (DeviceInitFlags)128)) // 128 == BASS_DEVICE_REINIT
return false;

if (Bass.CurrentDevice > 0)
{
string driver = Bass.GetDeviceInfo(Bass.CurrentDevice).Driver;
attemptWasapiInitialisation();

if (!string.IsNullOrEmpty(driver))
{
while (true)
{
if (!BassWasapi.GetDeviceInfo(++wasapiDevice, out WasapiDeviceInfo info))
break;

if (info.ID == driver)
break;
}
}
}
initialised_devices.Add(deviceId);
return true;
}

if (WasapiMixer != 0)
{
Bass.StreamFree(WasapiMixer);
BassWasapi.Free();
WasapiMixer = 0;
}
private static void attemptWasapiInitialisation()
{
if (RuntimeInfo.OS != RuntimeInfo.Platform.Windows)
return;

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

if (usingWasapi)
if (Bass.CurrentDevice > 0)
{
string driver = Bass.GetDeviceInfo(Bass.CurrentDevice).Driver;

if (!string.IsNullOrEmpty(driver))
{
BassWasapi.GetInfo(out var wasapiInfo);
WasapiMixer = BassMix.CreateMixerStream(wasapiInfo.Frequency, wasapiInfo.Channels, BassFlags.MixerNonStop | BassFlags.Decode | BassFlags.Float);
BassWasapi.Start();
while (true)
{
if (!BassWasapi.GetDeviceInfo(++wasapiDevice, out WasapiDeviceInfo info))
break;

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

initialised_devices.Add(deviceId);
return true;
if (WasapiMixer != 0)
{
Bass.StreamFree(WasapiMixer);
BassWasapi.Free();
WasapiMixer = 0;
}

return false;
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();
}
}

internal static void FreeDevice(int deviceId)
Expand Down

0 comments on commit 135c201

Please sign in to comment.