Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions dm/games/010069401ADB8000/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ General host changes first needed here; each applies to every game:

- `alloc_big_align` was first needed on 13 Sentinels ([its note](../01003FC01670C000/README.md#memory), [the profile keys](../README.md#keys)).
- **The hardware multi-stream Opus decoder's C interface** forwarded to the SDK's software decoder (`hle_codec.c`) and **`GenerateCryptographicallyRandomBytes` under the `sdk`'s own `getentropy`** (`HLE_FORCE`, `hle_os.c`): [Hardware Opus decoders built on the SDK's software decoders](../../../docs/reference/audio.md#hardware-opus-decoders-built-on-the-sdks-software-decoders), [Cryptographically random bytes](../../../docs/reference/services.md#cryptographically-random-bytes).
- **A 5 ms device buffer on the Mac** (`hle_audio.c`): with the Mac's default 512-frame buffer the game underran on 250–290 of every 2000 callbacks (clicks); with 240 frames, none: [The Mac's default I/O buffer](../../../docs/reference/audio.md#the-macs-default-io-buffer).
- **Vibration handles answered before a controller's haptics are routed** (`hle_hid.c`): the game asks for them once at startup, before GameController reports the pad, and every value went to handle 0 (seen in the log; the game has no rumble, so nothing changed in play) ([Vibration handles before a controller is reported](../../../docs/reference/services.md#vibration-handles-before-a-controller-is-reported)).

## Open
Expand Down
21 changes: 18 additions & 3 deletions dm/host/hle_audio.c
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,22 @@ static OSStatus au_render(void *ctx, AudioUnitRenderActionFlags *f, const AudioT
if (mute) memset(bl->mBuffers[0].mData, 0, bl->mBuffers[0].mDataByteSize); }
return noErr;
}
#if !TARGET_OS_IPHONE
// The Mac's I/O buffer: about 5 ms of the device's frames, as front_ios.m asks of the iOS audio session. The default is 512 device frames (10.7 ms at 48 kHz, 11.6 ms at 44.1 kHz),
// more than the game reliably has queued: Unicorn Overlord underran on 250-290 of every 2000 callbacks at 48 kHz (0.1-0.2 s of zeros, heard as clicks) and on about half of
// them at 44.1 kHz; with 240 frames, none. The buffer belongs to this process and to the device the default output follows, so it is set again when that device changes.
// DM_DBG_AU_FRAMES=N asks N frames instead (1024 = the iOS default, which reproduces the crackling)
static void au_buffer(AudioUnit au, const char *why) {
AudioStreamBasicDescription df; UInt32 sz = sizeof df; const char *e = getenv("DM_DBG_AU_FRAMES");
Float64 rate = !AudioUnitGetProperty(au, kAudioUnitProperty_StreamFormat, kAudioUnitScope_Output, 0, &df, &sz) && df.mSampleRate > 0 ? df.mSampleRate : RATE;
UInt32 fr = e ? (UInt32)atoi(e) : (UInt32)(rate / 200 + 0.5);
OSStatus st = AudioUnitSetProperty(au, kAudioDevicePropertyBufferFrameSize, kAudioUnitScope_Global, 0, &fr, sizeof fr);
LOG("%saudio device buffer: %u frames at %.0f Hz%s (status %d)", st ? "[RED] " : "", fr, rate, why ? why : "", (int)st);
}
static void au_device_changed(void *ctx, AudioUnit au, AudioUnitPropertyID id, AudioUnitScope scope, AudioUnitElement el) {
(void)ctx; (void)id; (void)scope; (void)el; au_buffer(au, ", for the new output device");
}
#endif
static OSStatus au_create(aout *a) {
AudioComponentDescription d = {.componentType = kAudioUnitType_Output,
#if TARGET_OS_IPHONE
Expand All @@ -177,9 +193,8 @@ static OSStatus au_create(aout *a) {
.mChannelsPerFrame = ch, .mBitsPerChannel = 16};
AURenderCallbackStruct cb = {au_render, a};
#if !TARGET_OS_IPHONE
{ const char *e = getenv("DM_DBG_AU_FRAMES"); if (e) { // diagnostic: on the Mac, make the sound card callback ask N frames each time (1024 = the iOS default, reproduces the crackling)
UInt32 fr = (UInt32)atoi(e); OSStatus st = AudioUnitSetProperty(a->au, kAudioDevicePropertyBufferFrameSize, kAudioUnitScope_Global, 0, &fr, sizeof fr);
LOG("audio device buffer frame count set to %u: status %d", fr, (int)st); } }
au_buffer(a->au, NULL);
AudioUnitAddPropertyListener(a->au, kAudioOutputUnitProperty_CurrentDevice, au_device_changed, NULL);
#endif
OSStatus st = AudioUnitSetProperty(a->au, kAudioUnitProperty_StreamFormat, kAudioUnitScope_Input, 0, &f, sizeof f);
if (!st) st = AudioUnitSetProperty(a->au, kAudioUnitProperty_SetRenderCallback, kAudioUnitScope_Input, 0, &cb, sizeof cb);
Expand Down
1 change: 1 addition & 0 deletions docs/agents/symptoms.md
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,7 @@ One line per symptom in the [subsystem references](../reference/): what you see
- no music and no sound effects, only the movies' sound (the log repeats `audren: first-frame commands …` every frame and never prints `DeviceSink inputs`). → [SDK 8: command IDs numbered differently](../reference/audio.md#sdk-8-command-ids-numbered-differently)
- no sound; red `audren: command 0 in the command list is malformed (first word 0x181901 size 0)`. → [SDK 21 DSP command header without a magic](../reference/audio.md#sdk-21-dsp-command-header-without-a-magic)
- crackling on iOS only. → [RemoteIO's default I/O buffer duration](../reference/audio.md#remoteios-default-io-buffer-duration)
- clicks and dropouts on the Mac, heard as clipping; the log's `audio #N device callbacks` lines show hundreds of underruns per 2000 callbacks. → [The Mac's default I/O buffer](../reference/audio.md#the-macs-default-io-buffer)
- sound stays silent after Siri, an alarm, a call or another app's audio; a game whose audio thread waits for its buffers back (Wwise's poll-only output, FMOD) then waits forever. → [Interruptions that stop RemoteIO callbacks](../reference/audio.md#interruptions-that-stop-remoteio-callbacks)
- combined numbers mislead: one fully fed output plus one fed by nobody look like "every other callback underruns, 2.0 blocks queued". → [Several outputs at once (`audio #N`)](../reference/audio.md#several-outputs-at-once-audio-n)
- a null read at `sdk+0x1f2314` as the first movie starts (the title menu's background, right after the first press), in `nn::audio::SetAudioOutVolume`, called from `main+0x1c4fbc`. → [AudioOut volume kept per output](../reference/audio.md#audioout-volume-kept-per-output)
Expand Down
7 changes: 7 additions & 0 deletions docs/reference/audio.md
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,13 @@ For a replaced DSP renderer the size query keeps the service's work-buffer contr
- **Fix:** `AVAudioSession` `setPreferredIOBufferDuration:0.005` (and 48 kHz) before the game opens audio and again on return to the foreground (`front_ios_audio_session` in `front_ios.m`); the device then calls back with 240 frames and nothing underruns.
- **Check:** when checking a new device, read the host's `audio #N device callbacks` line, logged every 2000 callbacks: frames per callback, queued blocks, underruns.

### The Mac's default I/O buffer

- **Symptom:** clicks and dropouts on the Mac, heard as clipping; the log's `audio #N device callbacks` lines show hundreds of underruns per 2000 callbacks.
- **Cause:** the Mac's default output asks for 512 device frames per callback (10.7 ms at 48 kHz; 557 frames of the 48 kHz stream when the device runs at 44.1 kHz), more than a game that keeps only a few small blocks queued has ready every time. Unicorn Overlord on a MacBook Pro's speakers: 250–290 underruns in every 2000 callbacks at 48 kHz (0.1–0.2 s of zeros), about 1,000 at 44.1 kHz. Whether a game underruns depends on how much it queues: 13 Sentinels (2.8 blocks queued, like Unicorn Overlord's 3.0, but larger ones) had none at 512 frames.
- **Fix:** like iOS's session buffer, the Mac sets its output unit's device buffer to about 5 ms of device frames (`kAudioDevicePropertyBufferFrameSize`, 240 at 48 kHz, 220 at 44.1 kHz) when it creates the unit, and again when the default output switches to another device (`au_buffer` in `hle_audio.c`). Unicorn Overlord at 240 frames: 0 underruns in 10,000 callbacks; 13 Sentinels stays at 0. The buffer is the process's own; other apps keep theirs. `DM_DBG_AU_FRAMES=N` asks N frames instead.
- **Found on:** [Unicorn Overlord](../../dm/games/010069401ADB8000/README.md) (Asian release 1.05 on the Mac).

### Interruptions that stop RemoteIO callbacks

- **Symptom:** sound stays silent after Siri, an alarm, a call or another app's audio; a game whose audio thread waits for its buffers back (Wwise's poll-only output, FMOD) then waits forever.
Expand Down
Loading