-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdecoder.go
83 lines (72 loc) · 1.53 KB
/
decoder.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
package caress
import (
"github.com/tkmn0/caress/opus"
)
type Decoder struct {
decoder *opus.Decoder
channels int
}
func NewDecoder(sampleRate uint32, channels uint16) (*Decoder, error) {
var err int32
d := opus.DecoderCreate(int32(sampleRate), int32(channels), &err)
if err != opus.Ok {
return nil, ErrorInitialize
} else {
return &Decoder{
decoder: d,
channels: int(channels),
}, nil
}
}
func (d *Decoder) Decode(buffer []byte, pcm []int16, fec bool) (int, error) {
if d.decoder == nil {
return 0, ErrorUnInitialized
}
if len(buffer) == 0 {
return 0, ErrorNoDataSupplied
}
if len(pcm) == 0 {
return 0, ErrorNoTargetbuffer
}
if cap(pcm)%d.channels != 0 {
return 0, ErrorSuppliedDataSize
}
var v int32 = 0
if fec {
v = 1
}
n := opus.Decode(d.decoder, data(buffer), int32(len(buffer)), pcm, int32(cap(pcm)/d.channels), v)
if n < 0 {
return 0, ErrorDecode
}
return int(n), nil
}
func (d *Decoder) DecodeFloat(buffer []byte, pcm []float32, fec bool) (int, error) {
if d.decoder == nil {
return 0, ErrorUnInitialized
}
if len(buffer) == 0 {
return 0, ErrorNoDataSupplied
}
if len(pcm) == 0 {
return 0, ErrorNoTargetbuffer
}
if cap(pcm)%d.channels != 0 {
return 0, ErrorSuppliedDataSize
}
var v int32 = 0
if fec {
v = 1
}
n := opus.DecodeFloat(d.decoder, data(buffer), int32(len(buffer)), pcm, int32(cap(pcm)/d.channels), v)
if n < 0 {
return 0, ErrorDecode
}
return int(n), nil
}
func (d *Decoder) Destroy() {
if d.decoder == nil {
return
}
opus.DecoderDestroy(d.decoder)
}