Repository navigation
fix(core): preview playback moves every frame between an audio track's time updates #7373
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
+228
−3
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,108 @@ | ||
| import { afterEach, beforeEach, describe, expect, it, vi } from "vitest"; | ||
| import { TransportClock } from "./clock"; | ||
| import { syncRuntimeMedia, type RuntimeMediaClip } from "./media"; | ||
|
|
||
| // Chromium moves an element's currentTime in steps; the element plays at whatever rate it is given. | ||
| function steppingAudio(stepMs: number) { | ||
| const el = document.createElement("audio"); | ||
| document.body.appendChild(el); | ||
| let position = 0; | ||
| let rate = 1; | ||
| let rateWrites = 0; | ||
| let seeks = 0; | ||
| Object.defineProperties(el, { | ||
| paused: { configurable: true, get: () => false }, | ||
| seeking: { configurable: true, get: () => false }, | ||
| readyState: { configurable: true, get: () => 4 }, | ||
| duration: { configurable: true, get: () => 60 }, | ||
| playbackRate: { | ||
| configurable: true, | ||
| get: () => rate, | ||
| set: (v: number) => { | ||
| rateWrites += 1; | ||
| rate = v; | ||
| }, | ||
| }, | ||
| currentTime: { | ||
| configurable: true, | ||
| get: () => (Math.floor((position * 1000) / stepMs) * stepMs) / 1000, | ||
| set: (v: number) => { | ||
| seeks += 1; | ||
| position = v; | ||
| }, | ||
| }, | ||
| }); | ||
| el.play = vi.fn(() => Promise.resolve()); | ||
| el.pause = vi.fn(); | ||
| return { | ||
| el, | ||
| advance: (dtMs: number) => (position += (dtMs / 1000) * rate), | ||
| rateWrites: () => rateWrites, | ||
| seeks: () => seeks, | ||
| }; | ||
| } | ||
|
|
||
| /** Plays one audio clip the clock follows for `seconds` of `hz` display frames, syncing media as the runtime does. */ | ||
| function play(stepMs: number, hz: number, seconds = 10) { | ||
| let ms = 0; | ||
| const audio = steppingAudio(stepMs); | ||
| const clock = new TransportClock({ nowMs: () => ms, duration: 60 }); | ||
| const clip: RuntimeMediaClip = { | ||
| el: audio.el, | ||
| start: 0, | ||
| mediaStart: 0, | ||
| duration: 60, | ||
| end: 60, | ||
| volume: null, | ||
| playbackRate: 1, | ||
| loop: false, | ||
| sourceDuration: null, | ||
| }; | ||
| clock.play(); | ||
| const tickMs = 1000 / hz; | ||
| for (let tick = 1; tick <= seconds * hz; tick++) { | ||
| ms = tick * tickMs; | ||
| audio.advance(tickMs); | ||
| clock.attachAudioSource({ el: audio.el, compositionStart: 0, mediaStart: 0 }); | ||
| syncRuntimeMedia({ | ||
| clips: [clip], | ||
| timeSeconds: clock.now(), | ||
| playing: true, | ||
| playbackRate: 1, | ||
| clockAhead: (el) => clock.aheadOf(el), | ||
| getCompositionDuration: () => 60, | ||
| }); | ||
| } | ||
| return { rateWrites: audio.rateWrites(), seeks: audio.seeks(), filmSpeed: clock.now() / seconds }; | ||
| } | ||
|
|
||
| describe("a clock that runs between the steps of the audio it follows, as Chromium reports it", () => { | ||
| beforeEach(() => { | ||
| vi.spyOn(navigator, "userAgent", "get").mockReturnValue( | ||
| "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/140.0.0.0 Safari/537.36", | ||
| ); | ||
| }); | ||
|
|
||
| afterEach(() => { | ||
| vi.restoreAllMocks(); | ||
| document.body.innerHTML = ""; | ||
| }); | ||
|
|
||
| it("does not steer that audio against its own lead, so the film keeps its speed", () => { | ||
| for (const [stepMs, hz] of [ | ||
| [45, 60], | ||
| [50, 144], | ||
| [60, 60], | ||
| [70, 60], | ||
| [90, 120], | ||
| ]) { | ||
| const run = play(stepMs, hz); | ||
| expect(run.rateWrites, `${stepMs} ms steps at ${hz} Hz: rate writes`).toBe(0); | ||
| expect(run.seeks, `${stepMs} ms steps at ${hz} Hz: seeks`).toBe(0); | ||
| expect( | ||
| Math.abs(run.filmSpeed - 1), | ||
| `${stepMs} ms steps at ${hz} Hz: film speed`, | ||
| ).toBeLessThan(0.001); | ||
| } | ||
| }); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
blocker: this clock now runs up to one step ahead of the element it follows, but
syncRuntimeMediastill rate-steers that element against the clock. Each frame,media.ts:682computesoffset = relTime − el.currentTime, which is now up to one full step. When it passesMEDIA_SYNC_TOLERANCE_SECONDS(40 ms),steerMedia(mediaSteer.ts:55) sets the followed<audio>to 1.03x. That speeds up the element the band is anchored to, so the playhead speeds up with it. On main the offset is about 0, because the clock is the element's time.To check this, I ran a throwaway vitest probe: this
TransportClockplus the realsteerMedia, with the element's position moving at its writtenplaybackRateandcurrentTimefloored to N ms steps. Over 10 s of wall time:Main gives 0 steered ticks and 1.0000x in every case. At 20/30 ms steps, and at 40/50 ms on 60 Hz, the PR is effectively clean, because the lead stays under 40 ms. That matches your manual table, which measured frame cadence but not the element's
playbackRate. The body says steps are "30-50 ms or more on many machines", so this is the population the fix targets. They would get audible 1.0↔1.03 rate flips (up to ~40 writes/s at 144 Hz) and a film running 0.5–1.5% fast.A possible fix: don't steer the clock's own element (
clock.audioElement()) by its offset against the clock. Or give media sync the raw element time for that element. Then add a test that runs the clock andsteerMediatogether, so the two can't drift apart again.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
✅ Fixed in 8c55d88:
clockAheadtakes the lead off for the followed element only.clock.steer.test.tsfails against the oldmedia.ts(45 ms @ 60 Hz: 69 rate writes) and passes now.