-
Notifications
You must be signed in to change notification settings - Fork 0
/
sound.go
58 lines (48 loc) · 1.12 KB
/
sound.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
package main
import (
"encoding/binary"
"errors"
"github.com/TheTipo01/roberto/queue"
"io"
)
// Plays a song in DCA format
func playSound(guildID string, el *queue.Element) bool {
var (
opusLen int16
err error
)
if server[guildID].vc == nil {
return false
}
_ = server[guildID].vc.Speaking(true)
for {
select {
case <-server[guildID].skip:
cleanUp(guildID, el.Closer)
return true
default:
// Read opus frame length from dca file.
err = binary.Read(el.Reader, binary.LittleEndian, &opusLen)
// If this is the end of the file, just return.
if errors.Is(err, io.EOF) || errors.Is(err, io.ErrUnexpectedEOF) {
cleanUp(guildID, el.Closer)
return false
}
// Read encoded pcm from dca file.
InBuf := make([]byte, opusLen)
err = binary.Read(el.Reader, binary.LittleEndian, &InBuf)
// Should not be any end of file errors
if err != nil {
cleanUp(guildID, el.Closer)
return false
}
server[guildID].vc.OpusSend <- InBuf
}
}
}
func cleanUp(guildID string, closer io.Closer) {
_ = server[guildID].vc.Speaking(false)
if closer != nil {
_ = closer.Close()
}
}