-
Notifications
You must be signed in to change notification settings - Fork 0
/
player.go
144 lines (124 loc) · 3.24 KB
/
player.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
package main
import (
"os"
"log"
"time"
"fmt"
"strings"
tea "github.com/charmbracelet/bubbletea"
"github.com/faiface/beep"
"github.com/faiface/beep/wav"
"github.com/faiface/beep/mp3"
"github.com/faiface/beep/speaker"
)
// Globals
var ctrl *beep.Ctrl
var format beep.Format
var streamer beep.StreamSeekCloser
var initialized bool = false
// Play a song
func playSong(song *Song, playNext chan bool, p *tea.Program) {
f, err := os.Open(song.FilePath)
if err != nil {
fmt.Println(err)
log.Fatal(err)
}
switch {
case strings.HasSuffix(song.FilePath, ".mp3"):
streamer, format, err = mp3.Decode(f)
case strings.HasSuffix(song.FilePath, ".wav"):
streamer, format, err = wav.Decode(f)
default:
f.Close()
return
}
if err != nil {
log.Fatal(err)
}
speaker.Clear()
if(!initialized) {
speaker.Init(format.SampleRate, format.SampleRate.N(time.Second/10))
initialized = true
}
speaker.Lock()
ctrl = &beep.Ctrl{
Streamer: beep.Seq(streamer, beep.Callback(func () {
// Hacky workaround for erroneous callbacks
// Sometimes this callback runs for a skipped song when I press the skip button, idk why
// This will simply ignore that case
position := format.SampleRate.D(streamer.Position()).Round(time.Second)
length := format.SampleRate.D(streamer.Len()).Round(time.Second)
if(position != length) {
return
}
// -- End hacky workaround
streamer.Close()
playNext <- true
})),
Paused: false,
}
speaker.Unlock()
speaker.Play(ctrl)
p.Send(ChangeSongMsg(song))
position := format.SampleRate.D(streamer.Position()).Round(time.Second)
length := format.SampleRate.D(streamer.Len()).Round(time.Second)
p.Send(ChangeDurationMsg{
duration: length,
position: position,
})
}
// Main function
func PlayerLoop(p *tea.Program, messages chan string){
// Create the channel
playNext := make(chan bool, 1)
playNext <- true
// Play the first song; send done when done
for {
select {
case <-playNext:
if(streamer != nil) {
streamer.Close()
}
// Play the next song if available
nextSong := GetNextSong()
if(nextSong == nil) {
messages <- "pause"
}
playSong(nextSong, playNext, p)
case message := <- messages:
switch message {
case "pause":
speaker.Lock()
ctrl.Paused = !ctrl.Paused
speaker.Unlock()
case "next":
if(streamer != nil) {
streamer.Close()
}
nextSong := GetNextSong()
if(nextSong == nil) {
messages <- "pause"
}
playSong(nextSong, playNext, p)
case "back":
if(streamer != nil) {
streamer.Close()
}
prevSong := GetPrevSong()
if(prevSong == nil) {
messages <- "pause"
}
playSong(prevSong, playNext, p)
}
case <-time.After(time.Second):
speaker.Lock()
position := format.SampleRate.D(streamer.Position()).Round(time.Second)
length := format.SampleRate.D(streamer.Len()).Round(time.Second)
p.Send(ChangeDurationMsg{
duration: length,
position: position,
})
speaker.Unlock()
}
}
}