Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions osu.Framework/NativeMethods.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
CoCreateInstance

MMDeviceEnumerator
IMMDeviceEnumerator

IAudioClient3
IAudioRenderClient
AUDCLNT_STREAMFLAGS_EVENTCALLBACK

CreateEventW
WaitForSingleObject
49 changes: 47 additions & 2 deletions osu.Framework/Threading/AudioThread.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,12 @@
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Diagnostics.CodeAnalysis;
using System.Linq;
using System.Threading;
using Windows.Win32;
using Windows.Win32.Media.Audio;
using Windows.Win32.System.Com;
using ManagedBass;
using ManagedBass.Mix;
using ManagedBass.Wasapi;
Expand Down Expand Up @@ -140,7 +145,7 @@

// Need to do more testing. Users reporting buffer underruns even with a large (20ms) buffer.
// Also playback latency improvements are not present across all users.
// attemptWasapiInitialisation();
attemptWasapiInitialisation();

initialised_devices.Add(deviceId);
return true;
Expand Down Expand Up @@ -245,7 +250,8 @@

BassWasapi.GetInfo(out var wasapiInfo);
globalMixerHandle.Value = BassMix.CreateMixerStream(wasapiInfo.Frequency, wasapiInfo.Channels, BassFlags.MixerNonStop | BassFlags.Decode | BassFlags.Float);
BassWasapi.Start();
// BassWasapi.Start();
new Thread(initWasapiNatively) { Priority = ThreadPriority.Highest }.Start();

BassWasapi.SetNotify(wasapiNotifyProcedure);
}
Expand All @@ -262,5 +268,44 @@
}

#endregion

[SuppressMessage("Interoperability", "CA1416:Validate platform compatibility")]
private void initWasapiNatively()
{
PInvoke.CoCreateInstance<IMMDeviceEnumerator>(typeof(MMDeviceEnumerator).GUID, null, CLSCTX.CLSCTX_ALL, out var deviceEnumerator).ThrowOnFailure();
deviceEnumerator.GetDefaultAudioEndpoint(EDataFlow.eRender, ERole.eConsole, out IMMDevice? device);

device.Activate(typeof(IAudioClient3).GUID, CLSCTX.CLSCTX_ALL, null, out object? audioClientObj);
var audioClient = (IAudioClient3)audioClientObj;

unsafe
{
audioClient.GetMixFormat(out var format);
audioClient.GetSharedModeEnginePeriod(format, out _, out _, out uint period, out _); // default,fundamental,minimum,maximum
audioClient.InitializeSharedAudioStream(PInvoke.AUDCLNT_STREAMFLAGS_EVENTCALLBACK, period, format);

var eventHandle = PInvoke.CreateEvent(null, false, false, (string?)null);
audioClient.SetEventHandle(eventHandle);

audioClient.GetService(typeof(IAudioRenderClient).GUID, out object? rendererObj);
var renderer = (IAudioRenderClient)rendererObj;

audioClient.Start();

while (true)
{
PInvoke.WaitForSingleObject(eventHandle, uint.MaxValue);

audioClient.GetCurrentPadding(out uint padding);
if (padding >= period) continue; // the magic line that keeps the latency low

uint framesRequired = period - padding;

renderer.GetBuffer(framesRequired, out byte* bufferPointer);
Bass.ChannelGetData(globalMixerHandle.Value!.Value, (IntPtr)bufferPointer, (int)framesRequired * 8);
renderer.ReleaseBuffer(framesRequired, 0);
}
}
}

Check failure on line 309 in osu.Framework/Threading/AudioThread.cs

View workflow job for this annotation

GitHub Actions / Code Quality

Function never returns in osu.Framework\Threading\AudioThread.cs on line 309
}
}
4 changes: 4 additions & 0 deletions osu.Framework/osu.Framework.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@
<PackageReference Include="managed-midi" Version="1.10.1" />
<PackageReference Include="FFmpeg.AutoGen" Version="4.3.0.1" />
<PackageReference Include="Microsoft.Extensions.ObjectPool" Version="5.0.11" />
<PackageReference Include="Microsoft.Windows.CsWin32" Version="0.3.205">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="ppy.ManagedBass" Version="2022.1216.0" />
<PackageReference Include="ppy.ManagedBass.Fx" Version="2022.1216.0" />
<PackageReference Include="ppy.ManagedBass.Mix" Version="2022.1216.0" />
Expand Down
Loading