-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathencoder.go
223 lines (195 loc) · 4.98 KB
/
encoder.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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
package caress
import (
"github.com/tkmn0/caress/opus"
)
type Encoder struct {
encoder *opus.Encoder
numChannels int
}
func NewEncoder(sampleRate uint32, channels uint16, application int32) (*Encoder, error) {
var err int32
e := opus.EncoderCreate(int32(sampleRate), int32(channels), application, &err)
if err != opus.Ok {
return nil, ErrorInitialize
} else {
return &Encoder{
encoder: e,
numChannels: int(channels),
}, nil
}
}
func (e *Encoder) Encode(pcm []int16, buffer []byte) (int, error) {
if e.encoder == nil {
return 0, ErrorUnInitialized
}
if len(pcm) == 0 {
return 0, ErrorNoDataSupplied
}
if len(buffer) == 0 {
return 0, ErrorNoTargetbuffer
}
// libopus talks about samples as 1 sample containing multiple channels. So
// e.g. 20 samples of 2-channel data is actually 40 raw data points.
if len(pcm)%e.numChannels != 0 {
return 0, ErrorSuppliedDataSize
}
samples := len(pcm) / e.numChannels
n := opus.Encode(e.encoder, pcm, int32(samples), buffer, int32(cap(buffer)))
if n < 0 {
return 0, ErrorEncode
}
return int(n), nil
}
func (e *Encoder) EncodeFloat(pcm []float32, buffer []byte) (int, error) {
if e.encoder == nil {
return 0, ErrorUnInitialized
}
if len(pcm) == 0 {
return 0, ErrorNoDataSupplied
}
if len(buffer) == 0 {
return 0, ErrorNoTargetbuffer
}
// libopus talks about samples as 1 sample containing multiple channels. So
// e.g. 20 samples of 2-channel data is actually 40 raw data points.
if len(pcm)%e.numChannels != 0 {
return 0, ErrorSuppliedDataSize
}
samples := len(pcm) / e.numChannels
n := opus.EncodeFloat(e.encoder, pcm, int32(samples), buffer, int32(cap(buffer)))
if n < 0 {
return 0, ErrorEncode
}
return int(n), nil
}
func (e *Encoder) Setbitrate(bitrate int32) error {
if e.encoder == nil {
return ErrorUnInitialized
}
if bitrate > opus.MaxBitrate {
return ErrorSetBitrateInvalidSize
}
if bitrate < opus.MinBitrate {
return ErrorSetBitrateInvalidSize
}
result := opus.EncoderSetBitrate(e.encoder, bitrate)
if result != opus.Ok {
return ErrorSetBitrate
}
return nil
}
func (e *Encoder) GetBitrate() (int32, error) {
if e.encoder == nil {
return 0, ErrorUnInitialized
}
result, bitrate := opus.EncoderGetBitrate(e.encoder)
if result != opus.Ok {
return 0, ErrorGetBitrate
}
return bitrate, nil
}
// if cpu usage is so high, you can make complexity value low.
// complexity value range is 0 to 10
func (e *Encoder) SetComplexity(complexity int32) error {
if e.encoder == nil {
return ErrorUnInitialized
}
if complexity > opus.MaxComplexity {
return ErrorSetComplexityInvalidSize
}
if complexity < opus.MinComplexity {
return ErrorSetComplexityInvalidSize
}
result := opus.EncoderSetComplexity(e.encoder, complexity)
if result != opus.Ok {
return ErrorSetComplexity
}
return nil
}
func (e *Encoder) GetComplexity() (int32, error) {
if e.encoder == nil {
return 0, ErrorUnInitialized
}
result, complexity := opus.EncoderGetComplexity(e.encoder)
if result != opus.Ok {
return 0, ErrorGetComplexity
}
return complexity, nil
}
func (e *Encoder) SetSignal(signal int32) error {
if e.encoder == nil {
return ErrorUnInitialized
}
if !(signal == opus.SignalAuto || signal == opus.SignalMusic || signal == opus.SignalVoice) {
return ErrorSetSignalInvalidValue
}
result := opus.EncoderSetSignal(e.encoder, signal)
if result != opus.Ok {
return ErrorSetSignal
}
return nil
}
func (e *Encoder) GetSignal() (int32, error) {
if e.encoder == nil {
return 0, ErrorUnInitialized
}
result, signal := opus.EncoderGetSignal(e.encoder)
if result != opus.Ok {
return 0, ErrorGetSignal
}
return signal, nil
}
// this is for "Forwad error Correction". this config could be useful for udp networking.
// see https://ddanilov.me/how-to-enable-in-band-fec-for-opus-codec/
// default value is false
func (e *Encoder) SetInBandFEC(enable bool) error {
if e.encoder == nil {
return ErrorUnInitialized
}
result := opus.EncoderSetInBandFEC(e.encoder, enable)
if result != opus.Ok {
return ErrorSetInBandFEC
}
return nil
}
func (e *Encoder) GetInBandFEC() (bool, error) {
if e.encoder == nil {
return false, ErrorUnInitialized
}
result, enabled := opus.EncoderGetInBandFEC(e.encoder)
if result != opus.Ok {
return false, ErrorGetInBandFEC
}
return enabled, nil
}
// default is 0
// percentage range is 0 to 100
func (e *Encoder) SetPacketLossPercentage(perc int32) error {
if e.encoder == nil {
return ErrorUnInitialized
}
if perc > 100 || perc < 0 {
return ErrorSetPacketLossPercInvalidValue
}
result := opus.EncoderSetPacketLossPerc(e.encoder, perc)
if result != opus.Ok {
return ErrorSetPacketLossPerc
}
return nil
}
func (e *Encoder) GetPacketLossPercentage() (int32, error) {
if e.encoder == nil {
return 0, ErrorUnInitialized
}
result, perc := opus.EncoderGetPacketLossPerc(e.encoder)
if result != opus.Ok {
return 0, ErrorGetPacketLossPerc
}
return perc, nil
}
func (e *Encoder) Destroy() {
if e.encoder == nil {
return
}
opus.EncoderDestroy(e.encoder)
}