* Start a buffer source, bounding it to the clip's authored window * (`data-duration`) so a trimmed clip stops at its edge instead of running the * buffer to the source file's natural end. `clipSourceLen` is the clip span in * buffer seconds; the third `start()` arg is the portion to play from th
(
node: AudioBufferSourceNode,
opts: {
elapsed: number;
mediaStart: number;
scheduledAt: number;
safeRate: number;
clipDuration: number;
},
)
| 31 | * play); the caller should discard the source. |
| 32 | */ |
| 33 | function startBoundedSource( |
| 34 | node: AudioBufferSourceNode, |
| 35 | opts: { |
| 36 | elapsed: number; |
| 37 | mediaStart: number; |
| 38 | scheduledAt: number; |
| 39 | safeRate: number; |
| 40 | clipDuration: number; |
| 41 | }, |
| 42 | ): boolean { |
| 43 | const { elapsed, mediaStart, scheduledAt, safeRate, clipDuration } = opts; |
| 44 | const hasBound = Number.isFinite(clipDuration) && clipDuration > 0; |
| 45 | const clipSourceLen = clipDuration * safeRate; |
| 46 | if (elapsed >= 0) { |
| 47 | const remaining = clipSourceLen - elapsed; |
| 48 | if (hasBound && remaining <= 0) return false; |
| 49 | if (hasBound) node.start(0, elapsed + mediaStart, remaining); |
| 50 | else node.start(0, elapsed + mediaStart); |
| 51 | return true; |
| 52 | } |
| 53 | const delay = -elapsed / safeRate; |
| 54 | if (hasBound) node.start(scheduledAt + delay, mediaStart, clipSourceLen); |
| 55 | else node.start(scheduledAt + delay, mediaStart); |
| 56 | return true; |
| 57 | } |
| 58 | |
| 59 | export type ScheduledSource = { |
| 60 | el: HTMLMediaElement; |