forked from dustinblackman/speakerbot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvoice.go
186 lines (156 loc) · 3.97 KB
/
voice.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
package main
import (
"encoding/binary"
"fmt"
"io"
"log"
"net/http"
"os/exec"
"runtime"
"strconv"
"strings"
"github.com/bwmarrin/discordgo"
"github.com/oleiade/lane"
)
var (
run *exec.Cmd
voiceInstances = map[string]*VoiceInstance{}
)
const (
channels int = 2
frameRate int = 48000
frameSize int = 960
)
// VoiceInstance is created for each connected server
type VoiceInstance struct {
discord *discordgo.Session
queue *lane.Queue
pcmChannel chan []int16
serverID string
skip bool
stop bool
trackPlaying bool
}
func (vi *VoiceInstance) playVideo(url string) {
vi.trackPlaying = true
resp, err := http.Get(url)
if err != nil {
log.Printf("Http.Get\nerror: %s\ntarget: %s\n", err, url)
return
}
defer resp.Body.Close()
if resp.StatusCode != 200 {
log.Printf("reading answer: non 200 status code received: '%s'", err)
}
run = exec.Command("ffmpeg", "-i", "-", "-f", "s16le", "-ar", strconv.Itoa(frameRate), "-ac", strconv.Itoa(channels), "pipe:1")
run.Stdin = resp.Body
stdout, err := run.StdoutPipe()
if err != nil {
fmt.Println("StdoutPipe Error:", err)
return
}
err = run.Start()
if err != nil {
fmt.Println("RunStart Error:", err)
return
}
// buffer used during loop below
audiobuf := make([]int16, frameSize*channels)
vi.discord.Voice.Speaking(true)
defer vi.discord.Voice.Speaking(false)
for {
// read data from ffmpeg stdout
err = binary.Read(stdout, binary.LittleEndian, &audiobuf)
if err == io.EOF || err == io.ErrUnexpectedEOF {
break
}
if err != nil {
fmt.Println("error reading from ffmpeg stdout :", err)
break
}
if vi.stop == true || vi.skip == true {
run.Process.Kill()
break
}
vi.pcmChannel <- audiobuf
}
vi.trackPlaying = false
}
// StopVideo marks to stop all tracks and clears queue on the next binary read.
func (vi *VoiceInstance) StopVideo() {
vi.stop = true
}
// SkipVideo skips the current playing track
func (vi *VoiceInstance) SkipVideo() {
vi.skip = true
}
func (vi *VoiceInstance) connectVoice() {
vi.discord, _ = discordgo.New(*email, *password)
// Open the websocket and begin listening.
err := vi.discord.Open()
if err != nil {
fmt.Println(err)
}
channels, err := vi.discord.GuildChannels(vi.serverID)
var voiceChannel string
voiceChannels := []string{}
for _, channel := range channels {
if channel.Type == "voice" {
voiceChannels = append(voiceChannels, channel.ID)
if strings.Contains(strings.ToLower(channel.Name), "music") && voiceChannel == "" {
voiceChannel = channel.ID
}
}
}
if voiceChannel == "" {
fmt.Println("Selecting first channel")
voiceChannel = voiceChannels[0]
}
err = vi.discord.ChannelVoiceJoin(vi.serverID, voiceChannel, false, true)
if err != nil {
fmt.Println(err)
return
}
// Hacky loop to prevent returning when voice isn't ready
// TODO: Find a better way.
for vi.discord.Voice.Ready == false {
runtime.Gosched()
}
}
// QueueVideo places a Youtube link in a queue
func (vi *VoiceInstance) QueueVideo(youtubeLink string) {
fmt.Println("Queuing video")
vi.queue.Enqueue(youtubeLink)
}
func (vi *VoiceInstance) processQueue() {
if vi.trackPlaying == false {
for {
vi.skip = false
link := vi.queue.Dequeue()
if link == nil || vi.stop == true {
break
}
vi.playVideo(link.(string))
}
// No more tracks in queue? Cleanup.
fmt.Println("Closing connections")
close(vi.pcmChannel)
vi.discord.Voice.Close()
vi.discord.Close()
delete(voiceInstances, vi.serverID)
fmt.Println("Done")
}
}
// CreateVoiceInstance accepts both a youtube query and a server id, boots up the voice connection, and plays the track.
func CreateVoiceInstance(youtubeLink string, serverID string) {
vi := new(VoiceInstance)
voiceInstances[serverID] = vi
fmt.Println("Connecting Voice...")
vi.serverID = serverID
vi.queue = lane.NewQueue()
vi.connectVoice()
vi.pcmChannel = make(chan []int16, 2)
go SendPCM(vi.discord.Voice, vi.pcmChannel)
vi.QueueVideo(youtubeLink)
vi.processQueue()
}