-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathopus_decoder.go
More file actions
54 lines (44 loc) · 1 KB
/
opus_decoder.go
File metadata and controls
54 lines (44 loc) · 1 KB
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
//go:build (amd64 && windows) || (amd64 && linux)
package audio_transcoder
/*
#include "opus_decoder.c"
*/
import "C"
import (
"fmt"
"unsafe"
)
func init() {
RegisterDecoder("OPUS", &OpusDecoder{})
}
type OpusDecoder struct {
dec unsafe.Pointer
sampleRate int
channels int
}
func (d *OpusDecoder) Decode(pkt []byte, pcm []byte) (int, error) {
n := C.opus_decoder_decode(d.dec, (*C.uint8_t)(&pkt[0]), C.int(len(pkt)), (*C.uint8_t)(&pcm[0]), C.int(OpusFrameSize))
return int(n), nil
}
func (d *OpusDecoder) Destroy() {
if d.dec != nil {
C.opus_decoder_destroy2(d.dec)
d.dec = nil
}
}
func (d *OpusDecoder) Create(sampleRate, channel int) error {
decoder := C.opus_decoder_create2(C.int(sampleRate), C.int(channel))
if decoder == nil {
return fmt.Errorf("failed to create opus decoder")
}
d.dec = decoder
d.sampleRate = sampleRate
d.channels = channel
return nil
}
func (d *OpusDecoder) SampleRate() int {
return d.sampleRate
}
func (d *OpusDecoder) Channels() int {
return d.channels
}