-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaud.go
133 lines (121 loc) · 2.36 KB
/
aud.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
package aud
// Source represents a sample source. Source is rate-agnostic, so the
// appropriate interpolation/compression should be applied to normalize all
// sources.
type Source interface {
// Next should be implemented to return the next data point from a Source.
// If the source is drained, eof will be true.
Next() (s Sample, eof bool)
}
// Sample represents a sequential datapoint in the sample space.
type Sample float64
// Zero represents the zero valued sample.
const Zero Sample = 0
// UInt8Source represents an uint8 sample source.
type UInt8Source interface {
Next() (s uint8, eof bool)
}
// Int16Source represents an int16 sample source.
type Int16Source interface {
Next() (s int16, eof bool)
}
// ForEach applies a function to each sample in a Source.
func ForEach(src Source, fn func(Sample)) {
for {
s, eof := src.Next()
if eof {
break
}
fn(s)
}
}
// Max finds the maximum sample value of a Source.
func Max(src Source) Sample {
max, eof := src.Next()
if eof {
return max
}
for {
s, eof := src.Next()
if eof {
break
}
if s > max {
max = s
}
}
return max
}
// Min finds the minimum sample value of a Source.
func Min(src Source) Sample {
min, eof := src.Next()
if eof {
return min
}
for {
s, eof := src.Next()
if eof {
break
}
if s < min {
min = s
}
}
return min
}
// ForEachUInt8 applies a function to each sample in a UInt8Source.
func ForEachUInt8(src UInt8Source, fn func(uint8)) {
for {
s, eof := src.Next()
if eof {
break
}
fn(s)
}
}
// ForEachUInt8Pair applies a function to a pair of UInt8Source objects.
func ForEachUInt8Pair(left, right UInt8Source, fn func(uint8, uint8)) {
for {
eofCount := 0
left, eof := left.Next()
if eof {
eofCount++
}
right, eof := right.Next()
if eof {
eofCount++
}
if eofCount == 2 {
break
}
fn(left, right)
}
}
// ForEachInt16 applies a function to each sample in an Int16Source.
func ForEachInt16(src Int16Source, fn func(int16)) {
for {
s, eof := src.Next()
if eof {
break
}
fn(s)
}
}
// ForEachInt16Pair applies a function to a pair of Int16Source objects.
func ForEachInt16Pair(left, right Int16Source, fn func(int16, int16)) {
for {
eofCount := 0
left, eof := left.Next()
if eof {
eofCount++
}
right, eof := right.Next()
if eof {
eofCount++
}
if eofCount == 2 {
break
}
fn(left, right)
}
}