From 135c20137c06a800c8b33f8bd9ea0b5c14aaeb2d Mon Sep 17 00:00:00 2001 From: Dean Herbert Date: Mon, 25 Dec 2023 14:54:50 +0900 Subject: [PATCH] Isolate wasapi initialisation to windows platform --- osu.Framework/Threading/AudioThread.cs | 72 ++++++++++++++------------ 1 file changed, 39 insertions(+), 33 deletions(-) diff --git a/osu.Framework/Threading/AudioThread.cs b/osu.Framework/Threading/AudioThread.cs index 491ee118c27..5086818f671 100644 --- a/osu.Framework/Threading/AudioThread.cs +++ b/osu.Framework/Threading/AudioThread.cs @@ -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)