mirror of
https://github.com/owntone/owntone-server.git
synced 2025-01-15 00:35:03 -05:00
[web] Improve the search of verse in the lyrics
This commit is contained in:
parent
3577d0e2a8
commit
d627033141
@ -52,30 +52,48 @@ export default {
|
|||||||
},
|
},
|
||||||
verse_index() {
|
verse_index() {
|
||||||
if (this.lyrics.length && this.lyrics[0].time) {
|
if (this.lyrics.length && this.lyrics[0].time) {
|
||||||
const curTime = this.player.item_progress_ms / 1000,
|
const currentTime = this.player.item_progress_ms / 1000,
|
||||||
la = this.lyrics,
|
la = this.lyrics,
|
||||||
trackChanged = this.player.item_id !== this.lastItemId,
|
trackChanged = this.player.item_id !== this.lastItemId,
|
||||||
trackSeeked =
|
trackSeeked =
|
||||||
this.lastIndex >= 0 &&
|
this.lastIndex >= 0 &&
|
||||||
this.lastIndex < la.length &&
|
this.lastIndex < la.length &&
|
||||||
la[this.lastIndex].time > curTime
|
la[this.lastIndex].time > currentTime
|
||||||
|
// Reset the cache when the track has changed or has been seeked
|
||||||
if (trackChanged || trackSeeked) {
|
if (trackChanged || trackSeeked) {
|
||||||
// Reset the cache when the track has changed or has been rewind
|
|
||||||
this.reset_scrolling()
|
this.reset_scrolling()
|
||||||
}
|
}
|
||||||
// Check the cached value to avoid searching the times
|
// Check the next two items and the last one before searching
|
||||||
if (
|
if (
|
||||||
this.lastIndex < la.length - 1 &&
|
(this.lastIndex < la.length - 1 &&
|
||||||
la[this.lastIndex + 1].time > curTime
|
la[this.lastIndex + 1].time > currentTime) ||
|
||||||
|
this.lastIndex === la.length - 1
|
||||||
)
|
)
|
||||||
return this.lastIndex
|
return this.lastIndex
|
||||||
if (
|
if (
|
||||||
this.lastIndex < la.length - 2 &&
|
this.lastIndex < la.length - 2 &&
|
||||||
la[this.lastIndex + 2].time > curTime
|
la[this.lastIndex + 2].time > currentTime
|
||||||
)
|
)
|
||||||
return this.lastIndex + 1
|
return this.lastIndex + 1
|
||||||
// Not found in the next 2 items, so start searching for the best time
|
// Not found, then start a binary search
|
||||||
return la.findLastIndex((verse) => verse.time <= curTime)
|
let start = 0,
|
||||||
|
end = la.length - 1,
|
||||||
|
index
|
||||||
|
while (start <= end) {
|
||||||
|
index = (start + end) >> 1
|
||||||
|
const currentVerse = la[index]
|
||||||
|
const nextVerse = la[index + 1]
|
||||||
|
if (
|
||||||
|
currentVerse.time <= currentTime &&
|
||||||
|
(nextVerse?.time > currentTime || !nextVerse)
|
||||||
|
) {
|
||||||
|
return index
|
||||||
|
}
|
||||||
|
currentVerse.time < currentTime
|
||||||
|
? (start = index + 1)
|
||||||
|
: (end = index - 1)
|
||||||
|
}
|
||||||
|
return -1
|
||||||
} else {
|
} else {
|
||||||
this.reset_scrolling()
|
this.reset_scrolling()
|
||||||
return -1
|
return -1
|
||||||
|
Loading…
Reference in New Issue
Block a user