Skip to content

Commit b7ad8e6

Browse files
committed
consider Album name at lyrics search
1 parent 590a6b6 commit b7ad8e6

File tree

1 file changed

+31
-5
lines changed

1 file changed

+31
-5
lines changed

src/plugins/synced-lyrics/providers/LRCLib.ts

Lines changed: 31 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,19 @@ export class LRCLib implements LyricProvider {
135135
filteredResults.push(item);
136136
}
137137

138+
filteredResults = filteredResults.map((lrc) => {
139+
if (album) {
140+
lrc.albumRatio = jaroWinkler(
141+
lrc.albumName.toLowerCase(),
142+
album.toLowerCase(),
143+
);
144+
} else {
145+
lrc.albumRatio = 0;
146+
}
147+
148+
return lrc;
149+
});
150+
138151
filteredResults = filteredResults.filter((lrc) => {
139152
return Math.abs(lrc.duration - songDuration) < 15;
140153
});
@@ -143,19 +156,31 @@ export class LRCLib implements LyricProvider {
143156

144157
filteredResults.sort(
145158
(
146-
{ duration: durationA, syncedLyrics: lyricsA },
147-
{ duration: durationB, syncedLyrics: lyricsB },
159+
{ duration: durationA, syncedLyrics: lyricsA, albumRatio: arA },
160+
{ duration: durationB, syncedLyrics: lyricsB, albumRatio: arB },
148161
) => {
149162
const hasLyricsA = lyricsA != null && lyricsA !== '';
150163
const hasLyricsB = lyricsB != null && lyricsB !== '';
151-
const left = Math.abs(durationA - songDuration);
152-
const right = Math.abs(durationB - songDuration);
153164

154165
if (hasLyricsA !== hasLyricsB) {
155166
return hasLyricsB ? 1 : -1;
156167
}
168+
const durationDiffA = Math.abs(durationA - songDuration);
169+
const durationDiffB = Math.abs(durationB - songDuration);
170+
171+
const normalizedDurationA = durationDiffA / songDuration;
172+
const normalizedDurationB = durationDiffB / songDuration;
173+
174+
const weightAlbumRatio = 0.7;
175+
const weightDuration = 0.3;
176+
177+
const scoreA =
178+
weightAlbumRatio * arA! - weightDuration * normalizedDurationA;
179+
const scoreB =
180+
weightAlbumRatio * arB! - weightDuration * normalizedDurationB;
157181

158-
return left - right;
182+
// Mayor score es mejor
183+
return scoreB - scoreA;
159184
},
160185
);
161186

@@ -198,4 +223,5 @@ type LRCLIBSearchResponse = {
198223
instrumental: boolean;
199224
plainLyrics: string;
200225
syncedLyrics: string;
226+
albumRatio?: number;
201227
}[];

0 commit comments

Comments
 (0)