mirror of
https://git.triforceproject.com/triforce/source
synced 2026-11-20 08:39:58 -04:00
Triforce v0.3.3
This commit is contained in:
@@ -601,6 +601,27 @@ static void au_device_name(unsigned device, char *out, size_t size) {
|
||||
#endif
|
||||
}
|
||||
static uint64_t au_ms(void) { return (uint64_t)GetTickCount64(); }
|
||||
/* (hookshot) Which output Windows has as its default now (the waveOut mapper's preferred device, its number and name):
|
||||
a key that changes when the default does; 0 when unknown. A device opened as the default stays on that device when
|
||||
Windows moves its default (a Bluetooth headset gone and back), so the output section asks every second */
|
||||
#ifndef DRVM_MAPPER_PREFERRED_GET
|
||||
#define DRVM_MAPPER_PREFERRED_GET (0x2000 + 21) /* (mmddk.h: DRVM_MAPPER + 21) */
|
||||
#endif
|
||||
static uint32_t au_default_key(void) {
|
||||
#ifdef _WIN32
|
||||
DWORD id = (DWORD)-1, flags = 0;
|
||||
if (waveOutMessage((HWAVEOUT)(UINT_PTR)WAVE_MAPPER, DRVM_MAPPER_PREFERRED_GET, (DWORD_PTR)&id, (DWORD_PTR)&flags) !=
|
||||
MMSYSERR_NOERROR || id == (DWORD)-1)
|
||||
return 0;
|
||||
char name[64];
|
||||
au_device_name(id, name, sizeof name);
|
||||
uint32_t h = 2166136261u ^ id;
|
||||
for (const char *p = name; *p; p++) h = (h ^ (uint8_t)*p) * 16777619u;
|
||||
return h ? h : 1;
|
||||
#else
|
||||
return 0; /* (linux: SDL follows the default device itself) */
|
||||
#endif
|
||||
}
|
||||
/* HOS_AUDIO_MUTE=1 (test runs): the device opened and fed as for a player, at volume 0 */
|
||||
static void au_opened(HWAVEOUT w) {
|
||||
#ifdef _WIN32
|
||||
@@ -735,6 +756,15 @@ static void output_error(const char *operation, MMRESULT result) {
|
||||
static int out_next; /* the next candidate: 0 = HOS_AUDIO_DEVICE, 1 = the default, 2.. = device n-2 */
|
||||
static uint64_t out_opened, out_stall_since;
|
||||
static unsigned out_played, out_peak, out_reported;
|
||||
/* (hookshot) A device gone (a Bluetooth headset off, the audio service restarted: "no driver", "invalid handle" from the
|
||||
next call) is closed and the default opened again; none opening, the default is tried every second until one does.
|
||||
Opened as the default, the output follows Windows': its key (au_default_key) looked at every second, a change reopens
|
||||
there. Before, a call that failed this way was only logged, the same block tried again for good, and the game had no
|
||||
sound for the rest of the run, on any device (RX 5500M laptop, a WH-CH720N headset) */
|
||||
static int out_retry, out_reopens;
|
||||
static uint64_t out_retry_at, out_default_seen;
|
||||
static uint32_t out_default_key; /* (0: not opened as the default, or Windows did not say which it is) */
|
||||
static int au_device_gone(MMRESULT r) { return r == 2 || r == 5 || r == 6; } /* BADDEVICEID, INVALHANDLE, NODRIVER */
|
||||
static int output_try(int candidate, const WAVEFORMATEX *f) {
|
||||
const char *want = getenv("HOS_AUDIO_DEVICE");
|
||||
unsigned device;
|
||||
@@ -749,6 +779,8 @@ static int output_try(int candidate, const WAVEFORMATEX *f) {
|
||||
return 0;
|
||||
}
|
||||
au_opened(wout);
|
||||
out_default_key = device == WAVE_MAPPER ? au_default_key() : 0;
|
||||
out_default_seen = au_ms();
|
||||
char name[64];
|
||||
if (device == WAVE_MAPPER) snprintf(name, sizeof name, "the default device");
|
||||
else au_device_name(device, name, sizeof name);
|
||||
@@ -790,6 +822,7 @@ static void output_open(void) {
|
||||
channels = 2;
|
||||
}
|
||||
out_channels = channels;
|
||||
out_retry = !wout, out_retry_at = au_ms(); /* (none opened: tried again in a second, output_frame) */
|
||||
if (!wout) return;
|
||||
for (int i = 0; i < NBLOCKS; i++) {
|
||||
memset(&hdr[i], 0, sizeof hdr[i]);
|
||||
@@ -802,18 +835,30 @@ static void output_open(void) {
|
||||
out_stall_since = 0;
|
||||
out_played = out_peak = out_reported = 0;
|
||||
}
|
||||
static void output_reopen(void) {
|
||||
static int reopens;
|
||||
out_stall_since = 0;
|
||||
if (++reopens > 6) return;
|
||||
hos_log("audio: the output device took no sound for 1.5 s: trying the next one");
|
||||
static void output_close(void) { /* (what fails here is not looked at: a device gone takes none of these calls) */
|
||||
waveOutReset(wout);
|
||||
for (int i = 0; i < NBLOCKS; i++)
|
||||
if (hdr[i].dwFlags & WHDR_PREPARED) waveOutUnprepareHeader(wout, &hdr[i], sizeof hdr[i]);
|
||||
waveOutClose(wout);
|
||||
wout = NULL;
|
||||
}
|
||||
static void output_reopen(void) { /* (a device that takes nothing: the next candidate, 6 times; again after 30 s of sound) */
|
||||
out_stall_since = 0;
|
||||
if (++out_reopens > 6) return;
|
||||
hos_log("audio: the output device took no sound for 1.5 s: trying the next one");
|
||||
output_close();
|
||||
output_open();
|
||||
}
|
||||
static void output_open_default(void) { /* (HOS_AUDIO_DEVICE first when it is set, as at the start) */
|
||||
const char *want = getenv("HOS_AUDIO_DEVICE");
|
||||
out_next = want && *want ? 0 : 1;
|
||||
output_open();
|
||||
}
|
||||
static void output_move(const char *why) {
|
||||
hos_log("audio: %s: the output opened again on the default device", why);
|
||||
output_close();
|
||||
output_open_default();
|
||||
}
|
||||
static void dump_finish(void) {
|
||||
if (!dump) return;
|
||||
uint8_t h[44];
|
||||
@@ -846,7 +891,19 @@ static void output_frame(const int16_t *pcm) {
|
||||
fseek(dump, at, SEEK_SET);
|
||||
}
|
||||
}
|
||||
if (!wout) return;
|
||||
if (!wout) {
|
||||
if (!out_retry || au_ms() - out_retry_at < 1000) return;
|
||||
output_open_default(); /* (the service back, a device plugged in) */
|
||||
if (!wout) return;
|
||||
}
|
||||
if (out_default_key && au_ms() - out_default_seen >= 1000) {
|
||||
out_default_seen = au_ms();
|
||||
const uint32_t now = au_default_key();
|
||||
if (now && now != out_default_key) {
|
||||
output_move("Windows' default output changed");
|
||||
if (!wout) return;
|
||||
}
|
||||
}
|
||||
for (int i = 0; i < FRAME * out_channels; i++) {
|
||||
const unsigned a = (unsigned)(pcm[i] < 0 ? -pcm[i] : pcm[i]);
|
||||
if (a > out_peak) out_peak = a;
|
||||
@@ -869,6 +926,7 @@ static void output_frame(const int16_t *pcm) {
|
||||
const MMRESULT result = waveOutUnprepareHeader(wout, h, sizeof *h);
|
||||
if (result != MMSYSERR_NOERROR) {
|
||||
output_error("waveOutUnprepareHeader", result);
|
||||
if (au_device_gone(result)) output_move("the output device went away");
|
||||
return; /* the driver has not released the header: do not change its samples or flags */
|
||||
}
|
||||
}
|
||||
@@ -881,10 +939,17 @@ static void output_frame(const int16_t *pcm) {
|
||||
if (result == MMSYSERR_NOERROR) {
|
||||
operation = "waveOutWrite";
|
||||
result = waveOutWrite(wout, h, sizeof *h);
|
||||
if (result == MMSYSERR_NOERROR) out_played++;
|
||||
if (result == MMSYSERR_NOERROR) {
|
||||
out_played++;
|
||||
if (out_reopens && au_ms() - out_opened > 30000) out_reopens = 0; /* (sound again: the stall rule's 6 anew) */
|
||||
}
|
||||
}
|
||||
if (result != MMSYSERR_NOERROR) {
|
||||
output_error(operation, result);
|
||||
if (au_device_gone(result)) {
|
||||
output_move("the output device went away");
|
||||
return;
|
||||
}
|
||||
/* A failed submission has no completion to set DONE. Reuse only a host-owned block; a prepared header
|
||||
is unprepared on its next turn. Without this, returning to this slot drops every later audio frame. */
|
||||
if (!(h->dwFlags & WHDR_INQUEUE)) h->dwFlags |= WHDR_DONE;
|
||||
|
||||
Reference in New Issue
Block a user