-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathilbc.go
executable file
·66 lines (51 loc) · 1.17 KB
/
ilbc.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
package ilbc
/*
#cgo CFLAGS: -I./lib/iLBC_rfc3951
#include <stdlib.h>
#include <math.h>
#include <string.h>
#include "ilbc.h"
void init(int mode);
int encode(unsigned char *samples, int sampleOffset, int sampleLength,
unsigned char *data, int dataOffset);
int decode(unsigned char *data, int dataOffset, int dataLength,
unsigned char *samples, int sampleOffset);
*/
import "C"
import (
"unsafe"
)
var iLBCHeaderPrefix = "#!iLBC"
var iLBCHeaderFormat = iLBCHeaderPrefix + "%d\n"
type frameMode int
const (
FrameMode20 frameMode = 20
FrameMode30 frameMode = 30
)
type codec struct {
mode int
}
func (d *codec) encode(samples []byte) (data []byte) {
data = make([]byte, len(samples))
C.init(C.int(d.mode))
encoded := C.encode(
(*C.uchar)(unsafe.Pointer(&samples[0])),
C.int(0),
C.int(len(samples)),
(*C.uchar)(unsafe.Pointer(&data[0])),
C.int(0),
)
return data[:encoded]
}
func (d *codec) decode(data []byte) (samples []byte) {
samples = make([]byte, len(data)*10)
C.init(C.int(d.mode))
decoded := C.decode(
(*C.uchar)(unsafe.Pointer(&data[0])),
C.int(0),
C.int(len(data)),
(*C.uchar)(unsafe.Pointer(&samples[0])),
C.int(0),
)
return samples[:decoded]
}