mirror of
https://github.com/OpenCut-app/OpenCut.git
synced 2026-07-13 21:52:53 +02:00
fix(audio): stabilize playback startup when timeline selection is heavy
This commit is contained in:
@@ -10,4 +10,6 @@ changes:
|
|||||||
text: "Effects system (with our first effect: Blur!)"
|
text: "Effects system (with our first effect: Blur!)"
|
||||||
- type: fixed
|
- type: fixed
|
||||||
text: "Fixed an issue where click-and-drag selection on one timeline track could accidentally select items from the track below."
|
text: "Fixed an issue where click-and-drag selection on one timeline track could accidentally select items from the track below."
|
||||||
|
- type: fixed
|
||||||
|
text: "Audio could flicker or cut out at the start of playback when certain elements were selected. The scheduler now recovers from timing slips without losing audio."
|
||||||
---
|
---
|
||||||
|
|||||||
@@ -29,6 +29,7 @@ export class AudioManager {
|
|||||||
private playbackSessionId = 0;
|
private playbackSessionId = 0;
|
||||||
private lastIsPlaying = false;
|
private lastIsPlaying = false;
|
||||||
private lastVolume = 1;
|
private lastVolume = 1;
|
||||||
|
private playbackLatencyCompensationSeconds = 0;
|
||||||
private unsubscribers: Array<() => void> = [];
|
private unsubscribers: Array<() => void> = [];
|
||||||
|
|
||||||
constructor(private editor: EditorCore) {
|
constructor(private editor: EditorCore) {
|
||||||
@@ -136,6 +137,7 @@ export class AudioManager {
|
|||||||
|
|
||||||
this.stopPlayback();
|
this.stopPlayback();
|
||||||
this.playbackSessionId++;
|
this.playbackSessionId++;
|
||||||
|
this.playbackLatencyCompensationSeconds = 0;
|
||||||
|
|
||||||
const tracks = this.editor.timeline.getTracks();
|
const tracks = this.editor.timeline.getTracks();
|
||||||
const mediaAssets = this.editor.media.getAssets();
|
const mediaAssets = this.editor.media.getAssets();
|
||||||
@@ -224,13 +226,21 @@ export class AudioManager {
|
|||||||
|
|
||||||
const clipStart = clip.startTime;
|
const clipStart = clip.startTime;
|
||||||
const clipEnd = clip.startTime + clip.duration;
|
const clipEnd = clip.startTime + clip.duration;
|
||||||
|
const playbackTimeAfterSinkReady = this.getPlaybackTime();
|
||||||
const iteratorStartTime = Math.max(startTime, clipStart);
|
const iteratorStartTime = Math.max(
|
||||||
|
startTime,
|
||||||
|
clipStart,
|
||||||
|
playbackTimeAfterSinkReady,
|
||||||
|
);
|
||||||
|
if (iteratorStartTime >= clipEnd) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
const sourceStartTime =
|
const sourceStartTime =
|
||||||
clip.trimStart + (iteratorStartTime - clip.startTime);
|
clip.trimStart + (iteratorStartTime - clip.startTime);
|
||||||
|
|
||||||
const iterator = sink.buffers(sourceStartTime);
|
const iterator = sink.buffers(sourceStartTime);
|
||||||
this.clipIterators.set(clip.id, iterator);
|
this.clipIterators.set(clip.id, iterator);
|
||||||
|
let consecutiveDroppedBufferCount = 0;
|
||||||
|
|
||||||
for await (const { buffer, timestamp } of iterator) {
|
for await (const { buffer, timestamp } of iterator) {
|
||||||
if (!this.editor.playback.getIsPlaying()) return;
|
if (!this.editor.playback.getIsPlaying()) return;
|
||||||
@@ -244,15 +254,41 @@ export class AudioManager {
|
|||||||
node.connect(this.masterGain ?? audioContext.destination);
|
node.connect(this.masterGain ?? audioContext.destination);
|
||||||
|
|
||||||
const startTimestamp =
|
const startTimestamp =
|
||||||
this.playbackStartContextTime + (timelineTime - this.playbackStartTime);
|
this.playbackStartContextTime +
|
||||||
|
this.playbackLatencyCompensationSeconds +
|
||||||
|
(timelineTime - this.playbackStartTime);
|
||||||
|
|
||||||
if (startTimestamp >= audioContext.currentTime) {
|
if (startTimestamp >= audioContext.currentTime) {
|
||||||
node.start(startTimestamp);
|
node.start(startTimestamp);
|
||||||
|
consecutiveDroppedBufferCount = 0;
|
||||||
} else {
|
} else {
|
||||||
const offset = audioContext.currentTime - startTimestamp;
|
const offset = audioContext.currentTime - startTimestamp;
|
||||||
if (offset < buffer.duration) {
|
if (offset < buffer.duration) {
|
||||||
node.start(audioContext.currentTime, offset);
|
node.start(audioContext.currentTime, offset);
|
||||||
|
consecutiveDroppedBufferCount = 0;
|
||||||
} else {
|
} else {
|
||||||
|
consecutiveDroppedBufferCount += 1;
|
||||||
|
if (consecutiveDroppedBufferCount >= 5) {
|
||||||
|
const nextCompensationSeconds = Math.max(
|
||||||
|
this.playbackLatencyCompensationSeconds,
|
||||||
|
Math.min(0.25, offset + 0.01),
|
||||||
|
);
|
||||||
|
if (
|
||||||
|
nextCompensationSeconds >
|
||||||
|
this.playbackLatencyCompensationSeconds + 0.001
|
||||||
|
) {
|
||||||
|
this.playbackLatencyCompensationSeconds =
|
||||||
|
nextCompensationSeconds;
|
||||||
|
}
|
||||||
|
const resyncStartTime = this.getPlaybackTime();
|
||||||
|
this.clipIterators.delete(clip.id);
|
||||||
|
void this.runClipIterator({
|
||||||
|
clip,
|
||||||
|
startTime: resyncStartTime,
|
||||||
|
sessionId,
|
||||||
|
});
|
||||||
|
return;
|
||||||
|
}
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user