Entry 620

The quit prompt holds the game's sound: the mixer stands the engine still, the prompt's own sounds on an SPU2 of their own

Files10 in 6 directories
README.mdcrates/piney-audio/src/driver.rslib.rsoutput.rscrates/piney-audio/tests/hold.rscrates/piney-game/src/lib.rsquit.rssession.rscrates/piney-game/src/session/tests/quit_sound.rsdocs/engine/sound.md

After 619, the maintainer noticed that the quit prompt (Escape, or the window's close button since 618) held the picture but not the sound. Its doc said the game "stands still under it", but the music, voices, a BGM.BIN track, looping 3D sounds and a playing movie's sound all went on. A movie came back out of step with its picture.

The cause

piney_audio's engine runs on the output's sample clock, not on game frames. The cpal callback renders the engine, and the engine steps SNDBASE's tick every 4,167 us of samples: the sequencers and the synthesizer tick, every SPU2 voice and the reverb advance, and the sound-data input (a voice line, a BGM.BIN track, a movie's or a STREAM's samples) is read on. The prompt stopped stepping the game, but nothing told the mixer. Worse, the prompt ran Audio::frame (the EE driver's fades, voice queue and option changes) on every one of its own frames (worklog 237: "only the sound driver runs on"). It played its cursor sounds through Audio::se_on, on the game's own port 0 and SPU2 voices.

No reader feeds the input from another thread. Voice lines are read in Audio::run on the game's thread before they are applied. BGM.BIN tracks are read whole in bgm_stream. A movie's sound is decoded whole when it opens (movie::Playing::open). A STREAM's samples arrive with its frame's events. So holding the engine holds every input with it.

The fix

The mixer. What the callback locks is now a Mixer: the game's Engine, a count of holds, and an aside for the port's own sounds.

  • Mixer::render renders the engine only while nothing holds it. While held, it fills silence and does not call the engine at all, so no tick, voice, envelope, pitch counter, loop position, reverb sample or input position moves.
  • The hold is a field behind the same mutex the callback already takes, so there is no second lock. When plans/modern.md's lock-free mixer comes, it moves with the mixer as one more command.
  • Audio::render (the headless renders, every test and harness) goes through the same Mixer::render. With no hold and no aside it is the old Engine::render, sample for sample.
  • Audio's own calls still see the engine through a guard (Locked) that derefs to it, so none of them changed.
  • SNDBASE's tick clock is a small Ticker, with the same arithmetic, shared by the engine and the aside.
  • driver::master_volume names sdCommand's (v << 14) - v >> 8.

The hold. Audio::hold() returns a Hold. Dropping it lets the game go on from the very sample it stopped at. The quit prompt keeps it: QuitPrompt::hold(picture, audio) takes the picture and the sound at the same moment, after the one game frame the prompt steps. The hold goes when the prompt is dropped, whichever path drops it: Cancel, Escape again, or the window closing. Quit just ends the loop. No thread waits on the hold, so nothing can hang.

The prompt's own sounds. ccDtMenu's cursor and OK sounds play through Hold::se_on, on an Aside. The aside is a second synthesizer with only the common SE bank on port 0, set up as ccSndCommSeLoad sets up the game's, and a second SPU2 with its own Ticker. It is made at the first hold and kept, so nothing is allocated in the callback.

  • The note is made by the game's driver's se_on, which only reads the game's state.
  • The aside takes the player's seVol, mainVol and output mode from the game's driver at each hold. The prompt is heard at the player's volume even when a mode change has set the game's port 0 to 0.
  • It is dry: the game's hall reverb is the game's.
  • It is mixed over the output, saturating, only while it has something to play or a voice still sounds. So the OK sound on Cancel rings on over the game's sound coming back.

I chose a separate synthesizer and SPU2 over a free voice of the game's because any shared voice is game state. Taking one would change the synthesizer's allocation, priority stealing, ENDX and the voice's own envelope, and the game after Cancel would differ from a game never held. With the aside, the game's state is the same before and after the prompt, whatever the prompt plays.

The prompt's frame. The prompt's held frames no longer run Audio::frame, because the game is not stepped. The one game frame the prompt steps on a neutral pad, to have a picture to hold, now runs its sound frame as every game frame does. It also goes into the pad log: it was a game frame missing from the log, so a log written across a quit prompt replayed one frame short. Its line is the pad log's own bytes .. buttons format (pad_bytes, shared with the frame loop's line).

A movie. The picture moves on game frames and the sound on samples. The prompt holds both: game frames are not stepped and no samples are rendered. So after Cancel they stand where they stood relative to each other, as in a run that never opened the prompt.

Other places the picture stands still

  • The console (F1). The game runs on under it on a neutral pad, picture and sound, so nothing is held.
  • The window losing focus. Nothing changes: the game runs on.
  • The window minimised. Where the compositor stops sending frame callbacks (Wayland), winit stops sending redraws. Game frames then wait while the IOP side plays on, and a stall over the catch-up cap is dropped. The OS holds this picture, not the port's own UI, and the PS2 has no such state, so it is listed and left alone (docs/engine/sound.md, "The port's own screens").
  • The game started by the launcher. It is the same program in game mode, so the same fix applies. The GPUI launcher has no sound and holds no game picture.
  • The unplugged pause. ccThMother's PAUSE is the game's own, checked as the game runs it, and is left as the game has it.

Checked

  • crates/piney-audio/tests/hold.rs makes a sounding state on Infection's disc: the desktop's music (sequencers, synthesizer, reverb), a looping positioned SE (230) and BGM.BIN track 0 in the input, two seconds in. Two runs are made of it.
    • One is held for 300 callbacks of odd sizes (441 to 589 frames). The output is all zero while held, and the printed state (every voice, the driver, the sequencers, the ticks, the input) is unchanged.
    • After the hold, 8 s come out bit for bit as the unheld run's, with the same state at the end.
    • The second test plays SE 4 twice through the hold. It is heard, and the game's state does not move. Let go while it rings, its tail differs from the unheld run only in the first frames back; from there on the samples and the state are the same.
    • A third test: with seVol 0 the prompt's sounds are silent.
  • session::tests::quit_sound powers Infection on with every frame's events routed to the sound, into OPENING (240 frames into it, its sound playing). It opens the real QuitPrompt there, as the window does: one stepped frame, then the hold.
    • The prompt is run on Up, Down and START on Cancel until it answers Back. Its output equals, sample for sample, the same prompt over a fresh sound with nothing playing: the movie is not heard under it and the window's sounds are.
    • Then 300 game frames: every picture equals the never-held run's. The samples are the same once the window's last sound has rung out, under 2 s. OPENING is still playing at the end.
  • With the hold made to do nothing, the tests fail (tried).

Checks

  • Crate tests: 829 passed, 0 failed, 1 ignored (826 plus the three in hold.rs).
  • The piney-game suite: 353 / 0 / 153 (352 plus quit_sound).
  • Clippy (workspace, all targets and features, -D warnings) and fmt: clean.
  • tools/docs.py check and cairns check: pass.
  • The full sweep (sweep.sh): 416 runs, all rc=0, each line's result the same as summary-main.txt. Among them are the sound harnesses (test_sound_rs, test_sound_rt, test_sound3d_rs, test_stream_rs, test_mother_pause_rs) on all four volumes. They never open the prompt, and they come out the same.
  • No drawing code changed, so no golden shots were taken.
  • The game was not run in a window: this desktop is the maintainer's. The window's path is quit_frame around the same QuitPrompt and Hold that quit_sound runs headless.
Still unknown

The prompt has not been heard through a real output device here. The samples the device has already queued (one callback's worth, tens of milliseconds) still play after the hold. At a device rate other than 48 kHz the resampler blends one sample across the hold's edges. Neither touches the game's state. Whether KDE's Wayland compositor stops the redraws of a minimised window was not watched. If it does, the game frames wait while its IOP side plays on, as before this change. That is the OS's hold, not the port's own screen, so it was left alone.