From ff41fa982997577a8ee4049d4dde205a51a2aa6a Mon Sep 17 00:00:00 2001 From: lyswhut Date: Thu, 26 May 2022 12:03:40 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8Dkuwo=E6=AD=8C=E8=AF=8D?= =?UTF-8?q?=E8=A7=A3=E6=9E=90?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/renderer/store/modules/player.js | 4 ++-- src/renderer/utils/music/kw/lyric.js | 4 +++- src/renderer/utils/music/kw/util.js | 34 ++++++++++++++++++++-------- 3 files changed, 30 insertions(+), 12 deletions(-) diff --git a/src/renderer/store/modules/player.js b/src/renderer/store/modules/player.js index d0a7ab1e..9da84e1f 100644 --- a/src/renderer/store/modules/player.js +++ b/src/renderer/store/modules/player.js @@ -222,8 +222,8 @@ const actions = { }) }, async getLrc({ commit, state }, musicInfo) { - const lrcInfo = await getStoreLyric(musicInfo) - // let lrcInfo = {} + let lrcInfo = await getStoreLyric(musicInfo) + // lrcInfo = {} // if (lrcRequest && lrcRequest.cancelHttp) lrcRequest.cancelHttp() if (existTimeExp.test(lrcInfo.lyric) && lrcInfo.tlyric != null) { // if (musicInfo.lrc.startsWith('\ufeff[id:$00000000]')) { diff --git a/src/renderer/utils/music/kw/lyric.js b/src/renderer/utils/music/kw/lyric.js index d7fe41c9..6e94db26 100644 --- a/src/renderer/utils/music/kw/lyric.js +++ b/src/renderer/utils/music/kw/lyric.js @@ -192,8 +192,10 @@ export default { let result = timeExp.exec(line) if (result) { const text = line.replace(timeExp, '').trim() + let time = RegExp.$1 + if (/\.\d\d$/.test(time)) time += '0' lrcArr.push({ - time: RegExp.$1, + time, text, }) } else if (lrcTools.rxps.tagLine.test(line)) { diff --git a/src/renderer/utils/music/kw/util.js b/src/renderer/utils/music/kw/util.js index ef8b0e5c..1d04a054 100644 --- a/src/renderer/utils/music/kw/util.js +++ b/src/renderer/utils/music/kw/util.js @@ -89,14 +89,26 @@ export const lrcTools = { isOK: false, lines: [], tags: [], - getWordInfo(str, str2) { + getWordInfo(str, str2, prevWord) { const offset = parseInt(str) const offset2 = parseInt(str2) - const startTime = Math.floor((offset + offset2) / (this.offset * 2)) - const timeLength = Math.floor((offset - offset2) / (this.offset2 * 2)) + let startTime = Math.abs((offset + offset2) / (this.offset * 2)) + let endTime = Math.abs((offset - offset2) / (this.offset2 * 2)) + startTime + if (prevWord) { + if (startTime < prevWord.endTime) { + prevWord.endTime = startTime + if (prevWord.startTime > prevWord.endTime) { + prevWord.startTime = prevWord.endTime + } + + prevWord.newTimeStr = `<${prevWord.startTime},${prevWord.endTime - prevWord.startTime}>` + // console.log(prevWord) + } + } return { startTime, - timeLength, + endTime, + timeStr: `<${startTime},${endTime - startTime}>`, } }, parseLine(line) { @@ -111,10 +123,13 @@ export const lrcTools = { const wordTimes = words.match(this.rxps.wordTimeAll) if (!wordTimes) return // console.log(wordTimes) + let preTimeInfo for (const timeStr of wordTimes) { const result = this.rxps.wordTime.exec(timeStr) - const wordInfo = this.getWordInfo(result[1], result[2]) - words = words.replace(timeStr, `<${wordInfo.startTime},${wordInfo.timeLength}>`) + const wordInfo = this.getWordInfo(result[1], result[2], preTimeInfo) + words = words.replace(timeStr, wordInfo.timeStr) + if (preTimeInfo?.newTimeStr) words = words.replace(preTimeInfo.timeStr, preTimeInfo.newTimeStr) + preTimeInfo = wordInfo } this.lines.push(time + words) return @@ -127,8 +142,8 @@ export const lrcTools = { content = content.substring(0, content.indexOf('][')) } const valueOf = parseInt(content, 8) - this.offset = Math.floor(valueOf / 10) - this.offset2 = Math.floor(valueOf % 10) + this.offset = Math.trunc(valueOf / 10) + this.offset2 = Math.trunc(valueOf % 10) if (this.offset == 0 || Number.isNaN(this.offset) || this.offset2 == 0 || Number.isNaN(this.offset2)) { this.isOK = false } @@ -147,12 +162,13 @@ export const lrcTools = { tools.tags = [] for (const line of lines) { - if (!tools.isOK) return '' + if (!tools.isOK) throw new Error('failed') tools.parseLine(line) } if (!tools.lines.length) return '' let lrcs = tools.lines.join('\n') if (tools.tags.length) lrcs = `${tools.tags.join('\n')}\n${lrcs}` + // console.log(lrcs) return lrcs }, }