Skip to content

Commit

Permalink
core: 修正部分排版,允许背景传入空值以指示当前没有背景
Browse files Browse the repository at this point in the history
player: 修正导入歌曲流程,修正 Safari 不能正确抓取 AMLL TTML DB 歌词的问题
  • Loading branch information
Steve-xmh committed Nov 10, 2024
1 parent fec5a37 commit 6d8a438
Show file tree
Hide file tree
Showing 6 changed files with 75 additions and 41 deletions.
49 changes: 39 additions & 10 deletions packages/core/src/bg-render/mesh-renderer/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -733,6 +733,7 @@ export class MeshGradientRenderer extends BaseRenderer {
) as HTMLCanvasElement;
private targetSize = Vec2.fromValues(0, 0);
private currentSize = Vec2.fromValues(0, 0);
private isNoCover = true;
private meshStates: MeshState[] = [];
private _disposed = false;

Expand Down Expand Up @@ -812,16 +813,34 @@ export class MeshGradientRenderer extends BaseRenderer {
latestMeshState.mesh.bind();
// 考虑到我们并不逐帧更新网格控制点,因此也不需要重复调用 updateMesh
if (this.manualControl) latestMeshState.mesh.updateMesh();
latestMeshState.alpha = Math.min(1, latestMeshState.alpha + delta / 500);
if (latestMeshState.alpha >= 1) {
const deleted = this.meshStates.splice(0, this.meshStates.length - 1);
if (this.isNoCover) {
for (const state of this.meshStates) {
state.alpha = Math.max(0, state.alpha - delta / 500);
}
const deleted = this.meshStates.filter((s) => s.alpha === 0);
this.meshStates = this.meshStates.filter((s) => s.alpha > 0);
for (const state of deleted) {
state.mesh.dispose();
state.texture.dispose();
}
}
if (this.meshStates.length === 1 && latestMeshState.alpha >= 1) {
canBeStatic = true;
if (this.meshStates.length === 0) {
canBeStatic = true;
}
} else {
latestMeshState.alpha = Math.min(
1,
latestMeshState.alpha + delta / 500,
);
if (latestMeshState.alpha >= 1) {
const deleted = this.meshStates.splice(0, this.meshStates.length - 1);
for (const state of deleted) {
state.mesh.dispose();
state.texture.dispose();
}
}
if (this.meshStates.length === 1 && latestMeshState.alpha >= 1) {
canBeStatic = true;
}
}
}

Expand Down Expand Up @@ -922,11 +941,16 @@ export class MeshGradientRenderer extends BaseRenderer {
this.requestTick();
}
override async setAlbum(
albumSource: string | HTMLImageElement | HTMLVideoElement,
albumSource?: string | HTMLImageElement | HTMLVideoElement,
isVideo?: boolean,
): Promise<void> {
if (typeof albumSource === "string" && albumSource.trim().length === 0)
throw new Error("Empty album url");
if (
albumSource === undefined ||
(typeof albumSource === "string" && albumSource.trim().length === 0)
) {
this.isNoCover = true;
return;
}
let res: HTMLImageElement | HTMLVideoElement | null = null;
let remainRetryTimes = 5;
while (!res && remainRetryTimes > 0) {
Expand All @@ -947,7 +971,12 @@ export class MeshGradientRenderer extends BaseRenderer {
remainRetryTimes--;
}
}
if (!res) return;
if (!res) {
console.error("Failed to load album resource", albumSource);
this.isNoCover = true;
return;
}
this.isNoCover = false;
// resize image
const c = this.reduceImageSizeCanvas;
const ctx = c.getContext("2d", {
Expand Down
7 changes: 5 additions & 2 deletions packages/core/src/bg-render/pixi-renderer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -195,10 +195,13 @@ export class PixiRenderer extends BaseRenderer {
}

override async setAlbum(
albumSource: string | HTMLImageElement | HTMLVideoElement,
albumSource?: string | HTMLImageElement | HTMLVideoElement,
isVideo?: boolean,
): Promise<void> {
if (typeof albumSource === "string" && albumSource.trim().length === 0)
if (
!albumSource ||
(typeof albumSource === "string" && albumSource.trim().length === 0)
)
return;
let res: HTMLImageElement | HTMLVideoElement | null = null;
let remainRetryTimes = 5;
Expand Down
43 changes: 20 additions & 23 deletions packages/core/src/lyric-player/base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -410,26 +410,28 @@ export abstract class LyricPlayerBase
this.hasDuetLine = this.processedLines.some((line) => line.isDuet);

// 将行间有较短空隙的两个歌词行的结束时间拉长,与下一行歌词行的开始时间一致,以便于更好的显示
this.processedLines.forEach((line, i, lines) => {
const nextLine = lines[i + 1];
const lastWord = line.words[line.words.length - 1];
if (lastWord) {
if (nextLine) {
if (nextLine.startTime > line.endTime) {
line.endTime = Math.min(line.endTime + 1500, nextLine.startTime);
}
} else {
line.endTime = line.endTime + 1500;
}
}
});

// 让背景歌词和上一行歌词一同出现
// Update: 感觉还是不太适用,所以移除了
// this.processedLines.forEach((line, i, lines) => {
// const nextLine = lines[i + 1];
// const lastWord = line.words[line.words.length - 1];
// if (lastWord) {
// if (nextLine) {
// if (nextLine.startTime > line.endTime) {
// line.endTime = Math.min(line.endTime + 1500, nextLine.startTime);
// }
// } else {
// line.endTime = line.endTime + 1500;
// }
// }
// });

// 让背景歌词和上一行歌词一同出现并一同消失
this.processedLines.forEach((line, i, lines) => {
if (line.isBG) return;
const nextLine = lines[i + 1];
if (nextLine?.isBG) {
nextLine.startTime = Math.min(nextLine.startTime, line.startTime);
nextLine.endTime = Math.max(nextLine.endTime, line.endTime);
}
});
for (const line of this.currentLyricLineObjects) {
Expand All @@ -440,6 +442,9 @@ export abstract class LyricPlayerBase
this.hotLines.clear();
this.bufferedLines.clear();
this.setCurrentTime(0, true);
if (import.meta.env.DEV) {
console.log("歌词处理完成", this.processedLines);
}
}

/**
Expand All @@ -460,15 +465,7 @@ export abstract class LyricPlayerBase
// 如果当前所有缓冲行都将被删除且没有新热行加入,则删除所有缓冲行,且也不会修改当前滚动位置
// 如果当前所有缓冲行都将被删除且有新热行加入,则删除所有缓冲行并加入新热行作为缓冲行,然后修改当前滚动位置

// this.initializeSeeking = isSeek;
this.currentTime = time;
// if (Math.abs(this.currentTime - this.lastCurrentTime) >= 100) {
// this.initializeSeeking = true;
// } else this.initializeSeeking = false;
// if (!this.isPageVisible) return;
// if (!this._getIsNonDynamic() && !this.supportMaskImage)
// this.element.style.setProperty("--amll-player-time", `${time}`);
// if (this.isScrolled) return;

if (!this.initialLayoutFinished && !isSeek) return;

Expand Down
2 changes: 1 addition & 1 deletion packages/player/src-tauri/capabilities/migrated.toml
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ identifier = "fs:allow-rename"
identifier = "fs:allow-remove"
[[permissions]]
identifier = "fs:scope"
allow = ["$APPDATA/**"]
allow = ["$APPDATA/**", "$HOME/**"]
[[permissions]]
identifier = "fs:allow-stat"
[[permissions]]
Expand Down
13 changes: 9 additions & 4 deletions packages/player/src/components/LocalMusicContext/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -282,8 +282,11 @@ const LyricContext: FC = () => {
let synced = 0;
let errored = 0;

await Promise.all(
shouldFetchList.keys().map(async (fileName: string) => {
const fetchTasks = [];

// Safari 目前不支持对迭代器对象使用 map 方法
for (const fileName of shouldFetchList.keys()) {
fetchTasks.push((async () => {
const lyricRes = await fetch(fileMap[fileName].download_url, {
signal: sig.signal,
redirect: "follow",
Expand Down Expand Up @@ -315,8 +318,10 @@ const LyricContext: FC = () => {
console.warn("下载并解析歌词文件", fileName, "失败", err);
errored++;
}
}),
);
})())
}

await Promise.all(fetchTasks);

console.log(
TTML_LOG_TAG,
Expand Down
2 changes: 1 addition & 1 deletion packages/player/src/pages/playlist/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -158,8 +158,8 @@ export const Component: FC = () => {
if (platform() !== "android" && platform() !== "ios") {
normalized = (await path.normalize(v)).replace(/\\/gi, "/");
}
console.log(await stat(v));
try {
console.log(await stat(v));
const pathMd5 = md5(normalized);
const musicInfo = await readLocalMusicMetadata(normalized);

Expand Down

0 comments on commit 6d8a438

Please sign in to comment.