forked from hraban/opus
-
Notifications
You must be signed in to change notification settings - Fork 1
/
utils_test.go
67 lines (59 loc) · 1.4 KB
/
utils_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
// Copyright © Go Opus Authors (see AUTHORS file)
//
// License for use of this code is detailed in the LICENSE file
package opus
import (
"math"
)
// utility functions for unit tests
func addSineFloat32(buf []float32, sampleRate int, freq float64) {
factor := 2 * math.Pi * freq / float64(sampleRate)
for i := range buf {
buf[i] += float32(math.Sin(float64(i) * factor))
}
}
func addSine(buf []int16, sampleRate int, freq float64) {
factor := 2 * math.Pi * freq / float64(sampleRate)
for i := range buf {
buf[i] += int16(math.Sin(float64(i)*factor) * (math.MaxInt16 - 1))
}
}
func maxDiff(a []int16, b []int16) int32 {
if len(a) != len(b) {
return math.MaxInt16
}
var max int32 = 0
for i := range a {
d := int32(a[i]) - int32(b[i])
if d < 0 {
d = -d
}
if d > max {
max = d
}
}
return max
}
func interleave(a []int16, b []int16) []int16 {
if len(a) != len(b) {
panic("interleave: buffers must have equal length")
}
result := make([]int16, 2*len(a))
for i := range a {
result[2*i] = a[i]
result[2*i+1] = b[i]
}
return result
}
func split(interleaved []int16) ([]int16, []int16) {
if len(interleaved)%2 != 0 {
panic("split: interleaved buffer must have even number of samples")
}
left := make([]int16, len(interleaved)/2)
right := make([]int16, len(interleaved)/2)
for i := range left {
left[i] = interleaved[2*i]
right[i] = interleaved[2*i+1]
}
return left, right
}