From 165832e703af08c50c87850401e1a18a885bf630 Mon Sep 17 00:00:00 2001 From: lyswhut Date: Wed, 1 Feb 2023 11:24:58 +0800 Subject: [PATCH] =?UTF-8?q?=E5=AE=8C=E5=96=84=E6=94=AF=E6=8C=81wy=E6=AD=8C?= =?UTF-8?q?=E8=AF=8D=E8=8E=B7=E5=8F=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../utils/lyric-font-player/line-player.js | 12 +++-- src/renderer/utils/musicSdk/wy/lyric.js | 49 ++++++++++++++----- 2 files changed, 46 insertions(+), 15 deletions(-) diff --git a/src/common/utils/lyric-font-player/line-player.js b/src/common/utils/lyric-font-player/line-player.js index ad56083a..657cd0f0 100644 --- a/src/common/utils/lyric-font-player/line-player.js +++ b/src/common/utils/lyric-font-player/line-player.js @@ -2,6 +2,8 @@ const { getNow, TimeoutTools } = require('./utils') const timeFieldExp = /^(?:\[[\d:.]+\])+/g const timeExp = /[\d:.]+/g +const timeLabelRxp = /^(\[[\d:]+\.)0+(\d+\])/ +const timeLabelFixRxp = /(?:\.0+|0+)$/ const tagRegMap = { title: 'ti', artist: 'ar', @@ -24,8 +26,9 @@ const parseExtendedLyric = (lrcLinesMap, extendedLyric) => { const times = timeField.match(timeExp) if (times == null) continue for (let time of times) { - if (!time.includes('.')) time += '.0' - const timeStr = time.replace(/(?:\.0+|0+)$/, '') + if (time.includes('.')) time = time.replace(timeLabelRxp, '$1$2') + else time += '.0' + const timeStr = time.replace(timeLabelFixRxp, '') const targetLine = lrcLinesMap[timeStr] if (targetLine) targetLine.extendedLyrics.push(text) } @@ -84,8 +87,9 @@ module.exports = class LinePlayer { const times = timeField.match(timeExp) if (times == null) continue for (let time of times) { - if (!time.includes('.')) time += '.0' - const timeStr = time.replace(/(?:\.0+|0+)$/, '') + if (time.includes('.')) time = time.replace(timeLabelRxp, '$1$2') + else time += '.0' + const timeStr = time.replace(timeLabelFixRxp, '') if (linesMap[timeStr]) { linesMap[timeStr].extendedLyrics.push(text) continue diff --git a/src/renderer/utils/musicSdk/wy/lyric.js b/src/renderer/utils/musicSdk/wy/lyric.js index 744209c4..b2120b35 100644 --- a/src/renderer/utils/musicSdk/wy/lyric.js +++ b/src/renderer/utils/musicSdk/wy/lyric.js @@ -99,8 +99,8 @@ const parseTools = { lxlrcLines.push(`${startTimeStr}${newWords}`) } return { - lyric: lrcLines.join('\n'), - lxlyric: lxlrcLines.join('\n'), + lyricLines: lrcLines, + lxlyricLines: lxlrcLines, } }, parseHeaderInfo(str) { @@ -119,7 +119,15 @@ const parseTools = { } }) }, - parse(ylrc, lrc, tlrc, rlrc) { + fixTimeTag(lrcLines, targetLrcLines) { + const timeTagRxp = /^\[[\d:.]+\]/ + return targetLrcLines.map((line, index) => { + const timeTag = timeTagRxp.exec(lrcLines[index]) + if (!timeTag) return line + return line.replace(timeTagRxp, timeTag[0]) + }) + }, + parse(ylrc, ytlrc, yrlrc, lrc, tlrc, rlrc) { const info = { lyric: '', tlyric: '', @@ -129,16 +137,32 @@ const parseTools = { if (ylrc) { let lines = this.parseHeaderInfo(ylrc) if (lines) { + const result = this.parseLyric(lines) + if (ytlrc) { + const lines = this.parseHeaderInfo(ytlrc) + if (lines) { + if (lines.length == result.lyricLines.length) { + info.tlyric = this.fixTimeTag(result.lyricLines, lines).join('\n') + } else info.tlyric = lines.join('\n') + } + } + if (yrlrc) { + const lines = this.parseHeaderInfo(yrlrc) + if (lines) { + if (lines.length == result.lyricLines.length) { + info.rlyric = this.fixTimeTag(result.lyricLines, lines).join('\n') + } else info.rlyric = lines.join('\n') + } + } + const timeRxp = /^\[[\d:.]+\]/ const headers = lines.filter(l => timeRxp.test(l)).join('\n') - const result = this.parseLyric(lines) - info.lxlyric = result.lxlyric - info.lyric = `${headers}\n${result.lyric}` - } else if (lrc) { - lines = this.parseHeaderInfo(lrc) - if (lines) info.lyric = lines.join('\n') + info.lyric = `${headers}\n${result.lyricLines.join('\n')}` + info.lxlyric = result.lxlyricLines.join('\n') + return info } - } else if (lrc) { + } + if (lrc) { const lines = this.parseHeaderInfo(lrc) if (lines) info.lyric = lines.join('\n') } @@ -203,7 +227,10 @@ export default songmid => { requestObj.promise = requestObj.promise.then(({ body }) => { // console.log(body) if (body.code !== 200 || !body?.lrc?.lyric) return Promise.reject(new Error('Get lyric failed')) - return parseTools.parse(body.yrc?.lyric, body.lrc.lyric, body.tlyric?.lyric, body.romalrc?.lyric) + const info = parseTools.parse(body.yrc?.lyric, body.ytlrc?.lyric, body.yromalrc?.lyric, body.lrc.lyric, body.tlyric?.lyric, body.romalrc?.lyric) + // console.log(info) + if (!info.lyric) return Promise.reject(new Error('Get lyric failed')) + return info }) return requestObj }