-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathsource.go
More file actions
82 lines (75 loc) · 1.76 KB
/
Copy pathsource.go
File metadata and controls
82 lines (75 loc) · 1.76 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
package audio
import (
"io"
"pipelined.dev/pipe"
"pipelined.dev/pipe/mutable"
"pipelined.dev/signal"
)
// Source implements signal source for any signal type.
func Source(sr signal.Frequency, s signal.Signal) pipe.SourceAllocatorFunc {
return func(mut mutable.Context, bufferSize int) (pipe.Source, error) {
return pipe.Source{
SourceFunc: signalSource(s),
SignalProperties: pipe.SignalProperties{
Channels: s.Channels(),
SampleRate: sr,
},
}, nil
}
}
func signalSource(s signal.Signal) (sourceFn pipe.SourceFunc) {
switch v := s.(type) {
case signal.Signed:
sourceFn = signedSource(v)
case signal.Unsigned:
sourceFn = unsignedSource(v)
case signal.Floating:
sourceFn = floatingSource(v)
}
return
}
func floatingSource(data signal.Floating) pipe.SourceFunc {
pos := 0
return func(out signal.Floating) (int, error) {
if pos == data.Length() {
return 0, io.EOF
}
end := pos + out.Length()
if end > data.Length() {
end = data.Length()
}
read := signal.FloatingAsFloating(data.Slice(pos, end), out)
pos += read
return read, nil
}
}
func signedSource(data signal.Signed) pipe.SourceFunc {
pos := 0
return func(out signal.Floating) (int, error) {
if pos == data.Length() {
return 0, io.EOF
}
end := pos + out.Length()
if end > data.Length() {
end = data.Length()
}
read := signal.SignedAsFloating(data.Slice(pos, end), out)
pos += read
return read, nil
}
}
func unsignedSource(data signal.Unsigned) pipe.SourceFunc {
pos := 0
return func(out signal.Floating) (int, error) {
if pos == data.Length() {
return 0, io.EOF
}
end := pos + out.Length()
if end > data.Length() {
end = data.Length()
}
read := signal.UnsignedAsFloating(data.Slice(pos, end), out)
pos += read
return read, nil
}
}