Repository navigation
Expand file tree
/
Copy pathdecoder_api_rate_reference_float_test.go
More file actions
208 lines (190 loc) · 6.58 KB
/
Copy pathdecoder_api_rate_reference_float_test.go
File metadata and controls
208 lines (190 loc) · 6.58 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
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
//go:build !gopus_fixed_point
package gopus
import (
"fmt"
"github.com/thesyncim/gopus/internal/libopustest"
"math"
"testing"
)
func apiRateReferenceHelperPath() (string, error) {
return libopusAPIRateRefdecodeHelper.Path(func() (string, error) {
return libopustest.BuildPublicAPIHelper(libopustest.CHelperConfig{
Label: "api-rate reference decode",
OutputBase: "gopus_libopus_refdecode_api_rate",
SourceFile: "libopus_refdecode_single.c",
CFlags: []string{"-O3", "-DNDEBUG"},
})
})
}
func TestCELTMonoRecoveryAfterLongGapMatchesLibopus(t *testing.T) {
requireLibopusAPIRateRefdecodeHelper(t)
type spec struct {
name string
bw Bandwidth
channels int
bitrate int
frameMs ExpertFrameDuration
frameSamp int
lossStart int
lossEnd int // exclusive
nFrames int
}
specs := []spec{
{"swb_mono_20ms_longgap", BandwidthSuperwideband, 1, 64000, ExpertFrameDuration20Ms, 960, 8, 20, 26},
{"swb_mono_10ms_longgap", BandwidthSuperwideband, 1, 64000, ExpertFrameDuration10Ms, 480, 8, 24, 30},
{"fb_mono_20ms_longgap", BandwidthFullband, 1, 96000, ExpertFrameDuration20Ms, 960, 8, 22, 28},
{"wb_mono_20ms_longgap", BandwidthWideband, 1, 48000, ExpertFrameDuration20Ms, 960, 8, 20, 26},
// Burst (stays in periodic PLC) — already bit-exact, guards against
// regressing the periodic path while fixing the noise path.
{"swb_mono_20ms_burst", BandwidthSuperwideband, 1, 64000, ExpertFrameDuration20Ms, 960, 10, 15, 24},
// Stereo long-gap — unaffected by the mono shadow, must stay bit-exact.
{"swb_stereo_20ms_longgap", BandwidthSuperwideband, 2, 128000, ExpertFrameDuration20Ms, 960, 8, 20, 26},
}
const sampleRate = 48000
for _, sp := range specs {
t.Run(sp.name, func(t *testing.T) {
packets := encodeCELTMonoRecoveryStream(t, sp.bw, sp.channels, sp.bitrate, sp.frameMs, sp.frameSamp, sp.nFrames)
mask := make([]bool, sp.nFrames)
for i := sp.lossStart; i < sp.lossEnd; i++ {
mask[i] = true
}
steps, recoveryStepIdx := buildCELTLossPlan(packets, mask)
if recoveryStepIdx < 0 {
t.Fatalf("%s: no recovery step in plan", sp.name)
}
want, wantRanges, err := decodeWithLibopusReferenceAPIRateFloat32StepsRanges(sampleRate, sp.channels, sp.frameSamp, steps)
if err != nil {
t.Fatalf("%s: oracle unavailable: %v", sp.name, err)
}
if len(want) != len(steps)*sp.frameSamp*sp.channels || len(wantRanges) != len(steps) {
t.Fatalf("%s: reference samples/ranges=%d/%d want=%d/%d", sp.name,
len(want), len(wantRanges), len(steps)*sp.frameSamp*sp.channels, len(steps))
}
dec, err := NewDecoder(DefaultDecoderConfig(sampleRate, sp.channels))
if err != nil {
t.Fatalf("NewDecoder: %v", err)
}
buf := make([]float32, sp.frameSamp*sp.channels)
off := 0
for i, s := range steps {
n, e := dec.Decode(s.packet, buf)
if e != nil {
t.Fatalf("%s: step %d decode: %v", sp.name, i, e)
}
if n != sp.frameSamp {
t.Fatalf("%s: step %d samples=%d want %d", sp.name, i, n, sp.frameSamp)
}
got := buf[:n*sp.channels]
wantSlice := want[off : off+n*sp.channels]
off += n * sp.channels
if gr := dec.FinalRange(); gr != wantRanges[i] {
t.Fatalf("%s: step %d final range gopus=%d libopus=%d (entropy desync)",
sp.name, i, gr, wantRanges[i])
}
kind := "normal"
if s.packet == nil {
kind = "PLC"
} else if i == recoveryStepIdx {
kind = "RECOVERY"
}
assertAPIRateFloat32BitsExact(t, got, wantSlice, fmt.Sprintf("%s step %d (%s)", sp.name, i, kind))
}
})
}
}
// encodeCELTMonoRecoveryStream encodes a continuous voiced tone as a stateful
// CELT packet stream so the decoder builds real pitch/energy state across frames.
func encodeCELTMonoRecoveryStream(t *testing.T, bw Bandwidth, channels, bitrate int, frameMs ExpertFrameDuration, frameSamp, nFrames int) [][]byte {
t.Helper()
const sampleRate = 48000
enc, err := NewEncoder(EncoderConfig{
SampleRate: sampleRate,
Channels: channels,
Application: ApplicationRestrictedCelt,
})
if err != nil {
t.Fatalf("NewEncoder: %v", err)
}
if err := enc.SetMode(EncoderModeCELT); err != nil {
t.Fatalf("SetMode CELT: %v", err)
}
if err := enc.SetFrameSize(frameSamp); err != nil {
t.Fatalf("SetFrameSize(%d): %v", frameSamp, err)
}
if err := enc.SetExpertFrameDuration(frameMs); err != nil {
t.Fatalf("SetExpertFrameDuration: %v", err)
}
if err := enc.SetBandwidth(bw); err != nil {
t.Fatalf("SetBandwidth: %v", err)
}
if err := enc.SetBitrate(bitrate); err != nil {
t.Fatalf("SetBitrate: %v", err)
}
_ = enc.SetSignal(SignalMusic)
if channels == 2 {
if err := enc.SetForceChannels(2); err != nil {
t.Fatalf("SetForceChannels: %v", err)
}
}
packets := make([][]byte, 0, nFrames)
for f := range nFrames {
pcm := celtRecoveryTonePCM(frameSamp, channels, sampleRate, f)
pkt, e := encodeOneFrame(enc, pcm)
if e != nil {
t.Fatalf("encode frame %d: %v", f, e)
}
if len(pkt) == 0 {
t.Fatalf("encoder produced empty packet at frame %d", f)
}
packets = append(packets, append([]byte(nil), pkt...))
}
return packets
}
func celtRecoveryTonePCM(frameSamples, channels, sampleRate, frameOffset int) []float32 {
pcm := make([]float32, frameSamples*channels)
base := 220.0
for i := range frameSamples {
gi := frameOffset*frameSamples + i
tm := float64(gi) / float64(sampleRate)
f0 := base * (1.0 + 0.02*math.Sin(2*math.Pi*3.0*tm))
for c := range channels {
ph := float64(c) * 0.13
v := 0.42*math.Sin(2*math.Pi*f0*tm+ph) +
0.18*math.Sin(2*math.Pi*2*f0*tm+0.21+ph) +
0.08*math.Sin(2*math.Pi*3*f0*tm+0.43+ph)
pcm[i*channels+c] = float32(v)
}
}
return pcm
}
// buildCELTLossPlan replays opus_demo.c's loss handling for a CELT stream (no
// LBRR, so every lost frame is a PLC step): after a non-lost packet that follows
// lostCount losses, run 1+lostCount decode calls (lostCount PLC then 1 normal).
// Returns the flat step list and the index of the first post-gap recovery step.
func buildCELTLossPlan(packets [][]byte, mask []bool) ([]libopusAPIRateDecodeStep, int) {
var steps []libopusAPIRateDecodeStep
lost := 0
recoveryStepIdx := -1
for i, pkt := range packets {
if mask[i] {
lost++
continue
}
runDecoder := 1 + lost
for fr := range runDecoder {
if fr < lost {
steps = append(steps, libopusAPIRateDecodeStep{packet: nil})
} else {
if lost > 0 && recoveryStepIdx < 0 {
recoveryStepIdx = len(steps)
}
steps = append(steps, libopusAPIRateDecodeStep{packet: pkt})
}
}
lost = 0
}
return steps, recoveryStepIdx
}
func privateFloatCELTAPIRateReferenceHelperPath() (string, error) {
return apiRateReferenceHelperPath()
}