@codexo/exojs-audio-fx 0.14.0-next.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/LICENSE +21 -0
- package/README.md +25 -0
- package/dist/esm/AudioAnalyser.d.ts +124 -0
- package/dist/esm/AudioAnalyser.js +472 -0
- package/dist/esm/AudioAnalyser.js.map +1 -0
- package/dist/esm/BeatDetector.d.ts +203 -0
- package/dist/esm/BeatDetector.js +422 -0
- package/dist/esm/BeatDetector.js.map +1 -0
- package/dist/esm/dsp/fft.d.ts +22 -0
- package/dist/esm/dsp/mel.d.ts +43 -0
- package/dist/esm/dsp/mel.js +70 -0
- package/dist/esm/dsp/mel.js.map +1 -0
- package/dist/esm/dsp/tempogram.d.ts +51 -0
- package/dist/esm/effects/ChorusEffect.d.ts +53 -0
- package/dist/esm/effects/ChorusEffect.js +154 -0
- package/dist/esm/effects/ChorusEffect.js.map +1 -0
- package/dist/esm/effects/CompressorEffect.d.ts +53 -0
- package/dist/esm/effects/CompressorEffect.js +122 -0
- package/dist/esm/effects/CompressorEffect.js.map +1 -0
- package/dist/esm/effects/DelayEffect.d.ts +35 -0
- package/dist/esm/effects/DelayEffect.js +113 -0
- package/dist/esm/effects/DelayEffect.js.map +1 -0
- package/dist/esm/effects/DuckingEffect.d.ts +43 -0
- package/dist/esm/effects/DuckingEffect.js +172 -0
- package/dist/esm/effects/DuckingEffect.js.map +1 -0
- package/dist/esm/effects/EqualizerEffect.d.ts +50 -0
- package/dist/esm/effects/EqualizerEffect.js +137 -0
- package/dist/esm/effects/EqualizerEffect.js.map +1 -0
- package/dist/esm/effects/GranularEffect.d.ts +62 -0
- package/dist/esm/effects/GranularEffect.js +107 -0
- package/dist/esm/effects/GranularEffect.js.map +1 -0
- package/dist/esm/effects/PitchShiftEffect.d.ts +44 -0
- package/dist/esm/effects/PitchShiftEffect.js +66 -0
- package/dist/esm/effects/PitchShiftEffect.js.map +1 -0
- package/dist/esm/effects/ReverbEffect.d.ts +37 -0
- package/dist/esm/effects/ReverbEffect.js +121 -0
- package/dist/esm/effects/ReverbEffect.js.map +1 -0
- package/dist/esm/effects/VocoderEffect.d.ts +48 -0
- package/dist/esm/effects/VocoderEffect.js +94 -0
- package/dist/esm/effects/VocoderEffect.js.map +1 -0
- package/dist/esm/index.d.ts +13 -0
- package/dist/esm/index.js +12 -0
- package/dist/esm/index.js.map +1 -0
- package/dist/esm/worklets/beat-detector.worklet.d.ts +1 -0
- package/dist/esm/worklets/beat-detector.worklet.js +647 -0
- package/dist/esm/worklets/beat-detector.worklet.js.map +1 -0
- package/dist/esm/worklets/granular.worklet.d.ts +1 -0
- package/dist/esm/worklets/granular.worklet.js +89 -0
- package/dist/esm/worklets/granular.worklet.js.map +1 -0
- package/dist/esm/worklets/pitch-shift.worklet.d.ts +1 -0
- package/dist/esm/worklets/pitch-shift.worklet.js +74 -0
- package/dist/esm/worklets/pitch-shift.worklet.js.map +1 -0
- package/dist/esm/worklets/vocoder.worklet.d.ts +1 -0
- package/dist/esm/worklets/vocoder.worklet.js +92 -0
- package/dist/esm/worklets/vocoder.worklet.js.map +1 -0
- package/package.json +46 -0
|
@@ -0,0 +1,647 @@
|
|
|
1
|
+
const beatDetectorWorkletSource = `
|
|
2
|
+
// ---- Hann window + FFT (radix-2 Cooley-Tukey) ----
|
|
3
|
+
function applyHannWindow(real, imag) {
|
|
4
|
+
var n = real.length;
|
|
5
|
+
for (var i = 0; i < n; i++) {
|
|
6
|
+
var w = 0.5 * (1 - Math.cos(2 * Math.PI * i / (n - 1)));
|
|
7
|
+
real[i] *= w;
|
|
8
|
+
imag[i] = 0;
|
|
9
|
+
}
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
function bitReversePermute(real, imag) {
|
|
13
|
+
var n = real.length;
|
|
14
|
+
var j = 0;
|
|
15
|
+
for (var i = 1; i < n; i++) {
|
|
16
|
+
var bit = n >> 1;
|
|
17
|
+
for (; j & bit; bit >>= 1) { j ^= bit; }
|
|
18
|
+
j ^= bit;
|
|
19
|
+
if (i < j) {
|
|
20
|
+
var t = real[i]; real[i] = real[j]; real[j] = t;
|
|
21
|
+
t = imag[i]; imag[i] = imag[j]; imag[j] = t;
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
function fftInPlace(real, imag) {
|
|
27
|
+
applyHannWindow(real, imag);
|
|
28
|
+
bitReversePermute(real, imag);
|
|
29
|
+
var n = real.length;
|
|
30
|
+
for (var len = 2; len <= n; len <<= 1) {
|
|
31
|
+
var halfLen = len >> 1;
|
|
32
|
+
var step = -2 * Math.PI / len;
|
|
33
|
+
for (var i = 0; i < n; i += len) {
|
|
34
|
+
for (var k = 0; k < halfLen; k++) {
|
|
35
|
+
var angle = step * k;
|
|
36
|
+
var cos = Math.cos(angle);
|
|
37
|
+
var sin = Math.sin(angle);
|
|
38
|
+
var re = real[i+k+halfLen]*cos - imag[i+k+halfLen]*sin;
|
|
39
|
+
var im = real[i+k+halfLen]*sin + imag[i+k+halfLen]*cos;
|
|
40
|
+
real[i+k+halfLen] = real[i+k] - re;
|
|
41
|
+
imag[i+k+halfLen] = imag[i+k] - im;
|
|
42
|
+
real[i+k] += re;
|
|
43
|
+
imag[i+k] += im;
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
// ---- Mel filterbank ----
|
|
50
|
+
function buildMelFilterbank(numBands, fMin, fMax, fftSize, sampleRate) {
|
|
51
|
+
var numBins = fftSize >> 1;
|
|
52
|
+
var nyquist = sampleRate / 2;
|
|
53
|
+
var melMin = 2595 * Math.log10(1 + fMin / 700);
|
|
54
|
+
var melMax = 2595 * Math.log10(1 + fMax / 700);
|
|
55
|
+
var melPoints = new Float32Array(numBands + 2);
|
|
56
|
+
for (var i = 0; i < numBands + 2; i++) {
|
|
57
|
+
melPoints[i] = melMin + (melMax - melMin) * i / (numBands + 1);
|
|
58
|
+
}
|
|
59
|
+
var binPoints = new Float32Array(numBands + 2);
|
|
60
|
+
for (var i = 0; i < numBands + 2; i++) {
|
|
61
|
+
var hz = 700 * (Math.pow(10, melPoints[i] / 2595) - 1);
|
|
62
|
+
binPoints[i] = Math.round(hz / nyquist * (numBins - 1));
|
|
63
|
+
}
|
|
64
|
+
var bands = [];
|
|
65
|
+
for (var b = 0; b < numBands; b++) {
|
|
66
|
+
var startBin = Math.max(0, Math.min(numBins - 1, binPoints[b]));
|
|
67
|
+
var peakBin = Math.max(0, Math.min(numBins - 1, binPoints[b+1]));
|
|
68
|
+
var endBin = Math.max(0, Math.min(numBins - 1, binPoints[b+2]));
|
|
69
|
+
var len = endBin - startBin + 1;
|
|
70
|
+
var weights = new Float32Array(len);
|
|
71
|
+
for (var i = 0; i < len; i++) {
|
|
72
|
+
var bin = startBin + i;
|
|
73
|
+
if (bin <= peakBin && peakBin > startBin) {
|
|
74
|
+
weights[i] = (bin - startBin) / (peakBin - startBin);
|
|
75
|
+
} else if (bin > peakBin && endBin > peakBin) {
|
|
76
|
+
weights[i] = (endBin - bin) / (endBin - peakBin);
|
|
77
|
+
} else {
|
|
78
|
+
weights[i] = 1;
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
bands.push({ startBin: startBin, peakBin: peakBin, endBin: endBin, weights: weights });
|
|
82
|
+
}
|
|
83
|
+
return bands;
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
function computeMelBands(mag, bands, out) {
|
|
87
|
+
for (var b = 0; b < bands.length; b++) {
|
|
88
|
+
var band = bands[b];
|
|
89
|
+
var energy = 0;
|
|
90
|
+
for (var i = 0; i < band.weights.length; i++) {
|
|
91
|
+
energy += mag[band.startBin + i] * band.weights[i];
|
|
92
|
+
}
|
|
93
|
+
out[b] = Math.log(1 + energy);
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
// ---- Processor ----
|
|
98
|
+
class BeatDetectorProcessor extends AudioWorkletProcessor {
|
|
99
|
+
constructor(options) {
|
|
100
|
+
super();
|
|
101
|
+
var opts = (options && options.processorOptions) || {};
|
|
102
|
+
this._sampleRate = sampleRate;
|
|
103
|
+
this._fftSize = opts.fftSize || 2048;
|
|
104
|
+
this._hopSize = opts.hopSize || 512;
|
|
105
|
+
this._minBpm = opts.minBpm || 50;
|
|
106
|
+
this._maxBpm = opts.maxBpm || 250;
|
|
107
|
+
this._melBands = opts.melBands || 24;
|
|
108
|
+
this._settlingMs = opts.settlingMs !== undefined ? opts.settlingMs : 1500;
|
|
109
|
+
this._tempoWindowSec = opts.tempoWindowSec || 6;
|
|
110
|
+
this._enableTsDetection = opts.enableTimeSignatureDetection !== false;
|
|
111
|
+
|
|
112
|
+
var numBins = this._fftSize >> 1;
|
|
113
|
+
this._real = new Float32Array(this._fftSize);
|
|
114
|
+
this._imag = new Float32Array(this._fftSize);
|
|
115
|
+
this._mag = new Float32Array(numBins);
|
|
116
|
+
this._ringBuffer = new Float32Array(this._fftSize);
|
|
117
|
+
this._ringWritePos = 0;
|
|
118
|
+
this._sampleCount = 0;
|
|
119
|
+
this._hopAccum = 0;
|
|
120
|
+
|
|
121
|
+
this._melBandFilters = buildMelFilterbank(this._melBands, 80, 8000, this._fftSize, this._sampleRate);
|
|
122
|
+
this._melOut = new Float32Array(this._melBands);
|
|
123
|
+
var fluxWindowLen = Math.ceil(this._tempoWindowSec * this._sampleRate / this._hopSize);
|
|
124
|
+
this._fluxWindow = new Float32Array(fluxWindowLen);
|
|
125
|
+
this._fluxWritePos = 0;
|
|
126
|
+
this._fluxCount = 0;
|
|
127
|
+
var LAG_K = 3;
|
|
128
|
+
this._prevMelFrames = [];
|
|
129
|
+
for (var i = 0; i < LAG_K; i++) {
|
|
130
|
+
this._prevMelFrames.push(new Float32Array(this._melBands));
|
|
131
|
+
}
|
|
132
|
+
this._prevMelFrameIdx = 0;
|
|
133
|
+
|
|
134
|
+
// Lag range in hops for BPM range
|
|
135
|
+
this._minLag = Math.max(1, Math.round(60 / this._maxBpm * this._sampleRate / this._hopSize));
|
|
136
|
+
this._maxLag = Math.round(60 / this._minBpm * this._sampleRate / this._hopSize);
|
|
137
|
+
|
|
138
|
+
this._hopsSinceACF = 0;
|
|
139
|
+
var ACF_INTERVAL = 15;
|
|
140
|
+
this._acfInterval = ACF_INTERVAL;
|
|
141
|
+
|
|
142
|
+
// Tempo state
|
|
143
|
+
this._bestBpm = 0;
|
|
144
|
+
this._bestScore = 0;
|
|
145
|
+
this._candidates = [];
|
|
146
|
+
|
|
147
|
+
// Phase state
|
|
148
|
+
this._lastBeatSample = -1;
|
|
149
|
+
this._ibiHistory = new Float32Array(4); // last 4 inter-beat intervals
|
|
150
|
+
this._ibiIdx = 0;
|
|
151
|
+
|
|
152
|
+
// Bar position state — parallel posteriors for 4/4 and 3/4
|
|
153
|
+
this._posterior4 = new Float32Array([0.25, 0.25, 0.25, 0.25]);
|
|
154
|
+
this._posterior3 = new Float32Array([1/3, 1/3, 1/3]);
|
|
155
|
+
this._ts4Confidence = 0.5;
|
|
156
|
+
this._ts3Confidence = 0.5;
|
|
157
|
+
this._activeTs = '4/4';
|
|
158
|
+
this._sustainCounter = 0;
|
|
159
|
+
this._barPosition = 1; // 1-indexed
|
|
160
|
+
this._barNumber = 0;
|
|
161
|
+
this._beatsSinceStart = 0;
|
|
162
|
+
|
|
163
|
+
// Confidence
|
|
164
|
+
this._confidence = 0;
|
|
165
|
+
|
|
166
|
+
// State snapshot cadence (~20 Hz)
|
|
167
|
+
var STATE_INTERVAL_HOPS = Math.round(this._sampleRate / this._hopSize / 20);
|
|
168
|
+
this._stateInterval = Math.max(1, STATE_INTERVAL_HOPS);
|
|
169
|
+
this._hopsSinceState = 0;
|
|
170
|
+
|
|
171
|
+
// Settling
|
|
172
|
+
this._startSample = currentFrame;
|
|
173
|
+
this._settledSamples = Math.round(this._settlingMs * 0.001 * this._sampleRate);
|
|
174
|
+
|
|
175
|
+
// Lookahead
|
|
176
|
+
this._lookahead = [];
|
|
177
|
+
|
|
178
|
+
// RMS / onset state
|
|
179
|
+
this._rms = 0;
|
|
180
|
+
this._onsetStrength = 0;
|
|
181
|
+
|
|
182
|
+
// Band energy (for state messages)
|
|
183
|
+
var LOW_BANDS = Math.round(this._melBands * 0.25);
|
|
184
|
+
var MID_BANDS = Math.round(this._melBands * 0.6);
|
|
185
|
+
this._lowBandEnd = LOW_BANDS;
|
|
186
|
+
this._midBandEnd = MID_BANDS;
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
process(inputs, _outputs, _parameters) {
|
|
190
|
+
var input = inputs[0];
|
|
191
|
+
if (!input || input.length === 0) return true;
|
|
192
|
+
|
|
193
|
+
var left = input[0] || [];
|
|
194
|
+
var right = input[1] || left;
|
|
195
|
+
var blockLen = left.length;
|
|
196
|
+
|
|
197
|
+
for (var s = 0; s < blockLen; s++) {
|
|
198
|
+
// Mono downmix
|
|
199
|
+
var mono = (left[s] + right[s]) * 0.5;
|
|
200
|
+
|
|
201
|
+
// Accumulate RMS
|
|
202
|
+
this._rms += mono * mono;
|
|
203
|
+
|
|
204
|
+
// Fill ring buffer
|
|
205
|
+
this._ringBuffer[this._ringWritePos] = mono;
|
|
206
|
+
this._ringWritePos = (this._ringWritePos + 1) & (this._fftSize - 1);
|
|
207
|
+
|
|
208
|
+
this._hopAccum++;
|
|
209
|
+
this._sampleCount++;
|
|
210
|
+
|
|
211
|
+
if (this._hopAccum >= this._hopSize) {
|
|
212
|
+
this._hopAccum = 0;
|
|
213
|
+
this._processHop();
|
|
214
|
+
}
|
|
215
|
+
}
|
|
216
|
+
|
|
217
|
+
return true;
|
|
218
|
+
}
|
|
219
|
+
|
|
220
|
+
_processHop() {
|
|
221
|
+
// Read ring buffer into real[] (oldest first)
|
|
222
|
+
var rb = this._ringBuffer;
|
|
223
|
+
var wp = this._ringWritePos;
|
|
224
|
+
var n = this._fftSize;
|
|
225
|
+
for (var i = 0; i < n; i++) {
|
|
226
|
+
this._real[i] = rb[(wp + i) & (n - 1)];
|
|
227
|
+
}
|
|
228
|
+
|
|
229
|
+
// FFT
|
|
230
|
+
fftInPlace(this._real, this._imag);
|
|
231
|
+
|
|
232
|
+
// Magnitude spectrum
|
|
233
|
+
var bins = n >> 1;
|
|
234
|
+
for (var i = 0; i < bins; i++) {
|
|
235
|
+
this._mag[i] = Math.sqrt(this._real[i]*this._real[i] + this._imag[i]*this._imag[i]);
|
|
236
|
+
}
|
|
237
|
+
|
|
238
|
+
// RMS (from time domain, using ring buffer)
|
|
239
|
+
var rmsAccum = 0;
|
|
240
|
+
for (var i = 0; i < n; i++) {
|
|
241
|
+
rmsAccum += rb[(wp + i) & (n - 1)] * rb[(wp + i) & (n - 1)];
|
|
242
|
+
}
|
|
243
|
+
this._rms = Math.sqrt(rmsAccum / n);
|
|
244
|
+
|
|
245
|
+
// Mel bands
|
|
246
|
+
computeMelBands(this._mag, this._melBandFilters, this._melOut);
|
|
247
|
+
|
|
248
|
+
// Spectral flux (SuperFlux-lite, lag k=3)
|
|
249
|
+
var K = this._prevMelFrames.length;
|
|
250
|
+
var flux = 0;
|
|
251
|
+
for (var b = 0; b < this._melBands; b++) {
|
|
252
|
+
var localMax = -Infinity;
|
|
253
|
+
for (var k = 0; k < K; k++) {
|
|
254
|
+
var prevVal = this._prevMelFrames[k][b];
|
|
255
|
+
if (prevVal > localMax) localMax = prevVal;
|
|
256
|
+
}
|
|
257
|
+
var diff = this._melOut[b] - localMax;
|
|
258
|
+
if (diff > 0) flux += diff;
|
|
259
|
+
}
|
|
260
|
+
this._onsetStrength = flux;
|
|
261
|
+
|
|
262
|
+
// Store current mel frame in circular buffer
|
|
263
|
+
var prevFrame = this._prevMelFrames[this._prevMelFrameIdx];
|
|
264
|
+
for (var b = 0; b < this._melBands; b++) {
|
|
265
|
+
prevFrame[b] = this._melOut[b];
|
|
266
|
+
}
|
|
267
|
+
this._prevMelFrameIdx = (this._prevMelFrameIdx + 1) % K;
|
|
268
|
+
|
|
269
|
+
// Add flux to sliding window
|
|
270
|
+
this._fluxWindow[this._fluxWritePos] = flux;
|
|
271
|
+
this._fluxWritePos = (this._fluxWritePos + 1) % this._fluxWindow.length;
|
|
272
|
+
if (this._fluxCount < this._fluxWindow.length) this._fluxCount++;
|
|
273
|
+
|
|
274
|
+
// Tempogram: compute ACF periodically
|
|
275
|
+
this._hopsSinceACF++;
|
|
276
|
+
if (this._hopsSinceACF >= this._acfInterval && this._fluxCount >= this._maxLag + 1) {
|
|
277
|
+
this._hopsSinceACF = 0;
|
|
278
|
+
this._computeACFAndCandidates();
|
|
279
|
+
}
|
|
280
|
+
|
|
281
|
+
// Phase tracker
|
|
282
|
+
var settled = (this._sampleCount - this._settledSamples) > 0;
|
|
283
|
+
if (this._bestBpm > 0 && settled) {
|
|
284
|
+
this._tickPhase(flux);
|
|
285
|
+
}
|
|
286
|
+
|
|
287
|
+
// State snapshot
|
|
288
|
+
this._hopsSinceState++;
|
|
289
|
+
if (this._hopsSinceState >= this._stateInterval) {
|
|
290
|
+
this._hopsSinceState = 0;
|
|
291
|
+
this._sendStateMessage();
|
|
292
|
+
}
|
|
293
|
+
}
|
|
294
|
+
|
|
295
|
+
_computeACFAndCandidates() {
|
|
296
|
+
var n = this._fluxCount;
|
|
297
|
+
var buf = this._fluxWindow;
|
|
298
|
+
var wp = this._fluxWritePos;
|
|
299
|
+
var len = buf.length;
|
|
300
|
+
|
|
301
|
+
// Compute ACF for lags in [minLag, maxLag]
|
|
302
|
+
var numLags = this._maxLag - this._minLag + 1;
|
|
303
|
+
var acf = new Float32Array(numLags);
|
|
304
|
+
for (var lagIdx = 0; lagIdx < numLags; lagIdx++) {
|
|
305
|
+
var lag = this._minLag + lagIdx;
|
|
306
|
+
var sum = 0, count = 0;
|
|
307
|
+
for (var t = lag; t < n; t++) {
|
|
308
|
+
var idxT = (wp - 1 - (n - 1 - t) + len) % len;
|
|
309
|
+
var idxTL = (wp - 1 - (n - 1 - (t - lag)) + len) % len;
|
|
310
|
+
sum += buf[idxT] * buf[idxTL];
|
|
311
|
+
count++;
|
|
312
|
+
}
|
|
313
|
+
acf[lagIdx] = count > 0 ? sum / count : 0;
|
|
314
|
+
}
|
|
315
|
+
|
|
316
|
+
// Normalise
|
|
317
|
+
var norm = 0;
|
|
318
|
+
for (var i = 0; i < numLags; i++) { if (acf[i] > norm) norm = acf[i]; }
|
|
319
|
+
if (norm > 0) {
|
|
320
|
+
for (var i = 0; i < numLags; i++) acf[i] /= norm;
|
|
321
|
+
}
|
|
322
|
+
|
|
323
|
+
// Find top peaks
|
|
324
|
+
var peaks = [];
|
|
325
|
+
for (var i = 1; i < numLags - 1; i++) {
|
|
326
|
+
if (acf[i] > acf[i-1] && acf[i] > acf[i+1] && acf[i] > 0) {
|
|
327
|
+
var lag2 = this._minLag + i;
|
|
328
|
+
var bpm = 60 * this._sampleRate / (lag2 * this._hopSize);
|
|
329
|
+
if (bpm >= this._minBpm && bpm <= this._maxBpm) {
|
|
330
|
+
peaks.push({ bpm: bpm, score: acf[i], lag: lag2 });
|
|
331
|
+
}
|
|
332
|
+
}
|
|
333
|
+
}
|
|
334
|
+
peaks.sort(function(a, b) { return b.score - a.score; });
|
|
335
|
+
this._candidates = peaks.slice(0, 3);
|
|
336
|
+
|
|
337
|
+
if (this._candidates.length === 0) return;
|
|
338
|
+
|
|
339
|
+
var top = this._candidates[0];
|
|
340
|
+
|
|
341
|
+
// Hysteresis
|
|
342
|
+
if (this._bestBpm <= 0) {
|
|
343
|
+
// First lock
|
|
344
|
+
this._bestBpm = top.bpm;
|
|
345
|
+
this._bestScore = top.score;
|
|
346
|
+
} else {
|
|
347
|
+
var isOctave = (Math.abs(top.bpm / this._bestBpm - 2) < 0.1) ||
|
|
348
|
+
(Math.abs(top.bpm / this._bestBpm - 0.5) < 0.05);
|
|
349
|
+
var margin = isOctave ? 1.5 : 1.15;
|
|
350
|
+
if (top.score > this._bestScore * margin) {
|
|
351
|
+
var oldBpm = this._bestBpm;
|
|
352
|
+
this._bestBpm = top.bpm;
|
|
353
|
+
this._bestScore = top.score;
|
|
354
|
+
// Only fire tempoChange if > 5% different
|
|
355
|
+
if (Math.abs(this._bestBpm - oldBpm) / oldBpm > 0.05) {
|
|
356
|
+
this.port.postMessage({ type: 'tempoChange', newTempo: this._bestBpm, oldTempo: oldBpm });
|
|
357
|
+
}
|
|
358
|
+
} else {
|
|
359
|
+
this._bestScore = this._bestScore * 0.99 + top.score * 0.01; // slowly decay
|
|
360
|
+
}
|
|
361
|
+
}
|
|
362
|
+
|
|
363
|
+
this._updateConfidence();
|
|
364
|
+
}
|
|
365
|
+
|
|
366
|
+
_tickPhase(flux) {
|
|
367
|
+
if (this._lastBeatSample < 0) {
|
|
368
|
+
// Bootstrap: set first beat at current sample
|
|
369
|
+
this._lastBeatSample = this._sampleCount;
|
|
370
|
+
return;
|
|
371
|
+
}
|
|
372
|
+
|
|
373
|
+
var beatIntervalSamples = 60 / this._bestBpm * this._sampleRate;
|
|
374
|
+
var phase = (this._sampleCount - this._lastBeatSample) / beatIntervalSamples;
|
|
375
|
+
|
|
376
|
+
// Snap correction: if novelty peak is strong and phase is 0.7..1.3, snap
|
|
377
|
+
if (phase >= 0.7 && phase <= 1.3 && flux > 0) {
|
|
378
|
+
// Compute mean recent flux
|
|
379
|
+
var recentMean = 0;
|
|
380
|
+
var count = Math.min(this._fluxCount, 16);
|
|
381
|
+
var wp = this._fluxWritePos;
|
|
382
|
+
var len = this._fluxWindow.length;
|
|
383
|
+
for (var i = 0; i < count; i++) {
|
|
384
|
+
recentMean += this._fluxWindow[(wp - 1 - i + len) % len];
|
|
385
|
+
}
|
|
386
|
+
recentMean /= count || 1;
|
|
387
|
+
if (flux > 1.5 * recentMean && recentMean > 0) {
|
|
388
|
+
// Snap to this sample
|
|
389
|
+
this._lastBeatSample = this._sampleCount;
|
|
390
|
+
phase = 1.0;
|
|
391
|
+
}
|
|
392
|
+
}
|
|
393
|
+
|
|
394
|
+
if (phase >= 1.0) {
|
|
395
|
+
var beatTime = (this._lastBeatSample + beatIntervalSamples) / this._sampleRate;
|
|
396
|
+
this._lastBeatSample += beatIntervalSamples;
|
|
397
|
+
|
|
398
|
+
// Update IBI history
|
|
399
|
+
this._ibiHistory[this._ibiIdx] = beatIntervalSamples;
|
|
400
|
+
this._ibiIdx = (this._ibiIdx + 1) & 3;
|
|
401
|
+
|
|
402
|
+
this._beatsSinceStart++;
|
|
403
|
+
|
|
404
|
+
// Update bar position
|
|
405
|
+
this._updateBarPosition(flux);
|
|
406
|
+
|
|
407
|
+
// Compute confidence
|
|
408
|
+
this._updateConfidence();
|
|
409
|
+
|
|
410
|
+
// Lookahead
|
|
411
|
+
this._updateLookahead(beatTime);
|
|
412
|
+
|
|
413
|
+
var isDownbeat = this._barPosition === 1;
|
|
414
|
+
var beatInfo = {
|
|
415
|
+
type: 'beat',
|
|
416
|
+
audioTime: beatTime,
|
|
417
|
+
tempo: this._bestBpm,
|
|
418
|
+
confidence: this._confidence,
|
|
419
|
+
beatPhase: 0,
|
|
420
|
+
energy: flux,
|
|
421
|
+
isDownbeat: isDownbeat,
|
|
422
|
+
beatInBar: this._barPosition,
|
|
423
|
+
};
|
|
424
|
+
this.port.postMessage(beatInfo);
|
|
425
|
+
|
|
426
|
+
if (isDownbeat) {
|
|
427
|
+
this.port.postMessage({
|
|
428
|
+
type: 'barStart',
|
|
429
|
+
audioTime: beatTime,
|
|
430
|
+
tempo: this._bestBpm,
|
|
431
|
+
confidence: this._confidence,
|
|
432
|
+
barNumber: this._barNumber,
|
|
433
|
+
});
|
|
434
|
+
}
|
|
435
|
+
}
|
|
436
|
+
}
|
|
437
|
+
|
|
438
|
+
_computeBeatLikelihood(flux) {
|
|
439
|
+
var count = Math.min(this._fluxCount, 32);
|
|
440
|
+
var wp = this._fluxWritePos;
|
|
441
|
+
var len = this._fluxWindow.length;
|
|
442
|
+
var totalFlux = 0;
|
|
443
|
+
for (var i = 0; i < count; i++) {
|
|
444
|
+
totalFlux += this._fluxWindow[(wp - 1 - i + len) % len];
|
|
445
|
+
}
|
|
446
|
+
var mean = count > 0 ? totalFlux / count : 1;
|
|
447
|
+
return Math.max(0.5, Math.min(1.5, mean > 0 ? flux / mean : 1));
|
|
448
|
+
}
|
|
449
|
+
|
|
450
|
+
_updateBarPosition(flux) {
|
|
451
|
+
var likelihood = this._computeBeatLikelihood(flux);
|
|
452
|
+
|
|
453
|
+
// --- 4/4 posterior ---
|
|
454
|
+
var p4 = this._posterior4;
|
|
455
|
+
var s4 = new Float32Array(4);
|
|
456
|
+
s4[0] = p4[3]; s4[1] = p4[0]; s4[2] = p4[1]; s4[3] = p4[2];
|
|
457
|
+
var sum4 = 0;
|
|
458
|
+
for (var i = 0; i < 4; i++) {
|
|
459
|
+
p4[i] = s4[i] * (likelihood + (i === 0 ? 0.3 : 0));
|
|
460
|
+
sum4 += p4[i];
|
|
461
|
+
}
|
|
462
|
+
if (sum4 > 0) { for (var i = 0; i < 4; i++) p4[i] /= sum4; }
|
|
463
|
+
|
|
464
|
+
// --- 3/4 posterior ---
|
|
465
|
+
var p3 = this._posterior3;
|
|
466
|
+
var s3 = new Float32Array(3);
|
|
467
|
+
s3[0] = p3[2]; s3[1] = p3[0]; s3[2] = p3[1];
|
|
468
|
+
var sum3 = 0;
|
|
469
|
+
for (var i = 0; i < 3; i++) {
|
|
470
|
+
p3[i] = s3[i] * (likelihood + (i === 0 ? 0.3 : 0));
|
|
471
|
+
sum3 += p3[i];
|
|
472
|
+
}
|
|
473
|
+
if (sum3 > 0) { for (var i = 0; i < 3; i++) p3[i] /= sum3; }
|
|
474
|
+
|
|
475
|
+
// --- Update TS confidences (EMA) ---
|
|
476
|
+
var max4 = 0;
|
|
477
|
+
for (var i = 0; i < 4; i++) { if (p4[i] > max4) max4 = p4[i]; }
|
|
478
|
+
var max3 = 0;
|
|
479
|
+
for (var i = 0; i < 3; i++) { if (p3[i] > max3) max3 = p3[i]; }
|
|
480
|
+
var alpha = 0.1;
|
|
481
|
+
this._ts4Confidence = (1 - alpha) * this._ts4Confidence + alpha * max4;
|
|
482
|
+
this._ts3Confidence = (1 - alpha) * this._ts3Confidence + alpha * max3;
|
|
483
|
+
|
|
484
|
+
// --- Hysteresis switching ---
|
|
485
|
+
if (this._enableTsDetection && this._beatsSinceStart > 8) {
|
|
486
|
+
var minSwitchMargin = 1.4;
|
|
487
|
+
var minSustainBeats = 12; // ~4 bars * 3 beats
|
|
488
|
+
var threeFavored = this._ts3Confidence > this._ts4Confidence * minSwitchMargin;
|
|
489
|
+
var fourFavored = this._ts4Confidence > this._ts3Confidence * minSwitchMargin;
|
|
490
|
+
|
|
491
|
+
if (this._activeTs === '4/4' && threeFavored) {
|
|
492
|
+
this._sustainCounter++;
|
|
493
|
+
if (this._sustainCounter >= minSustainBeats) {
|
|
494
|
+
this._activeTs = '3/4';
|
|
495
|
+
this._sustainCounter = 0;
|
|
496
|
+
}
|
|
497
|
+
} else if (this._activeTs === '3/4' && fourFavored) {
|
|
498
|
+
this._sustainCounter++;
|
|
499
|
+
if (this._sustainCounter >= minSustainBeats + 4) { // 16 beats for 4/4
|
|
500
|
+
this._activeTs = '4/4';
|
|
501
|
+
this._sustainCounter = 0;
|
|
502
|
+
}
|
|
503
|
+
} else {
|
|
504
|
+
this._sustainCounter = 0;
|
|
505
|
+
}
|
|
506
|
+
}
|
|
507
|
+
|
|
508
|
+
// --- Determine bar position from active TS ---
|
|
509
|
+
var barLen = this._activeTs === '3/4' ? 3 : 4;
|
|
510
|
+
var posterior = this._activeTs === '3/4' ? p3 : p4;
|
|
511
|
+
|
|
512
|
+
if (this._beatsSinceStart >= barLen) {
|
|
513
|
+
var maxP = -1, maxI = 0;
|
|
514
|
+
for (var i = 0; i < barLen; i++) {
|
|
515
|
+
if (posterior[i] > maxP) { maxP = posterior[i]; maxI = i; }
|
|
516
|
+
}
|
|
517
|
+
var newPos = maxI + 1; // 1-indexed
|
|
518
|
+
if (newPos === 1 && this._barPosition !== 1) {
|
|
519
|
+
this._barNumber++;
|
|
520
|
+
}
|
|
521
|
+
this._barPosition = newPos;
|
|
522
|
+
} else {
|
|
523
|
+
// Just advance sequentially
|
|
524
|
+
this._barPosition = ((this._barPosition) % barLen) + 1;
|
|
525
|
+
if (this._barPosition === 1) this._barNumber++;
|
|
526
|
+
}
|
|
527
|
+
}
|
|
528
|
+
|
|
529
|
+
_updateConfidence() {
|
|
530
|
+
if (this._candidates.length === 0) {
|
|
531
|
+
this._confidence = 0;
|
|
532
|
+
return;
|
|
533
|
+
}
|
|
534
|
+
|
|
535
|
+
// Peak contrast
|
|
536
|
+
var top1 = this._candidates[0] ? this._candidates[0].score : 0;
|
|
537
|
+
var top2 = this._candidates[1] ? this._candidates[1].score : top1;
|
|
538
|
+
var top3 = this._candidates[2] ? this._candidates[2].score : top2;
|
|
539
|
+
var peakContrast = (top2 + top3) > 0 ? top1 / ((top2 + top3) / 2) : 1;
|
|
540
|
+
|
|
541
|
+
// Phase consistency from IBI variance
|
|
542
|
+
var ibiMean = 0;
|
|
543
|
+
for (var i = 0; i < 4; i++) ibiMean += this._ibiHistory[i];
|
|
544
|
+
ibiMean /= 4;
|
|
545
|
+
var ibiVar = 0;
|
|
546
|
+
for (var i = 0; i < 4; i++) {
|
|
547
|
+
var d = this._ibiHistory[i] - ibiMean;
|
|
548
|
+
ibiVar += d * d;
|
|
549
|
+
}
|
|
550
|
+
ibiVar /= 4;
|
|
551
|
+
var phaseConsistency = ibiMean > 0 ? Math.max(0, 1 - ibiVar / (ibiMean * ibiMean)) : 0;
|
|
552
|
+
|
|
553
|
+
// Bar consistency (use the active posterior)
|
|
554
|
+
var activePosterior = this._activeTs === '3/4' ? this._posterior3 : this._posterior4;
|
|
555
|
+
var activeLen = this._activeTs === '3/4' ? 3 : 4;
|
|
556
|
+
var maxP = 0;
|
|
557
|
+
for (var i = 0; i < activeLen; i++) {
|
|
558
|
+
if (activePosterior[i] > maxP) maxP = activePosterior[i];
|
|
559
|
+
}
|
|
560
|
+
var barConsistency = maxP;
|
|
561
|
+
|
|
562
|
+
var c = Math.sqrt(Math.max(0, peakContrast / 2)) *
|
|
563
|
+
Math.sqrt(Math.max(0, phaseConsistency)) *
|
|
564
|
+
(0.5 + 0.5 * barConsistency);
|
|
565
|
+
this._confidence = Math.max(0, Math.min(1, c));
|
|
566
|
+
}
|
|
567
|
+
|
|
568
|
+
_updateLookahead(lastBeatTime) {
|
|
569
|
+
var lookahead = [];
|
|
570
|
+
var beatInterval = 60 / this._bestBpm;
|
|
571
|
+
var barPos = this._barPosition;
|
|
572
|
+
var barLen = this._activeTs === '3/4' ? 3 : 4;
|
|
573
|
+
for (var i = 0; i < 8; i++) {
|
|
574
|
+
var t = lastBeatTime + (i + 1) * beatInterval;
|
|
575
|
+
var bp = ((barPos - 1 + i) % barLen) + 1;
|
|
576
|
+
lookahead.push({
|
|
577
|
+
audioTime: t,
|
|
578
|
+
tempo: this._bestBpm,
|
|
579
|
+
isDownbeat: bp === 1,
|
|
580
|
+
beatInBar: bp,
|
|
581
|
+
});
|
|
582
|
+
}
|
|
583
|
+
this._lookahead = lookahead;
|
|
584
|
+
}
|
|
585
|
+
|
|
586
|
+
_computeBandEnergy() {
|
|
587
|
+
var low = 0, mid = 0, high = 0;
|
|
588
|
+
for (var b = 0; b < this._lowBandEnd; b++) low += this._melOut[b];
|
|
589
|
+
for (var b = this._lowBandEnd; b < this._midBandEnd; b++) mid += this._melOut[b];
|
|
590
|
+
for (var b = this._midBandEnd; b < this._melBands; b++) high += this._melOut[b];
|
|
591
|
+
var denom = this._lowBandEnd || 1;
|
|
592
|
+
return {
|
|
593
|
+
low: low / denom,
|
|
594
|
+
mid: mid / Math.max(1, this._midBandEnd - this._lowBandEnd),
|
|
595
|
+
high: high / Math.max(1, this._melBands - this._midBandEnd),
|
|
596
|
+
};
|
|
597
|
+
}
|
|
598
|
+
|
|
599
|
+
_sendStateMessage() {
|
|
600
|
+
var settled = (this._sampleCount - this._settledSamples) > 0;
|
|
601
|
+
var beatInterval = this._bestBpm > 0 ? 60 / this._bestBpm : 0;
|
|
602
|
+
var currentTime = this._sampleCount / this._sampleRate;
|
|
603
|
+
var lastBeatTime = this._lastBeatSample >= 0 ? this._lastBeatSample / this._sampleRate : 0;
|
|
604
|
+
var beatPhase = beatInterval > 0
|
|
605
|
+
? Math.min(1, (currentTime - lastBeatTime) / beatInterval)
|
|
606
|
+
: 0;
|
|
607
|
+
var nextBeatTime = lastBeatTime + beatInterval;
|
|
608
|
+
|
|
609
|
+
var nextDownbeatTime = nextBeatTime;
|
|
610
|
+
for (var i = 0; i < this._lookahead.length; i++) {
|
|
611
|
+
if (this._lookahead[i].isDownbeat) {
|
|
612
|
+
nextDownbeatTime = this._lookahead[i].audioTime;
|
|
613
|
+
break;
|
|
614
|
+
}
|
|
615
|
+
}
|
|
616
|
+
|
|
617
|
+
var be = this._computeBandEnergy();
|
|
618
|
+
|
|
619
|
+
this.port.postMessage({
|
|
620
|
+
type: 'state',
|
|
621
|
+
tempo: settled ? this._bestBpm : 0,
|
|
622
|
+
beatPhase: beatPhase,
|
|
623
|
+
confidence: settled ? this._confidence : 0,
|
|
624
|
+
gridStability: settled ? this._confidence : 0,
|
|
625
|
+
tempoCandidates: settled ? this._candidates.map(function(c) {
|
|
626
|
+
return { bpm: c.bpm, score: c.score };
|
|
627
|
+
}) : [],
|
|
628
|
+
rms: this._rms,
|
|
629
|
+
onsetStrength: this._onsetStrength,
|
|
630
|
+
bandEnergy: be,
|
|
631
|
+
barPosition: this._barPosition,
|
|
632
|
+
barLength: this._activeTs === '3/4' ? 3 : 4,
|
|
633
|
+
timeSignature: this._activeTs === '3/4'
|
|
634
|
+
? { numerator: 3, denominator: 4 }
|
|
635
|
+
: { numerator: 4, denominator: 4 },
|
|
636
|
+
lookahead: this._lookahead,
|
|
637
|
+
nextBeatTime: settled ? nextBeatTime : 0,
|
|
638
|
+
nextDownbeatTime: settled ? nextDownbeatTime : 0,
|
|
639
|
+
});
|
|
640
|
+
}
|
|
641
|
+
}
|
|
642
|
+
|
|
643
|
+
registerProcessor('exojs-beat-detector', BeatDetectorProcessor);
|
|
644
|
+
`;
|
|
645
|
+
|
|
646
|
+
export { beatDetectorWorkletSource };
|
|
647
|
+
//# sourceMappingURL=beat-detector.worklet.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"beat-detector.worklet.js","sources":["../../../../src/worklets/beat-detector.worklet.ts"],"sourcesContent":[null],"names":[],"mappings":"AAAO,MAAM,yBAAyB,GAAG;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare const granularWorkletSource = "\nconst sampleRate = globalThis.sampleRate;\n\nclass GranularProcessor extends AudioWorkletProcessor {\n static get parameterDescriptors() {\n return [\n { name: 'grainSize', defaultValue: 0.05, minValue: 0.005, maxValue: 0.5, automationRate: 'k-rate' },\n { name: 'density', defaultValue: 50, minValue: 1, maxValue: 500, automationRate: 'k-rate' },\n { name: 'spread', defaultValue: 0.5, minValue: 0, maxValue: 1.0, automationRate: 'k-rate' },\n { name: 'pitchMin', defaultValue: 1.0, minValue: 0.25, maxValue: 4.0, automationRate: 'k-rate' },\n { name: 'pitchMax', defaultValue: 1.0, minValue: 0.25, maxValue: 4.0, automationRate: 'k-rate' },\n { name: 'wet', defaultValue: 1.0, minValue: 0, maxValue: 1.0, automationRate: 'k-rate' },\n ];\n }\n\n constructor(options) {\n super();\n const opts = options.processorOptions ?? {};\n const bufferSeconds = opts.bufferSeconds ?? 2;\n this._bufferLength = Math.floor(bufferSeconds * sampleRate);\n this._buffer = new Float32Array(this._bufferLength);\n this._writePos = 0;\n this._timeUntilNextGrainSamples = 0;\n this._grains = []; // { startPos, ageSamples, lengthSamples, pitch }\n }\n\n process(inputs, outputs, parameters) {\n const input = inputs[0]?.[0];\n const output = outputs[0]?.[0];\n if (!input || !output) return true;\n\n const grainSize = parameters.grainSize[0];\n const density = parameters.density[0];\n const spread = parameters.spread[0];\n const pitchMin = parameters.pitchMin[0];\n const pitchMax = parameters.pitchMax[0];\n const wet = parameters.wet[0];\n\n const grainSizeSamples = Math.max(8, Math.floor(grainSize * sampleRate));\n const samplesPerGrain = sampleRate / Math.max(1, density);\n\n for (let i = 0; i < input.length; i++) {\n // Write input to circular buffer\n this._buffer[this._writePos] = input[i];\n this._writePos = (this._writePos + 1) % this._bufferLength;\n\n // Spawn new grain if scheduled\n this._timeUntilNextGrainSamples -= 1;\n if (this._timeUntilNextGrainSamples <= 0) {\n // Random offset into recent past, biased by spread\n const maxOffset = Math.floor(spread * this._bufferLength);\n const offset = Math.floor(Math.random() * Math.max(1, maxOffset));\n const startPos = (this._writePos - offset - grainSizeSamples + this._bufferLength) % this._bufferLength;\n // Random pitch in [pitchMin, pitchMax]\n const pitch = pitchMin + Math.random() * Math.max(0, pitchMax - pitchMin);\n\n this._grains.push({ startPos, ageSamples: 0, lengthSamples: grainSizeSamples, pitch });\n this._timeUntilNextGrainSamples = samplesPerGrain;\n }\n\n // Mix all active grains (apply Hann window)\n let grainSum = 0;\n for (let g = this._grains.length - 1; g >= 0; g--) {\n const grain = this._grains[g];\n if (grain.ageSamples >= grain.lengthSamples) {\n this._grains.splice(g, 1);\n continue;\n }\n\n const phase = grain.ageSamples / grain.lengthSamples;\n const window = 0.5 * (1 - Math.cos(2 * Math.PI * phase));\n const readPos = grain.startPos + grain.ageSamples * grain.pitch;\n const sampleIndex = Math.floor(readPos) % this._bufferLength;\n const safeIndex = (sampleIndex + this._bufferLength) % this._bufferLength;\n grainSum += this._buffer[safeIndex] * window;\n\n grain.ageSamples++;\n }\n\n output[i] = (1 - wet) * input[i] + wet * grainSum;\n }\n return true;\n }\n}\nregisterProcessor('exojs-granular', GranularProcessor);\n";
|