-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplayer.go
executable file
·185 lines (165 loc) · 4.8 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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
package main
import (
"github.com/faiface/beep"
"github.com/faiface/beep/mp3"
"github.com/faiface/beep/speaker"
"os"
"time"
)
type PlayerState int
const (
// PlayerNothingPlaying is the state when the player has nothing cued up/paused/buffered
PlayerNothingPlaying PlayerState = iota
PlayerResume PlayerState = iota
PlayerPlay PlayerState = iota
PlayerPause PlayerState = iota
PlayerStop PlayerState = iota
PlayerFastForward PlayerState = iota
PlayerRewind PlayerState = iota
PlayerExit PlayerState = iota
)
var (
playerPosition int = 0
// TODO get rid of this and just use file size
startTime time.Time
lengthOfFile int
playerControl chan PlayerState
fileChannel chan string
exitChannel chan bool
playing string
playerState = PlayerNothingPlaying
)
// StartPlayer starts the global player. The player is global since there is only one of them
// I'm not really a fan of moving it into an object as it should not be reused
func StartPlayer(configuration *Configuration) {
playerControl = make(chan PlayerState)
fileChannel = make(chan string)
exitChannel = make(chan bool)
go startPlayer(configuration)
}
// DisposePlayer sends a signal to the player to destroy itself, and then waits for the player to exit
// This function will deadlock if called twice
func DisposePlayer() {
playerControl <- PlayerExit
<-exitChannel
}
func TogglePlayerState() {
if playerState == PlayerPlay {
playerControl <- PlayerPause
} else if playerState == PlayerPause {
playerControl <- PlayerResume
}
}
func StopPlayer() {
playerState = PlayerStop
playerPosition = -1
playing = ""
lengthOfFile = 0 //set length
}
func GetLengthOfPlayingFile() int {
return lengthOfFile
}
func GetPlayerState() PlayerState {
return playerState
}
func sendPlayerMessage(state PlayerState) {
playerControl <- state
}
func SetPlaying(filename string) {
fileChannel <- filename
}
func GetPlayerPosition() int {
if playerPosition < 0 || playerPosition >= GetLengthOfPlayingFile() {
return 0
}
return playerPosition + int(time.Since(startTime).Seconds())
}
//this runs on its own thread to start/stop and select the media that is playing
func startPlayer(configuration *Configuration) {
var (
status = PlayerNothingPlaying
stopToExit = false
ctrl *beep.Ctrl = nil
format *beep.Format = nil
)
for stopToExit != true {
status = PlayerNothingPlaying //reset status
select {
case status = <-playerControl:
case playing = <-fileChannel:
continue
}
switch status {
case PlayerNothingPlaying:
playerState = PlayerNothingPlaying
case PlayerPlay:
if playerState != PlayerPause {
playerPosition = 0
}
ctrl, format = playFile()
case PlayerResume:
playerState = PlayerPlay
ctrl.Paused = false
startTime = time.Now()
case PlayerPause:
//save time and file then cleanup
ctrl.Paused = true
playerPosition += int(time.Since(startTime).Seconds())
playerState = PlayerPause
case PlayerStop:
ctrl.Paused = true
ctrl.Streamer = nil
StopPlayer()
case PlayerFastForward:
if playerState == PlayerPlay {
fastForwardPlayer(ctrl, format, configuration)
}
case PlayerRewind:
if playerState == PlayerPlay {
rewindPlayer(ctrl, format, configuration)
}
case PlayerExit:
speaker.Clear()
stopToExit = true
}
}
exitChannel <- true
}
func changePlayerPosition(inputFile beep.StreamSeekCloser, format *beep.Format, position int) {
if position < 0 {
position = 0
}
frames := format.SampleRate.N(time.Second * time.Duration(position))
if frames < inputFile.Len() {
_ = inputFile.Seek(frames)
} else {
_ = inputFile.Seek(inputFile.Len() - 1)
}
}
func playFile() (*beep.Ctrl, *beep.Format) {
playerState = PlayerPlay
inputFile, _ := os.Open(playing)
decodedMp3, format, _ := mp3.Decode(inputFile)
ctrl := &beep.Ctrl{Streamer: decodedMp3}
changePlayerPosition(decodedMp3, &format, playerPosition)
speaker.Init(format.SampleRate, format.SampleRate.N(time.Second/10))
speaker.Play(beep.Seq(ctrl, beep.Callback(StopPlayer)))
lengthOfFile = int(format.SampleRate.D(decodedMp3.Len()).Seconds())
startTime = time.Now()
return ctrl, &format
}
func fastForwardPlayer(ctrl *beep.Ctrl, format *beep.Format, configuration *Configuration) {
streamer := ctrl.Streamer.(beep.StreamSeekCloser)
playerPosition = GetPlayerPosition() + configuration.FastForwardLength
changePlayerPosition(streamer, format, playerPosition)
startTime = time.Now()
}
func rewindPlayer(ctrl *beep.Ctrl, format *beep.Format, configuration *Configuration) {
streamer := ctrl.Streamer.(beep.StreamSeekCloser)
playerPosition = GetPlayerPosition() - configuration.RewindLength
if playerPosition < 1 {
playerPosition = 0
}
changePlayerPosition(streamer, format, playerPosition)
startTime = time.Now()
}