Skip to content

Commit

Permalink
test: add lip sync test
Browse files Browse the repository at this point in the history
  • Loading branch information
guansss committed Mar 18, 2024
1 parent 12e9530 commit ee42229
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 1 deletion.
2 changes: 1 addition & 1 deletion test/env.ts
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,7 @@ export const test = baseTest.extend<CustomContext>({
onload,
onerror,
);
};
} as typeof originalCreateXHR;
});

const loaderMock: CustomContext["loaderMock"] = {
Expand Down
55 changes: 55 additions & 0 deletions test/features/sound.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
import { config } from "@/config";
import { SoundManager } from "@/cubism-common";
import { Cubism4InternalModel, Cubism4ModelSettings } from "@/cubism4";
import { afterEach, beforeEach, expect } from "vitest";
import soundUrl from "../assets/shizuku/sounds/tapBody_00.mp3";
import { TEST_MODEL4, test } from "../env";
import { delay } from "../utils";

beforeEach(() => {
config.sound = true;
});

afterEach(() => {
SoundManager.destroy();
});

test("lip sync", async () => {
const mouthParam = "ParamMouthOpenY";
const model = new Cubism4InternalModel(
await TEST_MODEL4.coreModel(),
new Cubism4ModelSettings(TEST_MODEL4.modelJsonWithUrl),
{ idleMotionGroup: "nonExistent" },
);

expect(model.coreModel.getParameterValueById(mouthParam)).toBe(0);

await expect(model.lipSync.play(soundUrl, { volume: 1 })).resolves.toBe(undefined);

const audio = model.lipSync.currentAudio!;
expect(audio).toBeTruthy();

let prevTime = 0;

async function seekAndGetParam(time: number) {
audio.currentTime = time / 1000;
await delay(100);

let value = NaN;

model.once("beforeModelUpdate", () => {
value = model.coreModel.getParameterValueById(mouthParam);
});
model.update(time - prevTime, time);

prevTime = time;

return value;
}

const timeOffsetBeforeWave = 100;
const timeOffsetDuringWave = 560;

await expect(seekAndGetParam(timeOffsetBeforeWave)).resolves.toBe(0);
await expect(seekAndGetParam(timeOffsetDuringWave)).resolves.toBeGreaterThan(0);
});
24 changes: 24 additions & 0 deletions test/load-cores.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,34 @@
declare const __CUBISM2_CORE_SOURCE__: string;
declare const __CUBISM4_CORE_SOURCE__: string;

const excludedLogs = [
`Live2D %s`,
`profile : Desktop`,
`[PROFILE_NAME]`,
`[USE_ADJUST_TRANSLATION]`,
`[USE_CACHED_POLYGON_IMAGE]`,
`[EXPAND_W]`,
`Live2D Cubism SDK Core Version`,
`[CSM][I]`,
];

const log = console.log;
console.log = function (...args) {
const firstArg = args[0];

if (typeof firstArg === "string" && excludedLogs.some((log) => firstArg.includes(log))) {
return;
}

log(...args);
};

(() => {
__CUBISM2_CORE_SOURCE__;
})();
(() => {
__CUBISM4_CORE_SOURCE__;
window.Live2DCubismCore = Live2DCubismCore;
})();

console.log = log;

0 comments on commit ee42229

Please sign in to comment.