第一次提交
This commit is contained in:
38
src/utils/lyric.ts
Normal file
38
src/utils/lyric.ts
Normal file
@ -0,0 +1,38 @@
|
||||
export interface LyricLine {
|
||||
time: number; // 秒
|
||||
text: string;
|
||||
}
|
||||
|
||||
export function parseLrc(lrcStr: string): LyricLine[] {
|
||||
const lines = lrcStr.split('\n');
|
||||
const result: LyricLine[] = [];
|
||||
const timeReg = /\[(\d{2}):(\d{2})\.(\d{2,3})\]/;
|
||||
for (const line of lines) {
|
||||
const match = line.match(timeReg);
|
||||
if (match) {
|
||||
const min = parseInt(match[1], 10);
|
||||
const sec = parseInt(match[2], 10);
|
||||
const ms = parseInt(match[3], 10) / (match[3].length === 3 ? 1000 : 100);
|
||||
const time = min * 60 + sec + ms;
|
||||
const text = line.replace(timeReg, '').trim();
|
||||
if (text) {
|
||||
result.push({ time, text });
|
||||
}
|
||||
}
|
||||
}
|
||||
// 按时长排序
|
||||
result.sort((a, b) => a.time - b.time);
|
||||
return result;
|
||||
}
|
||||
|
||||
export function getCurrentLyricIndex(lyrics: LyricLine[], currentTime: number): number {
|
||||
let index = -1;
|
||||
for (let i = 0; i < lyrics.length; i++) {
|
||||
if (currentTime >= lyrics[i].time) {
|
||||
index = i;
|
||||
} else {
|
||||
break;
|
||||
}
|
||||
}
|
||||
return index;
|
||||
}
|
||||
18
src/utils/song.ts
Normal file
18
src/utils/song.ts
Normal file
@ -0,0 +1,18 @@
|
||||
/**
|
||||
* 统一规范化歌曲对象,确保 al.picUrl、ar、dt 字段存在且合理
|
||||
*/
|
||||
export function normalizeSong(song: any) {
|
||||
const normalized = { ...song };
|
||||
// 封面 / 艺术家兼容
|
||||
if (!normalized.al?.picUrl && normalized.album?.picUrl) {
|
||||
normalized.al = { ...normalized.al, picUrl: normalized.album.picUrl };
|
||||
}
|
||||
if (!normalized.ar || normalized.ar.length === 0) {
|
||||
normalized.ar = normalized.artists || [];
|
||||
}
|
||||
// 时长:只保留合理的 dt(100ms ~ 2小时),否则置 0
|
||||
if (!normalized.dt || normalized.dt < 100 || normalized.dt > 7200000) {
|
||||
normalized.dt = 0;
|
||||
}
|
||||
return normalized;
|
||||
}
|
||||
Reference in New Issue
Block a user