-
Notifications
You must be signed in to change notification settings - Fork 4
/
delay.go
221 lines (193 loc) · 4.53 KB
/
delay.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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
package snd
import (
"log"
"time"
)
// bufc implements a circular buffer.
type bufc struct {
xs []float64
r, w int
}
// newbufc returns buffer of length n with read offset r.
func newbufc(n int, r int) *bufc { return &bufc{make([]float64, n), r, 0} }
func (b *bufc) read() (x float64) {
x = b.xs[b.r]
b.r++
if b.r == len(b.xs) {
b.r = 0
}
return
}
// readat returns x at r and next read position.
func (b *bufc) readat(r int) (x float64, n int) {
n = r
x = b.xs[n]
n++
if n == len(b.xs) {
n = 0
}
return
}
func (b *bufc) write(x float64) (end bool) {
b.xs[b.w] = x
b.w++
end = b.w == len(b.xs)
if end {
b.w = 0
}
return
}
// Dtof converts time duration to approximate number of representative frames.
func Dtof(d time.Duration, sr float64) (f int) {
return int(float64(d) / float64(time.Second) * sr)
}
// Ftod converts f, number of frames, to approximate time duration.
func Ftod(f int, sr float64) (d time.Duration) {
return time.Duration(float64(f) / sr * float64(time.Second))
}
// Delay represents a signal delayed by a given duration.
type Delay struct {
*mono
line *bufc
}
// NewDelay returns Delay with sample buffer of a length approximated by d.
func NewDelay(d time.Duration, in Sound) *Delay {
return &Delay{newmono(in), newbufc(Dtof(d, in.SampleRate()), 1)}
}
func (dly *Delay) Prepare(uint64) {
for i := range dly.out {
if dly.off {
dly.out[i] = 0
} else {
dly.out[i] = dly.line.read()
dly.line.write(dly.in.Index(i))
}
}
}
// Tap is a tapped delay line, essentially a shorter delay within a larger one.
//
// TODO consider some type of method on Delay instead of a separate type.
// For example, Tap intentionally does not expose dly via Inputs() so why is it
// its own type? Conversely, that'd make Delay a mixer of sorts.
type Tap struct {
*mono
dly *Delay
r int
}
func NewTap(d time.Duration, in *Delay) *Tap {
f := Dtof(d, in.SampleRate())
n := len(in.line.xs)
if f >= n {
f = n - 1
}
// TODO this would fall out of sync if toggled off
// separate from Delay suggesting a more intrinsic design
// is required here.
//
// get ahead of the delay's write position
r := in.line.w + (n - f)
if r >= n {
r -= n
}
return &Tap{newmono(nil), in, r}
}
func (tap *Tap) Prepare(uint64) {
for i := range tap.out {
if tap.off {
tap.out[i] = 0
} else {
tap.out[i], tap.r = tap.dly.line.readat(tap.r)
}
}
}
// Comb adds a delayed version of a signal onto itself.
type Comb struct {
*mono
line *bufc
gain float64
}
func NewComb(gain float64, d time.Duration, in Sound) *Comb {
return &Comb{newmono(in), newbufc(Dtof(d, in.SampleRate()), 1), gain}
}
func (cmb *Comb) Prepare(uint64) {
for i := range cmb.out {
if cmb.off {
cmb.out[i] = 0
} else {
cmb.out[i] = cmb.line.read()
cmb.line.write(cmb.in.Index(i) + cmb.out[i]*cmb.gain)
}
}
}
// Loop records a signal by a given duration, repeating the recording
// in subsequent playback.
//
// TODO cross-fade
type Loop struct {
*mono
line *bufc
rec bool
syncrec bool
sync int
syncpos int
}
// NewLoop returns a Loop with sample buffer of a length approximated by d.
func NewLoop(d time.Duration, in Sound) *Loop {
return &Loop{newmono(in), newbufc(Dtof(d, in.SampleRate()), 0), false, false, 0, 0}
}
// NewLoopFrames return a Loop with sample buffer of length nframes.
func NewLoopFrames(nframes int, in Sound) *Loop {
return &Loop{newmono(in), newbufc(nframes, 0), false, false, 0, 0}
}
func (lp *Loop) SetBPM(bpm BPM) {
lp.sync = Dtof(bpm.Dur(), lp.SampleRate())
}
func (lp *Loop) Syncing() bool { return lp.syncrec }
func (lp *Loop) Recording() bool { return lp.rec }
func (lp *Loop) Record() {
// TODO may want to replace lp.line with new instance instead?
// otherwise there will be left over data in buffer
// if adding Stop() method, unless that's desireable?
lp.line.w = 0
if lp.sync == 0 {
lp.rec = true
} else {
lp.syncpos = lp.sync
lp.syncrec = true
}
}
func (lp *Loop) Stop() {
lp.rec = false
lp.syncrec = false
lp.syncpos = 0
lp.line.r = 0
}
func (lp *Loop) Prepare(tc uint64) {
nf := tc * uint64(len(lp.out))
for i := range lp.out {
lp.out[i] = 0
if !lp.off {
if lp.syncrec {
if ((nf + uint64(i)) % uint64(lp.sync)) == 0 {
lp.syncrec = false
lp.rec = true
} else {
lp.syncpos--
if lp.syncpos == 0 {
log.Println("Failed to start loop.")
lp.syncrec = false
}
}
}
if lp.rec {
if done := lp.line.write(lp.in.Index(i)); done {
// lp.rec = false
// lp.line.r = 0
lp.Stop()
}
} else {
lp.out[i] = lp.line.read()
}
}
}
}