-
Notifications
You must be signed in to change notification settings - Fork 0
/
speed_shifter.go
307 lines (269 loc) · 9.89 KB
/
speed_shifter.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
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
package edau
// TODO: speed transitions. for smootheness, we need a progressive transition which
// needs to be dealt with internally. And even then ebitengine will introduce
// 125ms of delay or so. We can change buffer size to reduce to 50, which gets
// us to an average of 25ms. this is also broken by seeks.
import "io"
import "math"
import "sync"
// A SpeedShifter wraps an audio stream and allows playing it at a different
// speed than the original by resampling in real-time.
//
// Valid speed shifters can only be created through [NewDefaultSpeedShifter]
// or [NewSpeedShifter].
type SpeedShifter struct {
mutex sync.Mutex
source io.Reader
speed float64
windowSize int
interpolator InterpolatorFunc
fracPos float64
leftoverBytes int // from previous reads, not consumed yet
lookaheadBytes int // lookahead bytes ready for interpolation
leftWindow circularWindow
rightWindow circularWindow
auxReadBuffer []byte
}
// Creates a default [SpeedShifter], which uses a 6-point Hermite interpolator for the
// resampling. For custom interpolators, see [NewSpeedShifter] instead.
//
// The initial playback speed is 1.0.
func NewDefaultSpeedShifter(source io.Reader) *SpeedShifter {
// Parameters are source, initial speed, window size and interpolator function.
return NewSpeedShifter(source, 1.0, 6, InterpHermite6Pt3Ord)
}
// Creates a new [SpeedShifter] with a custom speed, window size and interpolator. If
// you do not want to worry about interpolators and resampling, see [NewDefaultSpeedShifter]
// instead.
//
// The interpolator's windowSize must be multiple of 2.
func NewSpeedShifter(source io.Reader, speed float64, windowSize int, interpolator InterpolatorFunc) *SpeedShifter {
if windowSize < 2 {
panic("NewSpeedShifter windowSize must be at least 2")
}
if windowSize % 2 != 0 {
panic("NewSpeedShifter windowSize must be multiple of 2 (may unrestrict in the future)")
// Note: odd window sizes require interpolating around [center - 0.5, center + 0.5],
// so it's a bit trickier than even sizes, and the reason I didn't add it yet
}
const numChannels = 2
bufferSize := windowSize*8
buffer := make([]float64, bufferSize*numChannels)
shifter := &SpeedShifter {
source: source,
speed: speed,
windowSize: windowSize,
interpolator: interpolator,
leftWindow: circularWindow{ winSize: windowSize, buffer: buffer[ : bufferSize] },
rightWindow: circularWindow{ winSize: windowSize, buffer: buffer[bufferSize : ] },
auxReadBuffer: nil,
}
shifter.internalReset()
return shifter
}
// Returns the currently configured playback speed.
func (self *SpeedShifter) Speed() float64 {
self.mutex.Lock()
speed := self.speed
self.mutex.Unlock()
return speed
}
// Sets the playback speed. Notice that the Ebitengine's player buffer size
// can cause the change to take a while to become audible. See Ebitengine's
// [Player.SetBufferSize] if you want to reduce latency. 20 milliseconds are
// reasonable for most desktop environments, while browsers often have trouble
// when pushed below 50 milliseconds. It also depends a lot on how much processing
// and effects you are adding to the audio.
//
// [Player.SetBufferSize]: https://pkg.go.dev/github.com/hajimehoshi/ebiten/v2/audio#Player.SetBufferSize
func (self *SpeedShifter) SetSpeed(speed float64) {
self.mutex.Lock()
self.speed = speed
self.mutex.Unlock()
}
// Implements [io.Reader]. This function will always try to fill the buffer as much
// as possible, even if this requires multiple reads on the underlying stream.
//
// The returned read length will always also be multiple of 4, aligning to Ebitengine's
// sample size.
func (self *SpeedShifter) Read(buffer []byte) (int, error) {
// do not read incomplete samples (always read a number of bytes multiple of 4)
buffer = buffer[0 : len(buffer) - (len(buffer) & 0b11)]
// keep reading until an error happens or we fill the buffer
bytesServed := 0
for bytesServed < len(buffer) {
// single read from underlying buffer
n, err := self.singleRead(buffer[bytesServed : ])
bytesServed += n
if err != nil { return bytesServed, err }
}
return bytesServed, nil
}
// Like Read, but only reads once from the underlying source. If the given
// buffer can't be filled, this method doesn't retry, it simply returns what
// it got.
func (self *SpeedShifter) singleRead(buffer []byte) (int, error) {
self.mutex.Lock()
defer self.mutex.Unlock()
// base case
if len(buffer) == 0 { return 0, nil }
// general case
requiredLookahead := (self.windowSize << 1) // *4/2
pendingLookahead := requiredLookahead - self.lookaheadBytes
readCompensation := pendingLookahead - self.leftoverBytes
samplesRequired := math.Ceil((float64(len(buffer))*self.speed)/4.0) // ceil needs to be applied on samples
bytesToRead := int(samplesRequired*4.0 + float64(readCompensation))
if bytesToRead <= 0 { panic("unexpected situation") }
// acquire aux buffer for reading
minReadBufferSize := self.leftoverBytes + bytesToRead
var readBuffer []byte
if len(self.auxReadBuffer) < minReadBufferSize {
if cap(self.auxReadBuffer) >= minReadBufferSize {
readBuffer = self.auxReadBuffer[0 : minReadBufferSize]
} else {
readBuffer = make([]byte, minReadBufferSize)
}
} else {
readBuffer = self.auxReadBuffer[0 : minReadBufferSize]
}
// copy leftoverBytes to the start of the buffer
if self.leftoverBytes > 0 {
copy(readBuffer[:], self.auxReadBuffer[len(self.auxReadBuffer) - self.leftoverBytes : ])
}
// read from underlying source
var srcBytesRead int
var err error
if bytesToRead > 0 {
srcBytesRead, err = self.source.Read(readBuffer[self.leftoverBytes : ])
}
srcBytesRead += self.leftoverBytes
readBuffer = readBuffer[0 : srcBytesRead]
// set read buffer as self.auxReadBuffer for next iterations
self.auxReadBuffer = readBuffer
// ensure lookahead is properly set
for self.lookaheadBytes < (self.windowSize << 1) {
if len(readBuffer) < 4 {
self.leftoverBytes = len(readBuffer)
return 0, err
}
left, right := GetSampleAsI16(readBuffer)
self.leftWindow.Push(float64(left))
self.rightWindow.Push(float64(right))
self.lookaheadBytes += 4
readBuffer = readBuffer[4 : ]
}
// process the bytes
bytesServed := 0
interpPosBase := float64(self.windowSize/2 - 1)
for len(readBuffer) >= 4 && bytesServed < len(buffer) {
// add sample
if self.fracPos < 1.0 {
left := self.interpolator(self.leftWindow.Get(), interpPosBase + self.fracPos)
right := self.interpolator(self.rightWindow.Get(), interpPosBase + self.fracPos)
StoreF64SampleAsL16(buffer[bytesServed : ], left, right)
bytesServed += 4
self.fracPos += self.speed
}
// advance position
for self.fracPos >= 1.0 && len(readBuffer) >= 4 {
left, right := GetSampleAsI16(readBuffer)
self.leftWindow.Push(float64(left))
self.rightWindow.Push(float64(right))
readBuffer = readBuffer[4 : ]
self.fracPos -= 1.0
}
}
// update leftover bytes
// if (srcBytesRead - bytesServed) & 0b11 != 0 {
// copy(readBuffer[:], self.auxReadBuffer[len(self.auxReadBuffer) - self.leftoverBytes : ])
// panic("unexpected situation")
// }
self.leftoverBytes = len(readBuffer)
if self.leftoverBytes < 0 { panic("unexpected situation") }
// return
return bytesServed, err
}
// Implements [io.Seeker], with the limitation that io.SeekCurrent seeks
// are not supported (unless the seek has an offset of 0, which is sometimes
// used to get the current playback position).
//
// You may use Seek to rewind and start playing after stoping, but not to loop
// or do seamless seeking with the resampled stream itself. Seamless seeking
// could only be done correctly if notifying the seek in advance of the
// interpolation window. That's not something anyone sane wants to figure out, so
// seeking will seek on the underlying buffer but reset the internal interpolation
// window of the speed shifter.
//
// This method panics if the underlying source doesn't implement [io.Seeker].
func (self *SpeedShifter) Seek(offset int64, whence int) (int64, error) {
self.mutex.Lock()
defer self.mutex.Unlock()
if whence == io.SeekCurrent {
if offset == 0 { return 0, nil }
panic("can't use relative seeks on a SpeedShifter (due to lookaheads)")
}
// seek underlying source
seeker := self.source.(io.Seeker)
position, err := seeker.Seek(offset, whence)
// Resets interpolation window and related state.
self.internalReset()
// return seek results
return position, err
}
// Resets the interpolation window and related state.
func (self *SpeedShifter) internalReset() {
self.leftoverBytes = 0
self.lookaheadBytes = 0
self.leftWindow.Reset()
self.rightWindow.Reset()
for i := 0; i < self.windowSize/2 - 1; i++ {
self.leftWindow.Push(0)
self.rightWindow.Push(0)
}
buffer := []byte{0, 0, 0, 0}
n, _ := self.source.Read(buffer)
if n == 4 {
left, right := GetSampleAsF64(buffer)
self.leftWindow.Push(left)
self.rightWindow.Push(right)
} else {
self.leftWindow.Push(0)
self.rightWindow.Push(0)
}
}
// --- helper circularWindow type for efficient interpolation ---
type circularWindow struct {
winSize int
buffer []float64
startIndex int
endIndex int
}
func (self *circularWindow) Reset() {
self.startIndex = 0
self.endIndex = 0
}
func (self *circularWindow) Get() []float64 {
return self.buffer[self.startIndex : self.endIndex]
}
func (self *circularWindow) Push(value float64) {
if self.endIndex < len(self.buffer) {
self.buffer[self.endIndex] = value
if self.winSize == self.endIndex - self.startIndex { // window is full
self.startIndex += 1
}
self.endIndex += 1
} else {
if self.winSize == self.endIndex - self.startIndex { // window is full
copy(self.buffer, self.buffer[self.startIndex + 1 : self.endIndex])
self.buffer[self.winSize - 1] = value
self.startIndex = 0
self.endIndex = self.winSize
} else {
copy(self.buffer, self.buffer[self.startIndex : self.endIndex])
newIndex := self.endIndex - self.startIndex
self.buffer[newIndex] = value
self.startIndex = 0
self.endIndex = newIndex + 1
}
}
}