Skip to content

Studio: write transcoded WAV blocks raw and fix the header once on close - #15917

Merged
danielhanchen merged 1 commit into
mainfrom
perf/r9-wav-writeraw
Oct 29, 2026
Merged

danielhanchen merged 1 commit into
mainfrom
perf/r9-wav-writeraw

Conversation

@danielhanchen

Copy link
Copy Markdown
Member

What changes

transcode() writes each resampled block (about 1024 samples) with wave.Wave_write.writeframes. In CPython that is writeframesraw followed by _patchheader() whenever the header's data length is stale, which on a stream of unknown length is every block: seek to the header, rewrite the RIFF and data sizes, seek back. This switches to writeframesraw; Wave_write.close(), which transcode() already calls before os.replace publishes the file, patches the header once with the final sizes. Per the Python wave docs, writeframesraw writes frames "without correcting nframes", and writeframes writes them "and make[s] sure nframes is correct"; close() makes sure nframes is correct. The output file is a regular seekable temp file, which header patching requires. Python 3.9 and 3.14 have the same writeframes and close logic.

Decoding, resampling, clipping, channel selection, the 413 cut-off and the published bytes are unchanged.

Results

Parity: 17 inputs (WAV s16/u8/f32, MP3, FLAC incl. 4 channel, Ogg Vorbis, AAC 5.1, MP4 and WebM video, 8 kHz to 96 kHz, 10 ms to 30 min, truncated, empty, no audio) x 6 option sets (default, 24 kHz mono, forced stereo, 16 kHz mono cut at 5 s, refused past 5 s, 8 kHz cut at 12.3 ms): 120 results, identical info dicts, errors and output SHA-256 against main. The save_from_media matrix (340 requests) is also identical.

Header rewrites (_patchheader calls) for the 102 small-input transcodes: 54001 on main, 68 here. For the 18 long-input transcodes: 297333 on main, 15 here.

Wall time, transcode() default options, median (main run twice for noise):

Source main main again this PR
30 min AAC, 48 kHz stereo (3 runs) 2580 ms 2521 ms 2074 ms
5 min MP3 (5 runs) 291 ms 284 ms 235 ms
60 s MP4 video (5 runs) 93 ms 97 ms 83 ms

This applies to every upload, voice reference, transcription input and media cut that goes through transcode().

Tests

  • New test_transcode_writes_the_wav_header_sizes_once_at_close: one header patch with the final data size, correct frame count and info. Fails on main (one patch per block), passes here.
  • test_audio_inputs.py, test_account_media_isolation.py, test_audio_voices.py, test_audio_edit_route.py, test_audio_edit_requests.py, test_audio_run_extras.py, test_video_gallery.py, test_audio_cpp_clone_requests.py, test_audio_cpp_convert_requests.py: 258 passed on main, 259 passed here, no failures on either side.
  • Compiles on Python 3.9 through 3.14.

Not covered

Measured on Linux only; the saving per block is a seek pair and a small write, so it should be larger on Windows file systems, but that was not run.

@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Oct 29, 2026 •

Copy link
Copy Markdown

Codex Review Summary

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-10-29T09:25:34.408481Z af758b4 Manual request
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. 🎉

Reviewed commit: af758b49ec

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

@danielhanchen
danielhanchen merged commit dfd90e6 into main Oct 29, 2026
39 of 50 checks passed
@danielhanchen
danielhanchen deleted the perf/r9-wav-writeraw branch October 29, 2026 10:15
danielhanchen added a commit that referenced this pull request Oct 29, 2026
…s in flight (#15953)

## Summary

`Chat UI Tests (extra)` fails now and then in
`tests/studio/playwright_ui_font_scale.py`, on PRs that do not touch the
UI (#15917, #15859):

```
[font-scale] FAIL in step 'default restores exactly' after 0.3s:
default drifted for navFont: 13.5938 -> 18.125
```

18.125 is the nav font at UI size 20 (13.5938 x 20 / 15), so a size the
test had left two steps earlier came back after it set the default. The
test is right; this is a real product race in `usePersonalizationSync`:

1. On load, the hook GETs `/api/settings/personalization` and, when the
answer arrives, `replaceAll`s the appearance store with the server copy.
2. Saves are debounced by 800 ms, so a size changed shortly before a
navigation is in localStorage but not on the server. In the run above,
20 was saved, then 12 was set and the page reloaded inside the debounce.
3. After the reload the user (here, the test) sets the size again. If
the GET resolves after that, the server's stale 20 replaces the value
they just picked.

A user who changes a setting in the first moments after opening Studio
can see it snap back the same way.

## What changed

- `use-personalization-sync.ts` notes the appearance customization when
the load starts. If the store holds a different object when the server
answers, the user edited during the request, so local wins: the store is
not overwritten, and because the baseline records the server's values,
the normal debounced push uploads the edit.
- Without an edit in flight, nothing changes: a saved server
customization still replaces the local one.

## Verification

- Two tests in
`studio/frontend/tests/personalization-locale-hydration.test.ts`, using
its existing hook harness with the GET held open:
- an edit made while the request is in flight survives and is the value
that gets saved (fails on main: the size becomes the server's 20)
  - with no edit in flight, the server customization still wins
- The whole file: 14 pass. Against the unfixed hook, only the in-flight
test fails.
- `npm run typecheck` passes and eslint is clean on both files. Biome
reports the same 2 errors as on main, all in lines this change does not
touch.

The fix changes when a setting is kept, not anything drawn, so there is
nothing to screenshot. The Playwright font-scale check in `Chat UI Tests
(extra)` is the end-to-end cover.
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.

1 participant