@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.
Files changed (56) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +25 -0
  3. package/dist/esm/AudioAnalyser.d.ts +124 -0
  4. package/dist/esm/AudioAnalyser.js +472 -0
  5. package/dist/esm/AudioAnalyser.js.map +1 -0
  6. package/dist/esm/BeatDetector.d.ts +203 -0
  7. package/dist/esm/BeatDetector.js +422 -0
  8. package/dist/esm/BeatDetector.js.map +1 -0
  9. package/dist/esm/dsp/fft.d.ts +22 -0
  10. package/dist/esm/dsp/mel.d.ts +43 -0
  11. package/dist/esm/dsp/mel.js +70 -0
  12. package/dist/esm/dsp/mel.js.map +1 -0
  13. package/dist/esm/dsp/tempogram.d.ts +51 -0
  14. package/dist/esm/effects/ChorusEffect.d.ts +53 -0
  15. package/dist/esm/effects/ChorusEffect.js +154 -0
  16. package/dist/esm/effects/ChorusEffect.js.map +1 -0
  17. package/dist/esm/effects/CompressorEffect.d.ts +53 -0
  18. package/dist/esm/effects/CompressorEffect.js +122 -0
  19. package/dist/esm/effects/CompressorEffect.js.map +1 -0
  20. package/dist/esm/effects/DelayEffect.d.ts +35 -0
  21. package/dist/esm/effects/DelayEffect.js +113 -0
  22. package/dist/esm/effects/DelayEffect.js.map +1 -0
  23. package/dist/esm/effects/DuckingEffect.d.ts +43 -0
  24. package/dist/esm/effects/DuckingEffect.js +172 -0
  25. package/dist/esm/effects/DuckingEffect.js.map +1 -0
  26. package/dist/esm/effects/EqualizerEffect.d.ts +50 -0
  27. package/dist/esm/effects/EqualizerEffect.js +137 -0
  28. package/dist/esm/effects/EqualizerEffect.js.map +1 -0
  29. package/dist/esm/effects/GranularEffect.d.ts +62 -0
  30. package/dist/esm/effects/GranularEffect.js +107 -0
  31. package/dist/esm/effects/GranularEffect.js.map +1 -0
  32. package/dist/esm/effects/PitchShiftEffect.d.ts +44 -0
  33. package/dist/esm/effects/PitchShiftEffect.js +66 -0
  34. package/dist/esm/effects/PitchShiftEffect.js.map +1 -0
  35. package/dist/esm/effects/ReverbEffect.d.ts +37 -0
  36. package/dist/esm/effects/ReverbEffect.js +121 -0
  37. package/dist/esm/effects/ReverbEffect.js.map +1 -0
  38. package/dist/esm/effects/VocoderEffect.d.ts +48 -0
  39. package/dist/esm/effects/VocoderEffect.js +94 -0
  40. package/dist/esm/effects/VocoderEffect.js.map +1 -0
  41. package/dist/esm/index.d.ts +13 -0
  42. package/dist/esm/index.js +12 -0
  43. package/dist/esm/index.js.map +1 -0
  44. package/dist/esm/worklets/beat-detector.worklet.d.ts +1 -0
  45. package/dist/esm/worklets/beat-detector.worklet.js +647 -0
  46. package/dist/esm/worklets/beat-detector.worklet.js.map +1 -0
  47. package/dist/esm/worklets/granular.worklet.d.ts +1 -0
  48. package/dist/esm/worklets/granular.worklet.js +89 -0
  49. package/dist/esm/worklets/granular.worklet.js.map +1 -0
  50. package/dist/esm/worklets/pitch-shift.worklet.d.ts +1 -0
  51. package/dist/esm/worklets/pitch-shift.worklet.js +74 -0
  52. package/dist/esm/worklets/pitch-shift.worklet.js.map +1 -0
  53. package/dist/esm/worklets/vocoder.worklet.d.ts +1 -0
  54. package/dist/esm/worklets/vocoder.worklet.js +92 -0
  55. package/dist/esm/worklets/vocoder.worklet.js.map +1 -0
  56. package/package.json +46 -0
@@ -0,0 +1,203 @@
1
+ import { type AudioBus, Signal, type Voice } from '@codexo/exojs';
2
+ export type BeatDetectorSource = AudioBus | Voice | MediaStream | AudioNode | null;
3
+ export interface BeatDetectorOptions {
4
+ /** Minimum detectable BPM. Default 50. */
5
+ minBpm?: number;
6
+ /** Maximum detectable BPM. Default 250. */
7
+ maxBpm?: number;
8
+ /** FFT size for onset detection. Default 2048. */
9
+ fftSize?: number;
10
+ /** Hop size in samples between successive FFTs. Default 512. */
11
+ hopSize?: number;
12
+ /** Sliding window duration for tempogram (seconds). Default 6. */
13
+ tempoWindowSec?: number;
14
+ /** Initial suppression period before beats are emitted (ms). Default 1500. */
15
+ settlingMs?: number;
16
+ /** Number of mel filterbank bands. Default 24. */
17
+ melBands?: number;
18
+ /**
19
+ * When true (default), the worklet runs parallel 3/4 and 4/4 posteriors and
20
+ * switches active time signature via hysteresis. Set false to lock to 4/4.
21
+ */
22
+ enableTimeSignatureDetection?: boolean;
23
+ /**
24
+ * Optional initial source. Equivalent to constructing then assigning
25
+ * `detector.source = value`; provided for ergonomic one-shot construction.
26
+ * The setter remains usable for runtime source switches.
27
+ */
28
+ source?: BeatDetectorSource;
29
+ /**
30
+ * Half-life in seconds for the {@link BeatDetector.pulse} envelope.
31
+ * Default 0.15 — `pulse` halves every 150ms after each beat.
32
+ */
33
+ pulseHalfLife?: number;
34
+ /**
35
+ * Half-life in seconds for the {@link BeatDetector.barPulse} envelope.
36
+ * Default 0.3 — slower than beat pulse for downbeat-emphasized visuals.
37
+ */
38
+ barPulseHalfLife?: number;
39
+ /**
40
+ * Time window in seconds for {@link BeatDetector.justBeat}. Default 0.03
41
+ * — true for the visual frame(s) within 30ms of a beat onset.
42
+ */
43
+ justBeatWindow?: number;
44
+ }
45
+ export interface BeatInfo {
46
+ /** audioContext.currentTime when the beat occurred. */
47
+ audioTime: number;
48
+ /** BPM at this beat. */
49
+ tempo: number;
50
+ /** Confidence 0..1. */
51
+ confidence: number;
52
+ /** Phase within beat (0 = start). */
53
+ beatPhase: number;
54
+ /** Novelty/onset strength at the beat. */
55
+ energy: number;
56
+ /** Is this beat the first in a bar? */
57
+ isDownbeat: boolean;
58
+ /** Beat position within the bar (1..N). */
59
+ beatInBar: number;
60
+ }
61
+ export interface UpcomingBeat {
62
+ audioTime: number;
63
+ tempo: number;
64
+ isDownbeat: boolean;
65
+ beatInBar: number;
66
+ }
67
+ export interface BarInfo {
68
+ audioTime: number;
69
+ tempo: number;
70
+ confidence: number;
71
+ /** Monotonically increasing bar counter since detector start. */
72
+ barNumber: number;
73
+ }
74
+ export interface TimeSignature {
75
+ numerator: number;
76
+ denominator: number;
77
+ }
78
+ export interface TempoCandidate {
79
+ bpm: number;
80
+ /** Peak strength 0..1. */
81
+ score: number;
82
+ }
83
+ export interface BandEnergy {
84
+ low: number;
85
+ mid: number;
86
+ high: number;
87
+ }
88
+ /**
89
+ * Real-time tempo + beat tracker. Splits work between the audio-rendering
90
+ * thread (an AudioWorklet that runs onset detection, tempogram analysis,
91
+ * and parallel 3/4 and 4/4 posterior estimation) and the main thread (this
92
+ * class — receives beats, fires Signals, handles configuration and source
93
+ * routing).
94
+ *
95
+ * Accepts a wide range of {@link BeatDetectorSource}s — a bus, an individual
96
+ * {@link Voice}, a raw MediaStream, or any AudioNode — and exposes a Signal
97
+ * for each notable event:
98
+ * - {@link BeatDetector.onBeat} — every detected beat
99
+ * - {@link BeatDetector.onDownbeat} — first beat of each bar
100
+ * - {@link BeatDetector.onBarStart} — bar boundary
101
+ * - {@link BeatDetector.onTempoChange} — when the tracked BPM changes
102
+ * - {@link BeatDetector.onBeatPredicted} — look-ahead schedule notice
103
+ *
104
+ * Detection is delayed by `settlingMs` (default 1500 ms) after a new
105
+ * source is attached; this prevents spurious beats during the algorithm's
106
+ * warm-up. Time-signature detection (3/4 vs 4/4) is on by default; lock
107
+ * to 4/4 by setting `enableTimeSignatureDetection: false`.
108
+ */
109
+ export declare class BeatDetector {
110
+ readonly onBeat: Signal<[BeatInfo]>;
111
+ readonly onTempoChange: Signal<[number, number]>;
112
+ readonly onDownbeat: Signal<[BeatInfo]>;
113
+ readonly onBarStart: Signal<[BarInfo]>;
114
+ readonly onBeatPredicted: Signal<[UpcomingBeat]>;
115
+ private readonly _options;
116
+ private _workletNode;
117
+ private _source;
118
+ private _tapSource;
119
+ private _streamSource;
120
+ private _ready;
121
+ private _pendingSourceSetup;
122
+ private readonly _onAudioContextReady;
123
+ private _tempo;
124
+ private _beatPhase;
125
+ private _nextBeatTime;
126
+ private _confidence;
127
+ private _gridStability;
128
+ private _tempoCandidates;
129
+ private _rms;
130
+ private _onsetStrength;
131
+ private _bandEnergy;
132
+ private _barPosition;
133
+ private _barLength;
134
+ private _timeSignature;
135
+ private _nextDownbeatTime;
136
+ private _lookahead;
137
+ /**
138
+ * Half-life in seconds for the {@link pulse} envelope. Mutable; default 0.15.
139
+ * Smaller values give a snappier pulse, larger values a longer afterglow.
140
+ */
141
+ pulseHalfLife: number;
142
+ /** Half-life for the {@link barPulse} envelope. Mutable; default 0.3. */
143
+ barPulseHalfLife: number;
144
+ /** Time window for {@link justBeat}. Mutable; default 0.03 (30ms). */
145
+ justBeatWindow: number;
146
+ constructor(options?: BeatDetectorOptions);
147
+ get source(): BeatDetectorSource;
148
+ set source(value: BeatDetectorSource);
149
+ get ready(): Promise<void>;
150
+ get tempo(): number;
151
+ get beatPhase(): number;
152
+ get nextBeatTime(): number;
153
+ get confidence(): number;
154
+ get gridStability(): number;
155
+ get rms(): number;
156
+ get onsetStrength(): number;
157
+ get bandEnergy(): BandEnergy;
158
+ get tempoCandidates(): readonly TempoCandidate[];
159
+ get barPosition(): number;
160
+ get barLength(): number;
161
+ get timeSignature(): TimeSignature;
162
+ get nextDownbeatTime(): number;
163
+ get lookahead(): readonly UpcomingBeat[];
164
+ /**
165
+ * Seconds elapsed since the most recent beat, derived from {@link beatPhase}
166
+ * and {@link tempo}. Returns 0 when the detector hasn't locked yet.
167
+ */
168
+ get secondsSinceLastBeat(): number;
169
+ /**
170
+ * 0..1 envelope, peaks at 1.0 the moment a beat fires and halves every
171
+ * {@link pulseHalfLife} seconds. Drives "pulse on the beat" visuals
172
+ * with a single multiplication: `sprite.scale = 1 + clock.pulse * 0.3`.
173
+ */
174
+ get pulse(): number;
175
+ /**
176
+ * Like {@link pulse} but resets on downbeats and decays per
177
+ * {@link barPulseHalfLife}. Useful for emphasizing the first beat of
178
+ * each bar (e.g. brighter flash on "1" vs "2,3,4").
179
+ */
180
+ get barPulse(): number;
181
+ /**
182
+ * True for the visual frame(s) within {@link justBeatWindow} seconds of
183
+ * a beat onset. Use for one-shot triggers (strobe flash, particle burst,
184
+ * sample retrigger). Default window 30ms covers a typical 60fps frame.
185
+ */
186
+ get justBeat(): boolean;
187
+ /**
188
+ * Phase 0..1 within a subdivision of the current beat. `division` is the
189
+ * number of subdivisions per beat: 2 for 8th notes, 4 for 16th notes,
190
+ * 3 for triplets. Use to drive sub-beat-resolution effects:
191
+ *
192
+ * const sixteenth = clock.subdivisionPhase(4);
193
+ * if (sixteenth < 0.05) flash();
194
+ */
195
+ subdivisionPhase(division: number): number;
196
+ destroy(): void;
197
+ private _setup;
198
+ private _onWorkletMessage;
199
+ private _connectSource;
200
+ private _resolveToAudioNode;
201
+ private _deferConnectionViaBus;
202
+ private _disconnectTap;
203
+ }
@@ -0,0 +1,422 @@
1
+ import { Signal, onAudioContextReady, isAudioContextReady, getAudioContext, registerAudioWorkletProcessor } from '@codexo/exojs';
2
+ import { beatDetectorWorkletSource } from './worklets/beat-detector.worklet.js';
3
+
4
+ const workletName = 'exojs-beat-detector';
5
+ /**
6
+ * Real-time tempo + beat tracker. Splits work between the audio-rendering
7
+ * thread (an AudioWorklet that runs onset detection, tempogram analysis,
8
+ * and parallel 3/4 and 4/4 posterior estimation) and the main thread (this
9
+ * class — receives beats, fires Signals, handles configuration and source
10
+ * routing).
11
+ *
12
+ * Accepts a wide range of {@link BeatDetectorSource}s — a bus, an individual
13
+ * {@link Voice}, a raw MediaStream, or any AudioNode — and exposes a Signal
14
+ * for each notable event:
15
+ * - {@link BeatDetector.onBeat} — every detected beat
16
+ * - {@link BeatDetector.onDownbeat} — first beat of each bar
17
+ * - {@link BeatDetector.onBarStart} — bar boundary
18
+ * - {@link BeatDetector.onTempoChange} — when the tracked BPM changes
19
+ * - {@link BeatDetector.onBeatPredicted} — look-ahead schedule notice
20
+ *
21
+ * Detection is delayed by `settlingMs` (default 1500 ms) after a new
22
+ * source is attached; this prevents spurious beats during the algorithm's
23
+ * warm-up. Time-signature detection (3/4 vs 4/4) is on by default; lock
24
+ * to 4/4 by setting `enableTimeSignatureDetection: false`.
25
+ */
26
+ class BeatDetector {
27
+ // ---- Signals ----
28
+ onBeat = new Signal();
29
+ onTempoChange = new Signal();
30
+ onDownbeat = new Signal();
31
+ onBarStart = new Signal();
32
+ onBeatPredicted = new Signal();
33
+ // ---- Options ----
34
+ // enableTimeSignatureDetection not in Required<> since it has a default; keep as full explicit type
35
+ _options;
36
+ // ---- Audio plumbing ----
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
+ // ---- Cached state from worklet ----
48
+ _tempo = 0;
49
+ _beatPhase = 0;
50
+ _nextBeatTime = 0;
51
+ _confidence = 0;
52
+ _gridStability = 0;
53
+ _tempoCandidates = [];
54
+ _rms = 0;
55
+ _onsetStrength = 0;
56
+ _bandEnergy = { low: 0, mid: 0, high: 0 };
57
+ _barPosition = 1;
58
+ _barLength = 4;
59
+ _timeSignature = { numerator: 4, denominator: 4 };
60
+ _nextDownbeatTime = 0;
61
+ _lookahead = Object.freeze([]);
62
+ /**
63
+ * Half-life in seconds for the {@link pulse} envelope. Mutable; default 0.15.
64
+ * Smaller values give a snappier pulse, larger values a longer afterglow.
65
+ */
66
+ pulseHalfLife;
67
+ /** Half-life for the {@link barPulse} envelope. Mutable; default 0.3. */
68
+ barPulseHalfLife;
69
+ /** Time window for {@link justBeat}. Mutable; default 0.03 (30ms). */
70
+ justBeatWindow;
71
+ constructor(options) {
72
+ this._options = {
73
+ minBpm: options?.minBpm ?? 50,
74
+ maxBpm: options?.maxBpm ?? 250,
75
+ fftSize: options?.fftSize ?? 2048,
76
+ hopSize: options?.hopSize ?? 512,
77
+ tempoWindowSec: options?.tempoWindowSec ?? 6,
78
+ settlingMs: options?.settlingMs ?? 1500,
79
+ melBands: options?.melBands ?? 24,
80
+ enableTimeSignatureDetection: options?.enableTimeSignatureDetection ?? true,
81
+ // Visual-state options aren't part of worklet config; cached in the public
82
+ // fields below but kept in the Required<> shape for type completeness.
83
+ source: options?.source ?? null,
84
+ pulseHalfLife: options?.pulseHalfLife ?? 0.15,
85
+ barPulseHalfLife: options?.barPulseHalfLife ?? 0.3,
86
+ justBeatWindow: options?.justBeatWindow ?? 0.03,
87
+ };
88
+ this.pulseHalfLife = this._options.pulseHalfLife;
89
+ this.barPulseHalfLife = this._options.barPulseHalfLife;
90
+ this.justBeatWindow = this._options.justBeatWindow;
91
+ if (isAudioContextReady()) {
92
+ this._setup(getAudioContext());
93
+ }
94
+ else {
95
+ onAudioContextReady.add(this._onAudioContextReady);
96
+ }
97
+ if (options?.source !== undefined && options.source !== null) {
98
+ this.source = options.source;
99
+ }
100
+ }
101
+ // -----------------------------------------------------------------------
102
+ // Source setter (polymorphic tap)
103
+ // -----------------------------------------------------------------------
104
+ get source() {
105
+ return this._source;
106
+ }
107
+ set source(value) {
108
+ if (value === this._source)
109
+ return;
110
+ this._disconnectTap();
111
+ this._source = value;
112
+ if (value === null)
113
+ return;
114
+ if (isAudioContextReady()) {
115
+ this._pendingSourceSetup = null;
116
+ this._connectSource(value, getAudioContext());
117
+ }
118
+ else {
119
+ if (this._pendingSourceSetup !== null) {
120
+ onAudioContextReady.remove(this._pendingSourceSetup);
121
+ }
122
+ const handler = (ctx) => {
123
+ onAudioContextReady.remove(handler);
124
+ this._pendingSourceSetup = null;
125
+ this._connectSource(value, ctx);
126
+ };
127
+ this._pendingSourceSetup = handler;
128
+ onAudioContextReady.add(handler);
129
+ }
130
+ }
131
+ // -----------------------------------------------------------------------
132
+ // Ready promise
133
+ // -----------------------------------------------------------------------
134
+ get ready() {
135
+ return this._ready ?? Promise.resolve();
136
+ }
137
+ // -----------------------------------------------------------------------
138
+ // Stage 1 state accessors
139
+ // -----------------------------------------------------------------------
140
+ get tempo() {
141
+ return this._tempo;
142
+ }
143
+ get beatPhase() {
144
+ return this._beatPhase;
145
+ }
146
+ get nextBeatTime() {
147
+ return this._nextBeatTime;
148
+ }
149
+ get confidence() {
150
+ return this._confidence;
151
+ }
152
+ get gridStability() {
153
+ return this._gridStability;
154
+ }
155
+ get rms() {
156
+ return this._rms;
157
+ }
158
+ get onsetStrength() {
159
+ return this._onsetStrength;
160
+ }
161
+ get bandEnergy() {
162
+ return this._bandEnergy;
163
+ }
164
+ get tempoCandidates() {
165
+ return this._tempoCandidates;
166
+ }
167
+ // -----------------------------------------------------------------------
168
+ // Stage 2 state accessors
169
+ // -----------------------------------------------------------------------
170
+ get barPosition() {
171
+ return this._barPosition;
172
+ }
173
+ get barLength() {
174
+ return this._barLength;
175
+ }
176
+ get timeSignature() {
177
+ return this._timeSignature;
178
+ }
179
+ get nextDownbeatTime() {
180
+ return this._nextDownbeatTime;
181
+ }
182
+ get lookahead() {
183
+ return this._lookahead;
184
+ }
185
+ // -----------------------------------------------------------------------
186
+ // Visual derived state — pure getters for per-frame polling
187
+ // -----------------------------------------------------------------------
188
+ /**
189
+ * Seconds elapsed since the most recent beat, derived from {@link beatPhase}
190
+ * and {@link tempo}. Returns 0 when the detector hasn't locked yet.
191
+ */
192
+ get secondsSinceLastBeat() {
193
+ if (this._tempo === 0)
194
+ return 0;
195
+ return this._beatPhase * (60 / this._tempo);
196
+ }
197
+ /**
198
+ * 0..1 envelope, peaks at 1.0 the moment a beat fires and halves every
199
+ * {@link pulseHalfLife} seconds. Drives "pulse on the beat" visuals
200
+ * with a single multiplication: `sprite.scale = 1 + clock.pulse * 0.3`.
201
+ */
202
+ get pulse() {
203
+ if (this._tempo === 0)
204
+ return 0;
205
+ return Math.pow(0.5, this.secondsSinceLastBeat / this.pulseHalfLife);
206
+ }
207
+ /**
208
+ * Like {@link pulse} but resets on downbeats and decays per
209
+ * {@link barPulseHalfLife}. Useful for emphasizing the first beat of
210
+ * each bar (e.g. brighter flash on "1" vs "2,3,4").
211
+ */
212
+ get barPulse() {
213
+ if (this._tempo === 0 || this._barLength === 0)
214
+ return 0;
215
+ const secondsPerBeat = 60 / this._tempo;
216
+ const lastDownbeat = this._nextDownbeatTime - this._barLength * secondsPerBeat;
217
+ const elapsed = Math.max(0, getAudioContext().currentTime - lastDownbeat);
218
+ return Math.pow(0.5, elapsed / this.barPulseHalfLife);
219
+ }
220
+ /**
221
+ * True for the visual frame(s) within {@link justBeatWindow} seconds of
222
+ * a beat onset. Use for one-shot triggers (strobe flash, particle burst,
223
+ * sample retrigger). Default window 30ms covers a typical 60fps frame.
224
+ */
225
+ get justBeat() {
226
+ return this._tempo > 0 && this.secondsSinceLastBeat < this.justBeatWindow;
227
+ }
228
+ /**
229
+ * Phase 0..1 within a subdivision of the current beat. `division` is the
230
+ * number of subdivisions per beat: 2 for 8th notes, 4 for 16th notes,
231
+ * 3 for triplets. Use to drive sub-beat-resolution effects:
232
+ *
233
+ * const sixteenth = clock.subdivisionPhase(4);
234
+ * if (sixteenth < 0.05) flash();
235
+ */
236
+ subdivisionPhase(division) {
237
+ if (!Number.isFinite(division) || division <= 0)
238
+ return 0;
239
+ return (this._beatPhase * division) % 1;
240
+ }
241
+ // -----------------------------------------------------------------------
242
+ // Lifecycle
243
+ // -----------------------------------------------------------------------
244
+ destroy() {
245
+ onAudioContextReady.remove(this._onAudioContextReady);
246
+ if (this._pendingSourceSetup !== null) {
247
+ onAudioContextReady.remove(this._pendingSourceSetup);
248
+ this._pendingSourceSetup = null;
249
+ }
250
+ this._disconnectTap();
251
+ this._workletNode?.disconnect();
252
+ this._workletNode = null;
253
+ this._ready = null;
254
+ this.onBeat.clear();
255
+ this.onTempoChange.clear();
256
+ this.onDownbeat.clear();
257
+ this.onBarStart.clear();
258
+ this.onBeatPredicted.clear();
259
+ }
260
+ // -----------------------------------------------------------------------
261
+ // Private helpers — setup
262
+ // -----------------------------------------------------------------------
263
+ _setup(audioContext) {
264
+ const opts = this._options;
265
+ this._ready = registerAudioWorkletProcessor(audioContext, workletName, beatDetectorWorkletSource).then(() => {
266
+ const node = new AudioWorkletNode(audioContext, workletName, {
267
+ numberOfInputs: 1,
268
+ numberOfOutputs: 0,
269
+ processorOptions: {
270
+ fftSize: opts.fftSize,
271
+ hopSize: opts.hopSize,
272
+ minBpm: opts.minBpm,
273
+ maxBpm: opts.maxBpm,
274
+ melBands: opts.melBands,
275
+ settlingMs: opts.settlingMs,
276
+ tempoWindowSec: opts.tempoWindowSec,
277
+ enableTimeSignatureDetection: opts.enableTimeSignatureDetection,
278
+ },
279
+ });
280
+ this._workletNode = node;
281
+ node.port.onmessage = this._onWorkletMessage.bind(this);
282
+ // If a source was set before worklet was ready, connect it now.
283
+ if (this._source !== null) {
284
+ this._connectSource(this._source, audioContext);
285
+ }
286
+ });
287
+ }
288
+ _onWorkletMessage(event) {
289
+ const message = event.data;
290
+ switch (message.type) {
291
+ case 'state':
292
+ this._tempo = message.tempo ?? 0;
293
+ this._beatPhase = message.beatPhase ?? 0;
294
+ this._nextBeatTime = message.nextBeatTime ?? 0;
295
+ this._nextDownbeatTime = message.nextDownbeatTime ?? 0;
296
+ this._confidence = message.confidence ?? 0;
297
+ this._gridStability = message.gridStability ?? 0;
298
+ this._tempoCandidates = Object.freeze(message.tempoCandidates ?? []);
299
+ this._rms = message.rms ?? 0;
300
+ this._onsetStrength = message.onsetStrength ?? 0;
301
+ this._bandEnergy = message.bandEnergy ?? { low: 0, mid: 0, high: 0 };
302
+ this._barPosition = message.barPosition ?? 1;
303
+ this._barLength = message.barLength ?? 4;
304
+ this._timeSignature = message.timeSignature ?? { numerator: 4, denominator: 4 };
305
+ {
306
+ const lookahead = message.lookahead ?? [];
307
+ this._lookahead = Object.freeze(lookahead);
308
+ if (lookahead.length > 0) {
309
+ this.onBeatPredicted.dispatch(lookahead[0]);
310
+ }
311
+ }
312
+ break;
313
+ case 'beat': {
314
+ const bi = {
315
+ audioTime: message.audioTime,
316
+ tempo: message.tempo,
317
+ confidence: message.confidence,
318
+ beatPhase: message.beatPhase,
319
+ energy: message.energy,
320
+ isDownbeat: message.isDownbeat,
321
+ beatInBar: message.beatInBar,
322
+ };
323
+ this.onBeat.dispatch(bi);
324
+ if (bi.isDownbeat)
325
+ this.onDownbeat.dispatch(bi);
326
+ break;
327
+ }
328
+ case 'tempoChange':
329
+ this.onTempoChange.dispatch(message.newTempo, message.oldTempo);
330
+ break;
331
+ case 'barStart': {
332
+ const info = {
333
+ audioTime: message.audioTime,
334
+ tempo: message.tempo,
335
+ confidence: message.confidence,
336
+ barNumber: message.barNumber,
337
+ };
338
+ this.onBarStart.dispatch(info);
339
+ break;
340
+ }
341
+ }
342
+ }
343
+ // -----------------------------------------------------------------------
344
+ // Private helpers — source tap
345
+ // -----------------------------------------------------------------------
346
+ _connectSource(source, audioContext) {
347
+ if (!this._workletNode)
348
+ return;
349
+ const tap = this._resolveToAudioNode(source, audioContext);
350
+ if (!tap) {
351
+ this._deferConnectionViaBus(source);
352
+ return;
353
+ }
354
+ this._tapSource = tap;
355
+ tap.connect(this._workletNode, 0, 0);
356
+ }
357
+ _resolveToAudioNode(source, audioContext) {
358
+ if (source === null)
359
+ return null;
360
+ // MediaStream — duck-type via getTracks
361
+ const asStream = source;
362
+ if (typeof asStream.getTracks === 'function') {
363
+ if (this._streamSource) {
364
+ this._streamSource.disconnect();
365
+ this._streamSource = null;
366
+ }
367
+ const msNode = audioContext.createMediaStreamSource(source);
368
+ this._streamSource = msNode;
369
+ return msNode;
370
+ }
371
+ // AudioBus — has _getOutputNode (checked before raw AudioNode)
372
+ const asBus = source;
373
+ if (typeof asBus._getOutputNode === 'function') {
374
+ return asBus._getOutputNode();
375
+ }
376
+ // Voice — tap its output node
377
+ const asVoice = source;
378
+ if ('output' in asVoice && asVoice.output) {
379
+ return asVoice.output;
380
+ }
381
+ // Raw AudioNode — duck-type: has connect & disconnect
382
+ const asNode = source;
383
+ if (typeof asNode.connect === 'function' && typeof asNode.disconnect === 'function') {
384
+ return source;
385
+ }
386
+ return null;
387
+ }
388
+ _deferConnectionViaBus(source) {
389
+ const asBus = source;
390
+ if (typeof asBus.onceSetup === 'function') {
391
+ asBus.onceSetup(() => {
392
+ if (this._source === source && this._workletNode && isAudioContextReady()) {
393
+ this._connectSource(source, getAudioContext());
394
+ }
395
+ });
396
+ return;
397
+ }
398
+ onAudioContextReady.once(() => {
399
+ if (this._source === source && this._workletNode && isAudioContextReady()) {
400
+ this._connectSource(source, getAudioContext());
401
+ }
402
+ });
403
+ }
404
+ _disconnectTap() {
405
+ if (this._tapSource && this._workletNode) {
406
+ try {
407
+ this._tapSource.disconnect(this._workletNode);
408
+ }
409
+ catch {
410
+ // Ignore
411
+ }
412
+ }
413
+ this._tapSource = null;
414
+ if (this._streamSource) {
415
+ this._streamSource.disconnect();
416
+ this._streamSource = null;
417
+ }
418
+ }
419
+ }
420
+
421
+ export { BeatDetector };
422
+ //# sourceMappingURL=BeatDetector.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"BeatDetector.js","sources":["../../../src/BeatDetector.ts"],"sourcesContent":[null],"names":[],"mappings":";;;AAsGA,MAAM,WAAW,GAAG,qBAAqB;AAEzC;;;;;;;;;;;;;;;;;;;;AAoBG;MACU,YAAY,CAAA;;AAEP,IAAA,MAAM,GAAG,IAAI,MAAM,EAAc;AACjC,IAAA,aAAa,GAAG,IAAI,MAAM,EAAoB;AAC9C,IAAA,UAAU,GAAG,IAAI,MAAM,EAAc;AACrC,IAAA,UAAU,GAAG,IAAI,MAAM,EAAa;AACpC,IAAA,eAAe,GAAG,IAAI,MAAM,EAAkB;;;AAI7C,IAAA,QAAQ;;IAGjB,YAAY,GAA4B,IAAI;IAC5C,OAAO,GAAuB,IAAI;IAClC,UAAU,GAAqB,IAAI;IACnC,aAAa,GAAsC,IAAI;IACvD,MAAM,GAAyB,IAAI;IACnC,mBAAmB,GAAyC,IAAI;AACvD,IAAA,oBAAoB,GAAG,CAAC,GAAiB,KAAU;AAClE,QAAA,mBAAmB,CAAC,MAAM,CAAC,IAAI,CAAC,oBAAoB,CAAC;AACrD,QAAA,IA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C,CAAC;IACJ;IAEQ,cAAc,GAAA;QACpB,IAAI,IAAI,CAAC,UAAU,IAAI,IAAI,CAAC,YAAY,EAAE;AACxC,YAAA,IAAI;gBACF,IAAI,CAAC,UAAU,CAAC,UAAU,CAAC,IAAI,CAAC,YAAY,CAAC;YAC/C;AAAE,YAAA,MAAM;;YAER;QACF;AACA,QAAA,IAAI,CAAC,UAAU,GAAG,IAAI;AAEtB,QAAA,IAAI,IAAI,CAAC,aAAa,EAAE;AACtB,YAAA,IAAI,CAAC,aAAa,CAAC,UAAU,EAAE;AAC/B,YAAA,IAAI,CAAC,aAAa,GAAG,IAAI;QAC3B;IACF;AACD;;;;"}
@@ -0,0 +1,22 @@
1
+ /**
2
+ * In-place radix-2 Cooley-Tukey FFT.
3
+ *
4
+ * Operates on the supplied real/imag arrays (length must be a power of 2).
5
+ * After the call, real[i] and imag[i] hold the complex spectrum.
6
+ * A Hann window is applied to the input before the transform.
7
+ *
8
+ * These functions are duplicated (as plain JS) inside the beat-detector worklet
9
+ * source string because AudioWorklets cannot import modules.
10
+ */
11
+ /** Apply a Hann window in-place to `real`. `imag` is zeroed. */
12
+ export declare function hannWindow(real: Float32Array, imag: Float32Array): void;
13
+ /**
14
+ * In-place FFT. `real` and `imag` must have the same length (power of 2).
15
+ * A Hann window is applied to `real` before the transform; `imag` is zeroed.
16
+ */
17
+ export declare function fft(real: Float32Array, imag: Float32Array): void;
18
+ /**
19
+ * Compute magnitude spectrum from complex FFT output.
20
+ * Returns a Float32Array of length `n/2` (positive frequencies only).
21
+ */
22
+ export declare function magnitudeSpectrum(real: Float32Array, imag: Float32Array): Float32Array;