-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmultistream_decode.go
More file actions
79 lines (67 loc) · 1.98 KB
/
multistream_decode.go
File metadata and controls
79 lines (67 loc) · 1.98 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
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
package gopus
import "github.com/thesyncim/gopus/multistream"
func (d *MultistreamDecoder) decodeFrameSize(data []byte) (int, error) {
if data == nil {
return d.lastFrameSize, nil
}
if len(data) == 0 {
return 0, multistream.ErrPacketTooShort
}
return multistream.PacketDuration(data, d.dec.Streams())
}
// Decode decodes an Opus multistream packet into float32 PCM samples.
//
// data: Opus multistream packet data, or nil for Packet Loss Concealment (PLC).
// pcm: Output buffer for decoded samples. Must be large enough to hold
// frameSize * channels samples.
//
// Returns the number of samples per channel decoded, or an error.
//
// When data is nil, the decoder performs packet loss concealment using
// the last successfully decoded frame parameters.
func (d *MultistreamDecoder) Decode(data []byte, pcm []float32) (int, error) {
frameSize, err := d.decodeFrameSize(data)
if err != nil {
return 0, err
}
needed := frameSize * d.channels
if len(pcm) < needed {
return 0, ErrBufferTooSmall
}
samples, err := d.dec.DecodeToFloat32(data, frameSize)
if err != nil {
return 0, err
}
copy(pcm, samples)
if data != nil && len(data) > 0 {
d.lastFrameSize = frameSize
}
return len(samples) / d.channels, nil
}
// DecodeInt16 decodes an Opus multistream packet into int16 PCM samples.
//
// data: Opus multistream packet data, or nil for PLC.
// pcm: Output buffer for decoded samples.
//
// Returns the number of samples per channel decoded, or an error.
func (d *MultistreamDecoder) DecodeInt16(data []byte, pcm []int16) (int, error) {
frameSize, err := d.decodeFrameSize(data)
if err != nil {
return 0, err
}
needed := frameSize * d.channels
if len(pcm) < needed {
return 0, ErrBufferTooSmall
}
samples, err := d.dec.Decode(data, frameSize)
if err != nil {
return 0, err
}
for i := 0; i < frameSize*d.channels; i++ {
pcm[i] = float64ToInt16(samples[i])
}
if data != nil && len(data) > 0 {
d.lastFrameSize = frameSize
}
return len(samples) / d.channels, nil
}