mirror of
https://github.com/tangerineturtle1/NX2navi.git
synced 2026-11-17 04:59:57 -04:00
Play the game's own cursor, select and back sounds in the launcher.
The launcher's sound effects were chimes made from sine tones. They are now the game's interface sounds, the ones its menus make for the cursor, a choice and going back: SE_Sys_Cursor00, SE_Sys_Do00 and SE_Sys_Cancel00, the files the in-game menu plays. The launcher decodes them from the player's built game or dump into data/cache/sounds (gamefiles.extract_sounds, a BWAV reader for PCM16 and DSP-ADPCM), beside the font it already reads that way, so no sound ships. The chimes stand in until the game has been read. The cursor's sound is a little under the others, as in the game's menu, and is left out for a quarter of a second after a choice, since one sound plays at a time and the pointer landing on a row of the next screen would cut the choice's sound short. Sounds stay off until the player picks a loudness. Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
This commit is contained in:
@@ -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/<version>/ 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)
|
||||
|
||||
@@ -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
|
||||
|
||||
|
||||
@@ -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('<H', b, 0x0e)[0]
|
||||
if channels not in (1, 2) or 0x10 + channels * 0x4c > len(b):
|
||||
return None
|
||||
rate, frames = struct.unpack_from('<II', b, 0x10 + 4)
|
||||
if not 8000 <= rate <= 96000 or not 0 < frames <= rate * 10:
|
||||
return None
|
||||
left, right = [0.0] * frames, [0.0] * frames
|
||||
for c in range(channels):
|
||||
info = 0x10 + c * 0x4c
|
||||
codec, pan = struct.unpack_from('<HH', b, info)
|
||||
count = min(struct.unpack_from('<I', b, info + 8)[0], frames)
|
||||
coef = struct.unpack_from('<16h', b, info + 0x10)
|
||||
off = struct.unpack_from('<I', b, info + 0x30)[0]
|
||||
if codec == 0:
|
||||
if off + count * 2 > 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('<I', 36 + len(pcm)) + b'WAVEfmt '
|
||||
+ struct.pack('<IHHIIHH', 16, 1, 2, rate, rate * 4, 4, 16) + b'data' + struct.pack('<I', len(pcm)) + pcm)
|
||||
|
||||
|
||||
def cached_sounds(layout):
|
||||
"""{effect: path} of the game's interface sounds decoded earlier."""
|
||||
d = layout.cache / 'sounds'
|
||||
return {name: d / f'{name}.wav' for name in SOUNDS if (d / f'{name}.wav').is_file()}
|
||||
|
||||
|
||||
def extract_sounds(layout, game_dir='', dump=''):
|
||||
"""Decode the game's cursor, select and back sounds into data/cache/sounds as WAV files; {effect: path} of the
|
||||
ones found."""
|
||||
romfs = open_romfs(layout, game_dir, dump)
|
||||
if romfs is None:
|
||||
return {}
|
||||
d = layout.cache / 'sounds'
|
||||
d.mkdir(parents=True, exist_ok=True)
|
||||
out = {}
|
||||
for name, rel in SOUNDS.items():
|
||||
try:
|
||||
got = bwav_to_pcm(romfs.read(rel))
|
||||
except Exception:
|
||||
got = None
|
||||
if not got:
|
||||
continue
|
||||
tmp = d / f'{name}.wav.tmp'
|
||||
tmp.write_bytes(pcm_to_wav(*got))
|
||||
os.replace(tmp, d / f'{name}.wav')
|
||||
out[name] = d / f'{name}.wav'
|
||||
return out
|
||||
|
||||
@@ -305,18 +305,29 @@ class App:
|
||||
return self.fcache[(family, max(7, int(px * smallest)), weight)]
|
||||
|
||||
def fonts_from_game(self, game_dir='', dump=''):
|
||||
"""Decode the game's fonts in the background and redraw with them."""
|
||||
if self.game_fonts:
|
||||
"""Decode the game's fonts and its interface sounds in the background, redraw with the fonts and take up
|
||||
the sounds."""
|
||||
fonts, sounds = not self.game_fonts, not self.sounds.game
|
||||
if not fonts and not sounds:
|
||||
return
|
||||
|
||||
def work():
|
||||
got = heard = {}
|
||||
try:
|
||||
got = gamefiles.extract_fonts(self.layout, game_dir, dump)
|
||||
if fonts:
|
||||
got = gamefiles.extract_fonts(self.layout, game_dir, dump)
|
||||
except Exception as e:
|
||||
self.ctx.log(f'fonts: {e}')
|
||||
got = {}
|
||||
try:
|
||||
if sounds:
|
||||
heard = gamefiles.extract_sounds(self.layout, game_dir, dump)
|
||||
self.ctx.log(f'sounds: {len(heard)} of {len(gamefiles.SOUNDS)} interface sounds from the game')
|
||||
except Exception as e:
|
||||
self.ctx.log(f'sounds: {e}')
|
||||
if got:
|
||||
self.root.after(0, self.reload_fonts)
|
||||
if heard:
|
||||
self.root.after(0, self.sounds.load_game)
|
||||
import threading
|
||||
threading.Thread(target=work, daemon=True).start()
|
||||
|
||||
@@ -940,7 +951,7 @@ class App:
|
||||
desc=('A gliding cursor, sliding screens and drifting sparks.',)),
|
||||
Row('enum', 'Sounds', get=lambda: dict(SOUND_LEVELS).get(self.settings.get('sounds'), 'Off'),
|
||||
left=lambda: sounds(-1), right=lambda: sounds(1),
|
||||
desc=('Short chimes when you move and choose. Off by default.',)),
|
||||
desc=('The game\'s cursor, select and back sounds. Off by default.',)),
|
||||
Row('divider'),
|
||||
Row('action', 'Back', on=lambda: self.show('home', self.home_row('Customize Launcher')), height=40,
|
||||
desc=('Back to the main menu.',)),
|
||||
|
||||
@@ -1,30 +1,38 @@
|
||||
"""The launcher's sound effects: short chimes made here from sine tones, so no sound file ships and none comes from
|
||||
the game.
|
||||
"""The launcher's sound effects: the game's own interface sounds, the ones its menus make for the cursor, a choice
|
||||
and going back, read from the player's game (launcher/gamefiles.py extract_sounds decodes them into
|
||||
data/cache/sounds, as runtime/menu_sound.c plays them in the in-game menu). No sound file ships: until the player's
|
||||
game has been read, and for a sound it does not hold, short chimes made here from sine tones stand in.
|
||||
|
||||
They are off until the player picks a loudness in Customize Launcher (the Sounds row). Each effect is a few plucked
|
||||
notes (a tone with two overtones, a quick attack and an exponential decay) written as a 16-bit mono WAV in memory.
|
||||
Windows plays it with PlaySound from that memory; elsewhere the WAV goes to a file in the cache folder and the first
|
||||
of pw-play, paplay and aplay found plays it. One effect sounds at a time: a new one cuts the last, which suits a
|
||||
cursor moving quickly down a list.
|
||||
They are off until the player picks a loudness in Customize Launcher (the Sounds row). An effect is a 16-bit WAV in
|
||||
memory: the game's sound scaled to the loudness (the cursor's a little under the others, as in the game's menu), or a
|
||||
few plucked notes (a tone with two overtones, a quick attack and an exponential decay). Windows plays it with
|
||||
PlaySound from that memory; elsewhere the WAV goes to a file in the cache folder and the first of pw-play, paplay
|
||||
and aplay found plays it. One effect sounds at a time: a new one cuts the last, so the cursor's sound is left out
|
||||
for a quarter of a second after a choice, which the pointer landing on a row of the next screen would otherwise cut.
|
||||
|
||||
move the cursor went to another row
|
||||
toggle a switch or a choice changed
|
||||
select a row was chosen (another screen, a folder dialog)
|
||||
back back to the screen before
|
||||
start Play
|
||||
done an install, update or rebuild finished
|
||||
error something could not be done
|
||||
move the cursor went to another row the game's cursor sound
|
||||
toggle a switch or a choice changed the cursor sound
|
||||
select a row was chosen (another screen) the game's decide sound
|
||||
back back to the screen before the game's cancel sound
|
||||
start Play the decide sound
|
||||
done an install, update or rebuild finished the decide sound
|
||||
error something could not be done the cancel sound
|
||||
"""
|
||||
import ctypes
|
||||
import os
|
||||
import shutil
|
||||
import struct
|
||||
import subprocess
|
||||
import time
|
||||
|
||||
import numpy as np
|
||||
|
||||
RATE = 44100
|
||||
LEVELS = {'off': 0.0, 'low': 0.18, 'medium': 0.4, 'high': 0.75} # the Sounds choice: the loudest sample's share of full scale
|
||||
# Which of the game's sounds (gamefiles.SOUNDS) each effect plays, and how loud beside the others.
|
||||
GAME_SOUND = {'move': ('move', 0.6), 'toggle': ('move', 0.6), 'select': ('select', 1.0), 'start': ('select', 1.0),
|
||||
'done': ('select', 1.0), 'back': ('back', 1.0), 'error': ('back', 1.0)}
|
||||
QUIET_AFTER = 0.25 # seconds after a choice in which the cursor's sound is left out
|
||||
|
||||
# Notes as (start in seconds, frequency in Hz, length in seconds, loudness 0..1).
|
||||
C5, D5, E5, G5, A5, C6, D6, E6, G6, C7 = 523.25, 587.33, 659.25, 783.99, 880.0, 1046.5, 1174.66, 1318.51, 1567.98, 2093.0
|
||||
@@ -62,13 +70,28 @@ def synth(name):
|
||||
return out / peak if peak > 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('<i2').tobytes()
|
||||
return (b'RIFF' + struct.pack('<I', 36 + len(pcm)) + b'WAVEfmt ' + struct.pack('<IHHIIHH', 16, 1, 1, RATE, RATE * 2, 2, 16)
|
||||
return (b'RIFF' + struct.pack('<I', 36 + len(pcm)) + b'WAVEfmt '
|
||||
+ struct.pack('<IHHIIHH', 16, 1, channels, rate, rate * 2 * channels, 2 * channels, 16)
|
||||
+ b'data' + struct.pack('<I', len(pcm)) + pcm)
|
||||
|
||||
|
||||
def read_wav(data):
|
||||
"""A 16-bit PCM WAV as written by gamefiles.pcm_to_wav: (rate, channels, samples in -1..1 with its loudest at 1),
|
||||
or None when it is another kind of file."""
|
||||
if len(data) < 44 or data[:4] != b'RIFF' or data[8:16] != b'WAVEfmt ' or data[36:40] != b'data':
|
||||
return None
|
||||
kind, channels, rate, _bytes, _align, bits = struct.unpack_from('<HHIIHH', data, 20)
|
||||
size = min(struct.unpack_from('<I', data, 40)[0], len(data) - 44)
|
||||
if kind != 1 or bits != 16 or channels not in (1, 2) or size < 2 * channels:
|
||||
return None
|
||||
a = np.frombuffer(data, dtype='<i2', count=size // 2, offset=44).astype(np.float32)
|
||||
peak = float(np.abs(a).max())
|
||||
return rate, channels, a / peak if peak > 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 <cache>/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
|
||||
|
||||
@@ -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('<HHIHH', 0xfeff, 1, 0, 0, len(channels)))
|
||||
at = 0x10 + 0x4c * len(channels)
|
||||
for codec, pan, count, data in channels:
|
||||
info = bytearray(0x4c)
|
||||
struct.pack_into('<HHIII', info, 0, codec, pan, rate, count, count)
|
||||
struct.pack_into('<I', info, 0x30, at)
|
||||
out += info
|
||||
at += len(data)
|
||||
for _codec, _pan, _count, data in channels:
|
||||
out += data
|
||||
return bytes(out)
|
||||
|
||||
def test_the_games_interface_sounds(self):
|
||||
sound = self.sound
|
||||
# one PCM16 channel in the middle: both sides, at 0.707
|
||||
mono = self.bwav([(0, 2, 3, struct.pack('<3h', 1000, -2000, 3000))])
|
||||
self.assertEqual(gamefiles.bwav_to_pcm(mono), (48000, [(707, 707), (-1414, -1414), (2121, 2121)]))
|
||||
# two DSP-ADPCM channels, left and right: a frame's header byte (coefficient pair 0, all zero here, shift 8)
|
||||
# and its four-bit samples 1, -1, 7 make 256, -256, 1792
|
||||
frame = bytes([0x08, 0x1f, 0x70]) + bytes(5)
|
||||
both = self.bwav([(1, 0, 3, frame), (1, 1, 2, frame)])
|
||||
self.assertEqual(gamefiles.bwav_to_pcm(both), (48000, [(256, 256), (-256, -256), (1792, 0)]))
|
||||
for bad in (b'', b'RIFF' + bytes(60), mono[:0x30], self.bwav([(7, 2, 3, bytes(6))]), mono[:-2]):
|
||||
self.assertIsNone(gamefiles.bwav_to_pcm(bad))
|
||||
# from a dump folder into data/cache/sounds, and from there into the effects
|
||||
dump = self.tmp / 'dump'
|
||||
(dump / 'exefs').mkdir(parents=True)
|
||||
(dump / 'exefs' / 'main').write_bytes(b'')
|
||||
stream = dump / 'romfs' / 'Sound' / 'Resource' / 'Stream'
|
||||
stream.mkdir(parents=True)
|
||||
loud = struct.pack('<4h', 0, 16000, -16000, 8000)
|
||||
(stream / 'SE_Sys_Cursor00.bwav').write_bytes(self.bwav([(0, 0, 4, loud), (0, 1, 4, loud)]))
|
||||
(stream / 'SE_Sys_Do00.bwav').write_bytes(b'not a sound')
|
||||
got = gamefiles.extract_sounds(self.layout, '', str(dump))
|
||||
self.assertEqual(set(got), {'move'})
|
||||
self.assertEqual(gamefiles.cached_sounds(self.layout), got)
|
||||
with mock.patch.object(sound.os, 'name', 'posix'), mock.patch.object(sound.shutil, 'which', return_value=None):
|
||||
s = sound.Sounds(self.layout.cache, 'high')
|
||||
self.assertEqual(set(s.game), {'move'})
|
||||
wav = s.wav('toggle') # the cursor's sound: the game's, stereo at its rate
|
||||
self.assertEqual(struct.unpack_from('<HHI', wav, 20), (1, 2, 48000))
|
||||
peak = max(abs(v) for v in struct.unpack_from('<8h', wav, 44))
|
||||
self.assertAlmostEqual(peak / 32767, sound.LEVELS['high'] * 0.6, delta=0.01)
|
||||
self.assertEqual(struct.unpack_from('<HHI', s.wav('select'), 20), (1, 1, sound.RATE)) # no decide sound: the chime
|
||||
self.assertFalse(sound.Sounds(self.tmp / 'empty', 'high').game)
|
||||
|
||||
def test_the_cursor_is_quiet_right_after_a_choice(self):
|
||||
sound = self.sound
|
||||
with mock.patch.object(sound.os, 'name', 'posix'), mock.patch.object(sound.shutil, 'which',
|
||||
return_value='/usr/bin/aplay'), \
|
||||
mock.patch.object(sound.subprocess, 'Popen') as popen, \
|
||||
mock.patch.object(sound.time, 'monotonic') as clock:
|
||||
clock.return_value = 100.0
|
||||
s = sound.Sounds(self.tmp, 'low')
|
||||
s.play('select')
|
||||
clock.return_value = 100.1
|
||||
s.play('move') # the pointer landing on the next screen's row
|
||||
self.assertEqual(popen.call_count, 1)
|
||||
clock.return_value = 100.4
|
||||
s.play('move')
|
||||
self.assertEqual(popen.call_count, 2)
|
||||
|
||||
def test_the_look_settings_survive_and_fall_back(self):
|
||||
path = self.tmp / 'launcher.json'
|
||||
s = settings.Settings(path)
|
||||
|
||||
Reference in New Issue
Block a user