-
Notifications
You must be signed in to change notification settings - Fork 0
/
audiostream.go
77 lines (66 loc) · 1.51 KB
/
audiostream.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
package disgo
import (
"io"
"github.com/bwmarrin/discordgo"
"github.com/ennes/dca"
)
type (
AudioStream struct {
Voice *discordgo.VoiceConnection
streamingSession *dca.StreamingSession
done chan error
}
)
const (
CHANNELS int = 2
FRAME_RATE int = 48000
FRAME_SIZE int = 960
MAX_BYTES int = (FRAME_SIZE * 2) * 2
)
func NewAudioStream(vc *discordgo.VoiceConnection) *AudioStream {
return &AudioStream{
Voice: vc,
}
}
func (audioS *AudioStream) Play(s Song) error {
url, err := s.URL()
if err != nil {
return err
}
options := dca.StdEncodeOptions
options.RawOutput = true
options.Bitrate = 96
options.Application = "lowdelay"
encodingSession, err := dca.EncodeFile(url, options)
if err != nil {
return err
}
defer encodingSession.Cleanup()
audioS.done = make(chan error)
audioS.streamingSession = dca.NewStream(encodingSession, audioS.Voice, audioS.done)
err = <-audioS.done
if err != nil && err != io.EOF {
return err
}
return nil
}
func (audioS *AudioStream) IsRunning() bool {
if audioS.streamingSession == nil {
return false
}
finished, _ := audioS.streamingSession.Finished()
return !finished
}
func (audioS *AudioStream) Pause() {
audioS.streamingSession.SetPaused(true)
}
func (audioS *AudioStream) Resume() {
audioS.streamingSession.SetPaused(false)
}
func (audioS *AudioStream) Stop() {
if !audioS.IsRunning() {
return
}
audioS.done <- io.EOF // By sending EOF, the stream will stop
audioS.streamingSession = nil // Clean up the session
}