Skip to content

🎥 Make videos programmatically with React

okaay

Jonny Burger2fe100d7e2a4
diff --git a/packages/core/src/audio/shared-audio-tags.tsx b/packages/core/src/audio/shared-audio-tags.tsx
index f086f923d8..0e73730fac 100644
--- a/packages/core/src/audio/shared-audio-tags.tsx
+++ b/packages/core/src/audio/shared-audio-tags.tsx
@@ -45,10 +45,9 @@ const EMPTY_AUDIO =
export type ScheduleAudioNodeOptions = {
node: AudioBufferSourceNode;
mediaTimestamp: number;
- currentMediaTime: number;
- combinedPlaybackRate: number;
- maxDuration: number | null;
- bufferOffset: number;
+ delay: number;
+ trimBefore: number;
+ duration: number | null;
};
type SharedContext = {
@@ -163,45 +162,22 @@ export const SharedAudioContextProvider: React.FC<{
return ({
node,
mediaTimestamp,
- currentMediaTime,
- combinedPlaybackRate,
- maxDuration,
- bufferOffset,
+ delay,
+ trimBefore,
+ duration,
}: ScheduleAudioNodeOptions): boolean => {
- const delayWithoutPlaybackRate = mediaTimestamp - currentMediaTime;
- const delay = delayWithoutPlaybackRate / combinedPlaybackRate;
-
- let startAt: number;
- let duration: number;
-
- if (delay >= 0) {
- startAt = audioContext.currentTime + delay;
- duration = maxDuration ?? (node.buffer?.duration ?? 0) - bufferOffset;
- node.start(startAt, bufferOffset, maxDuration ?? undefined);
- } else {
- const mediaOffset = -delayWithoutPlaybackRate;
- if (maxDuration !== null && maxDuration - mediaOffset <= 0) {
- return false;
- }
-
- const totalOffset = bufferOffset + mediaOffset;
-
- startAt = audioContext.currentTime;
- duration =
- maxDuration !== null
- ? maxDuration - mediaOffset
- : (node.buffer?.duration ?? 0) - totalOffset;
- node.start(
- startAt,
- totalOffset,
- maxDuration !== null ? maxDuration - mediaOffset : undefined,
- );
+ if (duration !== null && duration <= 0) {
+ return false;
}
- const end = startAt + duration;
- const mediaDuration =
- maxDuration ?? (node.buffer?.duration ?? 0) - bufferOffset;
- const mediaEnd = mediaTimestamp + mediaDuration;
+ const startAt =
+ delay >= 0 ? audioContext.currentTime + delay : audioContext.currentTime;
+ const effectiveDuration =
+ duration ?? (node.buffer?.duration ?? 0) - trimBefore;
+ node.start(startAt, trimBefore, duration ?? undefined);
+
+ const end = startAt + effectiveDuration;
+ const mediaEnd = mediaTimestamp + effectiveDuration;
const startGap =
lastScheduledEnd !== null &&
@@ -215,7 +191,7 @@ export const SharedAudioContextProvider: React.FC<{
// eslint-disable-next-line no-console
console.log(
- `[audio-schedule] start=%c${startAt.toFixed(4)}%c dur=${duration.toFixed(4)} end=${end.toFixed(4)} mediaStart=%c${mediaTimestamp.toFixed(4)}%c mediaEnd=${mediaEnd.toFixed(4)}`,
+ `[audio-schedule] start=%c${startAt.toFixed(4)}%c dur=${effectiveDuration.toFixed(4)} end=${end.toFixed(4)} mediaStart=%c${mediaTimestamp.toFixed(4)}%c mediaEnd=${mediaEnd.toFixed(4)}`,
startGap ? red : normal,
normal,
mediaGap ? red : normal,
diff --git a/packages/media/src/audio-iterator-manager.ts b/packages/media/src/audio-iterator-manager.ts
index 3413ecf98e..1f840d7ec9 100644
--- a/packages/media/src/audio-iterator-manager.ts
+++ b/packages/media/src/audio-iterator-manager.ts
@@ -82,6 +82,7 @@ export const audioIteratorManager = ({
maxDuration,
bufferOffset,
);
+
if (!started) {
node.disconnect();
return;
diff --git a/packages/media/src/media-player.ts b/packages/media/src/media-player.ts
index 29cdb0fb51..a2fb5c647b 100644
--- a/packages/media/src/media-player.ts
+++ b/packages/media/src/media-player.ts
@@ -306,27 +306,7 @@ export class MediaPlayer {
playbackRate: this.playbackRate * this.globalPlaybackRate,
startFromSecond: startTime,
getIsPlaying: () => this.playing,
- scheduleAudioNode: (
- node,
- mediaTimestamp,
- maxDuration,
- bufferOffset,
- ) => {
- if (!this.sharedAudioContext) {
- throw new Error('Shared audio context not found');
- }
-
- const currentTime = this.getAudioPlaybackTime();
- return this.sharedAudioContext.scheduleAudioNode({
- node,
- mediaTimestamp,
- currentMediaTime: currentTime,
- combinedPlaybackRate:
- this.playbackRate * this.globalPlaybackRate,
- maxDuration,
- bufferOffset,
- });
- },
+ scheduleAudioNode: this.scheduleAudioNode,
})
: Promise.resolve(),
this.videoIteratorManager
@@ -408,27 +388,7 @@ export class MediaPlayer {
nonce,
playbackRate: this.playbackRate * this.globalPlaybackRate,
getIsPlaying: () => this.playing,
- scheduleAudioNode: (
- node,
- mediaTimestamp,
- maxDuration,
- bufferOffset,
- ) => {
- if (!this.sharedAudioContext) {
- throw new Error('Shared audio context not found');
- }
-
- const currentTime = this.getAudioPlaybackTime();
- return this.sharedAudioContext.scheduleAudioNode({
- node,
- mediaTimestamp,
- currentMediaTime: currentTime,
- combinedPlaybackRate:
- this.playbackRate * this.globalPlaybackRate,
- maxDuration,
- bufferOffset,
- });
- },
+ scheduleAudioNode: this.scheduleAudioNode,
})
: null,
]);
@@ -450,26 +410,7 @@ export class MediaPlayer {
if (this.audioIteratorManager) {
this.audioIteratorManager.resumeScheduledAudioChunks({
playbackRate: this.playbackRate * this.globalPlaybackRate,
- scheduleAudioNode: (
- node,
- mediaTimestamp,
- maxDuration,
- bufferOffset,
- ) => {
- if (!this.sharedAudioContext) {
- throw new Error('Shared audio context not found');
- }
-
- const currentTime = this.getAudioPlaybackTime();
- return this.sharedAudioContext.scheduleAudioNode({
- node,
- mediaTimestamp,
- currentMediaTime: currentTime,
- combinedPlaybackRate: this.playbackRate * this.globalPlaybackRate,
- maxDuration,
- bufferOffset,
- });
- },
+ scheduleAudioNode: this.scheduleAudioNode,
});
}
@@ -609,26 +550,7 @@ export class MediaPlayer {
if (this.playing) {
this.audioIteratorManager.resumeScheduledAudioChunks({
playbackRate: this.playbackRate * this.globalPlaybackRate,
- scheduleAudioNode: (
- node,
- mediaTimestamp,
- maxDuration,
- bufferOffset,
- ) => {
- if (!this.sharedAudioContext) {
- throw new Error('Shared audio context not found');
- }
-
- const currentTime = this.getAudioPlaybackTime();
- return this.sharedAudioContext.scheduleAudioNode({
- node,
- mediaTimestamp,
- currentMediaTime: currentTime,
- combinedPlaybackRate: this.playbackRate * this.globalPlaybackRate,
- maxDuration,
- bufferOffset,
- });
- },
+ scheduleAudioNode: this.scheduleAudioNode,
});
}
}
@@ -694,6 +616,33 @@ export class MediaPlayer {
this.input.dispose();
}
+ private scheduleAudioNode = (
+ node: AudioBufferSourceNode,
+ mediaTimestamp: number,
+ maxDuration: number | null,
+ bufferOffset: number,
+ ): boolean => {
+ if (!this.sharedAudioContext) {
+ throw new Error('Shared audio context not found');
+ }
+
+ const currentTime = this.getAudioPlaybackTime();
+ const combinedPlaybackRate = this.playbackRate * this.globalPlaybackRate;
+ const delayWithoutPlaybackRate = mediaTimestamp - currentTime;
+ const delay = delayWithoutPlaybackRate / combinedPlaybackRate;
+ const mediaOffset = Math.max(0, -delayWithoutPlaybackRate);
+ const trimBefore = bufferOffset + mediaOffset;
+ const duration =
+ maxDuration !== null ? maxDuration - mediaOffset : null;
+ return this.sharedAudioContext.scheduleAudioNode({
+ node,
+ mediaTimestamp,
+ delay,
+ trimBefore,
+ duration,
+ });
+ };
+
private getAudioPlaybackTime(): number {
if (!this.sharedAudioContext) {
throw new Error('Shared audio context not found');
diff --git a/packages/media/src/shared-audio-context-for-media-player.ts b/packages/media/src/shared-audio-context-for-media-player.ts
index ed59e03351..2342efcc26 100644
--- a/packages/media/src/shared-audio-context-for-media-player.ts
+++ b/packages/media/src/shared-audio-context-for-media-player.ts
@@ -1,10 +1,9 @@
export type ScheduleAudioNodeOptions = {
node: AudioBufferSourceNode;
mediaTimestamp: number;
- currentMediaTime: number;
- combinedPlaybackRate: number;
- maxDuration: number | null;
- bufferOffset: number;
+ delay: number;
+ trimBefore: number;
+ duration: number | null;
};
export type SharedAudioContextForMediaPlayer = {