/
opus_audio_encoder.go
86 lines (69 loc) · 1.89 KB
/
opus_audio_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
package broadcast
import (
"errors"
"flag"
"strings"
)
type OpusAudioEncoder struct {
Bitrate int
opusEncoder *OpusEncoder
}
func (encoder *OpusAudioEncoder) Init() error {
if encoder.Bitrate == 0 {
encoder.Bitrate = 256000
}
opusEncoder, err := OpusEncoderCreate(encoder.Bitrate)
if err != nil {
return err
}
encoder.opusEncoder = opusEncoder
return nil
}
func (encoder *OpusAudioEncoder) Destroy() {
encoder.opusEncoder.Destroy()
}
func (encoder *OpusAudioEncoder) Encode(audio *Audio) ([]byte, error) {
opusBytes := make([]byte, 2048)
encodedLength, err := encoder.opusEncoder.EncodeFloat(audio.InterleavedFloats(), audio.SampleCount(), opusBytes, int32(len(opusBytes)))
if err != nil {
return nil, err
}
return opusBytes[:encodedLength], nil
}
type OpusAudioDecoder struct {
opusDecoder *OpusDecoder
}
func (decoder *OpusAudioDecoder) Init() error {
opusDecoder, err := OpusDecoderCreate()
if err != nil {
return err
}
decoder.opusDecoder = opusDecoder
return nil
}
func (decoder *OpusAudioDecoder) Destroy() {
decoder.opusDecoder.Destroy()
}
func (decoder *OpusAudioDecoder) Decode(data []byte) (*Audio, error) {
frameCount := 960
samples := make([]float32, frameCount*2)
decodedFrameCount, err := decoder.opusDecoder.DecodeFloat(data, samples, frameCount)
if err != nil {
return nil, err
}
if int(decodedFrameCount) != frameCount {
return nil, errors.New("Can't decode expected frameCount")
}
audio := NewAudio(frameCount, 2)
audio.LoadInterleavedFloats(samples, frameCount, 2)
return audio, nil
}
type OpusAudioEncoderConfig struct {
Bitrate int
}
func (config *OpusAudioEncoderConfig) Flags(flags *flag.FlagSet, prefix string) {
flags.IntVar(&config.Bitrate, strings.Join([]string{prefix, "bitrate"}, "-"), 256000, "The Opus stream bitrate")
}
func (config *OpusAudioEncoderConfig) Apply(opusEncoder *OpusAudioEncoder) {
opusEncoder.Bitrate = config.Bitrate
}