This repository has been archived by the owner on Dec 20, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 18
/
audio.go
200 lines (183 loc) · 3.93 KB
/
audio.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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
package main
import (
"github.com/jonas747/dca"
"io"
"log"
"time"
)
const (
channels int = 2 // 1 for mono, 2 for stereo
frameRate int = 48000 // audio sampling rate
frameSize int = 960 // uint16 size of each audio frame 960/48KHz = 20ms
bufferSize int = 1024 // max size of opus data 1K
)
/*
func (v *VoiceInstance) InitVoice() {
v.songSig = make(chan PkgSong)
v.radioSig = make(chan PkgRadio)
v.endSig = make(chan bool)
v.speaking = false
go v.Play(v.songSig, v.radioSig, v.endSig)
}
*/
/*
func (v *VoiceInstance) Play(songSig chan Song, radioSig chan string, endSig chan bool) {
for {
select {
case song := <-songSig:
if v.radioFlag {
v.Stop()
time.Sleep(200 * time.Millisecond)
}
go v.PlayQueue(song)
case radio := <-radioSig:
v.Stop()
time.Sleep(200 * time.Millisecond)
go v.Radio(radio)
case <-endSig:
v.Stop()
return
//time.Sleep(200 * time.Millisecond)
}
}
}
*/
func GlobalPlay(songSig chan PkgSong) {
for {
select {
case song := <-songSig:
if song.v.radioFlag {
song.v.Stop()
time.Sleep(200 * time.Millisecond)
}
go song.v.PlayQueue(song.data)
}
}
}
func GlobalRadio(radioSig chan PkgRadio) {
for {
select {
case radio := <-radioSig:
radio.v.Stop()
time.Sleep(200 * time.Millisecond)
go radio.v.Radio(radio.data)
}
}
}
func (v *VoiceInstance) PlayQueue(song Song) {
// add song to queue
v.QueueAdd(song)
if v.speaking {
// the bot is playing
return
}
go func() {
v.audioMutex.Lock()
defer v.audioMutex.Unlock()
for {
if len(v.queue) == 0 {
dg.UpdateStatus(0, o.DiscordStatus)
ChMessageSend(v.nowPlaying.ChannelID, "[**Music**] End of queue!")
return
}
v.nowPlaying = v.QueueGetSong()
go ChMessageSend(v.nowPlaying.ChannelID, "[**Music**] Playing, **`"+
v.nowPlaying.Title+"` - `("+v.nowPlaying.Duration+")` - **<@"+v.nowPlaying.ID+">\n") //*`"+ v.nowPlaying.User +"`***")
// If monoserver
if o.DiscordPlayStatus {
dg.UpdateStatus(0, v.nowPlaying.Title)
}
v.stop = false
v.skip = false
v.speaking = true
v.pause = false
v.voice.Speaking(true)
v.DCA(v.nowPlaying.VideoURL)
v.QueueRemoveFisrt()
if v.stop {
v.QueueRemove()
}
v.stop = false
v.skip = false
v.speaking = false
v.voice.Speaking(false)
}
}()
}
func (v *VoiceInstance) Radio(url string) {
v.audioMutex.Lock()
defer v.audioMutex.Unlock()
if o.DiscordPlayStatus {
dg.UpdateStatus(0, "Radio")
}
v.radioFlag = true
v.stop = false
v.speaking = true
v.pause = false
v.voice.Speaking(true)
v.DCA(url)
dg.UpdateStatus(0, o.DiscordStatus)
v.radioFlag = false
v.stop = false
v.speaking = false
v.voice.Speaking(false)
}
// DCA
func (v *VoiceInstance) DCA(url string) {
opts := dca.StdEncodeOptions
opts.RawOutput = true
opts.Bitrate = 96
opts.Application = "lowdelay"
encodeSession, err := dca.EncodeFile(url, opts)
if err != nil {
log.Println("FATA: Failed creating an encoding session: ", err)
}
v.encoder = encodeSession
done := make(chan error)
stream := dca.NewStream(encodeSession, v.voice, done)
v.stream = stream
for {
select {
case err := <-done:
if err != nil && err != io.EOF {
log.Println("FATA: An error occured", err)
}
// Clean up incase something happened and ffmpeg is still running
encodeSession.Cleanup()
return
}
}
}
// Stop stop the audio
func (v *VoiceInstance) Stop() {
v.stop = true
if v.encoder != nil {
v.encoder.Cleanup()
}
}
func (v *VoiceInstance) Skip() bool {
if v.speaking {
if v.pause {
return true
} else {
if v.encoder != nil {
v.encoder.Cleanup()
}
}
}
return false
}
// Pause pause the audio
func (v *VoiceInstance) Pause() {
v.pause = true
if v.stream != nil {
v.stream.SetPaused(true)
}
}
// Resume resume the audio
func (v *VoiceInstance) Resume() {
v.pause = false
if v.stream != nil {
v.stream.SetPaused(false)
}
}