Files
psxrecomp/runtime/include/audio_trace.h
T
Matthew Stanley 9306db59b3 cdrom: continuous disc clock — never freeze the read stream on INT ack
The read stream froze read_delay whenever the guest had an unacked
controller INT (and while the sector buffer was undrained). Real drives
never pause: measured on MMX4's attract/title XA music, the freeze
accumulated ~1M guest cycles/second (~3% of disc time), every XA sector
arrived 32-92 samples late, and the XA->SPU cd_ring starved ~147 samples
5-6x per second — audible as a 6 Hz crackle/dropout flutter in all
XA-streamed music (user report: MMX4 Zero ice-stage attract demo).

Faithful model, mirrored from the in-tree Beetle oracle (cdc.cpp
HandleSectorRead / SetAIP / CheckAIP / ClearAIP):

- Disc time NEVER pauses while reading; sectors are read on exact
  cycle schedule. XA-ADPCM realtime audio flows to the SPU decoder
  unconditionally (this alone removes the crackle).
- A data sector that comes due while the guest still has an unacked
  INT overwrites the sector buffer on schedule (hardware clobbers its
  FIFO the same way) and its data-ready INT1 pends ONE deep, presented
  the moment the guest fully acks (Beetle CheckAIP). A newer sector
  replacing an unpresented notification is counted + traced 'P'
  (Beetle "Previous notification skipped").
- Drive-state changes (Read/Play/Pause/Stop/Seek paths through
  start/stop_read_stream) cancel the pended notification (ClearAIP).
- The active-DMA refill path (deliver without new INT while a
  multi-sector CD DMA drains) is unchanged.

Verification on MMX4 (SLUS-00561) title/attract XA:
- CD_PUSH guest-cycle spacing: exactly 1,806,336 cycles (8 sectors at
  2x), min == max over 148 pushes (was 1.83-1.88M, systematically late).
- read_hold_cycles == 0 (was ~30.8M per 30 s) — kept as an always-on
  regression tripwire in cdrom_state.
- int1_pended == 0 during music: X4 acks fast; the pending path only
  arms for genuinely slow acks.
- spu_out/host tap rings: 6 Hz zero-gap storm (86-144 gaps/95 s of
  147-265 samples) eliminated; only real content silences remain.
- cd_underflow_frames stays 0 during playback.

Always-on observability added (ring-buffer doctrine):
- XA zero-run scanner in the decode path -> AUDIO_EV_XA_ZERO events
  (stage 0 = post-ADPCM, stage 1 = post-resample+volume, with LBA).
- CD_PUSH audio events now stamp the global guest-cycle clock (the
  spu_out sample stamp is pump-quantized and cannot measure cadence).
- cdrom_state: read_hold_cycles/read_hold_events (must stay 0),
  int1_pended / int1_lost / int1_pending_now.
- Savestate: pending_dataready fields appended to CDROM_SNAP_FIELDS.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
2026-07-10 13:55:52 -07:00

128 lines
5.5 KiB
C

/*
* audio_trace.h - always-on audio observability rings (PCM taps + event ring).
*
* Port of the snesrecomp audio-campaign measurement harness (audio_trace.{c,h},
* commit 67ead27 lineage) to the PSX pipeline, following the cross-system tap
* model in F:/Projects/_audio_round2/ROUND2_PLAN.md: always-on ring buffers
* recording every sample/event from process start, dumped AFTER the fact via
* TCP debug commands — never arm-then-reproduce.
*
* Taps (PSX pipeline):
* AUDIO_TAP_SPU_OUT "T1" — canonical spu_render() output @44100, recorded
* inside spu_render so every caller (pump, fade tail) is
* covered, PRE any host-side fade/mute gain.
* AUDIO_TAP_CD_IN "T2" — decoded XA/CD-DA PCM pushed onto the SPU CD
* input bus (already resampled to 44100 by the producer).
* AUDIO_TAP_HOST "T3" — the exact bytes handed to the host audio API
* (post fade ramps; what SDL_QueueAudio receives).
*
* The same module compiles into psx-beetle, where AUDIO_TAP_SPU_OUT records
* the libretro audio_batch callback — Beetle's fully-mixed reference SPU
* output — giving symmetric `audio_*` commands on ports 4370 and 4380.
*
* Threading: each tap has a single writer. Readers (debug-server thread)
* take a stable [oldest, head) snapshot; the PCM rings hold ~95 s at 44100,
* so a dump cannot be lapped mid-read in practice. Indices are C11 atomics
* (release on publish, acquire on read).
*/
#ifndef PSX_AUDIO_TRACE_H
#define PSX_AUDIO_TRACE_H
#include <stdint.h>
#ifdef __cplusplus
extern "C" {
#endif
enum {
AUDIO_TAP_SPU_OUT = 0,
AUDIO_TAP_CD_IN = 1,
AUDIO_TAP_HOST = 2,
AUDIO_TAP_VOICES = 3, /* T0: 24-voice sum only (pre CD mix, pre main) */
AUDIO_TAP_COUNT = 4
};
/* Event kinds. REG_WRITE covers every SPU register store (addr/value), which
* subsumes KEYON/KEYOFF/pitch/volume writes — the semantic KON ring stays in
* spu.c. The rest instrument the host delivery path. */
enum {
AUDIO_EV_NONE = 0,
AUDIO_EV_REG_WRITE = 1, /* a=SPU reg addr, b=value */
AUDIO_EV_RENDER = 2, /* a=frames rendered, b=host queued bytes before */
AUDIO_EV_PUMP_SKIP = 3, /* backpressure skip; a=queued bytes */
AUDIO_EV_UNDERRUN = 4, /* host queue empty at pump entry; a=queued bytes */
AUDIO_EV_MUTE = 5, /* turbo mute engaged; a=fade tail frames */
AUDIO_EV_UNMUTE = 6, /* pumping resumed; a=fade-in frames pending */
AUDIO_EV_CD_PUSH = 7, /* a=guest-cycle clock low32, b=ring fill after */
AUDIO_EV_DMA_WRITE = 8, /* SPU RAM DMA; a=words, b=transfer addr after */
AUDIO_EV_XA_ZERO = 9, /* zero-run in decoded XA PCM; a=lba,
b=(stage<<28)|(start_frame<<14)|run_len.
stage 0=post-ADPCM-decode (native rate),
stage 1=post-resample+volume (44100). */
};
typedef struct {
uint64_t seq; /* monotone event counter */
uint64_t sample_idx; /* AUDIO_TAP_SPU_OUT frames produced when recorded */
uint32_t frame; /* host-noted frame counter (vblank / retro_run) */
uint16_t kind;
uint16_t reserved;
uint32_t a;
uint32_t b;
} AudioTraceEvent;
typedef struct {
/* Per-tap PCM production. */
uint64_t tap_frames[AUDIO_TAP_COUNT]; /* frames ever recorded */
uint64_t tap_nonzero[AUDIO_TAP_COUNT]; /* frames with |L| or |R| > 0 */
uint64_t tap_audible[AUDIO_TAP_COUNT]; /* frames with |L| or |R| > 256 (~ -42 dBFS) */
int32_t tap_peak[AUDIO_TAP_COUNT]; /* running peak |sample| */
/* Host pump behavior. */
uint64_t pump_calls;
uint64_t pump_skips; /* backpressure early-outs */
uint64_t underruns; /* pump entered with 0 queued bytes (post first audio) */
uint32_t queue_hiwater; /* max queued bytes observed at pump entry */
uint32_t queue_lowater; /* min queued bytes observed at pump entry (post first audio) */
uint64_t mute_events;
uint64_t unmute_events;
/* Event ring. */
uint64_t events_total;
} AudioTraceStats;
void audio_trace_init(void);
/* Record `frames` interleaved stereo s16 frames into tap `tap`. */
void audio_trace_pcm(int tap, const int16_t *stereo, int frames);
/* Record a pipeline event (timestamps applied internally). */
void audio_trace_event(uint16_t kind, uint32_t a, uint32_t b);
/* Host frame tag for event timestamps (call once per vblank / retro_run). */
void audio_trace_note_frame(uint32_t frame);
void audio_trace_get_stats(AudioTraceStats *out);
uint64_t audio_trace_tap_total(int tap);
uint64_t audio_trace_events_total(void);
/* Taps default to 44100 Hz; the host tap may run at the device rate in
* bridge/pull mode. The WAV dumper stamps this rate — a mislabeled header
* silently poisons every offline A/B. */
void audio_trace_set_tap_rate(int tap, uint32_t rate);
uint32_t audio_trace_tap_rate(int tap);
/* Copy the most recent `max` events, oldest first. Returns count copied. */
uint32_t audio_trace_events_get(AudioTraceEvent *out, uint32_t max);
/* Write a tap slice as a stereo s16 WAV at the tap's rate. start=-1,count=0
* dumps the whole currently-buffered ring. `start`/`count` are absolute frame
* indices on the tap's production timeline. Returns frames written, or -1 on
* error (unwritable path / empty ring / slice already evicted). */
int64_t audio_trace_dump_wav(int tap, const char *path,
int64_t start, uint64_t count);
#ifdef __cplusplus
}
#endif
#endif /* PSX_AUDIO_TRACE_H */