This repository has been archived by the owner on Jul 11, 2020. It is now read-only.
forked from ljgago/MusicBot
-
Notifications
You must be signed in to change notification settings - Fork 4
/
audio.go
86 lines (69 loc) · 1.53 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
package main
import (
"github.com/jonas747/dca"
"io"
"log"
"time"
)
func (b *Bot) GlobalRadio() {
for {
select {
case radio := <-b.radioSignal:
radio.v.Stop()
time.Sleep(200 * time.Millisecond)
go b.Radio(radio.data, radio.v)
}
}
}
// Trigger to play radio.
func (b *Bot) Radio(url string, v *VoiceInstance) {
v.audioMutex.Lock()
defer v.audioMutex.Unlock()
if b.config.DiscordPlayStatus {
b.dg.UpdateStatus(0, v.station.Name)
}
log.Println("INFO: Playing URL ", url)
v.voice.Speaking(true)
v.is_playing = true
b.DCA(v, url)
v.is_playing = false
b.dg.UpdateStatus(0, b.config.DiscordStatus)
v.voice.Speaking(false)
}
// Connector to the DCA audio playback library
func (b *Bot) DCA(v *VoiceInstance, url string) {
opts := dca.StdEncodeOptions
opts.RawOutput = true
opts.Bitrate = 96
if v.volume != 0 {
opts.Volume = v.volume
} else {
opts.Volume = b.config.DiscordVolume
}
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 in case something happened and ffmpeg is still running
encodeSession.Cleanup()
return
}
}
}
// Stop the audio
func (v *VoiceInstance) Stop() {
v.is_playing = false
if v.encoder != nil {
v.encoder.Cleanup()
}
}