@codexo/exojs-audio-fx 0.15.3 → 0.16.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/esm/AudioAnalyser.d.ts +4 -3
- package/dist/esm/AudioAnalyser.d.ts.map +1 -0
- package/dist/esm/AudioAnalyser.js +365 -468
- package/dist/esm/AudioAnalyser.js.map +1 -1
- package/dist/esm/BeatDetector.d.ts +16 -15
- package/dist/esm/BeatDetector.d.ts.map +1 -0
- package/dist/esm/BeatDetector.js +370 -428
- package/dist/esm/BeatDetector.js.map +1 -1
- package/dist/esm/dsp/fft.d.ts +5 -5
- package/dist/esm/dsp/fft.d.ts.map +1 -0
- package/dist/esm/dsp/mel.d.ts +6 -6
- package/dist/esm/dsp/mel.d.ts.map +1 -0
- package/dist/esm/dsp/mel.js +55 -64
- package/dist/esm/dsp/mel.js.map +1 -1
- package/dist/esm/dsp/tempogram.d.ts +60 -20
- package/dist/esm/dsp/tempogram.d.ts.map +1 -0
- package/dist/esm/effects/AutoWahEffect.d.ts +3 -2
- package/dist/esm/effects/AutoWahEffect.d.ts.map +1 -0
- package/dist/esm/effects/AutoWahEffect.js +210 -237
- package/dist/esm/effects/AutoWahEffect.js.map +1 -1
- package/dist/esm/effects/BitCrusherEffect.d.ts +3 -2
- package/dist/esm/effects/BitCrusherEffect.d.ts.map +1 -0
- package/dist/esm/effects/BitCrusherEffect.js +72 -70
- package/dist/esm/effects/BitCrusherEffect.js.map +1 -1
- package/dist/esm/effects/ChorusEffect.d.ts +2 -1
- package/dist/esm/effects/ChorusEffect.d.ts.map +1 -0
- package/dist/esm/effects/ChorusEffect.js +132 -150
- package/dist/esm/effects/ChorusEffect.js.map +1 -1
- package/dist/esm/effects/CompressorEffect.d.ts +2 -1
- package/dist/esm/effects/CompressorEffect.d.ts.map +1 -0
- package/dist/esm/effects/CompressorEffect.js +104 -118
- package/dist/esm/effects/CompressorEffect.js.map +1 -1
- package/dist/esm/effects/ConvolutionEffect.d.ts +10 -5
- package/dist/esm/effects/ConvolutionEffect.d.ts.map +1 -0
- package/dist/esm/effects/ConvolutionEffect.js +208 -189
- package/dist/esm/effects/ConvolutionEffect.js.map +1 -1
- package/dist/esm/effects/DelayEffect.d.ts +1 -0
- package/dist/esm/effects/DelayEffect.d.ts.map +1 -0
- package/dist/esm/effects/DelayEffect.js +105 -109
- package/dist/esm/effects/DelayEffect.js.map +1 -1
- package/dist/esm/effects/DistortionEffect.d.ts +4 -3
- package/dist/esm/effects/DistortionEffect.d.ts.map +1 -0
- package/dist/esm/effects/DistortionEffect.js +190 -195
- package/dist/esm/effects/DistortionEffect.js.map +1 -1
- package/dist/esm/effects/DuckingEffect.d.ts +3 -2
- package/dist/esm/effects/DuckingEffect.d.ts.map +1 -0
- package/dist/esm/effects/DuckingEffect.js +91 -172
- package/dist/esm/effects/DuckingEffect.js.map +1 -1
- package/dist/esm/effects/EqualizerEffect.d.ts +1 -0
- package/dist/esm/effects/EqualizerEffect.d.ts.map +1 -0
- package/dist/esm/effects/EqualizerEffect.js +120 -133
- package/dist/esm/effects/EqualizerEffect.js.map +1 -1
- package/dist/esm/effects/FlangerEffect.d.ts +7 -6
- package/dist/esm/effects/FlangerEffect.d.ts.map +1 -0
- package/dist/esm/effects/FlangerEffect.js +172 -201
- package/dist/esm/effects/FlangerEffect.js.map +1 -1
- package/dist/esm/effects/GranularEffect.d.ts +3 -2
- package/dist/esm/effects/GranularEffect.d.ts.map +1 -0
- package/dist/esm/effects/GranularEffect.js +100 -95
- package/dist/esm/effects/GranularEffect.js.map +1 -1
- package/dist/esm/effects/LimiterEffect.d.ts +1 -0
- package/dist/esm/effects/LimiterEffect.d.ts.map +1 -0
- package/dist/esm/effects/LimiterEffect.js +182 -194
- package/dist/esm/effects/LimiterEffect.js.map +1 -1
- package/dist/esm/effects/PhaserEffect.d.ts +2 -1
- package/dist/esm/effects/PhaserEffect.d.ts.map +1 -0
- package/dist/esm/effects/PhaserEffect.js +214 -254
- package/dist/esm/effects/PhaserEffect.js.map +1 -1
- package/dist/esm/effects/PingPongDelayEffect.d.ts +2 -1
- package/dist/esm/effects/PingPongDelayEffect.d.ts.map +1 -0
- package/dist/esm/effects/PingPongDelayEffect.js +172 -185
- package/dist/esm/effects/PingPongDelayEffect.js.map +1 -1
- package/dist/esm/effects/PitchShiftEffect.d.ts +3 -2
- package/dist/esm/effects/PitchShiftEffect.d.ts.map +1 -0
- package/dist/esm/effects/PitchShiftEffect.js +60 -60
- package/dist/esm/effects/PitchShiftEffect.js.map +1 -1
- package/dist/esm/effects/ReverbEffect.d.ts +2 -1
- package/dist/esm/effects/ReverbEffect.d.ts.map +1 -0
- package/dist/esm/effects/ReverbEffect.js +113 -117
- package/dist/esm/effects/ReverbEffect.js.map +1 -1
- package/dist/esm/effects/RingModulatorEffect.d.ts +4 -3
- package/dist/esm/effects/RingModulatorEffect.d.ts.map +1 -0
- package/dist/esm/effects/RingModulatorEffect.js +153 -161
- package/dist/esm/effects/RingModulatorEffect.js.map +1 -1
- package/dist/esm/effects/TremoloEffect.d.ts +1 -0
- package/dist/esm/effects/TremoloEffect.d.ts.map +1 -0
- package/dist/esm/effects/TremoloEffect.js +193 -201
- package/dist/esm/effects/TremoloEffect.js.map +1 -1
- package/dist/esm/effects/VocoderEffect.d.ts +2 -1
- package/dist/esm/effects/VocoderEffect.d.ts.map +1 -0
- package/dist/esm/effects/VocoderEffect.js +69 -80
- package/dist/esm/effects/VocoderEffect.js.map +1 -1
- package/dist/esm/index.d.ts +2 -1
- package/dist/esm/index.d.ts.map +1 -0
- package/dist/esm/index.js +23 -22
- package/dist/esm/worklets/beat-detector.worklet.js +5 -1162
- package/dist/esm/worklets/beat-detector.worklet.js.map +1 -1
- package/dist/esm/worklets/bit-crusher.worklet.js +5 -44
- package/dist/esm/worklets/bit-crusher.worklet.js.map +1 -1
- package/dist/esm/worklets/ducking.worklet.js +6 -0
- package/dist/esm/worklets/ducking.worklet.js.map +1 -0
- package/dist/esm/worklets/granular.worklet.js +5 -97
- package/dist/esm/worklets/granular.worklet.js.map +1 -1
- package/dist/esm/worklets/pitch-shift.worklet.js +5 -128
- package/dist/esm/worklets/pitch-shift.worklet.js.map +1 -1
- package/dist/esm/worklets/vocoder.worklet.js +5 -93
- package/dist/esm/worklets/vocoder.worklet.js.map +1 -1
- package/package.json +7 -7
- package/dist/esm/index.js.map +0 -1
- package/dist/esm/worklets/beat-detector.worklet.d.ts +0 -1
- package/dist/esm/worklets/bit-crusher.worklet.d.ts +0 -1
- package/dist/esm/worklets/granular.worklet.d.ts +0 -1
- package/dist/esm/worklets/pitch-shift.worklet.d.ts +0 -1
- package/dist/esm/worklets/vocoder.worklet.d.ts +0 -1
package/dist/esm/BeatDetector.js
CHANGED
|
@@ -1,432 +1,374 @@
|
|
|
1
|
-
import
|
|
2
|
-
import {
|
|
1
|
+
import beat_detector_worklet_default from "./worklets/beat-detector.worklet.js";
|
|
2
|
+
import { Signal, getAudioContext, isAudioContextReady, onAudioContextReady, registerAudioWorkletProcessor } from "@codexo/exojs";
|
|
3
3
|
|
|
4
|
-
|
|
4
|
+
//#region src/BeatDetector.ts
|
|
5
|
+
const workletName = "exojs-beat-detector";
|
|
5
6
|
/**
|
|
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
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
// MediaStream — duck-type via getTracks
|
|
371
|
-
const asStream = source;
|
|
372
|
-
if (typeof asStream.getTracks === 'function') {
|
|
373
|
-
if (this._streamSource) {
|
|
374
|
-
this._streamSource.disconnect();
|
|
375
|
-
this._streamSource = null;
|
|
376
|
-
}
|
|
377
|
-
const msNode = audioContext.createMediaStreamSource(source);
|
|
378
|
-
this._streamSource = msNode;
|
|
379
|
-
return msNode;
|
|
380
|
-
}
|
|
381
|
-
// AudioBus — has _getOutputNode (checked before raw AudioNode)
|
|
382
|
-
const asBus = source;
|
|
383
|
-
if (typeof asBus._getOutputNode === 'function') {
|
|
384
|
-
return asBus._getOutputNode();
|
|
385
|
-
}
|
|
386
|
-
// Voice — tap its output node
|
|
387
|
-
const asVoice = source;
|
|
388
|
-
if ('output' in asVoice && asVoice.output) {
|
|
389
|
-
return asVoice.output;
|
|
390
|
-
}
|
|
391
|
-
// Raw AudioNode — duck-type: has connect & disconnect
|
|
392
|
-
const asNode = source;
|
|
393
|
-
if (typeof asNode.connect === 'function' && typeof asNode.disconnect === 'function') {
|
|
394
|
-
return source;
|
|
395
|
-
}
|
|
396
|
-
return null;
|
|
397
|
-
}
|
|
398
|
-
_deferConnectionViaBus(source) {
|
|
399
|
-
const asBus = source;
|
|
400
|
-
if (typeof asBus.onceSetup === 'function') {
|
|
401
|
-
asBus.onceSetup(() => {
|
|
402
|
-
if (this._source === source && this._workletNode && isAudioContextReady()) {
|
|
403
|
-
this._connectSource(source, getAudioContext());
|
|
404
|
-
}
|
|
405
|
-
});
|
|
406
|
-
return;
|
|
407
|
-
}
|
|
408
|
-
onAudioContextReady.once(() => {
|
|
409
|
-
if (this._source === source && this._workletNode && isAudioContextReady()) {
|
|
410
|
-
this._connectSource(source, getAudioContext());
|
|
411
|
-
}
|
|
412
|
-
});
|
|
413
|
-
}
|
|
414
|
-
_disconnectTap() {
|
|
415
|
-
if (this._tapSource && this._workletNode) {
|
|
416
|
-
try {
|
|
417
|
-
this._tapSource.disconnect(this._workletNode);
|
|
418
|
-
}
|
|
419
|
-
catch {
|
|
420
|
-
// Ignore
|
|
421
|
-
}
|
|
422
|
-
}
|
|
423
|
-
this._tapSource = null;
|
|
424
|
-
if (this._streamSource) {
|
|
425
|
-
this._streamSource.disconnect();
|
|
426
|
-
this._streamSource = null;
|
|
427
|
-
}
|
|
428
|
-
}
|
|
429
|
-
}
|
|
7
|
+
* Real-time tempo + beat tracker. Splits work between the audio-rendering
|
|
8
|
+
* thread (an AudioWorklet that runs onset detection, tempogram analysis,
|
|
9
|
+
* and parallel 3/4 and 4/4 posterior estimation) and the main thread (this
|
|
10
|
+
* class - receives beats, fires Signals, handles configuration and source
|
|
11
|
+
* routing).
|
|
12
|
+
*
|
|
13
|
+
* Accepts a wide range of {@link BeatDetectorSource}s - a bus, an individual
|
|
14
|
+
* {@link Voice}, a raw MediaStream, or any AudioNode - and exposes a Signal
|
|
15
|
+
* for each notable event:
|
|
16
|
+
* - {@link BeatDetector.onBeat} - every detected beat
|
|
17
|
+
* - {@link BeatDetector.onDownbeat} - first beat of each bar
|
|
18
|
+
* - {@link BeatDetector.onBarStart} - bar boundary
|
|
19
|
+
* - {@link BeatDetector.onTempoChange} - when the tracked BPM changes
|
|
20
|
+
* - {@link BeatDetector.onBeatPredicted} - look-ahead schedule notice
|
|
21
|
+
*
|
|
22
|
+
* Early beats arrive with low latency tagged `status: 'provisional'` (after a
|
|
23
|
+
* short `minSettlingMs` warm-up, default 400 ms) for snappy visual reactivity,
|
|
24
|
+
* then promote to `status: 'locked'` once the tempo grid is trustworthy - a
|
|
25
|
+
* single {@link BeatDetector.onBeat} signal carries both via {@link BeatInfo.status}.
|
|
26
|
+
* Set `emitProvisionalBeats: false` to receive only locked beats. Time-signature
|
|
27
|
+
* detection (3/4 vs 4/4) is on by default; lock to 4/4 by setting
|
|
28
|
+
* `enableTimeSignatureDetection: false`.
|
|
29
|
+
*/
|
|
30
|
+
var BeatDetector = class {
|
|
31
|
+
onBeat = new Signal();
|
|
32
|
+
onTempoChange = new Signal();
|
|
33
|
+
onDownbeat = new Signal();
|
|
34
|
+
onBarStart = new Signal();
|
|
35
|
+
onBeatPredicted = new Signal();
|
|
36
|
+
_options;
|
|
37
|
+
_workletNode = null;
|
|
38
|
+
_source = null;
|
|
39
|
+
_tapSource = null;
|
|
40
|
+
_streamSource = null;
|
|
41
|
+
_ready = null;
|
|
42
|
+
_pendingSourceSetup = null;
|
|
43
|
+
_onAudioContextReady = (ctx) => {
|
|
44
|
+
onAudioContextReady.remove(this._onAudioContextReady);
|
|
45
|
+
this._setup(ctx);
|
|
46
|
+
};
|
|
47
|
+
_tempo = 0;
|
|
48
|
+
_beatPhase = 0;
|
|
49
|
+
_nextBeatTime = 0;
|
|
50
|
+
_confidence = 0;
|
|
51
|
+
_gridStability = 0;
|
|
52
|
+
_tempoCandidates = [];
|
|
53
|
+
_rms = 0;
|
|
54
|
+
_onsetStrength = 0;
|
|
55
|
+
_bandEnergy = {
|
|
56
|
+
low: 0,
|
|
57
|
+
mid: 0,
|
|
58
|
+
high: 0
|
|
59
|
+
};
|
|
60
|
+
_barPosition = 1;
|
|
61
|
+
_barLength = 4;
|
|
62
|
+
_timeSignature = {
|
|
63
|
+
numerator: 4,
|
|
64
|
+
denominator: 4
|
|
65
|
+
};
|
|
66
|
+
_nextDownbeatTime = 0;
|
|
67
|
+
_lookahead = Object.freeze([]);
|
|
68
|
+
/**
|
|
69
|
+
* Half-life in seconds for the {@link pulse} envelope. Mutable; default 0.15.
|
|
70
|
+
* Smaller values give a snappier pulse, larger values a longer afterglow.
|
|
71
|
+
*/
|
|
72
|
+
pulseHalfLife;
|
|
73
|
+
/** Half-life for the {@link barPulse} envelope. Mutable; default 0.3. */
|
|
74
|
+
barPulseHalfLife;
|
|
75
|
+
/** Time window for {@link justBeat}. Mutable; default 0.03 (30ms). */
|
|
76
|
+
justBeatWindow;
|
|
77
|
+
constructor(options) {
|
|
78
|
+
this._options = {
|
|
79
|
+
minBpm: options?.minBpm ?? 50,
|
|
80
|
+
maxBpm: options?.maxBpm ?? 300,
|
|
81
|
+
fftSize: options?.fftSize ?? 2048,
|
|
82
|
+
hopSize: options?.hopSize ?? 512,
|
|
83
|
+
fastTempoWindowSec: options?.fastTempoWindowSec ?? 2.5,
|
|
84
|
+
stableTempoWindowSec: options?.stableTempoWindowSec ?? 8,
|
|
85
|
+
acfIntervalHops: options?.acfIntervalHops ?? 15,
|
|
86
|
+
minSettlingMs: options?.minSettlingMs ?? 400,
|
|
87
|
+
emitProvisionalBeats: options?.emitProvisionalBeats ?? true,
|
|
88
|
+
melBands: options?.melBands ?? 24,
|
|
89
|
+
enableTimeSignatureDetection: options?.enableTimeSignatureDetection ?? true,
|
|
90
|
+
source: options?.source ?? null,
|
|
91
|
+
pulseHalfLife: options?.pulseHalfLife ?? .15,
|
|
92
|
+
barPulseHalfLife: options?.barPulseHalfLife ?? .3,
|
|
93
|
+
justBeatWindow: options?.justBeatWindow ?? .03
|
|
94
|
+
};
|
|
95
|
+
this.pulseHalfLife = this._options.pulseHalfLife;
|
|
96
|
+
this.barPulseHalfLife = this._options.barPulseHalfLife;
|
|
97
|
+
this.justBeatWindow = this._options.justBeatWindow;
|
|
98
|
+
if (isAudioContextReady()) this._setup(getAudioContext());
|
|
99
|
+
else onAudioContextReady.add(this._onAudioContextReady);
|
|
100
|
+
if (options?.source !== void 0 && options.source !== null) this.source = options.source;
|
|
101
|
+
}
|
|
102
|
+
get source() {
|
|
103
|
+
return this._source;
|
|
104
|
+
}
|
|
105
|
+
set source(value) {
|
|
106
|
+
if (value === this._source) return;
|
|
107
|
+
this._disconnectTap();
|
|
108
|
+
this._source = value;
|
|
109
|
+
if (value === null) return;
|
|
110
|
+
if (isAudioContextReady()) {
|
|
111
|
+
this._pendingSourceSetup = null;
|
|
112
|
+
this._connectSource(value, getAudioContext());
|
|
113
|
+
} else {
|
|
114
|
+
if (this._pendingSourceSetup !== null) onAudioContextReady.remove(this._pendingSourceSetup);
|
|
115
|
+
const handler = (ctx) => {
|
|
116
|
+
onAudioContextReady.remove(handler);
|
|
117
|
+
this._pendingSourceSetup = null;
|
|
118
|
+
this._connectSource(value, ctx);
|
|
119
|
+
};
|
|
120
|
+
this._pendingSourceSetup = handler;
|
|
121
|
+
onAudioContextReady.add(handler);
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
get ready() {
|
|
125
|
+
return this._ready ?? Promise.resolve();
|
|
126
|
+
}
|
|
127
|
+
get tempo() {
|
|
128
|
+
return this._tempo;
|
|
129
|
+
}
|
|
130
|
+
get beatPhase() {
|
|
131
|
+
return this._beatPhase;
|
|
132
|
+
}
|
|
133
|
+
get nextBeatTime() {
|
|
134
|
+
return this._nextBeatTime;
|
|
135
|
+
}
|
|
136
|
+
get confidence() {
|
|
137
|
+
return this._confidence;
|
|
138
|
+
}
|
|
139
|
+
get gridStability() {
|
|
140
|
+
return this._gridStability;
|
|
141
|
+
}
|
|
142
|
+
get rms() {
|
|
143
|
+
return this._rms;
|
|
144
|
+
}
|
|
145
|
+
get onsetStrength() {
|
|
146
|
+
return this._onsetStrength;
|
|
147
|
+
}
|
|
148
|
+
get bandEnergy() {
|
|
149
|
+
return this._bandEnergy;
|
|
150
|
+
}
|
|
151
|
+
get tempoCandidates() {
|
|
152
|
+
return this._tempoCandidates;
|
|
153
|
+
}
|
|
154
|
+
get barPosition() {
|
|
155
|
+
return this._barPosition;
|
|
156
|
+
}
|
|
157
|
+
get barLength() {
|
|
158
|
+
return this._barLength;
|
|
159
|
+
}
|
|
160
|
+
get timeSignature() {
|
|
161
|
+
return this._timeSignature;
|
|
162
|
+
}
|
|
163
|
+
get nextDownbeatTime() {
|
|
164
|
+
return this._nextDownbeatTime;
|
|
165
|
+
}
|
|
166
|
+
get lookahead() {
|
|
167
|
+
return this._lookahead;
|
|
168
|
+
}
|
|
169
|
+
/**
|
|
170
|
+
* Seconds elapsed since the most recent beat, derived from {@link beatPhase}
|
|
171
|
+
* and {@link tempo}. Returns 0 when the detector hasn't locked yet.
|
|
172
|
+
*/
|
|
173
|
+
get secondsSinceLastBeat() {
|
|
174
|
+
if (this._tempo === 0) return 0;
|
|
175
|
+
return this._beatPhase * (60 / this._tempo);
|
|
176
|
+
}
|
|
177
|
+
/**
|
|
178
|
+
* 0..1 envelope, peaks at 1.0 the moment a beat fires and halves every
|
|
179
|
+
* {@link pulseHalfLife} seconds. Drives "pulse on the beat" visuals
|
|
180
|
+
* with a single multiplication: `sprite.scale = 1 + clock.pulse * 0.3`.
|
|
181
|
+
*/
|
|
182
|
+
get pulse() {
|
|
183
|
+
if (this._tempo === 0) return 0;
|
|
184
|
+
return Math.pow(.5, this.secondsSinceLastBeat / this.pulseHalfLife);
|
|
185
|
+
}
|
|
186
|
+
/**
|
|
187
|
+
* Like {@link pulse} but resets on downbeats and decays per
|
|
188
|
+
* {@link barPulseHalfLife}. Useful for emphasizing the first beat of
|
|
189
|
+
* each bar (e.g. brighter flash on "1" vs "2,3,4").
|
|
190
|
+
*/
|
|
191
|
+
get barPulse() {
|
|
192
|
+
if (this._tempo === 0 || this._barLength === 0) return 0;
|
|
193
|
+
const secondsPerBeat = 60 / this._tempo;
|
|
194
|
+
const lastDownbeat = this._nextDownbeatTime - this._barLength * secondsPerBeat;
|
|
195
|
+
const elapsed = Math.max(0, getAudioContext().currentTime - lastDownbeat);
|
|
196
|
+
return Math.pow(.5, elapsed / this.barPulseHalfLife);
|
|
197
|
+
}
|
|
198
|
+
/**
|
|
199
|
+
* True for the visual frame(s) within {@link justBeatWindow} seconds of
|
|
200
|
+
* a beat onset. Use for one-shot triggers (strobe flash, particle burst,
|
|
201
|
+
* sample retrigger). Default window 30ms covers a typical 60fps frame.
|
|
202
|
+
*/
|
|
203
|
+
get justBeat() {
|
|
204
|
+
return this._tempo > 0 && this.secondsSinceLastBeat < this.justBeatWindow;
|
|
205
|
+
}
|
|
206
|
+
/**
|
|
207
|
+
* Phase 0..1 within a subdivision of the current beat. `division` is the
|
|
208
|
+
* number of subdivisions per beat: 2 for 8th notes, 4 for 16th notes,
|
|
209
|
+
* 3 for triplets. Use to drive sub-beat-resolution effects:
|
|
210
|
+
*
|
|
211
|
+
* const sixteenth = clock.subdivisionPhase(4);
|
|
212
|
+
* if (sixteenth < 0.05) flash();
|
|
213
|
+
*/
|
|
214
|
+
subdivisionPhase(division) {
|
|
215
|
+
if (!Number.isFinite(division) || division <= 0) return 0;
|
|
216
|
+
return this._beatPhase * division % 1;
|
|
217
|
+
}
|
|
218
|
+
destroy() {
|
|
219
|
+
onAudioContextReady.remove(this._onAudioContextReady);
|
|
220
|
+
if (this._pendingSourceSetup !== null) {
|
|
221
|
+
onAudioContextReady.remove(this._pendingSourceSetup);
|
|
222
|
+
this._pendingSourceSetup = null;
|
|
223
|
+
}
|
|
224
|
+
this._disconnectTap();
|
|
225
|
+
this._workletNode?.disconnect();
|
|
226
|
+
this._workletNode = null;
|
|
227
|
+
this._ready = null;
|
|
228
|
+
this.onBeat.clear();
|
|
229
|
+
this.onTempoChange.clear();
|
|
230
|
+
this.onDownbeat.clear();
|
|
231
|
+
this.onBarStart.clear();
|
|
232
|
+
this.onBeatPredicted.clear();
|
|
233
|
+
}
|
|
234
|
+
_setup(audioContext) {
|
|
235
|
+
const opts = this._options;
|
|
236
|
+
this._ready = registerAudioWorkletProcessor(audioContext, workletName, beat_detector_worklet_default).then(() => {
|
|
237
|
+
const node = new AudioWorkletNode(audioContext, workletName, {
|
|
238
|
+
numberOfInputs: 1,
|
|
239
|
+
numberOfOutputs: 0,
|
|
240
|
+
processorOptions: {
|
|
241
|
+
fftSize: opts.fftSize,
|
|
242
|
+
hopSize: opts.hopSize,
|
|
243
|
+
minBpm: opts.minBpm,
|
|
244
|
+
maxBpm: opts.maxBpm,
|
|
245
|
+
melBands: opts.melBands,
|
|
246
|
+
minSettlingMs: opts.minSettlingMs,
|
|
247
|
+
emitProvisionalBeats: opts.emitProvisionalBeats,
|
|
248
|
+
fastTempoWindowSec: opts.fastTempoWindowSec,
|
|
249
|
+
stableTempoWindowSec: opts.stableTempoWindowSec,
|
|
250
|
+
acfIntervalHops: opts.acfIntervalHops,
|
|
251
|
+
enableTimeSignatureDetection: opts.enableTimeSignatureDetection
|
|
252
|
+
}
|
|
253
|
+
});
|
|
254
|
+
this._workletNode = node;
|
|
255
|
+
node.port.onmessage = this._onWorkletMessage.bind(this);
|
|
256
|
+
if (this._source !== null) this._connectSource(this._source, audioContext);
|
|
257
|
+
});
|
|
258
|
+
}
|
|
259
|
+
_onWorkletMessage(event) {
|
|
260
|
+
const message = event.data;
|
|
261
|
+
switch (message.type) {
|
|
262
|
+
case "state":
|
|
263
|
+
this._tempo = message.tempo ?? 0;
|
|
264
|
+
this._beatPhase = message.beatPhase ?? 0;
|
|
265
|
+
this._nextBeatTime = message.nextBeatTime ?? 0;
|
|
266
|
+
this._nextDownbeatTime = message.nextDownbeatTime ?? 0;
|
|
267
|
+
this._confidence = message.confidence ?? 0;
|
|
268
|
+
this._gridStability = message.gridStability ?? 0;
|
|
269
|
+
this._tempoCandidates = Object.freeze(message.tempoCandidates ?? []);
|
|
270
|
+
this._rms = message.rms ?? 0;
|
|
271
|
+
this._onsetStrength = message.onsetStrength ?? 0;
|
|
272
|
+
this._bandEnergy = message.bandEnergy ?? {
|
|
273
|
+
low: 0,
|
|
274
|
+
mid: 0,
|
|
275
|
+
high: 0
|
|
276
|
+
};
|
|
277
|
+
this._barPosition = message.barPosition ?? 1;
|
|
278
|
+
this._barLength = message.barLength ?? 4;
|
|
279
|
+
this._timeSignature = message.timeSignature ?? {
|
|
280
|
+
numerator: 4,
|
|
281
|
+
denominator: 4
|
|
282
|
+
};
|
|
283
|
+
{
|
|
284
|
+
const lookahead = message.lookahead ?? [];
|
|
285
|
+
this._lookahead = Object.freeze(lookahead);
|
|
286
|
+
if (lookahead.length > 0) this.onBeatPredicted.dispatch(lookahead[0]);
|
|
287
|
+
}
|
|
288
|
+
break;
|
|
289
|
+
case "beat": {
|
|
290
|
+
const bi = {
|
|
291
|
+
audioTime: message.audioTime,
|
|
292
|
+
tempo: message.tempo,
|
|
293
|
+
confidence: message.confidence,
|
|
294
|
+
beatPhase: message.beatPhase,
|
|
295
|
+
energy: message.energy,
|
|
296
|
+
isDownbeat: message.isDownbeat,
|
|
297
|
+
beatInBar: message.beatInBar,
|
|
298
|
+
status: message.status ?? "locked"
|
|
299
|
+
};
|
|
300
|
+
this.onBeat.dispatch(bi);
|
|
301
|
+
if (bi.isDownbeat) this.onDownbeat.dispatch(bi);
|
|
302
|
+
break;
|
|
303
|
+
}
|
|
304
|
+
case "tempoChange":
|
|
305
|
+
this.onTempoChange.dispatch(message.newTempo, message.oldTempo);
|
|
306
|
+
break;
|
|
307
|
+
case "barStart": {
|
|
308
|
+
const info = {
|
|
309
|
+
audioTime: message.audioTime,
|
|
310
|
+
tempo: message.tempo,
|
|
311
|
+
confidence: message.confidence,
|
|
312
|
+
barNumber: message.barNumber
|
|
313
|
+
};
|
|
314
|
+
this.onBarStart.dispatch(info);
|
|
315
|
+
break;
|
|
316
|
+
}
|
|
317
|
+
}
|
|
318
|
+
}
|
|
319
|
+
_connectSource(source, audioContext) {
|
|
320
|
+
if (!this._workletNode) return;
|
|
321
|
+
const tap = this._resolveToAudioNode(source, audioContext);
|
|
322
|
+
if (!tap) {
|
|
323
|
+
this._deferConnectionViaBus(source);
|
|
324
|
+
return;
|
|
325
|
+
}
|
|
326
|
+
this._tapSource = tap;
|
|
327
|
+
tap.connect(this._workletNode, 0, 0);
|
|
328
|
+
}
|
|
329
|
+
_resolveToAudioNode(source, audioContext) {
|
|
330
|
+
if (source === null) return null;
|
|
331
|
+
if (typeof source.getTracks === "function") {
|
|
332
|
+
if (this._streamSource) {
|
|
333
|
+
this._streamSource.disconnect();
|
|
334
|
+
this._streamSource = null;
|
|
335
|
+
}
|
|
336
|
+
const msNode = audioContext.createMediaStreamSource(source);
|
|
337
|
+
this._streamSource = msNode;
|
|
338
|
+
return msNode;
|
|
339
|
+
}
|
|
340
|
+
const asBus = source;
|
|
341
|
+
if (typeof asBus._getOutputNode === "function") return asBus._getOutputNode();
|
|
342
|
+
const asVoice = source;
|
|
343
|
+
if ("output" in asVoice && asVoice.output) return asVoice.output;
|
|
344
|
+
const asNode = source;
|
|
345
|
+
if (typeof asNode.connect === "function" && typeof asNode.disconnect === "function") return source;
|
|
346
|
+
return null;
|
|
347
|
+
}
|
|
348
|
+
_deferConnectionViaBus(source) {
|
|
349
|
+
const asBus = source;
|
|
350
|
+
if (typeof asBus.onceSetup === "function") {
|
|
351
|
+
asBus.onceSetup(() => {
|
|
352
|
+
if (this._source === source && this._workletNode && isAudioContextReady()) this._connectSource(source, getAudioContext());
|
|
353
|
+
});
|
|
354
|
+
return;
|
|
355
|
+
}
|
|
356
|
+
onAudioContextReady.once(() => {
|
|
357
|
+
if (this._source === source && this._workletNode && isAudioContextReady()) this._connectSource(source, getAudioContext());
|
|
358
|
+
});
|
|
359
|
+
}
|
|
360
|
+
_disconnectTap() {
|
|
361
|
+
if (this._tapSource && this._workletNode) try {
|
|
362
|
+
this._tapSource.disconnect(this._workletNode);
|
|
363
|
+
} catch {}
|
|
364
|
+
this._tapSource = null;
|
|
365
|
+
if (this._streamSource) {
|
|
366
|
+
this._streamSource.disconnect();
|
|
367
|
+
this._streamSource = null;
|
|
368
|
+
}
|
|
369
|
+
}
|
|
370
|
+
};
|
|
430
371
|
|
|
372
|
+
//#endregion
|
|
431
373
|
export { BeatDetector };
|
|
432
|
-
//# sourceMappingURL=BeatDetector.js.map
|
|
374
|
+
//# sourceMappingURL=BeatDetector.js.map
|