Skip to content

Offline bounce: read seekable clips at random access, not through the real-time rings - #43

Open
ethanrxla wants to merge 1 commit into
maolan:mainfrom
ethanrxla:offline-bounce-random-access
Open

ethanrxla wants to merge 1 commit into
maolan:mainfrom
ethanrxla:offline-bounce-random-access

Conversation

@ethanrxla

Copy link
Copy Markdown

Problem

An offline render (track bounce, stems) runs the same clip path as live playback, so a streamed clip is popped from
its producer rings, and those rings fill with silence whenever the producer thread falls behind. An offline render has
no deadline, so it should never drop audio, but its output depends on disk and CPU timing.

Measured on 0.4.29 with the ALSA null device and a 48 kHz WAV clip (a seekable stream), 3 runs of 24 checks each:

  • idle host: about 1 in 3 first bounces had gaps of 0.5 to 4.8 s, and every repeat bounce in a session lost at least
    256 frames;
  • host under load (a concurrent build): whole bounces came out silent.

Change

  • Track gets an offline_random_access flag. While it is set, a SeekableStreaming clip (PCM WAV at the engine
    rate) is read through the existing clip_sample_window random-access read, which blocks until it has exactly the
    requested frames, instead of the rings. The read is factored into TrackData::read_clip_window_offline.
  • The bounce and stems workers set the flag on the tracks they render through a small guard, OfflineClipReads, which
    clears it when dropped. However the render ends (finished, cancelled or failed), live playback goes back to the
    rings.
  • The bounce and stems handlers no longer send Ready themselves. process_offline_bounce and
    process_offline_stems_bounce now call the render (render_offline_bounce, render_offline_stems_bounce) and
    send Ready once it has returned. The engine starts a queued render as soon as it sees Ready, possibly on another
    worker over the same tracks. If Ready went out first, the first render's guard could clear the flag after the
    next render had set it.
  • Compressed and foreign-rate clips (Streaming) still use the rings in offline renders.
    StreamingClipBuffer::read_window_into decodes from the start of the file on every call and returns frames at the
    file's own rate, so calling it once per block would be quadratic and off-rate. I've opened a separate issue for
    that case.

Tests

  • New offline_random_access_is_exact_from_the_first_block_at_a_clip_offset in seekable_streaming.rs: a WAV clip
    with offset = 1000 and the flag set gives four frame-exact blocks with no wait for the producer. Without the
    change it fails on block 0 because the rings are still filling.
  • process_offline_bounce_cancels_and_restores_track_state and process_offline_bounce_emits_progress_and_completion
    now also assert that the flag is cleared afterwards.
  • New offline_render_releases_clip_reads_before_the_worker_is_returned: the render returns without sending Ready,
    with the flag already cleared, and Ready follows.
  • cargo test --features audio,midi,plugins: 528 passed, 1 ignored. cargo fmt and cargo clippy --all-targets are
    clean for the change. One clippy warning remains in src/midi/mtc.rs, which this change doesn't touch.
  • An earlier version of this change used the same read path but never cleared the flag. With it, the bounce checks
    above passed 24 of 24 in 3 runs, both under load and on an idle host.

Notes for review

  • While the flag is set, a live process() on the same track would take the blocking read too. The bounce already
    locks the live tracks and changes their transport position, playback state and level, so bouncing during playback
    isn't safe today either. Separating offline renders from live track state is a bigger change that I haven't
    attempted here.
  • If you'd rather the flag weren't a public field on Track, I'm happy to move it.

Prepared with the help of an AI coding assistant (Claude Code). The tests and measurements above were run, not
inferred.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants