Repository navigation
Expand file tree
/
Copy pathrepeat.go
More file actions
94 lines (88 loc) · 2.15 KB
/
Copy pathrepeat.go
File metadata and controls
94 lines (88 loc) · 2.15 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
83
84
85
86
87
88
89
90
91
92
93
94
package audio
import (
"context"
"io"
"sync"
"sync/atomic"
"pipelined.dev/pipe"
"pipelined.dev/pipe/mutable"
"pipelined.dev/signal"
)
// Repeater sinks the signal and sources it to multiple pipelines.
type Repeater struct {
m sync.Mutex
mut mutable.Context
bufferSize int
sampleRate signal.Frequency
channels int
sources []chan *message
}
type message struct {
buffer signal.Floating
sources int32
}
// Sink must be called once per repeater.
func (r *Repeater) Sink() pipe.SinkAllocatorFunc {
return func(mut mutable.Context, bufferSize int, props pipe.SignalProperties) (pipe.Sink, error) {
r.sampleRate = props.SampleRate
r.channels = props.Channels
r.bufferSize = bufferSize
p := signal.GetPoolAllocator(props.Channels, bufferSize, bufferSize)
return pipe.Sink{
SinkFunc: func(in signal.Floating) error {
r.m.Lock()
defer r.m.Unlock()
out := p.Float64()
signal.FloatingAsFloating(in, out)
for _, source := range r.sources {
source <- &message{
sources: int32(len(r.sources)),
buffer: out,
}
}
return nil
},
FlushFunc: func(ctx context.Context) error {
r.m.Lock()
defer r.m.Unlock()
for i := range r.sources {
close(r.sources[i])
}
r.sources = nil
return nil
},
}, nil
}
}
// Source must be called at least once per repeater.
func (r *Repeater) Source() pipe.SourceAllocatorFunc {
r.m.Lock()
defer r.m.Unlock()
source := make(chan *message, 1)
r.sources = append(r.sources, source)
return func(mut mutable.Context, bufferSize int) (pipe.Source, error) {
p := signal.GetPoolAllocator(r.channels, bufferSize, bufferSize)
var (
messagePtr *message
ok bool
)
return pipe.Source{
SourceFunc: func(b signal.Floating) (int, error) {
messagePtr, ok = <-source
if !ok {
return 0, io.EOF
}
read := signal.FloatingAsFloating(messagePtr.buffer, b)
if atomic.AddInt32(&messagePtr.sources, -1) == 0 {
messagePtr.buffer.Free(p)
}
return read, nil
},
SignalProperties: pipe.SignalProperties{
SampleRate: r.sampleRate,
Channels: r.channels,
},
},
nil
}
}