src/play-clock.ts (722 bytes)
1 // What a media player said about its position, and when it said it. Pure. 2 // 3 // A player does not report each millisecond. It reports a position, the 4 // time of the report, and the speed. The position now is computed from 5 // these three. This is a port of PlayClock.kt from SubRead Overlay. 6 7 export interface PlayClock { 8 positionMs: number; 9 /** The time of the report, on the same clock as the `nowMs` parameters. */ 10 reportedAtMs: number; 11 speed: number; 12 playing: boolean; 13 } 14 15 /** The position in the media at `nowMs`. */ 16 export function positionAt(clock: PlayClock, nowMs: number): number { 17 if (!clock.playing) return clock.positionMs; 18 return clock.positionMs + (nowMs - clock.reportedAtMs) * clock.speed; 19 }