Files
Triforce-Source/host/menu_sound.c
ruckus7382sfgsgdf b527e29adb Triforce v0.8.5
A Randomizer page in the launcher, a new title screen with Mods and Save Select tabs, scaling changes applied in game, the Jabu-Jabu bars fixed, renderer frame-time work and the brightness-flash fix, and fixes from the 0.8.4 reports.
2026-11-28 10:54:41 -06:00

419 lines
24 KiB
C

/* (Triforce) The options' sounds: the game's own interface sounds (Sound/Resource/Stream/SE_Sys_*.bwav of the player's
game files: the cursor, decide, cancel, the Plus menu's open and close, the tab moves), read once, decoded and mixed
over the game's output. After NX2navi's menu sounds (its runtime/menu_sound.c, the owner's): the decoder, the voices
and the mix are that file's. Nothing is shipped: game files without the sounds stay silent, and a mod's file is
played where a mod replaces one (hos_fs_rom_file, nn_fs.c).
BWAV: "BWAV", byte order mark, version, CRC32, a prefetch flag (u16) and the channel count (u16), then 0x4C bytes per
channel: codec (u16, 0 PCM16, 1 DSP-ADPCM), pan (u16, 0 left, 1 right, 2 middle), sample rate, sample count (u32),
the count again, 16 DSP coefficients (s16), the absolute offset of the samples (u32), ... The interface sounds are
48 kHz DSP-ADPCM, one channel in the middle or two, left and right: 8-byte frames of a header (coefficient pair
index, scale exponent) and 14 four-bit samples. A file of another rate (a mod's) is brought to 48 kHz when it is
read, in a straight line between its samples: the mixer's rate is the output's (nn_audio.c RATE), always.
Which sound: the options say (gpu/vk/vk_menu.cpp, gpu/vk/menu_sound_events.h), and the sound thread asks them at each
block it mixes, 5 ms (hos_menu_sound_frame: rx_option HOS_OPT_MENU_SOUND). So a sound is in the block after its
press, and what is heard later than that is the output's own delay, the game's sounds' too. The files are read on a
thread of their own, when the sound thread first finds the Menu sounds row above 0; a sound asked for before they
are read is not played.
hos_menu_sound_play starts a voice (a free one, else the oldest), hos_menu_sound_mix adds the voices to a block. A
voice is free while its place is negative; play sets its sound first and its place last (release), the mix reads
them in the other order (acquire) and moves the place on only if it is still the one it read, so the two may run on
different threads without a lock. The place is the frame with the number of the voice's start above it: NX2navi's
was the frame alone, and a voice started again while its first block was mixed had the frame it had before, 0, so
the mix moved the new sound on by a block it never played (or ended one shorter than a block unheard). Here both
run on the sound thread, one after the other.
((Triforce, 11-25) Whose sound: the settings' tabs, the Mods screen and the step back to the pause menu's System list
have a sound each for moving, choosing and going back (HOS_MENU_SOUND_SETTINGS_NAVIGATE and the six after it), the
system menu's files unless the environment names another (roles, roles_from_env).)
The level: the Audio tab's Menu sounds row (menu_sounds= in triforce_options.ini, 0 to 100, 80 unless set; 0
silent), read at each block, and the Master volume over it as over the game's own sound (the sounds are added before
render_frame applies it). They are added after the game's limiter, and the block is clipped as it is made 16-bit. */
#include <stdatomic.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "menu_sound.h"
#include "../gpu/vk/vk_api.h" /* (HOS_MENU_SOUND_*, HOS_OPT_MENU_SOUND, HOS_OPT_MENU_SOUND_VOLUME) */
#ifndef MENU_SOUND_TEST /* (tests/menu_sound_test.c has its own of these, and reads the files when it says) */
#include "hos.h"
#define WIN32_LEAN_AND_MEAN
#include <windows.h>
int rx_option(int id, int otherwise); /* rx_backend.c */
uint8_t *hos_fs_rom_file(const char *rel, uint64_t most, uint64_t *size); /* nn_fs.c */
static void load_all(void);
static DWORD WINAPI load_main(void *arg) {
(void)arg;
load_all();
return 0;
}
static void load_start(void) { /* (the files are read off the sound thread: a disk may take its time) */
const HANDLE t = CreateThread(NULL, 0, load_main, NULL, 0, NULL);
if (t) CloseHandle(t);
else hos_log("menu sounds: no thread to read the game's interface sounds: the options stay silent");
}
#endif
#define MIX_RATE 48000 /* (nn_audio.c RATE: the output's) */
#define SOUNDS HOS_MENU_SOUND_N /* (by HOS_MENU_SOUND_*: 0 is no sound) */
#define FILE_MOST (4u << 20) /* (the longest of the game's seven is 138 KB) */
typedef struct {
float *pcm; /* interleaved stereo at 48 kHz */
uint32_t frames;
} Sound;
static const char *files[SOUNDS] = { /* (a role's is another's when the environment says so: roles_from_env) */
[HOS_MENU_SOUND_OPEN] = "Sound/Resource/Stream/SE_Sys_PlusMenu_Open00.bwav",
[HOS_MENU_SOUND_CLOSE] = "Sound/Resource/Stream/SE_Sys_PlusMenu_Close00.bwav",
[HOS_MENU_SOUND_CURSOR] = "Sound/Resource/Stream/SE_Sys_Cursor00.bwav",
[HOS_MENU_SOUND_DECIDE] = "Sound/Resource/Stream/SE_Sys_Do00.bwav",
[HOS_MENU_SOUND_CANCEL] = "Sound/Resource/Stream/SE_Sys_Cancel00.bwav",
[HOS_MENU_SOUND_TAB_LEFT] = "Sound/Resource/Stream/SE_Sys_MinusMenu_TabMove_L00.bwav",
[HOS_MENU_SOUND_TAB_RIGHT] = "Sound/Resource/Stream/SE_Sys_MinusMenu_TabMove_R00.bwav",
/* ((Triforce) the Mods screen's install done: the game's own item-get fanfare, its short da-na-na-naaa (4 s; _Intro is the
whole chest's opening before it)) */
[HOS_MENU_SOUND_FANFARE] = "Sound/Resource/Stream/BGM_ItemGet_Medium.bwav",
/* ((Triforce) 11-17) the Mods entry under the title's list and the System page: the sounds those lists make (the lab's
file opens: the title's cursor, its Continue and the Load list's choice) */
[HOS_MENU_SOUND_SYS_CURSOR] = "Sound/Resource/Stream/SE_Sys_SysMenu_Cursor00.bwav",
[HOS_MENU_SOUND_SYS_DECIDE] = "Sound/Resource/Stream/SE_Sys_SysMenu_DecideL00.bwav",
/* ((Triforce, 11-25) the owner: "The SFX are incorrect. They should be using the title screen's SFX instead of the
in-game Menu SFX." The settings' tabs, the Mods screen and the step back to the pause menu's System list, a sound
each: the system menu's cursor, its decide and its cancel (Cansel: the file's own spelling), as the owner chose them
from the game's own (the settings' Select the large decide, the title's Continue's; the Mods screen's the small
one). The in-game menu's three above, the cursor's, decide's and cancel's, are what these were until then) */
[HOS_MENU_SOUND_SETTINGS_NAVIGATE] = "Sound/Resource/Stream/SE_Sys_SysMenu_Cursor00.bwav",
[HOS_MENU_SOUND_SETTINGS_SELECT] = "Sound/Resource/Stream/SE_Sys_SysMenu_DecideL00.bwav",
[HOS_MENU_SOUND_SETTINGS_BACK] = "Sound/Resource/Stream/SE_Sys_SysMenu_Cansel00.bwav",
[HOS_MENU_SOUND_MODS_NAVIGATE] = "Sound/Resource/Stream/SE_Sys_SysMenu_Cursor00.bwav",
[HOS_MENU_SOUND_MODS_SELECT] = "Sound/Resource/Stream/SE_Sys_SysMenu_DecideS00.bwav",
[HOS_MENU_SOUND_MODS_BACK] = "Sound/Resource/Stream/SE_Sys_SysMenu_Cansel00.bwav",
[HOS_MENU_SOUND_PAUSE_BACK] = "Sound/Resource/Stream/SE_Sys_SysMenu_Cansel00.bwav",
/* ((Triforce, 11-25) a toggle of the settings' tabs switched On, and Off (the owner, in the lab: 'In the display, the
on and off SFX are incorrect ... let me pick the correct SFX for the on and off selection for the settings'): as the
owner chose them, On the system menu's small decide, Off its cancel */
[HOS_MENU_SOUND_SETTINGS_ON] = "Sound/Resource/Stream/SE_Sys_SysMenu_DecideS00.bwav",
[HOS_MENU_SOUND_SETTINGS_OFF] = "Sound/Resource/Stream/SE_Sys_SysMenu_Cansel00.bwav",
};
/* ((Triforce, 11-25) the seven roles above, each with the variable of the environment that names another sound for it, so
that one can be tried without a build: HOS_MENU_SOUND_SETTINGS_NAVIGATE=SE_Sys_Cursor00, in the environment or in
triforce_env.txt (host/hos_main.c env_line_allowed takes these names and a sound's bare name). The value is a file's
name in Sound/Resource/Stream without its .bwav: letters, digits and _, 64 at most, so it names nothing outside that
folder; any other value is left out with a line in the log. Read once, when the files are (load_all) */
static const struct { int id; const char *env; } roles[] = {
{HOS_MENU_SOUND_SETTINGS_NAVIGATE, "HOS_MENU_SOUND_SETTINGS_NAVIGATE"},
{HOS_MENU_SOUND_SETTINGS_SELECT, "HOS_MENU_SOUND_SETTINGS_SELECT"},
{HOS_MENU_SOUND_SETTINGS_BACK, "HOS_MENU_SOUND_SETTINGS_BACK"},
{HOS_MENU_SOUND_MODS_NAVIGATE, "HOS_MENU_SOUND_MODS_NAVIGATE"},
{HOS_MENU_SOUND_MODS_SELECT, "HOS_MENU_SOUND_MODS_SELECT"},
{HOS_MENU_SOUND_MODS_BACK, "HOS_MENU_SOUND_MODS_BACK"},
{HOS_MENU_SOUND_PAUSE_BACK, "HOS_MENU_SOUND_PAUSE_BACK"},
{HOS_MENU_SOUND_SETTINGS_ON, "HOS_MENU_SOUND_SETTINGS_ON"},
{HOS_MENU_SOUND_SETTINGS_OFF, "HOS_MENU_SOUND_SETTINGS_OFF"},
};
#define ROLES (int)(sizeof roles / sizeof *roles)
#define ROLE_NAME_MOST 64
static char role_files[ROLES][sizeof "Sound/Resource/Stream/.bwav" + ROLE_NAME_MOST];
static const char *role_own[ROLES]; /* (a role's own file while the environment's is in files[]; NULL: files[] has its own) */
/* 1 when name is a sound's bare name: letters, digits and _, 1 to ROLE_NAME_MOST of them */
static int role_name_ok(const char *name) {
size_t n = 0;
for (; name[n]; n++) {
const char c = name[n];
if (n >= ROLE_NAME_MOST) return 0;
if (!((c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z') || (c >= '0' && c <= '9') || c == '_')) return 0;
}
return n > 0;
}
#ifndef MENU_SOUND_ENV /* (tests/menu_sound_test.c: the environment the test says) */
#define MENU_SOUND_ENV(name) getenv(name)
#endif
static void roles_from_env(void) {
for (int k = 0; k < ROLES; k++) {
const char *name = MENU_SOUND_ENV(roles[k].env);
if (!name || !*name) continue;
if (!role_name_ok(name)) {
hos_log("menu sounds: %s: its value is not a sound's name (letters, digits and _, 64 at most, as SE_Sys_Cursor00): "
"left out", roles[k].env);
continue;
}
snprintf(role_files[k], sizeof role_files[k], "Sound/Resource/Stream/%s.bwav", name);
if (!strcmp(role_files[k], files[roles[k].id])) continue; /* (its own: nothing to say) */
role_own[k] = files[roles[k].id], files[roles[k].id] = role_files[k];
hos_log("menu sounds: %s: %s, the environment's (its own: %s)", roles[k].env, role_files[k], role_own[k]);
}
}
static Sound sounds[SOUNDS];
static atomic_int loaded; /* 0 not yet, 1 being read (sounds[] is written before the release store of 2), 2 ready */
#define VOICES 8
static struct {
_Atomic int64_t at; /* the number of its start << 32 | the frame; -1 free */
atomic_int sound;
uint64_t started;
} voices[VOICES];
static uint64_t started_n;
#define AT_FRAME(at) ((uint32_t)(at))
#define AT_START(at) ((at) & ~(int64_t)0xffffffff)
static uint16_t u16(const uint8_t *p) { return (uint16_t)(p[0] | p[1] << 8); }
static uint32_t u32(const uint8_t *p) { return (uint32_t)p[0] | (uint32_t)p[1] << 8 | (uint32_t)p[2] << 16 | (uint32_t)p[3] << 24; }
static int16_t clamp16(int64_t v) { return (int16_t)(v < -32768 ? -32768 : v > 32767 ? 32767 : v); }
/* One channel into dst[2 * i] and dst[2 * i + 1] by its pan (0 the left alone, 1 the right alone, else both). 0 when
the samples do not lie inside the file. */
static int decode_channel(const uint8_t *b, uint64_t size, const uint8_t *info, float *dst, uint32_t frames) {
const uint16_t codec = u16(info), pan = u16(info + 2);
uint32_t count = u32(info + 8);
const uint32_t off = u32(info + 0x30);
if (count > frames) count = frames;
int16_t coef[16];
for (int i = 0; i < 16; i++) coef[i] = (int16_t)u16(info + 0x10 + 2 * i);
const float scale = 1.0f / 32768.0f;
if (codec == 0) { /* PCM16 */
if ((uint64_t)off + (uint64_t)count * 2 > size) return 0;
for (uint32_t i = 0; i < count; i++) {
const float v = (int16_t)u16(b + off + 2 * (uint64_t)i) * scale;
if (pan != 1) dst[2 * (size_t)i] += v;
if (pan != 0) dst[2 * (size_t)i + 1] += v;
}
return 1;
}
if (codec != 1) return 0;
const uint64_t need = ((uint64_t)count + 13) / 14 * 8;
if ((uint64_t)off + need > size) return 0;
int32_t h1 = 0, h2 = 0;
for (uint32_t i = 0; i < count; i++) {
const uint8_t *fr = b + off + (uint64_t)(i / 14) * 8;
const uint32_t k = i % 14;
const int idx = fr[0] >> 4 & 7, sh = fr[0] & 15;
int nib = fr[1 + k / 2] >> (k & 1 ? 0 : 4) & 15;
if (nib >= 8) nib -= 16;
/* (64 bits: coefficients a file made up could carry the sum past 32) */
const int64_t s = (int64_t)nib * (1 << sh) * 2048 + (int64_t)coef[2 * idx] * h1 + (int64_t)coef[2 * idx + 1] * h2;
const int16_t v = clamp16((s + 1024) >> 11);
h2 = h1, h1 = v;
if (pan != 1) dst[2 * (size_t)i] += v * scale;
if (pan != 0) dst[2 * (size_t)i + 1] += v * scale;
}
return 1;
}
/* pcm (*frames stereo frames at `rate`) at 48 kHz, in a straight line between its samples; pcm is freed */
static float *to_mix_rate(float *pcm, uint32_t *frames, uint32_t rate) {
const uint64_t n = ((uint64_t)*frames * MIX_RATE + rate - 1) / rate;
float *out = (float *)malloc((size_t)n * 2 * sizeof *out);
for (uint64_t i = 0; out && i < n; i++) {
const uint64_t at = i * rate; /* (in 48000ths of a frame of pcm) */
const uint32_t k = (uint32_t)(at / MIX_RATE), k1 = k + 1 < *frames ? k + 1 : k;
const float t = (float)(at % MIX_RATE) / MIX_RATE;
out[2 * i] = pcm[2 * (size_t)k] + (pcm[2 * (size_t)k1] - pcm[2 * (size_t)k]) * t;
out[2 * i + 1] = pcm[2 * (size_t)k + 1] + (pcm[2 * (size_t)k1 + 1] - pcm[2 * (size_t)k + 1]) * t;
}
free(pcm);
if (out) *frames = (uint32_t)n;
return out;
}
/* ((Triforce, 11-20) the samples of a channel in the whole DSP-ADPCM frames the file holds, `frames` at most. A tune's last
frame may be cut short at the file's end: the shop theme's second channel ends 4 bytes into its last frame of 8 (the 5
samples it still counts are there, the frame is not), which decode_channel refuses, as it must for an interface sound.
The tune is cut to the whole frames: 5 samples of a minute) */
static uint32_t frames_held(const uint8_t *info, uint64_t size, uint32_t frames) {
const uint16_t codec = u16(info);
const uint32_t off = u32(info + 0x30);
if (off >= size) return 0;
const uint64_t have = size - off;
uint64_t n = 0;
if (codec == 0) n = have / 2;
else if (codec == 1) n = have / 8 * 14;
return n < frames ? (uint32_t)n : frames;
}
/* (the decoder, for a file of at most `seconds`: the interface sounds' ten, the Mods screen's tune's more) */
static float *decode_most(const uint8_t *b, uint64_t size, uint32_t *frames_out, uint32_t seconds) {
if (size < 0x10 || memcmp(b, "BWAV", 4)) return NULL;
const uint16_t nch = u16(b + 0x0e);
if (!nch || nch > 2 || 0x10 + (uint64_t)nch * 0x4c > size) return NULL;
const uint32_t rate = u32(b + 0x10 + 4);
uint32_t frames = u32(b + 0x10 + 8);
/* (the interface sounds are 48 kHz and short: at most ten seconds of a rate a sound file has) */
if (rate < 8000 || rate > 192000 || !frames || frames > (uint64_t)rate * seconds) return NULL;
if (seconds > 10) /* (a tune: as long as the samples its channels hold, the last frame's cut-off ones left out) */
for (uint16_t c = 0; c < nch; c++) frames = frames_held(b + 0x10 + c * 0x4c, size, frames);
if (!frames) return NULL;
float *pcm = (float *)calloc((size_t)frames * 2, sizeof *pcm);
int ok = pcm != NULL;
for (uint16_t c = 0; ok && c < nch; c++) ok = decode_channel(b, size, b + 0x10 + c * 0x4c, pcm, frames);
if (!ok) {
free(pcm);
return NULL;
}
if (nch == 1 && u16(b + 0x10 + 2) > 1) /* a middle-panned mono sound in both */
for (size_t i = 0; i < (size_t)frames * 2; i++) pcm[i] *= 0.7071f;
if (rate != MIX_RATE && !(pcm = to_mix_rate(pcm, &frames, rate))) return NULL;
*frames_out = frames;
return pcm;
}
float *hos_menu_sound_decode(const uint8_t *b, uint64_t size, uint32_t *frames_out) { return decode_most(b, size, frames_out, 10); }
/* ((Triforce, 11-20) a tune: the Mods screen's music, host/mods_music.c) */
float *hos_menu_sound_decode_long(const uint8_t *b, uint64_t size, uint32_t *frames_out, uint32_t seconds) {
return decode_most(b, size, frames_out, seconds);
}
/* sounds[id] from files[id]: 1 when it plays. ((Triforce, 11-25) a file a sound before it has is not read again, and its
samples are both sounds': the roles' seven are three files, and one of them the system menu's cursor's) */
static int load_sound(int id) {
for (int k = 1; k < id; k++)
if (!strcmp(files[k], files[id])) return sounds[id] = sounds[k], sounds[id].pcm != NULL;
uint64_t size = 0;
uint8_t *b = hos_fs_rom_file(files[id], FILE_MOST, &size);
if (!b) return 0;
uint32_t frames = 0;
float *pcm = hos_menu_sound_decode(b, size, &frames);
free(b);
if (!pcm) {
hos_log("menu sounds: %s is not a BWAV file the options play (PCM16 or DSP-ADPCM, one or two channels, at most 10 s): "
"left out", files[id]);
return 0;
}
sounds[id] = (Sound){pcm, frames};
return 1;
}
static void load_all(void) { /* (once: hos_menu_sound_frame's store of 1 into loaded starts it) */
int n = 0;
roles_from_env();
for (int id = 1; id < SOUNDS; id++) {
int ok = load_sound(id);
/* ((Triforce, 11-25) a role's sound from the environment that the game's files do not have, or that is not a
sound: the role's own again, and said: not a role gone silent for a name typed wrong) */
for (int k = 0; !ok && k < ROLES; k++)
if (roles[k].id == id && role_own[k]) {
hos_log("menu sounds: %s: no sound %s in the game's files: its own, %s", roles[k].env, files[id], role_own[k]);
files[id] = role_own[k], role_own[k] = NULL;
ok = load_sound(id);
}
n += ok;
}
for (int i = 0; i < VOICES; i++) atomic_store(&voices[i].at, -1);
hos_log("menu sounds: %d of %d interface sounds from the game's files", n, SOUNDS - 1);
atomic_store_explicit(&loaded, 2, memory_order_release);
}
float hos_menu_sound_gain(int percent) { return percent <= 0 ? 0.0f : percent >= 100 ? 1.0f : (float)percent / 100.0f; }
/* ((Triforce, 11-26) The navigation sounds: the cursor's tick, the settings' tabs' and the Mods screen's own, and the system
menu's cursor, as the game's own Options screen plays its cursor. The owner sent a clip of the game's Options and ours
navigated quickly: "match the game's vanilla's navigation effects" and "Increase to the same exact volume as the vanilla
navigation sound effect". Measured in it: the game's cursor sound starts with each move and the one before rings on under
it (none is cut), at 0.70 of the file's level; ours were cut short by the next (11-25) and played at 0.43, the 0.6 the
ticks had under the other sounds. Now each plays whole, at the whole level, beside the ones before it; a held direction
moves at the game's own pace (vk_menu.cpp: 330 ms, then each 135 ms), so they ring over one another no more than the
game's do. Only an ask within NAV_GAP_NS of the sound before is left out: one press that asks twice, or a move that comes
with a tab's or a page's own sound */
#define NAV_GAP_NS 45000000ll
static _Atomic int64_t last_start_ns = -NAV_GAP_NS;
/* ((Triforce, 11-25) the ticks: the cursor's, and the settings' tabs' and the Mods screen's own now that each has a sound of
its own) */
static int is_tick(int id) {
return id == HOS_MENU_SOUND_CURSOR || id == HOS_MENU_SOUND_SETTINGS_NAVIGATE || id == HOS_MENU_SOUND_MODS_NAVIGATE;
}
/* (the navigation sounds: the ticks and the system menu's cursor) */
static int is_nav(int id) { return is_tick(id) || id == HOS_MENU_SOUND_SYS_CURSOR; }
void hos_menu_sound_play(int id, int64_t now_ns) {
if (id <= 0 || id >= SOUNDS) return;
if (is_nav(id) && now_ns - atomic_load(&last_start_ns) < NAV_GAP_NS) return;
atomic_store(&last_start_ns, now_ns);
if (atomic_load_explicit(&loaded, memory_order_acquire) != 2 || !sounds[id].pcm) return;
/* A decision replaces the tick of the same press (a row changes its value in the tick the press is seen), and a sound
that replaces itself restarts instead of stacking up. ((Triforce, 11-19) the system menu's tick too: the mouse onto
Mods or Quit and its click in one tick is their decide alone.) ((Triforce, 11-26) A navigation sound never takes
another's voice this way: the one before rings on beside it, as the game's) */
int pick = -1;
for (int i = 0; !is_nav(id) && i < VOICES; i++) {
const int64_t at = atomic_load_explicit(&voices[i].at, memory_order_acquire);
const int was = atomic_load(&voices[i].sound);
if (at >= 0 && (was == id || is_nav(was)) && AT_FRAME(at) < 2400) { /* started under 50 ms ago */
pick = i;
break;
}
}
/* else a free voice; else the oldest navigation sound's (with all eight playing, the first of a held run, near its end),
so that a longer sound (the Mods screen's fanfare, the options' opening) is not cut by a tick; else the oldest */
if (pick < 0) {
int nav = -1, oldest = 0;
for (int i = 0; i < VOICES && pick < 0; i++) {
const int64_t at = atomic_load_explicit(&voices[i].at, memory_order_acquire);
if (at < 0) { pick = i; break; }
if (is_nav(atomic_load(&voices[i].sound)) && (nav < 0 || voices[i].started < voices[nav].started)) nav = i;
if (voices[i].started < voices[oldest].started) oldest = i;
}
if (pick < 0) pick = nav >= 0 ? nav : oldest;
}
atomic_store_explicit(&voices[pick].at, -1, memory_order_release);
atomic_store_explicit(&voices[pick].sound, id, memory_order_relaxed);
voices[pick].started = __atomic_add_fetch(&started_n, 1, __ATOMIC_RELAXED);
atomic_store_explicit(&voices[pick].at, (int64_t)(voices[pick].started & 0x7fffffff) << 32, memory_order_release);
}
#ifndef MENU_SOUND_BETWEEN /* (tests/menu_sound_test.c: a play that lands between the mix's reads, 0, or its adds and its store, 1) */
#define MENU_SOUND_BETWEEN(where) ((void)0)
#endif
void hos_menu_sound_mix(float *left, float *right, uint32_t frames, float gain) {
if (atomic_load_explicit(&loaded, memory_order_acquire) != 2) return;
for (int v = 0; v < VOICES; v++) {
int64_t at = atomic_load_explicit(&voices[v].at, memory_order_acquire);
if (at < 0) continue;
MENU_SOUND_BETWEEN(0);
const int which = atomic_load_explicit(&voices[v].sound, memory_order_acquire);
const Sound *s = &sounds[which];
/* hos_menu_sound_play may restart the voice between the two loads: the frame would then belong to the old sound,
past the end of a shorter new one; such a voice is mixed from the next call on */
if (atomic_load_explicit(&voices[v].at, memory_order_acquire) != at) continue;
const uint32_t pos = AT_FRAME(at);
if (pos >= s->frames) { /* (past its sound's end, should that ever be: free again, not held for good) */
atomic_compare_exchange_strong(&voices[v].at, &at, -1);
continue;
}
/* ((Triforce, 11-26) every sound at the whole level, the cursor's ticks too: the game's own cursor sound is (the
owner's clip) as loud as the rest of its menu's) */
const uint32_t n = s->frames - pos < frames ? s->frames - pos : frames;
const float *src = s->pcm + (size_t)pos * 2;
for (uint32_t i = 0; gain != 0.0f && i < n; i++) {
left[i] += src[2 * (size_t)i] * gain;
right[i] += src[2 * (size_t)i + 1] * gain;
}
MENU_SOUND_BETWEEN(1);
/* Only advance if the voice was not restarted meanwhile. */
atomic_compare_exchange_strong(&voices[v].at, &at, pos + n >= s->frames ? -1 : AT_START(at) | (int64_t)(pos + n));
}
}
void hos_menu_sound_frame(float *left, float *right, uint32_t frames) {
const int volume = rx_option(HOS_OPT_MENU_SOUND_VOLUME, -1);
if (volume < 0) return; /* (no renderer with the options: nothing asks for a sound) */
int none = 0;
if (volume > 0 && atomic_compare_exchange_strong(&loaded, &none, 1)) load_start();
const int64_t now = hos_now_ns();
/* (what was asked for since the last block: a press or two at most. With the row at 0 the options ask for none,
and one asked for as it went to 0 is taken and not played) */
for (int k = 0, id; k < 8 && (id = rx_option(HOS_OPT_MENU_SOUND, 0)) > 0; k++)
if (volume > 0) hos_menu_sound_play(id, now);
hos_menu_sound_mix(left, right, frames, hos_menu_sound_gain(volume));
}