forked from Steve-xmh/applemusic-like-lyrics
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.ts
324 lines (304 loc) · 8.46 KB
/
test.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
/**
* @fileoverview
* 此处是一个简易的组件加载测试脚本,用来调试歌词
*
* @author SteveXMH
*/
import GUI from "lil-gui";
import { BackgroundRender, PixiRenderer, EplorRenderer } from "./bg-render";
import Stats from "stats.js";
import { LyricLineMouseEvent, LyricPlayer } from "./lyric-player";
import { parseTTML } from "@applemusic-like-lyrics/ttml";
import { SpringParams } from "./utils/spring";
import {
type LyricLine as RawLyricLine,
parseLrc,
parseYrc,
parseLys,
parseQrc,
} from "@applemusic-like-lyrics/lyric";
import { LyricLine } from ".";
const audio = document.createElement("audio");
audio.preload = "auto";
const debugValues = {
lyric: new URL(location.href).searchParams.get("lyric") || "",
music: new URL(location.href).searchParams.get("music") || "",
album: new URL(location.href).searchParams.get("album") || "",
enableSpring: true,
bgFPS: 30,
bgMode: new URL(location.href).searchParams.get("bg") || "eplor",
bgScale: 0.5,
bgFlowSpeed: 2,
bgPlaying: true,
bgStaticMode: true,
currentTime: 0,
enableBlur: true,
playing: false,
async mockPlay() {
this.playing = true;
const startTime = Date.now();
const baseTime = this.currentTime * 1000;
while (this.playing && this.currentTime < 300) {
const time = Date.now() - startTime;
this.currentTime = (baseTime + time) / 1000;
progress.updateDisplay();
lyricPlayer.setCurrentTime(baseTime + time);
await waitFrame();
}
},
play() {
this.playing = true;
audio.load();
audio.play();
},
pause() {
this.playing = false;
if (audio.paused) {
audio.play();
} else {
audio.pause();
}
},
lineSprings: {
posX: {
mass: 1,
damping: 10,
stiffness: 100,
soft: false,
} as SpringParams,
posY: {
mass: 1,
damping: 15,
stiffness: 100,
soft: false,
} as SpringParams,
scale: {
mass: 1,
damping: 20,
stiffness: 100,
soft: false,
} as SpringParams,
},
};
function recreateBGRenderer(mode: string) {
window.globalBackground?.dispose();
if (mode === "pixi") {
window.globalBackground = BackgroundRender.new(PixiRenderer);
} else if (mode === "eplor") {
window.globalBackground = BackgroundRender.new(EplorRenderer);
} else {
throw new Error("Unknown renderer mode");
}
const bg = window.globalBackground;
bg.setFPS(30);
bg.setStaticMode(debugValues.bgStaticMode);
bg.getElement().style.position = "absolute";
bg.getElement().style.top = "0";
bg.getElement().style.left = "0";
bg.getElement().style.width = "100%";
bg.getElement().style.height = "100%";
bg.setAlbumImage(debugValues.album);
}
audio.src = debugValues.music;
audio.load();
const gui = new GUI();
gui.close();
gui.title("AMLL 歌词测试页面");
gui
.add(debugValues, "lyric")
.name("歌词文件")
.onFinishChange(async (url: string) => {
lyricPlayer.setLyricLines(parseTTML(await (await fetch(url)).text()));
});
gui
.add(debugValues, "music")
.name("歌曲")
.onFinishChange((v: string) => {
audio.src = v;
});
gui
.add(debugValues, "album")
.name("专辑图片")
.onFinishChange((v: string) => {
window.globalBackground.setAlbumImage(v);
});
const bgGui = gui.addFolder("背景");
bgGui
.add(debugValues, "bgPlaying")
.name("播放")
.onFinishChange((v: boolean) => {
if (v) {
window.globalBackground.resume();
} else {
window.globalBackground.pause();
}
});
bgGui
.add(debugValues, "bgMode", ["pixi", "eplor"])
.name("背景渲染器")
.onFinishChange((v: string) => {
recreateBGRenderer(v);
});
bgGui
.add(debugValues, "bgScale", 0.01, 1, 0.01)
.name("分辨率比率")
.onChange((v: number) => {
window.globalBackground.setRenderScale(v);
});
bgGui
.add(debugValues, "bgFPS", 1, 60, 1)
.name("帧率")
.onFinishChange((v: number) => {
window.globalBackground.setFPS(v);
});
bgGui
.add(debugValues, "bgFlowSpeed", 0, 10, 0.1)
.name("流动速度")
.onFinishChange((v: number) => {
window.globalBackground.setFlowSpeed(v);
});
bgGui
.add(debugValues, "bgStaticMode")
.name("静态模式")
.onFinishChange((v: boolean) => {
window.globalBackground.setStaticMode(v);
});
{
const animation = gui.addFolder("歌词行动画/效果");
animation
.add(debugValues, "enableBlur")
.name("启用歌词模糊")
.onChange((v: boolean) => {
lyricPlayer.setEnableBlur(v);
});
animation
.add(debugValues, "enableSpring")
.name("使用弹簧动画")
.onChange((v: boolean) => {
lyricPlayer.setEnableSpring(v);
});
function addSpringDbg(name: string, obj: SpringParams, onChange: () => void) {
const x = animation.addFolder(name);
x.close();
x.add(obj, "mass").name("质量").onFinishChange(onChange);
x.add(obj, "damping").name("阻力").onFinishChange(onChange);
x.add(obj, "stiffness").name("弹性").onFinishChange(onChange);
x.add(obj, "soft")
.name("强制软弹簧(当阻力小于 1 时有用)")
.onFinishChange(onChange);
}
addSpringDbg("水平位移弹簧", debugValues.lineSprings.posX, () => {
lyricPlayer.setLinePosXSpringParams(debugValues.lineSprings.posX);
});
addSpringDbg("垂直位移弹簧", debugValues.lineSprings.posY, () => {
lyricPlayer.setLinePosYSpringParams(debugValues.lineSprings.posY);
});
addSpringDbg("缩放弹簧", debugValues.lineSprings.scale, () => {
lyricPlayer.setLineScaleSpringParams(debugValues.lineSprings.scale);
});
}
const playerGui = gui.addFolder("音乐播放器");
const progress = playerGui
.add(debugValues, "currentTime")
.min(0)
.step(1)
.name("当前进度")
.onChange((v: number) => {
audio.currentTime = v;
lyricPlayer.setCurrentTime(v * 1000, true);
});
playerGui.add(debugValues, "play").name("加载/播放");
playerGui.add(debugValues, "pause").name("暂停/继续");
const lyricPlayer = new LyricPlayer();
lyricPlayer.addEventListener("line-click", (evt) => {
const e = evt as LyricLineMouseEvent;
evt.preventDefault();
evt.stopImmediatePropagation();
evt.stopPropagation();
console.log(e.line, e.lineIndex);
});
const stats = new Stats();
stats.showPanel(0);
document.body.appendChild(stats.dom);
let lastTime = -1;
const frame = (time: number) => {
stats.end();
if (lastTime === -1) {
lastTime = time;
}
if (!audio.paused) {
const time = (audio.currentTime * 1000) | 0;
debugValues.currentTime = (time / 1000) | 0;
progress.max(audio.duration | 0);
progress.updateDisplay();
lyricPlayer.setCurrentTime(time);
}
lyricPlayer.update(time - lastTime);
lastTime = time;
stats.begin();
requestAnimationFrame(frame);
};
requestAnimationFrame(frame);
declare global {
interface Window {
globalLyricPlayer: LyricPlayer;
globalBackground:
| BackgroundRender<PixiRenderer>
| BackgroundRender<EplorRenderer>;
}
}
window.globalLyricPlayer = lyricPlayer;
const waitFrame = (): Promise<void> =>
new Promise((resolve) => requestAnimationFrame(resolve));
const mapLyric = (
line: RawLyricLine,
i: number,
lines: RawLyricLine[],
): LyricLine => ({
words: line.words,
startTime: line.words[0]?.startTime ?? 0,
endTime: line.words[line.words.length - 1]?.endTime ?? Infinity,
translatedLyric: "",
romanLyric: "",
isBG: false,
isDuet: false,
});
async function loadLyric() {
const lyricFile = debugValues.lyric;
const content = await (await fetch(lyricFile)).text();
if (lyricFile.endsWith(".ttml")) {
lyricPlayer.setLyricLines(parseTTML(content));
} else if (lyricFile.endsWith(".lrc")) {
lyricPlayer.setLyricLines(parseLrc(content).map(mapLyric));
} else if (lyricFile.endsWith(".yrc")) {
lyricPlayer.setLyricLines(parseYrc(content).map(mapLyric));
} else if (lyricFile.endsWith(".lys")) {
lyricPlayer.setLyricLines(parseLys(content).map(mapLyric));
} else if (lyricFile.endsWith(".qrc")) {
lyricPlayer.setLyricLines(parseQrc(content).map(mapLyric));
}
}
const lys = String.raw`
[35610,4170](35610,360,0)I (35970,540,0)cast (36510,390,0)us (36900,390,0)out (37290,1050,0)of (38340,1440,0)paradise
`.trim();
// [0]This (500,1100)is (1600,250)a (1850,250)long(2100,2000) syll(4100,400)a(4500,250)ble(4750,1000) lyrics(5750,500)
const l = parseYrc(lys).map(mapLyric);
(async () => {
recreateBGRenderer(debugValues.bgMode);
audio.style.display = "none";
// lyricPlayer.getBottomLineElement().innerHTML = "Test Bottom Line";
const player = document.getElementById("player");
if (player) {
player.appendChild(audio);
player.appendChild(window.globalBackground.getElement());
player.appendChild(lyricPlayer.getElement());
}
if (!debugValues.enableSpring) {
lyricPlayer.setEnableSpring(false);
}
await loadLyric();
lyricPlayer.setLyricLines(l);
// debugValues.play();
debugValues.currentTime = 34;
debugValues.mockPlay();
})();