forked from padster/go-sound
-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.go
198 lines (170 loc) · 3.24 KB
/
utils.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
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
package cq
import (
"encoding/binary"
// "fmt"
"io"
"math"
"math/cmplx"
)
func GenerateHeights(octaves int) func() int {
limit := unsafeShift(octaves - 1)
at := limit
return func() int {
result := TerminalZeros(at) + 1
if at == limit {
at = 0
}
at++
return result
}
}
func TerminalZeros(val int) int {
cnt := 0
for ; (val & 1) == 0; cnt++ {
val /= 2
}
return cnt
}
// Math Utils
func mean(fs []float64) float64 {
s := 0.0
for _, v := range fs {
s += v
}
return s / float64(len(fs))
}
func maxidx(row []complex128) int {
idx, max := 0, cmplx.Abs(row[0])
for i, v := range row {
vAbs := cmplx.Abs(v)
if vAbs > max {
idx, max = i, vAbs
}
}
return idx
}
func complexTimes(c complex128, f float64) complex128 {
return complex(real(c)*f, imag(c)*f)
}
// IsPowerOf2 returns true if x is a power of 2, else false.
func isPowerOf2(x int) bool {
return x&(x-1) == 0
}
// NextPowerOf2 returns the next power of 2 >= x.
func nextPowerOf2(x int) int {
if isPowerOf2(x) {
return x
}
return int(math.Pow(2, math.Ceil(math.Log2(float64(x)))))
}
func round(x float64) int {
return int(x + 0.5)
}
func roundUp(x float64) int {
return int(math.Ceil(x))
}
func roundDown(x float64) int {
return int(x)
}
func unsafeShift(s int) int {
return 1 << uint(s)
}
// TODO: Move into global exported utils
func UnsafeShift(s int) int {
return unsafeShift(s)
}
func clampUnit(v float64) float64 {
switch {
case v > 1.0:
return 1.0
case v < -1.0:
return -1.0
default:
return v
}
}
func realParts(values []complex128) []float64 {
n := len(values)
reals := make([]float64, n, n)
for i, c := range values {
reals[i] = real(c)
}
return reals
}
func minInt(a, b int) int {
if a < b {
return a
} else {
return b
}
}
func maxInt(a, b int) int {
if a > b {
return a
} else {
return b
}
}
func calcGcd(a int, b int) int {
if b == 0 {
return a
}
return calcGcd(b, a%b)
}
func bessel0(x float64) float64 {
b := 0.0
for i := 0; i < 20; i++ {
b += besselTerm(x, i)
}
return b
}
func besselTerm(x float64, i int) float64 {
if i == 0 {
return 1.0
}
f := float64(factorial(i))
return math.Pow(x/2.0, float64(i)*2.0) / (f * f)
}
func factorial(i int) int {
if i == 0 {
return 1
}
return i * factorial(i-1)
}
// IO Utils
func WriteComplexArray(w io.Writer, array []complex128) {
// Uncomment for a weird effect going through the writecq -> readcq cycle
// WriteInt32(w, int32(len(array)))
for _, c := range array {
WriteComplex(w, c)
}
}
func WriteComplex(w io.Writer, c complex128) {
WriteFloat32(w, float32(real(c)))
WriteFloat32(w, float32(imag(c)))
}
func WriteInt32(w io.Writer, i int32) {
binary.Write(w, binary.LittleEndian, i)
}
func WriteFloat32(w io.Writer, f float32) {
binary.Write(w, binary.LittleEndian, f)
}
func WriteByte(w io.Writer, b byte) {
binary.Write(w, binary.LittleEndian, b)
}
func ReadComplexArray(r io.Reader, size int) []complex128 {
array := make([]complex128, size, size)
for i := 0; i < size; i++ {
array[i] = ReadComplex(r)
}
return array
}
func ReadComplex(r io.Reader) complex128 {
f1, f2 := ReadFloat32(r), ReadFloat32(r)
return complex(float64(f1), float64(f2))
}
func ReadFloat32(r io.Reader) float32 {
var f float32
binary.Read(r, binary.LittleEndian, &f)
return f
}