Commit Graph
434 Commits
Author SHA1 Message Date
Codex 4f24f73070 audio: retain fractional phase in aligned S16 conversion
Correct sub-phase rounding after bounded offline chunks without changing normal playback processing. Check the actual correction kernel against a nine-hour exact sample-clock oracle.
2026-11-16 20:01:26 +01:00
Codex 00904f5d64 audio: expose aligned cold starts for fixed-ratio S16 sinc conversion
Center offline output on source frame zero and report the input lookahead needed for tail draining. Existing processing and reset paths remain unchanged. This supplies latency handling for worker-owned fractional recording conversion.

Check impulses, independent fractional-rate tone phase, chunk invariance and tail drain at 8-192 kHz. GCC/Clang sanitizers, Windows C89 and four NDK ABI builds pass; missing and one-frame compensation mutants are rejected.
2026-11-16 20:01:26 +01:00
Codex 35aff88575 audio: avoid rate-change restarts for inactive output
Accept input-rate metadata changes when audio is deliberately disabled or uses the null backend and no device or resampler exists. This avoids rebuilding recording scratch and resynchronizing video for timing-only AV-info updates. Keep failed real backends on the reinitialization path.

Cover idle metadata, failed-backend recovery and scratch retention through real AV-info calls with threaded video on and off.
2026-11-16 13:13:00 +01:00
libretroadmin e12fbf782e resampler: state() saves and loads a stream
A core saving a state mid-stream needs the resampler's: the rings, the
clocks, the decimation stages and their crossfade. Each backend had
these as private fields, so a core kept its own copy of the struct
layout and broke on every resync. The vtable gains an optional state():
the size with no buffer, else a copy out or in, refused when the size
does not match the instance's configuration. The sinc carries its whole
state block and every clock, the nearest its fraction.
samples/audio/resampler_state saves at fixed ratios and below the design
ratio, mid-crossfade and settled, loads into an instance with a stream
of its own and requires the continuation bit for bit, and checks a
wrong size is refused.
2026-11-09 20:34:47 +01:00
LibretroAdmin 0400ea8f54 audio_mixer: sum voices over the game audio without copying it first
The first voice to mix reads the game audio in place of the mix, and a
stream that needs no resampling is read where it was decoded.
2026-11-09 03:50:52 +01:00
LibretroAdmin f30aef5c1c microphone: 16-bit at the requested rate reads straight into the ring
The device filled the frontend's scratch only for it to be copied into
the ring unchanged.
2026-11-09 02:53:00 +01:00
LibretroAdmin 4c668e24a0 qsa: lend the staging block
The frontend now produces straight into the block that goes to the
device instead of into its own buffer and copying it in.
2026-11-09 02:07:57 +01:00
LibretroAdmin 6282e850e8 wdmks: a packet pin lends the next packet
The looped WaveRT path already lent its ring. A packet pin now has the
frontend produce straight into the packet it sends, instead of into its
own buffer and copying it in.
2026-11-09 02:05:40 +01:00
LibretroAdmin a32271ff0a audio: ASIO, AudioWorklet and WaveOut lend their output buffers
The frontend now produces straight into each driver's ring or wave block
instead of into its own buffer and copying it in.
2026-11-09 02:02:35 +01:00
LibretroAdmin 3ab5a0fefa audio: PSP, Vita, PS4, 3DS and Switch lend their output buffers
The frontend now produces straight into each driver's ring or wave buffer
instead of into its own buffer and copying it in.
2026-11-09 01:58:48 +01:00
LibretroAdmin dbab7c6444 audio: PS3, SDL 1.2 and RSound lend their rings
The frontend now produces straight into each driver's ring instead of
into its own buffer and copying it in.
2026-11-09 01:47:48 +01:00
LibretroAdmin fa213b2b48 audio: extra channels at unity are read where they lie
A bypassed resample of a core's extra channels copied the input into
the output scratch only for the merge to read it there. Same format, so
the merge now borrows the input.
2026-11-09 00:33:34 +01:00
libretroadmin 7e92aa944e oss: RetroFW writes whole 16-byte DMA units
RetroFW's jz4760 OSS driver moves a node to the codec in 16-byte DMA
units: it drops a write's remainder, and a write under one unit starts
a transfer of none. The threaded pipeline writes any number of frames,
so most writes lost one to three frames - static at its rate of writes -
and a short one could leave the device playing nothing it was given.
On RetroFW a write now goes out in whole units, its remainder held to
lead the next one.

samples/audio/oss_report models that DMA; built with RETROFW=1, which
CI now does, it checks every node is whole units and plays in order.

Fixes #20006
2026-11-07 19:06:01 +01:00
LibretroAdmin a5c01e4ea0 audio: the resume ramp's flag is handed over, not raced
On the threaded pipeline audio_driver_pause_fade() armed the resume
ramp by writing fade_in_pending, a plain bool, on the main thread, and
the producer read and cleared it in audio_driver_submit_width() - on
the thread that submits, which for a core delivering from a thread of
its own is not the main thread. A data race; a resume could also lose
its ramp if the clear landed after a new arm.

fade_in_pending is now a retro_atomic_int_t. The main thread stores it;
whoever applies the ramp takes it with fetch-and against zero
(audio_driver_fade_take(); exchange is not on every retro_atomic.h
backend), in the producer and in audio_driver_arm_resume(). No lock,
and nothing waits.

ff_speedup gains a lane with the producer on a thread of its own while
the main thread ends frames, holds and releases fast-forward and
pauses, the consumer's share taken on the main thread. Under
ThreadSanitizer, which CI already runs ff_speedup with, it reported
this race and nothing else after the fast-forward measurement fix
(e24dafdc), and reports none now. The harness clock can run on real
time for a case with threads.
2026-11-07 18:18:41 +01:00
LibretroAdmin 51028e640e pipeline_clocked: one setting per lane, the CI step in 12 s
The CI step runs its lanes at once, in real time, so it takes as long
as the longest lane: rate_control, four settings at each of two device
latencies, 24 s; ff_exit, five transitions, 21 s; setpoint, three
latencies twice over, 18 s.

setpoint and rate_control now take a device latency (setpoint_16,
_32, _64; rate_control_32, _64) and ff_exit a transition (ff_exit_1
to _5), as audio_paced already did, and the step runs all fourteen at
once. The longest is now a single setting: 12 s of wall time for the
step here, three runs, no failures and no retries, on two CPUs. The
whole-case names still run the whole case, and a name that matches
nothing fails instead of passing on no run.
2026-11-07 18:18:38 +01:00
LibretroAdmin a840c07266 audio: the s16 gain and the synth sum carry the audio, not a copy
Three copies on the flush path that the next pass overwrote:

- Integer front pair at unity to an s16 device with a master gain:
  the batch was copied into the output scratch, then the gain ran over
  it in place. The gain now reads the batch and writes the scratch, one
  pass (audio_driver_gain_s16(), the same Q16 multiply).
- In-process MIDI synth on the integer path: the game audio was copied
  into the writable view, then the synth summed into it. The sum is now
  written from the game audio, one pass, and a synth that rendered
  nothing leaves the batch where it is, uncopied.
- In-process MIDI synth for a float core: copied in, then summed. The
  sum is written in the pass that brings the audio in. The synth renders
  before the path is chosen, so one that has nothing this batch no
  longer forces an s16 core off its passthrough through two conversions.

Output is unchanged sample for sample. Measured on 800-frame stereo
batches: the float synth sum 167 -> 132 ns per batch; the s16 gain
saves the 23 ns copy, about 0.5% of that pass; a silent synth saves the
copy and, on the float path, an s16 -> float -> s16 round trip.

unity_identity gains lanes: the integer gain at an attenuation and at a
saturating boost against the Q16 oracle; the synth sum on the integer
pair and for a float core against what the copy and the add gave; and
a silent synth leaving the batch on its passthrough, which on the float
path failed before (one s16 -> float and one float -> s16 conversion).
The shared stubs' synth is replaced there by one the cases control.
2026-11-07 17:52:40 +01:00
LibretroAdmin e24dafdc4d audio: the fast-forward measurement has one writer
The fast-forward speed measurement (last_flush_time and its two
averages) is taken by one thread: the submitter on the inline
pipeline, the main thread's frame end on the threaded pipeline and for
a callback core. Other threads wrote it too. Inline, the main thread's
frame end, pause and stop reset it while a core submitting from a
thread of its own was measuring. A callback core's audio thread reset
it beside the main thread's frame-end measurement. On the threaded
pipeline the producer read and reset it, and pipe_ff_frames was a
plain size_t added on the submitting thread and zeroed on the main one.
These are data races, and on 32-bit targets a 64-bit store can tear.

Now only the measuring thread writes the measurement. Any other thread
asks: one flag, stored only when not already up, which the measuring
thread takes before it next measures (audio_driver_ff_mult_sync(),
taken with fetch-and since exchange is not on every retro_atomic.h
backend). An idle frame costs one load. pipe_ff_frames is atomic: added
by the submitter, taken by what is there at the frame end, never stored
to zero. The producer knows the first frame of a hold from a flag of its
own instead of reading the main thread's measurement. A flush outside
fast-forward on the measuring thread still resets outright, which the
inline transport's source stamp overwrites as before.

This replaces the request-handler resets added for pause_fade in
0e35c138 with the same ask every other thread uses.

discrete_multichannel gains a lane with a core submitting from a
thread of its own while the main thread ends frames, holds and releases
fast-forward and pauses. Under ThreadSanitizer it reported nine races
on last_flush_time and pipe_ff_frames on the old code and none now; the
CI step builds it with TSan and runs that lane. Checks that read the
measurement straight after another thread's reset now accept the
pending ask.
2026-11-07 17:52:36 +01:00
LibretroAdmin 3b86a2126d pipeline_wakeups: the fast-forward stall lane in 2.3 s, not 3
check-ff-stall runs in real time, twice, and again under
ThreadSanitizer in CI. Each run spent 0.5 s settling into the hold,
1.5 s stalled and 1 s after it.

The stall is now 1.25 s, the shortest that still takes the old code's
estimate past the stretch's 4x duration bound with margin (from a bit
over 1 s); at 0.5 s the old code passes. A quarter of a second settles
on each side and half a second is judged, as before.

The lane still fails on the code before the gap fix (the stretch off
60 of 60 frames) and passes with it, three runs each variant, and under
ThreadSanitizer.
2026-11-07 17:09:02 +01:00
davey 4635151a1d audio: seed the fast-forward estimate again after a stall
A stall inside a fast-forward hold was measured as one long flush
interval, and a long enough one pushed the speed estimate past the time
stretch's duration range. The stretch then stayed off for the rest of
the hold: the threaded transport suspends and resumes only from a
drained ring, which fast-forward keeps full; and with Speedup off the
inline flush put back the stamp from before the interval, so every
later flush measured from it.

An interval longer than the average's whole window at 1x
(AUDIO_FF_EXP_AVG_SAMPLES expected intervals) is now a gap, not a speed:
the estimate seeds again at the configured ratio, as after a pause.
Shorter intervals are still measured. One of them can take the estimate
of a hold running at 1x or faster down to about half speed at most,
inside the stretch's range, and any single interval that could push it
out is a gap. The inline, threaded and callback-core paths share the
measurement.

Inline, the stamp is put back only for the Speedup fallback, which may
measure the interval again. Put back otherwise, it stays stale until it
ages into a gap.

The ff_speedup and discrete_multichannel harnesses stall a 2x hold for
three seconds, threaded with and without Speedup, and inline. Inline, a
slow spell of intervals under a gap also takes the estimate out of
range, and the stretch has to be back within a few frames of the hold
returning to 2x. pipeline_wakeups gains a lane on the real wrapper
without Audio Sync, where the consumer primes again after the stall; it
checks that the hold's audio and the stretch come back. The native
transport CI job runs it, also under ThreadSanitizer.
2026-11-07 17:09:02 +01:00
davey 11b38de1bf audio: don't measure a pause as a fast-forward flush interval
A pause inside a fast-forward hold left the flush timestamp from before
it, so the first flush after the resume measured the whole pause as one
interval. The speed estimate then read the core several times slower
than real time, outside the time stretch's duration range, and the
stretch stayed off until fast-forward was released; inline, only with
Speedup off.

Reset the measurement in audio_driver_pause_fade(), on the main thread
as the frame end resets it. Every pause and resume goes through it, and
so do the jump fades of a state load or core reset, where the reseed is
harmless. The next flush seeds the estimate at the configured ratio, as
the start of a hold does.

The ff_speedup and discrete_multichannel harnesses pause a 2x hold for
125 ms, threaded and inline, and check that the speed estimate and the
stretch hold through it.
2026-11-07 17:09:02 +01:00
libretroadmin e098569e4d oss: free closes a device that cannot reset
free() returned when SNDCTL_DSP_RESET failed, which is what a device
that has gone away does, leaking the descriptor, the stage and the
driver. The reset is now best-effort and the device is closed and
freed either way.

samples/audio/oss_report wraps close and frees a heap driver whose
reset fails; LeakSanitizer in the CI lane catches the old early return.
2026-11-07 01:00:35 +01:00
LibretroAdmin 3038603c62 oss: a device whose select() does not wait for room waits in its write
RetroFW's jz4760 OSS driver has no poll method, so select() reports the
descriptor ready at once whether or not a fragment is free. Eight wakes
without room now latch that: from then on wait_writable() claims the
room and the blocking write sleeps in the driver on its fragment-done
interrupt, rather than handing back 0 for the audio thread to ask again
at once - at real-time priority on a single core, nothing else ran.
A non-blocking descriptor claims nothing. Logged once at teardown.

samples/audio/oss_report models that driver with wrapped ioctl, select
and write: a full poll-less device hands every pass to the write, and a
device whose select() does wait still skips the pass.

Fixes #20006
2026-11-06 02:38:50 +01:00
LibretroAdmin f3316c77d8 Samples: discrete_multichannel in a quarter of the time
The test took 49 s, nearly all of it in two places that test nothing:
- each of the hundreds of audio re-inits built the sinc resampler's
  tables from scratch;
- eight scheduler cases call a consumer pass with nothing queued,
  which waits out the frontend's one-second idle bound each time.

The driver's resamplers now come from a cache in the test: one parent
per configuration, kept for the run, and the driver gets a sibling of
it -- its own fresh stream over the parent's tables, freed as usual. A
backend without siblings is made the usual way. And the test sets the
idle bound to 20 ms, far past anything a case waits for; the driver
keeps its one second unless a build defines AUDIO_PIPE_WAIT_MAX_US.

49 -> 12 s, 20 s under ASan and UBSan, every case passing.
2026-11-03 19:01:27 +01:00
LibretroAdmin 39ed0e4fd6 Samples: pipeline_rate_control's default run at its own length under sanitizers
Under ASan and TSan the default run was doubled to 32 s, so the sink
estimate would still settle with more of its windows left out. All
eight runs of the CI step go at once, so these two set its length;
the rest take 12 to 16 s.

At 16 s under both sanitizers, eight runs at once on two cores, three
rounds: the estimate applies two or three times, settled every time,
and the mean is within 22 ppm of the 500 allowed. With two CPU hogs
on top both lengths drift alike, 387 ppm at 16 s and 401 at 32, so
the longer run bought no margin. The doubling is gone.

The CI step 36 -> 23 s with its two builds.
2026-11-03 19:01:22 +01:00
LibretroAdmin 0ef3b08d45 Audio: fit() for the nearest and CC resamplers
A flush into a lent span fills it straight from the resampler only as
far as fit() says is sure to fit; without one it sizes from the ratio's
bound, which leaves a few frames of the span to the copy at the wrap.
Only sinc had a fit.

Both now run process()'s own clock without the samples: nearest steps
its fraction, CC its distance, in the same float operations, so the
answer is exact. Every CC arm, C, SSE and NEON, moves distance the
same way, so one model covers them; a CC stream made for downsampling
and run above a ratio of one is moved to the upsampler from its start,
as process() does. Builds whose float arithmetic is not repeatable --
x87 extended precision, fast-math -- and the PSP's VFPU arm have no
fit and keep the bound.

lend_write's fit case now runs all three backends, nearest and CC made
at one ratio and run at eight others, and also asks before any input
has been processed: 2296 fits exact. Mutations of each fit's step,
threshold and the CC switch are each caught.
2026-11-03 19:01:15 +01:00
LibretroAdmin 39a6ae0120 CI: run the native audio transport checks' runs at once
Linux-audio-transport took twelve minutes a matrix entry, most of it
waiting: the wide formats (32 runs, 220 s), the tempo extremes and live
controls (20 runs, 150 s), the speed-linked low-pass (8 runs, 84 s) and
the lend through the real wrapper (3 runs of a second each, 53 s). Each
run is a stretch of audio against a clocked device on next to no CPU,
run one after another in shell loops and recipes.

Each run is its own make target now, and a check runs its runs under a
sub-make with PAR (-j16, each run's output kept together); PAR= gives
the old order. The live set is one list the layout, speed-lpf, runloop
and auto checks all run with their own environment, check-wide runs
every format through both layouts and the live set on each at once, and
the lend, pause, menu, consumer-exit and kept-device checks run their
runs the same way.

On two cores: check-wide 220 -> 16 s, check-tempo 150 -> 8 s,
check-speed-lpf 84 -> 11 s, check-lend 53 -> 18 s, the same under
ASan within a few seconds; the whole job 1459 -> 234 s for both matrix
entries here, every check run twice without a failure.
2026-11-03 19:01:11 +01:00
LibretroAdmin 57fc8ce875 Audio: fill the span before the wrap exactly, from the resampler's clock
The flush in parts left one copy at the driver's wrap: the input given
to the first span was sized from the output bound, which keeps a margin
of sixteen frames and one more over the ratio, so the resampler's
output fell some twenty-five frames short of the span and that sliver
was filled from the frontend's buffer.

The sinc resampler can say exactly what the next process() makes of
each input frame: a frame is pushed while the phase clock is a phase
or more, a frame comes out at each step of the integer ratio until it
is again, and nothing else enters into it on the nominal path. The
resampler interface gains an optional fit(): the most input frames
whose process() is sure to produce no more than the frames asked for,
exact for the stream's state where the backend can tell, 0 where it
cannot (the decimated fast-forward path, or a backend without one, and
the caller sizes from the bound as before). The flush sizes the first
span's input from it, so the span is filled to within the frames one
input frame makes - a frame or two at 1.5x, often none at all - and the
copy left at the wrap is that or nothing. The bypass fills its span
exactly as it is, a frame in being a frame out.

retro_resampler_t's new member is last, so a backend that does not set
it is NULL there; libretro-common is RetroArch's, for the cores to
sync.

lend_write_test checks the fit is exact: for every quality, with and
without HQ oversampling, at ratios above and below one, after runs
that leave the stream at any state, the input fit() names makes no
more than asked and one frame more makes more. Off by one either way
it fails; a fit that overshoots also publishes past the span, which
the device stand-in refuses.
2026-11-03 16:02:32 +01:00
LibretroAdmin 7fb9eea29d Audio: resample across the driver's wrap, the flush in parts
Where the driver's ring wrapped inside a flush, a float stereo device
still took that flush the old way: the lend came back short and was
abandoned, the resampler wrote the frontend's buffer, and write()
copied every frame into the ring - about one flush in sixteen on a
typical ring, and the last copy the lend left on that path.

The flush now runs its output stage in parts. The resampler is given
as much input as is sure to fit the short span (audio_driver_input_
bound), writes it there carrying its state, and the span is published.
Its output falls a little short of the bound, so a sliver of a few
frames is left before the wrap: enough input to fill it is produced in
the frontend's buffer, the sliver copied from there - the one small
copy left - and the frames over are carried to the head of the next
span, where the rest of the flush is produced past the wrap. The
mixer, the clamp, the resume ramp and the pause mute run on each part
in turn, each carrying its state, so the stream is the one the whole
flush made: the lend test compares it bit for bit. The bypass's copy
splits the same way. An int16 device keeps narrowing into its spans
on the way out; a device that lends nothing, or no room after a
bounded wait, takes the staged write as before.

The produce, the finish and the sink accounting are their own
functions now, run once per part; audio_driver_input_bound() is built
for every configuration.

lend_write_test's rings wrap 1000 frames into a 1200-frame flush, and
one wraps twice: a float stereo device resampled or at unity, threaded
and inline, must give the written stream bit for bit with write()
never called. A ring that cuts a frame at its end still sends what is
left through write(). Dropping the carry, not shifting it, or letting
the fit overshoot the span each fail it.
2026-11-03 14:51:41 +01:00
LibretroAdmin 34e276c35c Audio: make the device frames span by span where the driver's ring wraps
A lend asks the driver for one span to hold a whole flush. Where the
driver's ring wraps inside it the span comes back short, the lend is
abandoned, and the flush is produced in the frontend's buffers and
copied in by write() - about one flush in sixteen on a typical ring.

The resampler's output cannot be split there, but every stage after it
makes the device's frames one at a time, its state carried from call
to call: the narrowing of a float mix for an int16 stereo device, the
upmix for a wider one (int16, float, or float narrowed for an int16
device, the core's own surround channels merged over it), and the
binaural render on headphones. audio_driver_stage_spans() runs such a
stage into as many spans as the driver lends - both sides of the wrap
- in whole frames; audio_driver_stage_write() takes the rest, a frame
the wrap would cut or what has no room, into the stage's own buffer at
the offset the whole would have had, so a short threaded write's tail
is still where audio_driver_retain_output() looks for it, and writes
it. The upmix and binaural lend, which took one span or none, is this
now; a float stereo device's resampler output still goes through
write() at the wrap. The extras merge takes the first frame of the
span it works on.

lend_write_test gains a device whose ring wraps inside the span, of
whole frames and of a size that cuts a frame at its end: int16 and
float stereo, 5.1 float and int16, headphones float and int16, a core
sending its own 5.1, threaded and inline. Each stream must equal the
written one bit for bit, with write() never called where the ring
holds whole frames. On the previous audio_driver.c those cases call
write(); a merge that ignores the span's first frame fails the 5.1
core's.
2026-11-03 14:23:47 +01:00
LibretroAdmin d09c384983 Microphone: write the core's samples straight into the outgoing ring
Every microphone flush ended the same way: its last stage - the
narrowing of float samples, or the left channel of the integer
resampler's stereo - produced the samples in final_frames, and
retro_spsc_write() then copied them into the ring the core reads.

microphone_enqueue() takes the ring's span at the head, in the one
span or two its wrap leaves, and has the stage write the samples
there: the float fast path, the integer resampler's left channel and
the float resampler's narrowing all reach the ring without the stop in
between. final_frames is gone from the arena and from
microphone_driver_state_t; the resampler's output bound is the smaller
of the two buffers still on the way. An int16 device at its rate keeps
its single copy, from what the driver read.

The copy it saves on an 800-sample flush, a 60 Hz frame at 48 kHz:
0.03 us, and an intermediate buffer of the largest flush.

mic_handshake_test gains a lane that puts the ring's head at every
place it can be and enqueues runs of every length up to the whole
ring, with both fills: the ring must hold exactly what the stage used
to produce on its own, and report the samples it took. Writing only
the first span, or taking the right channel, fails it.
2026-11-03 14:03:47 +01:00
LibretroAdmin 1b7b9818fe Audio: lend the driver's buffer to the upmix and the binaural render
The lend of the driver's buffer stopped at stereo: on a wider device
the upmix produced the device frames in upmix_buf (upmix_i16 for an
int16 device) and write() copied them into the driver's buffer, and
with Headphone Virtual Surround on the binaural render did the same
into its own two-channel buffer.

audio_driver_write_frames() now borrows the span for the frames the
widening stage is about to produce - the upmix, its int16 form, the
narrowing of a float mix for an int16 device, or the binaural render -
and has the stage write them there, the core's own surround channels
merged in place as before, then publishes them. Inline and blocking it
waits for the room first, as write() would have; a span cut short, or
no room after the wait, leaves the stage writing its own buffer and
write() taking it as it always has.

The copy it saves, each 60 Hz flush into a 64 ms device: 0.28 us for
5.1 float, 0.13 us for 5.1 int16, 0.09 / 0.05 us on headphones - and
the pass over the stage's own buffer before it.

lend_write_test runs a 5.1 device and Headphone Virtual Surround,
float and int16, resampled and rate-matched, threaded and inline,
checks the stage under test is the one running, and compares the lent
stream bit for bit with the written one: write() is never called,
and a driver cutting spans short still gets the same stream through
write(). On the previous audio_driver.c the wider cases each call
write().
2026-11-03 14:03:44 +01:00
LibretroAdmin 1460bf0e49 CI: run the sinc variants and the paced lane's latencies at once
The slowest steps left in Linux-samples-audio:

- sinc fast-forward, 40 s: an ASan build and run, an AVX one, then a
  MinGW one under wine, one after another, each run on one thread.
  Each is now built to its own binary and the three run at once.
- sinc sibling, 20 s: its ASan and MinGW runs, likewise.
- the fill cap lane, 36 s: its audio_paced lane runs three device
  latencies of four settings each in real time. PACED_ONLY takes
  audio_paced_16, _32 or _64 for one latency, so the step runs the
  three beside the other four lanes; an unknown one is refused.

Measured on two cores: sinc fast-forward 40 -> 21-24 s, sinc sibling
20 -> 15 s, the fill cap lane 36 -> 24-27 s, each three runs in a row
or more without a failure.
2026-11-03 02:54:21 +01:00
LibretroAdmin 7050e1cda2 CI: run wasapi_exclusive_pacing's scenarios at once
wasapi_exclusive_pacing_test was the slowest step left in
Linux-samples-audio, 87 s: twelve scenarios of a fake device on the
wall clock, three seconds each and again with retries, then the clock
and bitstream cases - once under ASan and once under TSan, one after
another, on next to no CPU.

PACING_SCENARIO=<n> now runs scenario n alone, PACING_SCENARIO=rest the
cases after the scenarios, and PACING_SCENARIO=count prints how many
scenarios there are, so the step runs every one under both sanitizers
at once through run-parallel.sh and picks up a scenario added later on
its own. A number past the last one is refused rather than passing on
nothing. Unset, the test runs everything as before.

On two cores the runs take 8 s instead of 86 s, with no scenario
needing a retry in three runs in a row.
2026-11-03 02:28:57 +01:00
LibretroAdmin 342b224021 Samples: read the XAudio2 room after the lend, not before it
xaudio_lend_test failed on GitHub's runner with "a lend is within the
free room and the allocation". The test read write_avail() and then
called write_begin(), and checked the lend against the earlier
reading - but XAudio2's thread frees buffers whenever it finishes one,
so a lend taken a moment later can rightly find more room than was
read before it. A runner slow enough between the two calls failed
the check with the driver doing nothing wrong.

The room is now read right after the lend. Only the test's thread
takes room, so it can only have grown since the lend measured it, and
a lend past it is still a lend past the room. With a 3 ms sleep put
between the two calls, the old check fails on every run and the new
one passes.
2026-11-03 02:15:52 +01:00
LibretroAdmin 8360f25509 CI: run the slow audio samples' real-time lanes at once
Three steps of Linux-samples-audio took six minutes between them, and
two of them spent it waiting: pipeline_rate_control's eight runs and
pipeline_clocked's fill cap lanes are seconds of audio each against a
device that drains in real time, on almost no CPU, run one after
another.

.github/workflows/scripts/run-parallel.sh starts commands at once and,
when all are done, prints each one's output in order with a pass or
FAIL line, failing the step if any failed. The rate control step builds
its ASan and TSan binaries in turn and runs all eight at once; the fill
cap step runs its five lanes at once, pipeline_clocked_test taking
PACED_ONLY=<lane> to run one (PACED_ONLY=1 still runs them all).
discrete_multichannel_test is CPU-bound - most of it building Kaiser
tables - so it is now built at -O1 instead of -O0, and its plain and
ASan runs share the runner's cores.

Measured on two cores: pipeline_rate_control 162 -> 33 s of runs, the
fill cap lanes 116 -> 36 s, discrete_multichannel 87 -> 40 s. Each new
arrangement passed three times in a row there.
2026-11-03 01:59:17 +01:00
LibretroAdmin fff1632fac XAudio2: lend the frontend the buffers it plays
The XAudio2 driver had no write_begin / write_end, so every flush's
output was produced in the frontend's own buffer and then copied by
xa_write() into the buffers XAudio2 plays - the copy WASAPI, DirectSound
and the other lending drivers no longer make.

The driver's sixteen buffers are cut from one allocation, so the free
ones after the head are one span up to the allocation's end.
xa_write_begin() lends that span, never more than the free room, and
xa_write_end() moves the head over what was published, submitting each
buffer it fills. The span stops where the free room does, so every
buffer it fills has a slot and write_end never waits. A span cut short
at the allocation's end leaves the frontend's staged write to wrap, as
for the other drivers. The submit is shared with xa_write().

samples/audio/xaudio_lend runs the shipping driver on wine's XAudio2
(Windows' own on Windows): lends stay within the free room and the
allocation, lends and writes make one stream, write_end publishes all
it is given at once even filling the whole room, an abandoned lend
takes nothing, and lent audio plays at the pace written audio does.
Lending past the room hangs it; a write_end that does not move the
head fails it. Linux-samples-audio runs it.
2026-11-03 01:28:00 +01:00
LibretroAdmin 868cdc1718 Samples: stop three host-timing checks from flaking
Each failed on a loaded CI host while the code it checks was right.

asio_teardown judged whether the teardown wait ended as the last
callback left by a 100 ms wall-clock limit, and a busy host was late
to wake the waiter (170 ms seen). It now watches the driver's bounded
wait instead: a bound that runs out with no callback left inside is a
notify the wait slept through. The negative build, whose last callback
out does not notify, fails that way on every run however fast the host
is; a waiter that is merely woken late passes.

pipeline_clocked retried its shallow setpoint run when the host ran
the device dry or held the core, but not when the host took the
consumer off its core during the first second and drained it below the
setpoint, which then failed the startup check. That dip is retried as
well. Priming short of the setpoint does it in every run, and still
fails.

d3d11_turns' unfair core must be caught keeping the frontend waiting,
which only shows when the core is running on another CPU as the
frontend asks; on a host that had it off the CPU, the unfair core let
the frontend in at once and the must-fail run passed. An ask now
counts only when the core was seen taking turns just before it; a run
with too few such asks reports INCONCLUSIVE and is run again, and the
unfair core gets up to five runs to be caught.
2026-11-02 23:24:28 +01:00
LibretroAdmin bc198f7611 Audio: borrow the driver's buffer on the inline pipeline too
With the threaded pipeline off, the flush always resampled into
output_samples_buf (or narrowed into output_samples_int16) and write()
then copied the whole chunk into the driver's ring, even though every
driver with a ring in front of the device - pulse, pipewire, alsa,
coreaudio, wasapi, sdl2, aaudio, opensl, jack, dsound, wdmks - lends
that ring through write_begin/write_end. Only the threaded consumer
took the lend.

The inline flush now takes it as well. A blocking device is first
waited on for the chunk's room through wait_writable(), the wait
write() would otherwise have made piece by piece. When the driver
cannot lend the whole chunk - the span is cut short by the ring's
wrap, or the bounded wait found too little room - the lend is
abandoned and the staged write runs and blocks as before. That saves
one copy of each flush's output, about 6.4 KB a frame for float
stereo at 48 kHz and 60 fps; the resampler still dominates the
flush's cost.

The lend write sample runs its four cases inline as well: the lent
stream is bit for bit the written one, write() is not called for a
resampled chunk, and every begin is balanced by an end.
2026-11-02 23:24:19 +01:00
LibretroAdmin bcf1062b4e Threaded video and audio: keep each thread's per-frame writes on its own lines
In thread_video_t the main thread's per-push writes shared cache lines
with fields the video thread writes every frame: core_time with
miss_run, drain_cooldown and drain_pending; last_time with the lent
slots' busy flags; the handoff counts and the published pacing values
with repeat_request, repeat_group and margin_extra_us. In
audio_driver_state_t the pipe's consumer (pipe_gen, pipe_stalled, the
underrun and priming state, the source wait counts) and producer
(pipe_data_gen, the runloop snapshot, pipe_ctrl_avail, writer_held,
the fast-forward multiplier) were interleaved over the same four
lines. Each write by one thread took the line back from the other.

The main thread's fields now sit together behind a line of padding,
with the values it publishes on a line of their own, the way the
struct already separates its other shared fields. The audio pipe's
consumer and producer fields form two groups, each between full lines
of padding, under a new AUDIO_PIPE_LINE. A full line on each side
keeps the separation wherever the fields land, without relying on
the allocation's alignment. It costs 184 bytes in thread_video_t and
192 in audio_driver_state_t.

The threaded video sample's line separation lane and the audio
pipeline wakeups sample assert the new separations by offset, so a
field added above a pad cannot quietly put them back on one line.
2026-11-02 23:24:14 +01:00
LibretroAdmin b28b8a4b9f audio: menu silence behind a callback core writes only what fits
A core with an audio callback runs behind the threaded wrapper, which
keeps the device blocking whatever the menu asks. At Menu Frame Rate
'Display Rate' the menu feeds silence by the clock, so it now writes
only what the device takes without waiting, keeping half its buffer,
and does not make up time the device could not take. The recording
still receives every frame. The menu runs at the display's rate behind
such cores again.

samples/audio/menu_callback_feed holds the feed against a device that
counts every write that would have blocked.

Fixes #19971
2026-11-01 22:33:01 +01:00
LibretroAdmin 050acdd596 audio: drop a kept device before the next one is built
A kept device that does not match the next core's settings - a core
override of the audio latency, say - is closed before the pipeline is
rebuilt and before the next device opens, so it is released when the
next one may want the same device.

The threaded wrapper freed while parked now leaves from the park: it
no longer starts the device and runs one consumer pass on whatever
pipeline is current, nor reports a departed consumer or wakes it.
alive is published before the release from the park so the thread
sees it on the way out. Lane: pipeline_wakeups KEPT_DROP.
2026-11-01 21:14:06 +01:00
LibretroAdmin c74c00f5f8 asio: teardown parks on the last callback out
A callback may still be inside after ASIOStop, so teardown and the
reclaim of a parked instance waited for the in-callback count to reach
zero by yielding and then sleeping a millisecond at a time. The last
callback out now notifies an eventcount and the wait parks on it; the
200 ms deadline remains only for a driver whose callback never returns.

The eventcount is built for Win32 ASIO builds without threads too, as
it already is for DirectSound, XAudio and waveOut: the callback runs on
the driver's thread either way, so the write's park no longer falls
back to Sleep(1).

samples/audio/asio_teardown runs the driver's count and wait, pulled
out of asio.c, under TSan, ASan and wine; a build whose last callback
does not notify fails.
2026-11-01 00:11:58 +01:00
libretroadminandClaude Opus 5.5 689518cf6b Top a resumed blocking writer up to rate control's setpoint
A resume fills the drained device with silence before the core's first
audio. With a blocking writer it now fills only to rate control's
setpoint, half the buffer, instead of to full, where the writes after it
waited for the surplus to play out: a hitch of a frame or more on every
return from the menu, a state load or any other jump, which a VRR display
shows. A non-blocking writer keeps the full top-up as its cushion.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01LBQPGeAnyXTjoifK18NSfm
2026-10-31 14:06:30 +01:00
libretroadminandClaude Opus 5.5 591237dc48 Clear what fast-forward queued when it ends
Leaving fast-forward drops what it left in the threaded pipeline's ring
behind a jump fade, and the device's writer drops core audio until the
device has played down to rate control's setpoint, so a blocking writer
is no longer held for as long as the queue takes to play out. That held
the core in device-period steps for seconds after every fast-forward,
which a VRR display shows as judder until the menu is toggled (#19940),
and cost every other configuration a hitch on exit. The five-frame
non-blocking window Sync to Exact Content Framerate used for this is
removed; it never drained the queue.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01LBQPGeAnyXTjoifK18NSfm
2026-10-31 09:10:23 +01:00
LibretroAdmin 400d7ed4a3 audio: a format change no longer parks the audio thread
audio_driver_set_core_float() decides a source format change at once
and hands it to the threaded pipeline's consumer as a request word, to
install before its next pass; the producer writes nothing into the ring
until it has. A populated ring keeps its native format, as before, and
the producer converts its writes meanwhile. The stage a running or
suspended transport needs in the new format is made by the producer and
the old one handed back to it to free; one that cannot be made drops
the transport, keeping the new source native.

That was the last control to park the audio thread, so
audio_thread_apply_control() goes. Harnesses that parked the consumer
to look at its state stop and start the wrapper instead, and
thread_handshake checks audio_thread_parked() across its round trips.
pipeline_format hands its format change to a consumer pass and checks
nothing takes effect before it.
2026-10-30 09:42:01 +01:00
LibretroAdmin 8fcc656cb4 audio: transport changes no longer park the audio thread
Suspending, resuming, preparing and releasing the pitch-preserving
transport are decided on the producer's side, which keeps the transport
as it has decided it in pipe_transport_mode, and handed to the
consumer as a request word to install before its next pass. Nothing
waits and the device keeps running, so the speed changes that switch
the transport on and off no longer stop and start it.

The producer keeps writing meanwhile: the source in the ring is the
same whichever consumer takes it. A resume or a prepare still wants the
source drained, and puts its processing request into the metadata queue
behind what is already there rather than resetting the queue; the
consumer leaves it for a later pass while a short device write is
still to finish. A prepared stage is made by the producer, and the
replaced or released ones are handed back to it to free. With the
consumer parked the change is carried out in place, as before.

pipeline_wakeups' automatic transport lane checks the device is not
stopped across the suspension and the recovery.
2026-10-30 09:41:57 +01:00
LibretroAdmin 94d658724d audio: a rate change no longer parks the audio thread
audio_driver_set_input_rate() decides the change on the calling thread
and hands it to the threaded pipeline's consumer as a request word, as
the pause requests are, to install before its next pass. Until it has,
the producer writes nothing into the ring, so the ring, the slice and
the ratio are the consumer's alone and neither thread waits on the
other; the device is no longer stopped and started for it. With the
consumer parked, or gone, the caller installs the change itself.

A ring that has to grow is made by the caller and moved into by the
consumer with retro_spsc_grow_into(), which allocates nothing; the old
buffer and resamplers come back to the caller to free with the next
change or at teardown. The extra lanes of a multichannel core, which
used to refuse the change, are dropped with the front pair they share
tables with and remade at the new ratio by the next pass.

pipeline_rate_change hands each threaded change to a consumer pass and
checks nothing takes effect before it; pipeline_wakeups' kept-device
lane changes the rate under the real wrapper with the device running;
discrete_multichannel changes it with the extra lanes up; retro_spsc
moves a ring into a caller's buffer.
2026-10-30 09:41:53 +01:00
LibretroAdmin 0fc5e5356d audio: no state lock where the atomics have pointer operations
Without the threaded pipeline, the thread that submits the core's audio
holds the inline state: a submit takes inline_busy with a compare-and-
swap and records itself as the owner. The main thread's pause, resume,
format change and stop reset are published as request words, as the
threaded pipeline's already are, and carried out by that thread at its
next submit - before its gates, so a silenced core's thread writes its
pause tail - or at once by the main thread when it is the submitter, or
nothing has submitted yet. A callback core's thread settles them in its
loop. A submit that finds the main thread carrying a request out drops
its batch rather than wait. The core format the mixer follows is
published by the flush. audio_driver_sample_batch grows by the call
that settles requests on the submitting thread, and its hot-function
reference moves with it.

With that, every holder of the state lock is a pass that no other runs
beside, and the lock is only made where the atomics have no pointer
operations; elsewhere audio_driver_state_lock() counts the pass and
nothing more.

ff_speedup pauses a core that submits from its own thread and checks
the main thread writes no tail and that thread writes it at its next
submit.
2026-10-30 09:41:48 +01:00
LibretroAdmin 17244c53a2 audio: the consumer writes the pause tail, the main thread only asks
With the threaded pipeline a pause or resume no longer takes the state
lock or writes to the device from the main thread. It publishes its
discard as before and a request word, and the consumer, the only thread
that writes to the device, carries the requests out before its next
pass, oldest first: the period-matched tail and its mute for a pause,
the mute reset and top-up for a resume. A consumer parked for data
returns to do it, and a stop settles what is left, writing no tail into
the stopped device. Without the threaded pipeline the tail is still
written where the call is made, under the lock.

pipeline_wakeups' pause boundary checks the pause writes nothing from
the main thread, and waits for the consumer's tail before counting stale
audio.
2026-10-30 02:07:57 +01:00
LibretroAdmin e84db153cc audio: the DSP filter and the record tap swap without the state lock
Both are published with pointer atomics, and a holder of the state lock
- a pass - counts itself in and out of pass_seq. The main thread swaps
the DSP filter in or out without waiting for a pass: what it takes out
is freed at once when no pass runs, and otherwise once pass_seq has
moved on, at the next swap or the next frame. Readers outside a pass
see what the filter allows through dsp_caps. The record tap is
installed with a store after its begin and comes out with a
compare-and-swap; removal ends it once no pass that could have seen it
remains, and both still answer "not yet" while a pass runs. A backend
without pointer atomics changes them under the lock.

mixer_lock builds with the DSP filter, swaps and frees it while a pass
holds the state lock, and checks nothing is freed before the pass ends.
2026-10-30 00:43:42 +01:00