Improve voice generation and caption audio linking

This commit is contained in:
haixin.yang committed 2026-07-21 18:04:16 +08:00
1 parent 27399270c6
commit 58ce75d55f
14 files changed
+688 -58

No files matched your search

+118
View File
@@ -0,0 +1,118 @@
import { expect, test } from "@playwright/test";
const ONE_PIXEL_PNG = Buffer.from(
"iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAQAAAC1HAwCAAAAC0lEQVR42mP8/x8AAusB9Y9ZQmcAAAAASUVORK5CYII=",
"base64",
);
function createSilentWav(durationSeconds = 2) {
const sampleRate = 8_000;
const sampleCount = Math.round(sampleRate * durationSeconds);
const buffer = Buffer.alloc(44 + sampleCount * 2);
buffer.write("RIFF", 0); buffer.writeUInt32LE(buffer.length - 8, 4); buffer.write("WAVEfmt ", 8);
buffer.writeUInt32LE(16, 16); buffer.writeUInt16LE(1, 20); buffer.writeUInt16LE(1, 22);
buffer.writeUInt32LE(sampleRate, 24); buffer.writeUInt32LE(sampleRate * 2, 28);
buffer.writeUInt16LE(2, 32); buffer.writeUInt16LE(16, 34); buffer.write("data", 36);
buffer.writeUInt32LE(sampleCount * 2, 40);
return buffer;
}
async function dragHorizontally(page, locator, deltaX) {
const box = await locator.boundingBox();
if (!box) throw new Error("Timeline clip geometry is unavailable");
const x = box.x + box.width / 2;
const y = box.y + box.height / 2;
await page.mouse.move(x, y);
await page.mouse.down();
await page.mouse.move(x + deltaX, y, { steps: 8 });
await page.mouse.up();
}
test("caption voiceover link can align, unlink, and relink without deleting either clip", async ({ page }) => {
const pageErrors = [];
page.on("pageerror", (error) => pageErrors.push(error.message));
await page.setViewportSize({ width: 412, height: 915 });
await page.addInitScript(() => {
localStorage.clear();
localStorage.setItem("ai-voiceover-ui-language", "en");
});
await page.goto("/");
const input = page.locator('input[type="file"][multiple]');
await input.setInputFiles({ name: "visual.png", mimeType: "image/png", buffer: ONE_PIXEL_PNG });
await page.keyboard.press("Escape");
await page.getByText("Media", { exact: true }).last().click();
await input.setInputFiles({ name: "linked-voice.wav", mimeType: "audio/wav", buffer: createSilentWav(2) });
await page.getByRole("button", { name: "Add to voiceover" }).click();
await expect(page.locator(".audio-clip:not(.is-source):not(.is-music)")).toHaveCount(1);
await page.setViewportSize({ width: 1440, height: 900 });
await page.getByRole("button", { name: "Captions", exact: true }).click();
await page.getByRole("button", { name: "Add caption", exact: true }).click();
const caption = page.locator(".caption-segment");
const voice = page.locator(".audio-clip:not(.is-source):not(.is-music)");
await expect(caption).toHaveCount(1);
const linkPanel = page.getByTestId("caption-audio-link");
await expect(linkPanel).toContainText("No linked voiceover");
await linkPanel.getByRole("button", { name: "Link audio", exact: true }).click();
await expect(linkPanel).toContainText("Linked voiceover");
await caption.click({ button: "right" });
const contextMenu = page.getByRole("menu", { name: "Timeline context menu" });
await expect(contextMenu.getByRole("menuitem", { name: "Unlink", exact: true })).toBeVisible();
await expect(contextMenu.getByRole("menuitem", { name: "Align to audio", exact: true })).toBeVisible();
await page.keyboard.press("Escape");
await voice.click({ button: "right" });
await expect(contextMenu.getByRole("menuitem", { name: "Unlink", exact: true })).toBeVisible();
await expect(contextMenu.getByRole("menuitem", { name: "Align to audio", exact: true })).toBeVisible();
await page.keyboard.press("Escape");
const captionLeftBeforeLinkedMove = await caption.evaluate((element) => element.style.getPropertyValue("--caption-left"));
await dragHorizontally(page, voice, 90);
const captionLeftAfterLinkedMove = await caption.evaluate((element) => element.style.getPropertyValue("--caption-left"));
expect(captionLeftAfterLinkedMove).not.toBe(captionLeftBeforeLinkedMove);
await caption.click();
await expect(linkPanel).toContainText("Linked voiceover");
await linkPanel.getByRole("button", { name: "Unlink", exact: true }).click();
await expect(linkPanel).toContainText("No linked voiceover");
await expect(voice).toHaveCount(1);
await expect(caption).toHaveCount(1);
const captionLeftBeforeUnlinkedMove = await caption.evaluate((element) => element.style.getPropertyValue("--caption-left"));
await dragHorizontally(page, voice, 70);
const captionLeftAfterUnlinkedMove = await caption.evaluate((element) => element.style.getPropertyValue("--caption-left"));
expect(captionLeftAfterUnlinkedMove).toBe(captionLeftBeforeUnlinkedMove);
await caption.click();
await linkPanel.getByRole("button", { name: "Link audio", exact: true }).click();
await linkPanel.getByRole("button", { name: "Align to audio", exact: true }).click();
const [captionBox, voiceBox] = await Promise.all([caption.boundingBox(), voice.boundingBox()]);
expect(Math.abs(captionBox.x - voiceBox.x)).toBeLessThan(2);
expect(Math.abs(captionBox.width - voiceBox.width)).toBeLessThan(2);
await page.setViewportSize({ width: 412, height: 915 });
await caption.click();
const mobileActions = page.locator(".timeline-mobile-clip-actions");
await expect(mobileActions.locator("button")).toHaveText([
"Back", "Edit", "Split", "Copy", "Unlink", "Align to audio", "Delete",
]);
const back = mobileActions.getByRole("button", { name: "Back", exact: true });
const backLeftBeforeScroll = (await back.boundingBox()).x;
const actionScroller = mobileActions.locator(".timeline-mobile-clip-action-scroller");
const actionsBox = await actionScroller.boundingBox();
await page.mouse.move(actionsBox.x + actionsBox.width - 20, actionsBox.y + actionsBox.height / 2);
await page.mouse.wheel(500, 0);
await expect.poll(() => actionScroller.evaluate((element) => element.scrollLeft)).toBeGreaterThan(0);
expect(Math.abs((await back.boundingBox()).x - backLeftBeforeScroll)).toBeLessThan(1);
await expect(mobileActions.getByRole("button", { name: "Unlink", exact: true })).toBeVisible();
await expect(mobileActions.getByRole("button", { name: "Align to audio", exact: true })).toBeVisible();
await expect(mobileActions.getByRole("button", { name: "Delete", exact: true })).toBeVisible();
await page.mouse.wheel(-500, 0);
await expect.poll(() => actionScroller.evaluate((element) => element.scrollLeft)).toBe(0);
await mobileActions.getByRole("button", { name: "Edit", exact: true }).click();
await expect(page.getByTestId("caption-audio-link")).toBeVisible();
await expect(page.getByTestId("caption-audio-link").getByRole("button", { name: "Align to audio", exact: true })).toBeVisible();
expect(pageErrors).toEqual([]);
});
+124
View File
@@ -123,6 +123,130 @@ test("desktop timeline keeps its existing draggable playhead behavior", async ({
await expect(page.locator(".mobile-fixed-playhead")).toBeHidden();
});
test("desktop trackpad zoom keeps ruler and track geometry synchronized before commit", async ({ page }) => {
await page.setViewportSize({ width: 1280, height: 900 });
await page.addInitScript(() => localStorage.setItem("ai-voiceover-ui-language", "zh"));
await page.goto("/");
await page.locator('input[type="file"][multiple]').setInputFiles({
name: "desktop-wheel-fixture.png",
mimeType: "image/png",
buffer: ONE_PIXEL_PNG,
});
await page.keyboard.press("Escape");
const clip = page.locator(".image-clip");
await expect(clip).toBeVisible();
const box = await clip.boundingBox();
if (!box) throw new Error("Timeline clip is unavailable");
const readGeometry = () => page.evaluate(() => {
const track = document.querySelector(".track-scroll")?.getBoundingClientRect();
const ruler = document.querySelector(".timeline-ruler-canvas")?.getBoundingClientRect();
const trackPlayhead = document.querySelector(".playhead")?.getBoundingClientRect();
const rulerPlayhead = document.querySelector(".playhead-ruler")?.getBoundingClientRect();
return {
trackWidth: track?.width || 0,
rulerWidth: ruler?.width || 0,
trackPlayheadX: trackPlayhead?.left || 0,
rulerPlayheadX: rulerPlayhead?.left || 0,
};
});
const before = await readGeometry();
await clip.dispatchEvent("wheel", {
deltaY: -120,
deltaX: 0,
deltaMode: 0,
ctrlKey: true,
clientX: box.x + box.width / 2,
clientY: box.y + box.height / 2,
bubbles: true,
cancelable: true,
});
await page.waitForTimeout(32);
const during = await readGeometry();
await page.waitForTimeout(260);
const committed = await readGeometry();
expect(during.trackWidth).toBeGreaterThan(before.trackWidth);
expect(during.rulerWidth).toBeCloseTo(during.trackWidth, 0);
expect(during.rulerPlayheadX).toBeCloseTo(during.trackPlayheadX, 0);
expect(committed.rulerWidth).toBeCloseTo(committed.trackWidth, 0);
expect(committed.rulerPlayheadX).toBeCloseTo(committed.trackPlayheadX, 0);
});
test("desktop horizontal scrolling keeps the ruler playhead aligned on every scroll event", async ({ page }) => {
await page.setViewportSize({ width: 1280, height: 900 });
await page.addInitScript(() => localStorage.setItem("ai-voiceover-ui-language", "zh"));
await page.goto("/");
await page.locator('input[type="file"][multiple]').setInputFiles({
name: "desktop-scroll-fixture.png",
mimeType: "image/png",
buffer: ONE_PIXEL_PNG,
});
await page.keyboard.press("Escape");
await expect(page.locator(".image-clip")).toBeVisible();
await page.getByRole("button", { name: "放大时间线" }).click();
await page.getByRole("button", { name: "放大时间线" }).click();
await page.getByRole("button", { name: "放大时间线" }).click();
await page.waitForTimeout(180);
const offsets = await page.locator(".tracks").evaluate((element) => {
const samples = [];
for (const ratio of [0.2, 0.5, 0.8, 0.35]) {
element.scrollLeft = (element.scrollWidth - element.clientWidth) * ratio;
element.dispatchEvent(new Event("scroll", { bubbles: true }));
const trackPlayhead = document.querySelector(".playhead")?.getBoundingClientRect();
const rulerPlayhead = document.querySelector(".playhead-ruler")?.getBoundingClientRect();
samples.push(Math.abs((trackPlayhead?.left || 0) - (rulerPlayhead?.left || 0)));
}
return samples;
});
for (const offset of offsets) expect(offset).toBeLessThanOrEqual(1);
});
test("desktop fast trackpad wheel scrolling advances ruler and tracks in the same frame", async ({ page }) => {
await page.setViewportSize({ width: 1280, height: 900 });
await page.addInitScript(() => localStorage.setItem("ai-voiceover-ui-language", "zh"));
await page.goto("/");
await page.locator('input[type="file"][multiple]').setInputFiles({
name: "desktop-fast-scroll-fixture.png",
mimeType: "image/png",
buffer: ONE_PIXEL_PNG,
});
await page.keyboard.press("Escape");
const clip = page.locator(".image-clip");
await expect(clip).toBeVisible();
await page.getByRole("button", { name: "放大时间线" }).click();
await page.getByRole("button", { name: "放大时间线" }).click();
await page.getByRole("button", { name: "放大时间线" }).click();
await page.waitForTimeout(180);
const box = await clip.boundingBox();
if (!box) throw new Error("Timeline clip is unavailable");
const offsets = [];
for (const deltaX of [42, 88, 136, -54, 172, -96]) {
await clip.dispatchEvent("wheel", {
deltaX,
deltaY: 2,
deltaMode: 0,
clientX: box.x + Math.min(40, box.width / 2),
clientY: box.y + box.height / 2,
bubbles: true,
cancelable: true,
});
offsets.push(await page.evaluate(() => {
const trackPlayhead = document.querySelector(".playhead")?.getBoundingClientRect();
const rulerPlayhead = document.querySelector(".playhead-ruler")?.getBoundingClientRect();
return Math.abs((trackPlayhead?.left || 0) - (rulerPlayhead?.left || 0));
}));
}
for (const offset of offsets) expect(offset).toBeLessThanOrEqual(1);
});
test("mobile trackpad wheel zoom uses pixels and does not jump when committed", async ({ page }) => {
await page.setViewportSize({ width: 412, height: 915 });
await page.addInitScript(() => localStorage.setItem("ai-voiceover-ui-language", "zh"));