Skip to content

Run Windows games on Apple Silicon — free, open source, with an open compatibility database that tells you what actually works.

Games follow the Mac's output when it changes (highball#460)

Nikolayc690f23410df
diff --git a/Sources/HighballKit/Bottle.swift b/Sources/HighballKit/Bottle.swift
index 287c069..e85f606 100644
--- a/Sources/HighballKit/Bottle.swift
+++ b/Sources/HighballKit/Bottle.swift
@@ -915,9 +915,10 @@ public struct Bottle: Sendable {
// that goes over full scale, as the last stage of Windows' audio engine does, where the Mac
// would clip it (Paperback, highball-db#349). Builds after 20261008 cap microphones the same
// way, where Wine dropped 32 frames a period (highball#350), and run Wine's per-stream audio
- // clock in real time, which a busy Mac otherwise woke late (highball#444). HB_AUDIOBUF=0 in
- // an environment's variables leaves the library out, HB_AUDIOLIMIT=0 keeps it without the
- // limiter, HB_AUDIORT=0 without the real-time clock.
+ // clock in real time, which a busy Mac otherwise woke late (highball#444), and move a game
+ // to the Mac's output when it changes (highball#460). HB_AUDIOBUF=0 in an environment's
+ // variables leaves the library out, HB_AUDIOLIMIT=0 keeps it without the limiter,
+ // HB_AUDIORT=0 without the real-time clock, HB_AUDIOFOLLOW=0 without the move.
if let lib = engine.audioBufferLibrary, env["HB_AUDIOBUF"] != "0" {
let inserted = (env["DYLD_INSERT_LIBRARIES"] ?? "").split(separator: ":").map(String.init)
env["DYLD_INSERT_LIBRARIES"] = ([lib.path] + inserted.filter { $0 != lib.path }).joined(separator: ":")
diff --git a/spike/audiobuf/audiobuf.c b/spike/audiobuf/audiobuf.c
index 6e464d0..f3fba49 100644
--- a/spike/audiobuf/audiobuf.c
+++ b/spike/audiobuf/audiobuf.c
@@ -1,4 +1,4 @@
-/* audiobuf: loaded into Wine's processes with DYLD_INSERT_LIBRARIES, it does three things.
+/* audiobuf: loaded into Wine's processes with DYLD_INSERT_LIBRARIES, it does four things.
*
* It caps the I/O buffer of each audio unit Wine starts at 5 ms (half of Wine's 10 ms shared-mode period). The device
* otherwise moves its whole buffer at once, 512 frames, 10.7 ms at 48 kHz, more than a period. For output, a game that
@@ -24,13 +24,22 @@
* priority 0 until it runs again, so on a busy Mac the clock woke games late, up to 29 ms, and their sound ran dry
* (highball#444). The Wine 11 engines run it at a fixed high priority, which a busy Mac still delayed.
*
- * HB_AUDIOLIMIT=0 turns the limiter off, HB_AUDIORT=0 the real-time clock. HB_AUDIOBUF_DEBUG=1 logs each unit, each
- * clock made real time and, every 10 s of sound, the frames limited.
+ * It keeps a game on the Mac's output when the Mac changes it. Wine binds each stream's unit to the device that was the
+ * default when the stream opened and never listens for a change, so a game went on playing to the speakers after
+ * AirPods connected (highball#460). The library listens for the change and moves each unit that was on Wine's default
+ * to the new one, also a stream opened later, because Wine keeps the default its process started with. The device is
+ * set on the running unit and the stream goes on after a gap of 0.3 to 0.5 s (measured on Wine 10, AirPods and a
+ * MacBook's speakers). A unit on any other device stays there: the game picked it. Microphones stay where they are.
+ *
+ * HB_AUDIOLIMIT=0 turns the limiter off, HB_AUDIORT=0 the real-time clock, HB_AUDIOFOLLOW=0 the move to the Mac's new
+ * output. HB_AUDIOBUF_DEBUG=1 logs each unit, each clock made real time, each move and, every 10 s of sound, the
+ * frames limited.
*
* Shipped as an engine component, whose Wine has no source to patch for the Sikarugir builds.
* Build: Scripts/build-audiobuf.sh */
#include <AudioToolbox/AudioToolbox.h>
#include <CoreAudio/CoreAudio.h>
+#include <dispatch/dispatch.h>
#include <mach/mach.h>
#include <mach/mach_time.h>
#include <mach/thread_policy.h>
@@ -154,10 +163,9 @@ static int is_capture(AudioUnit unit)
return in_on && !out_on;
}
-static OSStatus hb_AudioUnitInitialize(AudioUnit unit)
+static void cap_io_buffer(AudioUnit unit, int capture)
{
AudioStreamBasicDescription hw;
- int capture = is_capture(unit);
UInt32 hw_size = sizeof(hw), cur = 0, cur_size = sizeof(cur);
if (AudioUnitGetProperty(unit, kAudioUnitProperty_StreamFormat, capture ? kAudioUnitScope_Input : kAudioUnitScope_Output,
capture ? 1 : 0, &hw, &hw_size) == noErr
@@ -171,11 +179,83 @@ static OSStatus hb_AudioUnitInitialize(AudioUnit unit)
(unsigned)cur, (unsigned)frames, hw.mSampleRate, (int)st);
}
}
+}
+
+static AudioUnit followers[MAX_UNITS];
+static pthread_mutex_t followers_lock = PTHREAD_MUTEX_INITIALIZER;
+static AudioDeviceID wine_default; /* the default when this process first opened a stream, which Wine keeps using */
+
+static AudioDeviceID default_output(void)
+{
+ AudioObjectPropertyAddress a = { kAudioHardwarePropertyDefaultOutputDevice, kAudioObjectPropertyScopeGlobal, kAudioObjectPropertyElementMain };
+ AudioDeviceID dev = kAudioObjectUnknown;
+ UInt32 size = sizeof(dev);
+ return AudioObjectGetPropertyData(kAudioObjectSystemObject, &a, 0, NULL, &size, &dev) == noErr ? dev : kAudioObjectUnknown;
+}
+
+static AudioDeviceID unit_device(AudioUnit unit)
+{
+ AudioDeviceID dev = kAudioObjectUnknown;
+ UInt32 size = sizeof(dev);
+ AudioUnitGetProperty(unit, kAudioOutputUnitProperty_CurrentDevice, kAudioUnitScope_Global, 0, &dev, &size);
+ return dev;
+}
+
+static void move_unit(AudioUnit unit, AudioDeviceID to)
+{
+ AudioDeviceID from = unit_device(unit);
+ if (to == kAudioObjectUnknown || from == to) return;
+ OSStatus st = AudioUnitSetProperty(unit, kAudioOutputUnitProperty_CurrentDevice, kAudioUnitScope_Global, 0, &to, sizeof(to));
+ if (debug()) fprintf(stderr, "audiofollow: unit %p device %u -> %u, status %d\n", (void *)unit, (unsigned)from, (unsigned)to, (int)st);
+ if (st == noErr) cap_io_buffer(unit, 0); /* the size is per device, and the new device starts at its own */
+}
+
+/* Runs on a queue of our own, so a move does not hold up Core Audio's notification thread. */
+static void default_changed(void)
+{
+ AudioDeviceID to = default_output();
+ pthread_mutex_lock(&followers_lock);
+ for (int i = 0; i < MAX_UNITS; i++) if (followers[i]) move_unit(followers[i], to);
+ pthread_mutex_unlock(&followers_lock);
+}
+
+static void follow_default(AudioUnit unit)
+{
+ const char *off = getenv("HB_AUDIOFOLLOW");
+ if (off && off[0] == '0') return;
+ static dispatch_once_t once;
+ dispatch_once(&once, ^{
+ AudioObjectPropertyAddress a = { kAudioHardwarePropertyDefaultOutputDevice, kAudioObjectPropertyScopeGlobal, kAudioObjectPropertyElementMain };
+ wine_default = default_output();
+ AudioObjectAddPropertyListenerBlock(kAudioObjectSystemObject, &a, dispatch_queue_create("highball.audiofollow", NULL),
+ ^(UInt32 n, const AudioObjectPropertyAddress *addrs) { default_changed(); });
+ });
+ if (unit_device(unit) != wine_default) return;
+ pthread_mutex_lock(&followers_lock);
+ for (int i = 0; i < MAX_UNITS; i++) if (!followers[i]) { followers[i] = unit; break; }
+ move_unit(unit, default_output()); /* a stream opened after a change starts on the new output */
+ pthread_mutex_unlock(&followers_lock);
+}
+
+static OSStatus hb_AudioUnitInitialize(AudioUnit unit)
+{
+ int capture = is_capture(unit);
+ if (!capture) follow_default(unit);
+ cap_io_buffer(unit, capture);
OSStatus r = AudioUnitInitialize(unit);
if (r == noErr) add_limiter(unit);
return r;
}
+/* Wine disposes of a unit without uninitializing it. */
+static OSStatus hb_AudioComponentInstanceDispose(AudioComponentInstance unit)
+{
+ pthread_mutex_lock(&followers_lock);
+ for (int i = 0; i < MAX_UNITS; i++) if (followers[i] == unit) followers[i] = NULL;
+ pthread_mutex_unlock(&followers_lock);
+ return AudioComponentInstanceDispose(unit);
+}
+
/* Wine starts one clock thread per audio stream, at THREAD_PRIORITY_TIME_CRITICAL, to wake the stream's client every
* period, and the thread names itself audio_client_timer first thing. The policy asks for a slice every 10 ms (Wine's
* shared-mode period) within that period. macOS 27 raises a smaller computation to half the constraint anyway (asked
@@ -223,5 +303,6 @@ __attribute__((used)) static const struct { const void *replacement; const void
__attribute__((section("__DATA,__interpose"))) = {
{ (const void *)hb_AudioUnitInitialize, (const void *)AudioUnitInitialize },
{ (const void *)hb_AudioUnitUninitialize, (const void *)AudioUnitUninitialize },
+ { (const void *)hb_AudioComponentInstanceDispose, (const void *)AudioComponentInstanceDispose },
{ (const void *)hb_pthread_setname_np, (const void *)pthread_setname_np },
};
diff --git a/spike/audiobuf/follow-check.c b/spike/audiobuf/follow-check.c
new file mode 100644
index 0000000..7e9277b
--- /dev/null
+++ b/spike/audiobuf/follow-check.c
@@ -0,0 +1,103 @@
+/* Check for audiobuf's move to the Mac's new output (highball#460). Flips the default output to another device and
+ * back, restores it, and plays silence, so nothing is heard. Needs two output devices.
+ * clang -o /tmp/follow-check spike/audiobuf/follow-check.c -framework AudioToolbox -framework CoreAudio
+ * DYLD_INSERT_LIBRARIES=dist/audiobuf/libhbaudiobuf.dylib /tmp/follow-check */
+#include <AudioToolbox/AudioToolbox.h>
+#include <assert.h>
+#include <stdio.h>
+#include <string.h>
+#include <unistd.h>
+
+static AudioObjectPropertyAddress addr(UInt32 sel, UInt32 scope) { AudioObjectPropertyAddress a = { sel, scope, kAudioObjectPropertyElementMain }; return a; }
+
+static AudioDeviceID get_default(void)
+{
+ AudioObjectPropertyAddress a = addr(kAudioHardwarePropertyDefaultOutputDevice, kAudioObjectPropertyScopeGlobal);
+ AudioDeviceID d = 0; UInt32 z = sizeof(d);
+ assert(!AudioObjectGetPropertyData(kAudioObjectSystemObject, &a, 0, NULL, &z, &d));
+ return d;
+}
+
+static void set_default(AudioDeviceID d)
+{
+ AudioObjectPropertyAddress a = addr(kAudioHardwarePropertyDefaultOutputDevice, kAudioObjectPropertyScopeGlobal);
+ assert(!AudioObjectSetPropertyData(kAudioObjectSystemObject, &a, 0, NULL, sizeof(d), &d));
+ usleep(1500000);
+}
+
+static AudioDeviceID other_output(AudioDeviceID not_this)
+{
+ AudioObjectPropertyAddress a = addr(kAudioHardwarePropertyDevices, kAudioObjectPropertyScopeGlobal);
+ AudioDeviceID ids[64]; UInt32 z = sizeof(ids);
+ assert(!AudioObjectGetPropertyData(kAudioObjectSystemObject, &a, 0, NULL, &z, ids));
+ for (unsigned i = 0; i < z / sizeof(ids[0]); i++) {
+ AudioObjectPropertyAddress s = addr(kAudioDevicePropertyStreams, kAudioObjectPropertyScopeOutput);
+ UInt32 q = 0;
+ AudioObjectGetPropertyDataSize(ids[i], &s, 0, NULL, &q);
+ if (q && ids[i] != not_this) return ids[i];
+ }
+ return 0;
+}
+
+static volatile unsigned long calls;
+static OSStatus silence(void *u, AudioUnitRenderActionFlags *f, const AudioTimeStamp *t, UInt32 b, UInt32 n, AudioBufferList *d)
+{
+ calls++;
+ for (UInt32 i = 0; i < d->mNumberBuffers; i++) memset(d->mBuffers[i].mData, 0, d->mBuffers[i].mDataByteSize);
+ return noErr;
+}
+
+/* A unit set up the way Wine's get_audiounit and ca_setup_audiounit do it. */
+static AudioUnit open_unit(AudioDeviceID dev)
+{
+ AudioComponentDescription d = { kAudioUnitType_Output, kAudioUnitSubType_HALOutput, 0, kAudioUnitManufacturer_Apple, 0 };
+ AudioUnit u;
+ assert(!AudioComponentInstanceNew(AudioComponentFindNext(NULL, &d), &u));
+ assert(!AudioUnitSetProperty(u, kAudioOutputUnitProperty_CurrentDevice, kAudioUnitScope_Global, 0, &dev, sizeof(dev)));
+ AudioStreamBasicDescription f = { 48000, kAudioFormatLinearPCM, kAudioFormatFlagIsFloat | kAudioFormatFlagIsPacked, 8, 1, 8, 2, 32, 0 };
+ assert(!AudioUnitSetProperty(u, kAudioUnitProperty_StreamFormat, kAudioUnitScope_Input, 0, &f, sizeof(f)));
+ AURenderCallbackStruct r = { silence, NULL };
+ assert(!AudioUnitSetProperty(u, kAudioUnitProperty_SetRenderCallback, kAudioUnitScope_Input, 0, &r, sizeof(r)));
+ assert(!AudioUnitInitialize(u));
+ assert(!AudioOutputUnitStart(u));
+ return u;
+}
+
+static AudioDeviceID device_of(AudioUnit u)
+{
+ AudioDeviceID d = 0; UInt32 z = sizeof(d);
+ assert(!AudioUnitGetProperty(u, kAudioOutputUnitProperty_CurrentDevice, kAudioUnitScope_Global, 0, &d, &z));
+ return d;
+}
+
+int main(void)
+{
+ AudioDeviceID home = get_default(), away = other_output(home);
+ assert(away);
+ AudioUnit follows = open_unit(home), pinned = open_unit(away); /* the game picked "away" while it was not the default */
+ usleep(500000);
+
+ set_default(away);
+ int moved = device_of(follows) == away;
+ unsigned long before = calls;
+ usleep(500000);
+ int plays = calls > before;
+ AudioUnit late = open_unit(home); /* Wine's stale default, opened after the change */
+ usleep(500000);
+ int late_moved = device_of(late) == away;
+
+ set_default(home);
+ int back = device_of(follows) == home && device_of(late) == home;
+ int stayed = device_of(pinned) == away;
+ AudioOutputUnitStop(late); AudioComponentInstanceDispose(late);
+ set_default(away); /* a disposed unit must not be touched */
+ set_default(home);
+ AudioOutputUnitStop(follows); AudioComponentInstanceDispose(follows);
+ AudioOutputUnitStop(pinned); AudioComponentInstanceDispose(pinned);
+
+ printf("moved %d, plays after the move %d, stream opened later moved %d, back %d, pinned stayed %d, default restored %d\n",
+ moved, plays, late_moved, back, stayed, get_default() == home);
+ assert(moved && plays && late_moved && back && stayed && get_default() == home);
+ puts("ok");
+ return 0;
+}