Skip to content

Record your screen, ship a demo. Free and open-source, GPU-accelerated, no watermarks, no subscriptions. Windows, macOS, Linux. Actively maintained.

refactor(editor): three imported-audio cleanups

Benjamin Freeman2fadb52913cd
diff --git a/src/components/ai-edition/v4/V4Timeline.tsx b/src/components/ai-edition/v4/V4Timeline.tsx
index 037d959ec..136b241b8 100644
--- a/src/components/ai-edition/v4/V4Timeline.tsx
+++ b/src/components/ai-edition/v4/V4Timeline.tsx
@@ -355,6 +355,7 @@ const AudioLanePill = memo(function AudioLanePill({
onStartDrag,
onSelect,
label,
+ outputGain,
}: {
track: AxcutAudioTrack;
url: string | undefined;
@@ -365,6 +366,9 @@ const AudioLanePill = memo(function AudioLanePill({
onStartDrag: (e: ReactPointerEvent, track: AxcutAudioTrack, mode: "move" | "l" | "r") => void;
onSelect: (id: string) => void;
label: string;
+ /** Linear project output gain, applied on top of the track gain — the mixer
+ * applies both, so the bars must too or they under-read the exported level. */
+ outputGain: number;
}) {
const duration = assetDurationSec ?? track.durationSec;
return (
@@ -395,7 +399,9 @@ const AudioLanePill = memo(function AudioLanePill({
assetDurationSec={duration}
sourceStartSec={track.trimStartSec}
sourceEndSec={track.trimEndSec ?? duration}
- gain={audioGainScalar(track.gainDb)}
+ // Track gain AND the project output gain — `finish_audio` applies both and
+ // clamps, so scaling by the track gain alone under-read a boosted output.
+ gain={audioGainScalar(track.gainDb) * outputGain}
/>
<span className={styles.laneAudioLabel}>
<Music size={11} />
@@ -1731,6 +1737,7 @@ export function V4Timeline({
onStartDrag={startAudioDrag}
onSelect={tl.selectAudioTrack}
label={track.label || asset?.label || ts("audioTrack.defaultLabel")}
+ outputGain={audioGainScalar(settings.audioGainDb)}
/>
);
})
diff --git a/src/lib/ai-edition/store/useTimeline.ts b/src/lib/ai-edition/store/useTimeline.ts
index 2e1eeab86..f96b50faa 100644
--- a/src/lib/ai-edition/store/useTimeline.ts
+++ b/src/lib/ai-edition/store/useTimeline.ts
@@ -259,11 +259,14 @@ export function useTimeline() {
!probedAudioAssetIdsRef.current.has(a.id),
);
if (missing.length === 0) return;
+ // Mark every candidate BEFORE the first await. Marking each only as its turn
+ // came meant a document change that re-entered this effect while asset #1 was
+ // still awaiting found #2+ unmarked and probed them a second time.
+ for (const a of missing) probedAudioAssetIdsRef.current.add(a.id);
let cancelled = false;
void (async () => {
const probed: Record<string, number> = {};
for (const a of missing) {
- probedAudioAssetIdsRef.current.add(a.id);
const durationSec = await probeAudioDuration(toFileUrl(a.originalPath));
if (durationSec != null && durationSec > 0) probed[a.id] = durationSec;
}
diff --git a/src/lib/ai-edition/timeline/duration.ts b/src/lib/ai-edition/timeline/duration.ts
index 87d01ab11..41f47dd33 100644
--- a/src/lib/ai-edition/timeline/duration.ts
+++ b/src/lib/ai-edition/timeline/duration.ts
@@ -1,47 +1,47 @@
-// Probe a video file's actual duration by mounting a hidden <video> and
-// waiting for loadedmetadata. Used by the timeline store to size
-// freshly-inserted clips at the real source duration, so the user sees
-// the correct clip width immediately on drop instead of the placeholder.
-//
-// Falls back to null on error / timeout / non-finite duration. Caller
-// decides whether to fall back to a placeholder (60s) or surface an error.
-//
-// ponytail: probe via DOM <video> rather than the existing VirtualPreview.
-// We need this BEFORE the clip is on the timeline (so insertClipAt can
-// size it correctly), but VirtualPreview only mounts once a clip exists.
-// A throwaway <video> is the cleanest no-extra-component solution.
-
const DEFAULT_TIMEOUT_MS = 5000;
-export function probeVideoDuration(
+// The duration probe: mount a hidden media element and wait for loadedmetadata.
+// Falls back to null on error / timeout / non-finite duration; the caller decides
+// whether to use a placeholder (60s) or surface an error.
+//
+// One function for both <video> and <audio> — only the tag differs, and every
+// property touched (preload, style, onloadedmetadata, onerror, duration,
+// removeAttribute, load, src) lives on HTMLMediaElement, which both are. Sharing
+// it keeps a fix to the settle/cleanup/timeout logic from drifting between the two.
+//
+// ponytail: probe via a throwaway DOM element rather than VirtualPreview, which
+// only mounts once a clip exists — we need the duration BEFORE that, so
+// insertClipAt can size the clip correctly on drop.
+function probeMediaDuration(
+ tag: "video" | "audio",
src: string,
- timeoutMs: number = DEFAULT_TIMEOUT_MS,
+ timeoutMs: number,
): Promise<number | null> {
return new Promise((resolve) => {
if (typeof document === "undefined" || !src) {
resolve(null);
return;
}
- const video = document.createElement("video");
- video.preload = "metadata";
- video.style.position = "absolute";
- video.style.width = "1px";
- video.style.height = "1px";
- video.style.opacity = "0";
- video.style.pointerEvents = "none";
- video.style.left = "-9999px";
+ const el = document.createElement(tag);
+ el.preload = "metadata";
+ el.style.position = "absolute";
+ el.style.width = "1px";
+ el.style.height = "1px";
+ el.style.opacity = "0";
+ el.style.pointerEvents = "none";
+ el.style.left = "-9999px";
let settled = false;
const cleanup = () => {
- video.onloadedmetadata = null;
- video.onerror = null;
+ el.onloadedmetadata = null;
+ el.onerror = null;
clearTimeout(timer);
try {
- video.removeAttribute("src");
- video.load();
+ el.removeAttribute("src");
+ el.load();
} catch {
// ignore — browser may refuse if already detached
}
- if (video.parentNode) video.parentNode.removeChild(video);
+ if (el.parentNode) el.parentNode.removeChild(el);
};
const settle = (value: number | null) => {
if (settled) return;
@@ -50,74 +50,35 @@ export function probeVideoDuration(
resolve(value);
};
const timer = setTimeout(() => settle(null), timeoutMs);
- video.onloadedmetadata = () => {
- const d = video.duration;
+ el.onloadedmetadata = () => {
+ const d = el.duration;
settle(Number.isFinite(d) && d > 0 ? d : null);
};
- video.onerror = () => settle(null);
- // ponytail: append to body so some browsers (Firefox) actually fire
- // loadedmetadata for fully-detached <video> elements.
- document.body.appendChild(video);
- video.src = src;
+ el.onerror = () => settle(null);
+ // Append to body so some browsers (Firefox) actually fire loadedmetadata
+ // for a fully-detached media element.
+ document.body.appendChild(el);
+ el.src = src;
});
}
+/** Duration of a video file, to size a freshly-inserted clip at its real length. */
+export function probeVideoDuration(
+ src: string,
+ timeoutMs: number = DEFAULT_TIMEOUT_MS,
+): Promise<number | null> {
+ return probeMediaDuration("video", src, timeoutMs);
+}
+
/**
- * Duration of an imported audio file (issue #350), probed by mounting a hidden
- * <audio> and waiting for loadedmetadata — the audio-only counterpart of
- * `probeVideoDuration`. Used when an external voiceover / BGM / SFX file is added
- * so the timeline can size its track pill at the real length immediately.
- *
- * Falls back to null on error / timeout / non-finite duration, exactly like the
- * video probe; the caller decides whether to retry or use a placeholder.
+ * Duration of an imported audio file (issue #350) — the audio counterpart of
+ * `probeVideoDuration`, used to size a voiceover / BGM / SFX track pill on add.
*/
export function probeAudioDuration(
src: string,
timeoutMs: number = DEFAULT_TIMEOUT_MS,
): Promise<number | null> {
- return new Promise((resolve) => {
- if (typeof document === "undefined" || !src) {
- resolve(null);
- return;
- }
- const audio = document.createElement("audio");
- audio.preload = "metadata";
- audio.style.position = "absolute";
- audio.style.width = "1px";
- audio.style.height = "1px";
- audio.style.opacity = "0";
- audio.style.pointerEvents = "none";
- audio.style.left = "-9999px";
- let settled = false;
- const cleanup = () => {
- audio.onloadedmetadata = null;
- audio.onerror = null;
- clearTimeout(timer);
- try {
- audio.removeAttribute("src");
- audio.load();
- } catch {
- // ignore — browser may refuse if already detached
- }
- if (audio.parentNode) audio.parentNode.removeChild(audio);
- };
- const settle = (value: number | null) => {
- if (settled) return;
- settled = true;
- cleanup();
- resolve(value);
- };
- const timer = setTimeout(() => settle(null), timeoutMs);
- audio.onloadedmetadata = () => {
- const d = audio.duration;
- settle(Number.isFinite(d) && d > 0 ? d : null);
- };
- audio.onerror = () => settle(null);
- // Append to body so some browsers (Firefox) fire loadedmetadata for an
- // otherwise-detached media element — same reason as the video probe.
- document.body.appendChild(audio);
- audio.src = src;
- });
+ return probeMediaDuration("audio", src, timeoutMs);
}
/** Native pixel dimensions, same probe shape as `probeVideoDuration` (separate DOM element —