From 2febbea6ecc91677ec6116ce42ee42b33bb030ec Mon Sep 17 00:00:00 2001 From: Azalea <22280294+hykilpikonna@users.noreply.github.com> Date: Sun, 23 Nov 2025 14:41:22 +0800 Subject: [PATCH] [O] Parallelize song preparation --- README.md | 2 +- src/lib/server/songs.ts | 15 ++++++++------- 2 files changed, 9 insertions(+), 8 deletions(-) diff --git a/README.md b/README.md index 6bffef1..88d3f94 100644 --- a/README.md +++ b/README.md @@ -35,7 +35,7 @@ Practice Japanese Karaoke lyrics reading and typing at the same time with KaraDa * [x] Update an existing playlist * [ ] Allow users to correct lyric pronunciations through correction feedback * [ ] Correct lyrics timing inconsistencies (i.e. 网易云的歌词因为是业余用户上传的,时间戳不一定准确。但是 waveform 里面可以分析出每句歌词的具体开始结束时间,也许可以自动修正) -* [ ] Processing lyrics and audio should be parallel +* [x] Processing lyrics and audio should be parallel ## Development server diff --git a/src/lib/server/songs.ts b/src/lib/server/songs.ts index 90dc354..d998f92 100644 --- a/src/lib/server/songs.ts +++ b/src/lib/server/songs.ts @@ -153,16 +153,13 @@ export const prepareSong = async (songId: number) => { songProcessingStatus.set(songId, state) const addTask = (id: string, task: string) => ({ id, task, progress: 0 }).also(it => state.items.push(it)) - try { + const processLyrics = async () => { // 1. Get Lyrics const taskLyrics = addTask('lyrics', '从网易云获取歌词') const raw = await getLyricsRaw(songId) taskLyrics.progress = 1 - if (raw.lang !== 'jpn') { - addTask('error', '错误: 不是日语歌曲').progress = -1 - return state.status = 'error' - } + if (raw.lang !== 'jpn') throw new Error('不是日语歌曲') // 2. AI Process const taskAI = addTask('ai', 'AI 标注歌词读音') @@ -174,7 +171,9 @@ export const prepareSong = async (songId: number) => { await dbs.lyricsProcessed.replaceOne({ _id: songId as any }, { _id: songId, data: lrc }, { upsert: true }) taskAI.progress = 1 } + } + const processMusic = async () => { // 3. Audio const taskAudio = addTask('music', '从网易云获取音乐') await getSongUrl(songId) @@ -192,9 +191,11 @@ export const prepareSong = async (songId: number) => { addTask('error', `错误: ${e.message}`).progress = -1 // Don't fail the whole process, just this step } - - state.status = 'done' + } + try { + await Promise.all([processLyrics(), processMusic()]) + state.status = 'done' } catch (e) { addTask('error', `错误: ${eToString(e)}`).progress = -1 state.status = 'error'