forked from jogramming/dca
-
Notifications
You must be signed in to change notification settings - Fork 0
/
dca.go
57 lines (44 loc) · 1.11 KB
/
dca.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
package dca
import (
"encoding/binary"
"errors"
"io"
"log"
"time"
)
// Define constants
const (
// The current version of the DCA format
FormatVersion int8 = 1
// The current version of the DCA program
LibraryVersion string = "0.0.5"
// The URL to the GitHub repository of DCA
GitHubRepositoryURL string = "https://github.com/kayabe/dca"
)
type OpusReader interface {
OpusFrame() (frame []byte, err error)
FrameDuration() time.Duration
}
var Logger *log.Logger
// logln logs to assigned logger or standard logger
func logln(s ...interface{}) {
if Logger != nil {
Logger.Println(s...)
return
}
log.Println(s...)
}
var ErrNegativeFrameSize = errors.New("frame size is negative, possibly corrupted")
// DecodeFrame decodes a dca frame from an io.Reader and returns the raw opus audio ready to be sent to discord
func DecodeFrame(r io.Reader) (frame []byte, err error) {
var size int16
if err = binary.Read(r, binary.LittleEndian, &size); err != nil {
return
}
if size < 0 {
return nil, ErrNegativeFrameSize
}
frame = make([]byte, size)
err = binary.Read(r, binary.LittleEndian, &frame)
return
}