forked from gopxl/beep
-
Notifications
You must be signed in to change notification settings - Fork 0
/
resample_test.go
114 lines (100 loc) · 2.47 KB
/
resample_test.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
package beep_test
import (
"fmt"
"reflect"
"testing"
"github.com/gopxl/beep/v2"
"github.com/gopxl/beep/v2/internal/testtools"
)
func TestResample(t *testing.T) {
for _, numSamples := range []int{8, 100, 500, 1000, 50000} {
for _, old := range []beep.SampleRate{100, 2000, 44100, 48000} {
for _, new := range []beep.SampleRate{100, 2000, 44100, 48000} {
if numSamples/int(old)*int(new) > 1e6 {
continue // skip too expensive combinations
}
t.Run(fmt.Sprintf("numSamples_%d_old_%d_new_%d", numSamples, old, new), func(t *testing.T) {
s, data := testtools.RandomDataStreamer(numSamples)
want := resampleCorrect(3, old, new, data)
got := testtools.Collect(beep.Resample(3, old, new, s))
if !reflect.DeepEqual(want, got) {
t.Fatal("Resample not working correctly")
}
})
}
}
}
}
func resampleCorrect(quality int, old, new beep.SampleRate, p [][2]float64) [][2]float64 {
ratio := float64(old) / float64(new)
pts := make([]point, quality*2)
var resampled [][2]float64
for i := 0; ; i++ {
j := float64(i) * ratio
if int(j) >= len(p) {
break
}
var sample [2]float64
for c := range sample {
for k := range pts {
l := int(j) + k - (len(pts)-1)/2
if l >= 0 && l < len(p) {
pts[k] = point{X: float64(l), Y: p[l][c]}
} else {
pts[k] = point{X: float64(l), Y: 0}
}
}
startK := 0
for k, pt := range pts {
if pt.X >= 0 {
startK = k
break
}
}
endK := 0
for k, pt := range pts {
if pt.X < float64(len(p)) {
endK = k + 1
}
}
y := lagrange(pts[startK:endK], j)
sample[c] = y
}
resampled = append(resampled, sample)
}
return resampled
}
func lagrange(pts []point, x float64) (y float64) {
y = 0.0
for j := range pts {
l := 1.0
for m := range pts {
if j == m {
continue
}
l *= (x - pts[m].X) / (pts[j].X - pts[m].X)
}
y += pts[j].Y * l
}
return y
}
type point struct {
X, Y float64
}
func FuzzResampler_SetRatio(f *testing.F) {
f.Add(44100, 48000, 0.5, 1.0, 8.0)
f.Fuzz(func(t *testing.T, original, desired int, r1, r2, r3 float64) {
if original <= 0 || desired <= 0 || r1 <= 0 || r2 <= 0 || r3 <= 0 {
t.Skip()
}
s, _ := testtools.RandomDataStreamer(1e4)
r := beep.Resample(4, beep.SampleRate(original), beep.SampleRate(desired), s)
testtools.CollectNum(1024, r)
r.SetRatio(r1)
testtools.CollectNum(1024, r)
r.SetRatio(r2)
testtools.CollectNum(1024, r)
r.SetRatio(r3)
testtools.CollectNum(1024, r)
})
}