diff --git a/changelog/v0.1.0-beta4.txt b/changelog/v0.1.0-beta4.txt index c6e41f1e5..1f63a0629 100644 --- a/changelog/v0.1.0-beta4.txt +++ b/changelog/v0.1.0-beta4.txt @@ -46,6 +46,7 @@ SYNC MUSIC - Autoplay radio no longer stops after about 20 songs. A station keeps adding songs while it has more to play. +- Instrumental songs no longer show KuGou's notice as their lyrics. - Clicking an empty part of the music bar now opens the full player. The buttons, the seek line and dragging up work as before. - Hiding or showing the Queue or Lyrics pane in the full player no longer stretches the title and controls, and the artwork moves with the text. diff --git a/src/lib/lyrics/lyricsHelper.test.ts b/src/lib/lyrics/lyricsHelper.test.ts index 2187224b4..cb7738957 100644 --- a/src/lib/lyrics/lyricsHelper.test.ts +++ b/src/lib/lyrics/lyricsHelper.test.ts @@ -10,6 +10,17 @@ vi.mock("./registry", () => ({ })); vi.mock("../../store/useAppSettingsStore", () => ({ getSettingValue: () => "" })); +const disk = vi.hoisted(() => new Map()); +vi.mock("../api/lyrics", () => ({ + getCachedLyrics: async (id: string) => disk.get(id) ?? null, + putCachedLyrics: async (id: string, entries: string) => { + disk.set(id, entries); + }, + evictCachedLyrics: async (id: string) => { + disk.delete(id); + }, +})); + const { getLyrics, forceRefresh } = await import("./lyricsHelper"); const { seedOfflineLyrics } = await import("./offline"); @@ -40,6 +51,7 @@ const request = () => ({ videoId: `video-${++id}`, title: "Song", artist: "Band" beforeEach(() => { providers.length = 0; + disk.clear(); localStorage.clear(); }); afterEach(() => { @@ -47,6 +59,21 @@ afterEach(() => { }); describe("lyrics helper", () => { + it("drops KuGou's instrumental notice saved in the disk cache", async () => { + const song = request(); + disk.set(song.videoId, JSON.stringify([{ time: 1580, text: "纯音乐,请欣赏" }])); + expect(await getLyrics(song)).toBeNull(); + expect(disk.has(song.videoId)).toBe(false); + }); + + it("still falls back to other line-synced lyrics from the disk cache", async () => { + const song = request(); + disk.set(song.videoId, JSON.stringify(lineSynced)); + const result = await getLyrics(song); + expect(result?.provider).toBe("DiskCache"); + expect(disk.has(song.videoId)).toBe(true); + }); + it("returns word sync at once without asking later providers", async () => { const later = provider("Later", async () => lineSynced); providers.push(provider("Words", async () => wordSynced), later); diff --git a/src/lib/lyrics/lyricsHelper.ts b/src/lib/lyrics/lyricsHelper.ts index 999028d76..1cabb781d 100644 --- a/src/lib/lyrics/lyricsHelper.ts +++ b/src/lib/lyrics/lyricsHelper.ts @@ -1,7 +1,7 @@ import type { LyricsEntry, LyricsProvider, LyricsResult } from "./types"; import type { SongItem } from "../../types/music"; import { getEnabledProviders, parseProviderEnabledStates } from "./registry"; -import { filterCreditLines, normalizeEntries, stripWordTimings } from "./lyricsUtils"; +import { filterCreditLines, isInstrumentalNotice, normalizeEntries, stripWordTimings } from "./lyricsUtils"; import { cleanArtist, cleanTitle } from "./matching"; import { entriesAreSynced, hasWordSync, hasReasonableTimestamps } from "./sync"; import * as cache from "./cache"; @@ -125,11 +125,13 @@ async function fetchLyrics(request: LyricsRequest, signal: AbortSignal): Promise const disk = await cache.getDisk(videoId); if (disk && disk.length > 0) { const norm = normalizeEntries(sorted(disk)); - if (hasWordSync(norm) && hasReasonableTimestamps(norm, durationSec)) { + // A notice saved before KuGou's was read as no lyrics would otherwise come back as the fallback. + const usable = !isInstrumentalNotice(norm) && hasReasonableTimestamps(norm, durationSec); + if (hasWordSync(norm) && usable) { cache.setMemory(videoId, norm); return { entries: norm, provider: "DiskCache" }; } - if (hasReasonableTimestamps(norm, durationSec)) { + if (usable) { cachedFallback = { entries: stripWordTimings(norm), provider: "DiskCache" }; } else { cache.evict(videoId); diff --git a/src/lib/lyrics/lyricsUtils.ts b/src/lib/lyrics/lyricsUtils.ts index 0fe69b061..ada32a80e 100644 --- a/src/lib/lyrics/lyricsUtils.ts +++ b/src/lib/lyrics/lyricsUtils.ts @@ -54,6 +54,12 @@ export function filterCreditLines(entries: LyricsEntry[]): LyricsEntry[] { }); } +// KuGou answers instrumentals with one line, "纯音乐,请欣赏" ("pure music, please enjoy"), not with no lyrics. +const INSTRUMENTAL_NOTICE = /^纯音乐[,,]?\s*请欣赏$/; + +export const isInstrumentalNotice = (entries: LyricsEntry[]): boolean => + entries.length > 0 && entries.every((entry) => INSTRUMENTAL_NOTICE.test(entry.text.trim())); + export function normalizeEntries(entries: LyricsEntry[]): LyricsEntry[] { return entries.map((e) => ({ ...e, diff --git a/src/lib/lyrics/providers/kugou.test.ts b/src/lib/lyrics/providers/kugou.test.ts new file mode 100644 index 000000000..d9b9f304d --- /dev/null +++ b/src/lib/lyrics/providers/kugou.test.ts @@ -0,0 +1,61 @@ +import { beforeEach, describe, expect, it, vi } from "vitest"; + +const requests = vi.hoisted(() => ({ downloads: [] as number[] })); +const answers = vi.hoisted(() => ({ + songs: [] as Array<{ hash: string; songname: string; singername: string; duration: number }>, + keyword: [] as Array<{ id: number; accesskey: string; song: string; singer: string; duration: number }>, + lrc: new Map(), +})); + +vi.mock("../http", () => ({ + enc: encodeURIComponent, + lyricsGetJson: async (url: string) => { + if (url.includes("/search/song")) return { data: { info: answers.songs } }; + if (url.includes("/download")) { + const id = Number(new URL(url).searchParams.get("id")); + requests.downloads.push(id); + const bytes = new TextEncoder().encode(answers.lrc.get(id) ?? ""); + return { content: btoa(String.fromCharCode(...bytes)) }; + } + const hash = new URL(url).searchParams.get("hash"); + if (hash) return { candidates: [{ id: Number(hash), accesskey: "key" }] }; + return { candidates: answers.keyword }; + }, +})); + +import { kugouProvider } from "./kugou"; + +const NOTICE = "[00:01.58]纯音乐,请欣赏"; +const VOCALS = "[00:10.00]First\n[00:20.00]Second"; +const hit = (hash: number, duration: number) => ({ hash: String(hash), songname: "Song", singername: "Band", duration }); +const lookup = () => kugouProvider.getLyrics("id", "Song", "Band", 200, null); + +describe("KuGou lookup", () => { + beforeEach(() => { + requests.downloads.length = 0; + answers.songs = []; + answers.keyword = []; + answers.lrc = new Map([ + [1, NOTICE], + [2, VOCALS], + ]); + }); + + it("stops at the instrumental notice, so a vocal version of the song can't answer for it", async () => { + answers.songs = [hit(1, 200), hit(2, 180)]; + expect(await lookup()).toEqual([]); + expect(requests.downloads).toEqual([1]); + }); + + it("reads a notice from the keyword search as no lyrics too", async () => { + answers.keyword = [{ id: 1, accesskey: "key", song: "Song", singer: "Band", duration: 200_000 }]; + expect(await lookup()).toEqual([]); + }); + + it("still tries the next match when one has no lyrics at all", async () => { + answers.lrc.delete(1); + answers.songs = [hit(1, 200), hit(2, 180)]; + expect(await lookup()).toHaveLength(2); + expect(requests.downloads).toEqual([1, 2]); + }); +}); diff --git a/src/lib/lyrics/providers/kugou.ts b/src/lib/lyrics/providers/kugou.ts index 528514c98..de5faf9a3 100644 --- a/src/lib/lyrics/providers/kugou.ts +++ b/src/lib/lyrics/providers/kugou.ts @@ -1,6 +1,6 @@ import type { LyricsEntry, LyricsProvider } from "../types"; import { lyricsGetJson, enc } from "../http"; -import { parseLyrics } from "../lyricsUtils"; +import { isInstrumentalNotice, parseLyrics } from "../lyricsUtils"; import { ranked, type MatchSong } from "../matching"; const SONG_SEARCH = "https://mobileservice.kugou.com/api/v3/search/song"; @@ -142,7 +142,8 @@ export const kugouProvider: LyricsProvider = { const entries = await fromCandidate( await firstCandidate(`${LYRIC_SEARCH}?ver=1&man=yes&client=pc&hash=${enc(song.hash)}`), ); - if (entries.length > 0) return entries; + // The notice is KuGou's answer for this song; a later match could be a vocal version of it. + if (entries.length > 0) return isInstrumentalNotice(entries) ? [] : entries; } let url = `${LYRIC_SEARCH}?ver=1&man=yes&client=pc`; @@ -154,6 +155,7 @@ export const kugouProvider: LyricsProvider = { request, (c) => ({ title: stripTags(c.song), artist: stripTags(c.singer), durationSec: Math.round((c.duration ?? 0) / 1000) }), ); - return fromCandidate(candidates[0] ?? null); + const entries = await fromCandidate(candidates[0] ?? null); + return isInstrumentalNotice(entries) ? [] : entries; }, }; diff --git a/src/lib/lyrics/providers/providers.test.ts b/src/lib/lyrics/providers/providers.test.ts index b34788848..a6c56a7b7 100644 --- a/src/lib/lyrics/providers/providers.test.ts +++ b/src/lib/lyrics/providers/providers.test.ts @@ -6,6 +6,7 @@ import { matchKuGou, normalizeKuGouLrc } from "./kugou"; import { matchAppleSongs, parseItunesSearch } from "./paxsenix"; import { isUncachedMiss } from "./betterLyrics"; import { lrcOffsetMs, parseOfflineLyrics } from "../offline"; +import { isInstrumentalNotice, parseLyrics } from "../lyricsUtils"; describe("BetterLyrics", () => { it("reads an uncached 401 as a miss for this song only", () => { @@ -81,6 +82,14 @@ describe("KuGou", () => { const lrc = ["[00:00.00]Song - Singer", "[00:01.00]作词:Someone", "plain", "[00:10.00]First", "[00:20.00]Second"].join("\n"); expect(normalizeKuGouLrc(lrc)).toBe("[00:10.00]First\n[00:20.00]Second"); }); + + it("reads the instrumental notice as no lyrics, but not lyrics that contain it", () => { + const notice = ["\uFEFF[id:$00000000]", "[ar:arkady sevidov]", "[ti:June]", "[total:320317]", "[00:01.58]纯音乐,请欣赏"].join("\n"); + expect(isInstrumentalNotice(parseLyrics(normalizeKuGouLrc(notice)))).toBe(true); + expect(isInstrumentalNotice(parseLyrics("[00:01.58]纯音乐,请欣赏"))).toBe(true); + expect(isInstrumentalNotice(parseLyrics("[00:01.00]纯音乐,请欣赏\n[00:05.00]First"))).toBe(false); + expect(isInstrumentalNotice([])).toBe(false); + }); }); describe("Paxsenix", () => {