重写controller部分,修改search界面,添加歌词滚动效果,部分资源添加到bundle,修复拖动进度条时产生的噪音

This commit is contained in:
Aynakeya
2022-12-24 03:51:21 -08:00
parent c47d338a9e
commit 9ec4057412
52 changed files with 777 additions and 376 deletions

View File

@@ -34,7 +34,7 @@ type PlayEvent struct {
type LyricUpdateEvent struct {
Lyrics *Lyric
Time float64
Lyric *LyricLine
Lyric *LyricContext
}
type LyricReloadEvent struct {

View File

@@ -16,9 +16,11 @@ type LyricLine struct {
}
type LyricContext struct {
Current *LyricLine
Prev []*LyricLine
Next []*LyricLine
Now *LyricLine
Index int
Total int
Prev []*LyricLine
Next []*LyricLine
}
type Lyric struct {
@@ -55,30 +57,58 @@ func LoadLyric(lyric string) *Lyric {
return &Lyric{Lyrics: lrcs}
}
func (l *Lyric) Find(time float64) *LyricLine {
func (l *Lyric) findIndexV1(time float64) int {
for i := 0; i < len(l.Lyrics)-1; i++ {
if l.Lyrics[i].Time <= time && time < l.Lyrics[i+1].Time {
return l.Lyrics[i]
return i
}
}
return nil
return -1
}
func (l *Lyric) findIndex(time float64) int {
start := 0
end := len(l.Lyrics) - 1
mid := (start + end) / 2
for start < end {
if l.Lyrics[mid].Time <= time && time < l.Lyrics[mid+1].Time {
return mid
}
if l.Lyrics[mid].Time > time {
end = mid
} else {
start = mid
}
mid = (start + end) / 2
}
return -1
}
func (l *Lyric) Find(time float64) *LyricLine {
idx := l.findIndex(time)
if idx == -1 {
return nil
}
return l.Lyrics[idx]
}
func (l *Lyric) FindContext(time float64, prev int, next int) *LyricContext {
for i := 0; i < len(l.Lyrics)-1; i++ {
if l.Lyrics[i].Time <= time && time < l.Lyrics[i+1].Time {
if (i + prev) < 0 {
prev = -i
}
if (i + 1 + next) > len(l.Lyrics) {
next = len(l.Lyrics) - i - 1
}
return &LyricContext{
Current: l.Lyrics[i],
Prev: l.Lyrics[i+prev : i],
Next: l.Lyrics[i+1 : i+1+next],
}
}
prev = -prev
idx := l.findIndex(time)
if idx == -1 {
return nil
}
if (idx + prev) < 0 {
prev = -idx
}
if (idx + 1 + next) > len(l.Lyrics) {
next = len(l.Lyrics) - idx - 1
}
return &LyricContext{
Now: l.Lyrics[idx],
Index: idx,
Total: len(l.Lyrics),
Prev: l.Lyrics[idx+prev : idx],
Next: l.Lyrics[idx+1 : idx+1+next],
}
return nil
}

View File

@@ -2,6 +2,7 @@ package model
import (
"fmt"
"github.com/magiconair/properties/assert"
"testing"
)
@@ -21,3 +22,10 @@ func TestLyricFind(t *testing.T) {
fmt.Println(l)
}
}
func TestLyricFindV2(t *testing.T) {
lryic := LoadLyric(testLyric)
for i := 0.0; i < 170; i += 0.01 {
assert.Equal(t, lryic.FindV1(i), lryic.Find(i))
}
}