Skip to main content
← PSoXide SDK guides and examples · SDK 80d1c329e977 · All Cargo features enabled; check feature gates before use.

Module xa_clock

Module xa_clock 

Source
Expand description

A song clock for XA-ADPCM music: where the playing song is, in milliseconds, every frame, for beat sync, spectrum bars and rhythm games.

The XA player has no sample counter to read. What the drive can say is which sector its head is in (Player::poll asks with GetlocP), and a song’s sectors stream at a fixed rate (75 a second at single speed, 150 at double), so the head’s place in the file is the song’s time. The clock turns that coarse, occasional reading into a smooth one the way PlaybackClock does for CD-DA: it anchors on the reading and advances with the caller’s own tick counter in between, so the drive is asked rarely (once per 15 to 30 ticks is plenty) and the display and CD oscillators cannot drift apart for long.

ⓘ
let mut clock = SongClock::new(60);
// once per tick:
player.poll();                               // or every 15..30 ticks
if let Some(ms) = clock.update(&player, tick) {
    // ms into the song file, smooth and never backwards
}

§Precision

These are the errors this module can account for. All of them are reasoned from how the clock works and checked against a simulated drive in the unit tests; none is measured on a console.

  • Sector quantum, 13.3 ms at single speed (6.7 ms at double). The head reading is a whole sector. The clock takes the middle of it, so the error is at most half a sector either way (6.7 ms), and zero on average.
  • Tick granularity. The anchor is stamped with the tick of the call, but the drive answers some milliseconds into the frame. Allow up to one tick (16.7 ms at 60 Hz) on top, one-sided: the clock reads slightly early. Call SongClock::update right after poll, at the same place in the frame every time, so the offset is constant.
  • Drift. Between readings the clock runs on the tick counter. The display and CD crystals differ by well under 0.1 percent, so 30 ticks (half a second) drift a fraction of a millisecond, and every reading re-anchors it. Nothing accumulates over a song.
  • Never backwards. Small corrections only move the clock forward and large ones are held until a second reading agrees (the PlaybackClock policy), so the output does not step back inside one run of the song. A loop or a resume starts a new run.
  • Head versus sound: not known. The drive reads ahead of what the speakers play. The sector the head is in is decoded and queued at the SPU, which then needs time to play it out. The player’s own doc says “a sector or two”, and the XA decoder works in 4-sector groups, so a constant offset up to roughly 55 ms is plausible. Nothing in this crate can observe it and the emulator does not model it. It has to be measured on the console (the hardware-test suite’s XA clock step is there for that) and put in with SongClock::with_head_lead_ms. Until then the clock reports where the head is, and the sound is behind it by that unmeasured amount.

So: the clock is ahead of the sound by the unmeasured head lead, and otherwise within about 7 ms either way plus up to 17 ms early. Rhythm timing windows in the tens of milliseconds (gh-psx uses plus or minus 90 ms) fit with a margin; sample-accurate sync does not.

§What would sharpen it

The SPU writes the CD input it is playing into its capture buffer in SPU RAM, and that write position moves every 22.7 microseconds. Reading it would pin the phase inside a sector. It is not used: reaching it needs an SPU IRQ address or a RAM read-back that nothing in this SDK has proved on silicon, and a 90 ms window does not need it. A per-sector interrupt (the Report mode bit) would timestamp each sector boundary but needs a CD interrupt handler, which the polled players avoid. Both are options if a game ever needs under 10 ms.

Structs§

SongClock
Smooth, monotonic song time for the XA song a Player is playing.