diff --git a/docs/launcher.md b/docs/launcher.md index 971df128..b2f9c72d 100644 --- a/docs/launcher.md +++ b/docs/launcher.md @@ -47,9 +47,13 @@ Customize Launcher (on the home screen) makes the look the player's own: - Animations (on by default): the cursor glides from row to row, a new screen slides into place, sparks drift up over the backdrop and Navi bobs in the title band with a pulsing halo (`App.animate`). They move by the time that passed, stand still while the window is hidden or minimised, and Off stops them all. -- Sounds (off by default; Low, Medium or High): a short chime when the cursor moves, a choice changes, a row is - chosen, the game starts and a build ends or fails. The chimes are made from sine tones in `launcher/ui/sound.py` - and played from memory (PlaySound on Windows; `pw-play`, `paplay` or `aplay` elsewhere). +- Sounds (off by default; Low, Medium or High): the game's own interface sounds, the cursor, decide and cancel + sounds of its menus, when the cursor moves or a choice changes, a row is chosen and the player goes back. The + launcher decodes them from the player's built game or dump into `data/cache/sounds` (`gamefiles.extract_sounds`: + `Sound/Resource/Stream/SE_Sys_Cursor00.bwav`, `SE_Sys_Do00.bwav` and `SE_Sys_Cancel00.bwav`, the files + `runtime/menu_sound.c` plays in the in-game menu), so no sound ships; until they are decoded, chimes made from sine + tones in `launcher/ui/sound.py` stand in. They are played from memory (PlaySound on Windows; `pw-play`, `paplay` + or `aplay` elsewhere). The choices are `theme`, `background`, `background_dim`, `animations` and `sounds` in `data/launcher.json`. @@ -87,7 +91,8 @@ app// launcher/, runtime/, recomp/, tools/, bin/nx2navi_xlate pydeps/ zbic, downloaded from PyPI on this PC toolchain/msys64/ucrt64 MSYS2's clang and libraries, downloaded from the MSYS2 repository on this PC toolchain/pkgs/ the downloaded package files -data/launcher.json settings; data/logs/launcher.log (and crash.log); data/cache/fonts; +data/launcher.json settings; data/logs/launcher.log (and crash.log); data/cache/fonts and data/cache/sounds + (the game's font and interface sounds, decoded from the player's game); data/background.npy (the player's background picture, fitted to the window) game/ the built game, unless another folder was chosen (its layout: README-BuildKit.md), with saves/ for saves to import and the backups imports make (below) diff --git a/docs/releases/v0.6.2.md b/docs/releases/v0.6.2.md index 334ef247..28ab3581 100644 --- a/docs/releases/v0.6.2.md +++ b/docs/releases/v0.6.2.md @@ -8,10 +8,10 @@ Draft notes for the release after 0.6.1. The version number is not decided yet. and checked quickly. An import copies your current save to the `saves` folder of the game folder first; in the game it takes effect at the next start. - Customize Launcher (on the launcher's home screen): six colour schemes, a background picture of your own with - adjustable dimming, animations (a gliding cursor, sliding screens, drifting sparks) and sound effects. The sounds - are off until you turn them on. -- HDR switches between off and the tone mapper in the settings menu without a restart. Only the game's native HDR - still waits for one. + adjustable dimming, animations (a gliding cursor, sliding screens, drifting sparks) and sound effects: the game's + own cursor, select and back sounds, read from your game. The sounds are off until you turn them on. +- Three more settings take effect without a restart: HDR between off and the tone mapper (only the game's native + HDR still waits for one), the camera's stick sensitivity, and the speakers (stereo or 5.1). ## Fixed diff --git a/launcher/gamefiles.py b/launcher/gamefiles.py index 9efdc28e..5147a01b 100644 --- a/launcher/gamefiles.py +++ b/launcher/gamefiles.py @@ -1,14 +1,20 @@ -"""The player's game files, as far as the launcher reads them itself: finding a .nup dump, and the game's Cetra -Display font for the window (runtime/menu_assets.c reads the same font for the in-game menu's titles). +"""The player's game files, as far as the launcher reads them itself: finding a .nup dump, the game's Cetra +Display font for the window (runtime/menu_assets.c reads the same font for the in-game menu's titles) and the game's +interface sounds for the window's sound effects (runtime/menu_sound.c plays the same files in the in-game menu). -The font comes from the built game's romfs/ (romfs.bin and index.json, tools/make_game.py) or, before the first -build, straight from the .nup. They are decoded once into data/cache/fonts, which stays on the player's PC like the -rest of what is built from their game. Nothing of the game ships with the launcher. +They come from the built game's romfs/ (romfs.bin and index.json, tools/make_game.py) or, before the first +build, straight from the .nup. They are decoded once into data/cache/fonts and data/cache/sounds, which stay on the +player's PC like the rest of what is built from their game. Nothing of the game ships with the launcher. - .zs files are zstd frames, most with one of the dictionaries of Pack/ZsDic.pack.zs (a SARC, compressed without a dictionary; a frame names its dictionary by id). - A .bfotf font is an OpenType file behind an 8-byte header, every big-endian 32-bit word XORed with one key: the first word is 0x7f9a0218 under it, the second the payload size. +- A .bwav sound: "BWAV", a byte order mark, a version, a CRC32, a prefetch flag and the channel count (u16 at 0x0e), + then 0x4c bytes per channel from 0x10: the codec (u16: 0 PCM16, 1 DSP-ADPCM), the pan (u16: 0 left, 1 right, + 2 middle), the sample rate and the sample count (u32 each), 16 DSP coefficients (s16 from +0x10) and the offset of + the samples in the file (u32 at +0x30). DSP-ADPCM: 8-byte frames of a header byte (coefficient pair, scale + exponent) and 14 four-bit samples. """ import json import os @@ -20,6 +26,11 @@ TITLE_ID = 0x0400486024DEC000 FONTS = { # role -> RomFS path; the window sets all its text in Cetra Display 'title': 'Font2/Scalable/CetraDisplayPro-Medium.Nin_Ounce_NVN2.bfotf.zs', } +SOUNDS = { # the window's sound effect (launcher/ui/sound.py) -> the game's interface sound + 'move': 'Sound/Resource/Stream/SE_Sys_Cursor00.bwav', + 'select': 'Sound/Resource/Stream/SE_Sys_Do00.bwav', + 'back': 'Sound/Resource/Stream/SE_Sys_Cancel00.bwav', +} DICTS = 'Pack/ZsDic.pack.zs' DICT_NAMES = ('zs.zsdic', 'pack.zsdic', 'bcett.byml.zsdic', 'bkprb.zsdic') MAX_RAW = 64 << 20 @@ -320,3 +331,90 @@ def extract_fonts(layout, game_dir='', dump=''): (d / f'{role}.family').write_text(fam, encoding='utf-8') out[role] = (d / f'{role}.otf', fam) return out + + +# ---------------------------------------------------------------- interface sounds + +def bwav_to_pcm(b): + """A .bwav as (sample rate, frames): frames a list of (left, right) 16-bit samples, or None when it is no BWAV of + one or two channels this reads. A middle-panned channel goes to both sides at 0.707 when it is the only one.""" + if not b or len(b) < 0x10 or b[:4] != b'BWAV': + return None + channels = struct.unpack_from(' len(b): + return None + rate, frames = struct.unpack_from(' len(b): + return None + samples = struct.unpack_from(f'<{count}h', b, off) + elif codec == 1: + if off + (count + 13) // 14 * 8 > len(b): + return None + samples, h1, h2 = [], 0, 0 + for i in range(count): + fr = off + i // 14 * 8 + k = i % 14 + c1, c2 = coef[2 * (b[fr] >> 4 & 7)], coef[2 * (b[fr] >> 4 & 7) + 1] + nib = b[fr + 1 + k // 2] >> (0 if k & 1 else 4) & 15 + if nib >= 8: + nib -= 16 + v = (((nib << (b[fr] & 15)) << 11) + c1 * h1 + c2 * h2 + 1024) >> 11 + v = -32768 if v < -32768 else 32767 if v > 32767 else v + h2, h1 = h1, v + samples.append(v) + else: + return None + gain = 0.7071 if channels == 1 else 1.0 + for i, v in enumerate(samples): + if pan != 1: + left[i] += v * gain + if pan != 0: + right[i] += v * gain + clip = lambda v: -32768 if v < -32768 else 32767 if v > 32767 else int(round(v)) # noqa: E731 + return rate, [(clip(a), clip(c)) for a, c in zip(left, right)] + + +def pcm_to_wav(rate, frames): + """A 16-bit stereo WAV of frames, a list of (left, right) samples.""" + pcm = struct.pack(f'<{len(frames) * 2}h', *[v for fr in frames for v in fr]) + return (b'RIFF' + struct.pack(' 0 else out -def wav_bytes(samples, level): - """A 16-bit mono WAV of samples (-1..1) at level (0..1 of full scale).""" +def wav_bytes(samples, level, rate=RATE, channels=1): + """A 16-bit WAV of samples (-1..1; frames by channels when there are two) at level (0..1 of full scale).""" pcm = (np.clip(samples * level, -1, 1) * 32767).astype(' 0 else a + + class Sounds: """Plays the effects at the level of the Sounds choice. Every failure is silent: a PC without a sound device, or without a player program, just has a quiet launcher.""" @@ -77,6 +100,8 @@ class Sounds: self.cache = cache_dir self.log = log or (lambda line: None) self.level = None # set_level below: 'off' unless the player chose a loudness + self.game = {} # the game's sounds decoded from the player's files: name -> (rate, channels, samples) + self.last_choice = 0.0 # when a sound other than the cursor's last started self.wavs = {} # name -> WAV bytes at the current level (Windows plays from this memory: it must stay) self.files = {} # name -> the WAV file elsewhere self.player = None # elsewhere: the program that plays a WAV file @@ -92,6 +117,22 @@ class Sounds: else: self.player = next((p for p in ('pw-play', 'paplay', 'aplay') if shutil.which(p)), None) self.set_level(level) + self.load_game() + + def load_game(self): + """Takes up the game's sounds decoded into /sounds (again after gamefiles.extract_sounds wrote them).""" + game = {} + for name in {g for g, _gain in GAME_SOUND.values()}: + try: + got = read_wav((self.cache / 'sounds' / f'{name}.wav').read_bytes()) + except OSError: + got = None + if got: + game[name] = got + if set(game) != set(self.game): + self.stop() + self.game, self.wavs, self.files = game, {}, {} + return bool(game) def set_level(self, level): level = level if level in LEVELS else 'off' @@ -103,7 +144,12 @@ class Sounds: def wav(self, name): if name not in self.wavs: - self.wavs[name] = wav_bytes(synth(name), LEVELS[self.level]) + source, gain = GAME_SOUND[name] + if source in self.game: + rate, channels, samples = self.game[source] + self.wavs[name] = wav_bytes(samples, LEVELS[self.level] * gain, rate, channels) + else: + self.wavs[name] = wav_bytes(synth(name), LEVELS[self.level]) return self.wavs[name] def stop(self): @@ -117,6 +163,12 @@ class Sounds: def play(self, name): if LEVELS.get(self.level, 0) <= 0 or self.failed or name not in EFFECTS: return + now = time.monotonic() + if GAME_SOUND[name][0] == 'move': + if now - self.last_choice < QUIET_AFTER: + return + else: + self.last_choice = now try: if self.winmm: # SND_ASYNC | SND_NODEFAULT | SND_MEMORY: PlaySound reads the bytes while it plays, and self.wavs diff --git a/tests/launcher_test.py b/tests/launcher_test.py index 83e36a37..4735feb5 100644 --- a/tests/launcher_test.py +++ b/tests/launcher_test.py @@ -18,7 +18,8 @@ Run: python tests/launcher_test.py (Python 3.14 for the zstd tests; the window a source checkout's build/xlate/), the prompts step with a stand-in prompt tool, the object cache of the compile step with the system's C compiler, and its windres call under folders with spaces (a script in windres's place) - jobs: an install driven by a fake make_game.py, with and without the prompts row -- the look: the colour schemes, the background picture's fitting and kept copy, the sound effects (off by default) +- the look: the colour schemes, the background picture's fitting and kept copy, the sound effects (off by default, + the game's own interface sounds decoded from a made-up .bwav) - the window: --selftest draws every screen (skipped without a display) """ import contextlib @@ -2549,6 +2550,71 @@ class LookTest(Tmp): s.play('move') popen.assert_called_once() + @staticmethod + def bwav(channels, rate=48000): + """A .bwav of channels given as (codec, pan, samples, data): the header, 0x4c bytes a channel, the data.""" + out = bytearray(b'BWAV' + struct.pack('