-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest.js
More file actions
883 lines (770 loc) · 30.6 KB
/
Copy pathtest.js
File metadata and controls
883 lines (770 loc) · 30.6 KB
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
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
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
import test, { almost, ok, is } from 'tst'
import { detect, onsets, energyOnsets, phaseOnsets, bandOnsets, tempo, combTempo, beatTrack } from './index.js'
import { spectralFlux, energyFlux } from '@audio/onset'
import { fmKick, fmSnare, fmHihat, bassNote, mixHits, edm as edmBeat, hiphop as hiphopBeat, disco as discoBeat, jazz as jazzSwing, reggae as reggaeBeat, rock as rockFn, funk as funkFn, ballad as balladFn, breakbeat as breakbeatFn } from './synth.js'
import { renderFloatbeat, sanxion } from './floatbeats.js'
let fs = 44100
/** Check if BPM matches expected value within tolerance.
* Reports octave errors (half/double) explicitly — they pass but are flagged. */
function nearBpm(actual, expected, tol) {
if (Math.abs(actual - expected) <= tol) {
ok(true, `${actual.toFixed(1)} ≈ ${expected} ±${tol}`)
return
}
// detect octave error: half or double tempo
if (Math.abs(actual - expected * 2) <= tol) {
ok(true, `${actual.toFixed(1)} ≈ ${expected}×2 (octave double — known limitation)`)
return
}
if (Math.abs(actual - expected / 2) <= tol) {
ok(true, `${actual.toFixed(1)} ≈ ${expected}/2 (octave half — known limitation)`)
return
}
// anything else is a real failure
ok(false, `${actual.toFixed(1)} not near ${expected} ±${tol}`)
}
// ═══════════════════════════════════════════
// Signal generators
// ═══════════════════════════════════════════
/** Sharp click impulse at given sample position */
function impulse(n, pos) {
let d = new Float32Array(n)
for (let j = 0; j < 100 && pos + j < n; j++)
d[pos + j] = Math.exp(-j / 10) * (j % 2 ? -1 : 1)
return d
}
/** Regular clicks at given BPM */
function clicks(bpm, duration, sr = fs) {
let n = Math.floor(duration * sr)
let d = new Float32Array(n)
let interval = Math.floor(sr * 60 / bpm)
for (let i = 0; i < n; i += interval) {
for (let j = 0; j < 100 && i + j < n; j++)
d[i + j] = Math.exp(-j / 10) * (j % 2 ? -1 : 1)
}
return d
}
/** Clicks mixed with white noise at given signal-to-noise ratio (dB) */
function clicksInNoise(bpm, duration, snrDb, sr = fs) {
let clean = clicks(bpm, duration, sr)
let noise = new Float32Array(clean.length)
let signalPower = 0
for (let i = 0; i < clean.length; i++) signalPower += clean[i] * clean[i]
signalPower /= clean.length
let noisePower = signalPower / Math.pow(10, snrDb / 10)
let amp = Math.sqrt(noisePower)
for (let i = 0; i < noise.length; i++) noise[i] = (Math.random() * 2 - 1) * amp
let mix = new Float32Array(clean.length)
for (let i = 0; i < clean.length; i++) mix[i] = clean[i] + noise[i]
return mix
}
/** White noise */
function noise(duration, sr = fs) {
let d = new Float32Array(Math.floor(duration * sr))
for (let i = 0; i < d.length; i++) d[i] = Math.random() * 2 - 1
return d
}
/** Sustained tone (drone) */
function drone(freq, duration, sr = fs) {
let n = Math.floor(duration * sr)
let d = new Float32Array(n)
for (let i = 0; i < n; i++) d[i] = 0.5 * Math.sin(2 * Math.PI * freq * i / sr)
return d
}
/** Silence */
function silence(duration, sr = fs) { return new Float32Array(Math.floor(duration * sr)) }
/** Kick drum: low sine with fast pitch sweep + noise attack */
function kick(pos, sr = fs) {
let len = Math.floor(sr * 0.15)
let d = new Float32Array(len)
for (let i = 0; i < len; i++) {
let t = i / sr
let freq = 150 * Math.exp(-t * 30) + 40
let env = Math.exp(-t * 15)
d[i] = env * Math.sin(2 * Math.PI * freq * t) * 0.7
if (i < Math.floor(sr * 0.005))
d[i] += Math.exp(-i / (sr * 0.002)) * (Math.random() * 2 - 1) * 0.4
}
return { d, pos, len }
}
/** Snare: noise burst + mid tone */
function snare(pos, sr = fs) {
let len = Math.floor(sr * 0.1)
let d = new Float32Array(len)
for (let i = 0; i < len; i++) {
let t = i / sr
let env = Math.exp(-t * 20)
d[i] = env * ((Math.random() * 2 - 1) * 0.4 + Math.sin(2 * Math.PI * 200 * t) * 0.3)
}
return { d, pos, len }
}
/** Hi-hat: filtered noise burst, short */
function hihat(pos, sr = fs) {
let len = Math.floor(sr * 0.04)
let d = new Float32Array(len)
for (let i = 0; i < len; i++) {
let t = i / sr
d[i] = Math.exp(-t * 80) * (Math.random() * 2 - 1) * 0.3
}
return { d, pos, len }
}
/** Generate a rock beat: kick-snare pattern at given BPM.
* Pattern per bar: kick . snare . kick . snare . */
function rockBeat(bpm, bars = 2, sr = fs) {
let beatLen = Math.floor(sr * 60 / bpm)
let barLen = beatLen * 4
let duration = (bars * barLen) / sr + 0.5
let hits = []
for (let bar = 0; bar < bars; bar++) {
let base = bar * barLen
// kick on 1 and 3
hits.push(kick(base, sr))
hits.push(kick(base + beatLen * 2, sr))
// snare on 2 and 4
hits.push(snare(base + beatLen, sr))
hits.push(snare(base + beatLen * 3, sr))
// hi-hat on every 8th
for (let i = 0; i < 8; i++)
hits.push(hihat(base + Math.floor(beatLen * i / 2), sr))
}
return mixHits(hits, duration, sr)
}
/** Son clave (3-2) pattern. The most fundamental Afro-Cuban rhythm.
* 3-side: x . . x . . x . . . . . 2-side: . . . . x . . x . . . .
* Returns onset positions in samples. */
function sonClave(bpm, bars = 2, sr = fs) {
let beatLen = Math.floor(sr * 60 / bpm)
let sixteenth = Math.floor(beatLen / 4)
// 3-side offsets in 16th notes: 0, 3, 6, 10, 12
// 2-side offsets in 16th notes: 0, 2, 4, 6, 8, 10, 12, 14
// Full clave in one bar of 4/4 (16 16ths):
// 3-side: pos 0, 3, 6 | 2-side: pos 8, 12 (relative to bar start)
// Actually the standard 3-2 son clave pattern (1-indexed 16ths):
// 3-side: 1, 4, 7, (within first half = 8 16ths)
// 2-side: 9, 13
// Using 0-indexed: 0, 3, 6, 8, 12
let clavePattern = [0, 3, 6, 8, 12]
let n = Math.floor(bars * beatLen * 4 + beatLen) // bars of 4/4
let d = new Float32Array(n)
for (let bar = 0; bar < bars; bar++) {
let base = bar * beatLen * 4
for (let p of clavePattern) {
let pos = base + p * sixteenth
// wooden clave sound: short, bright click
for (let j = 0; j < 60 && pos + j < n; j++)
d[pos + j] += Math.exp(-j / 8) * Math.sin(2 * Math.PI * 2500 * j / sr) * 0.6
}
}
return d
}
/** Rumba clave (3-2). Like son clave but last 3-side note is on 16th 7 instead of 6.
* Pattern: 0, 3, 7, 8, 12 */
function rumbaClave(bpm, bars = 2, sr = fs) {
let beatLen = Math.floor(sr * 60 / bpm)
let sixteenth = Math.floor(beatLen / 4)
let clavePattern = [0, 3, 7, 8, 12]
let n = Math.floor(bars * beatLen * 4 + beatLen)
let d = new Float32Array(n)
for (let bar = 0; bar < bars; bar++) {
let base = bar * beatLen * 4
for (let p of clavePattern) {
let pos = base + p * sixteenth
for (let j = 0; j < 60 && pos + j < n; j++)
d[pos + j] += Math.exp(-j / 8) * Math.sin(2 * Math.PI * 2500 * j / sr) * 0.6
}
}
return d
}
/** Cascara pattern (Afro-Cuban shell rhythm).
* 16th-note pattern: x . x x . x . x . x x . x . x . */
function cascara(bpm, bars = 2, sr = fs) {
let beatLen = Math.floor(sr * 60 / bpm)
let sixteenth = Math.floor(beatLen / 4)
let pattern = [0, 2, 3, 5, 7, 9, 10, 12, 14] // 0-indexed 16ths
let n = Math.floor(bars * beatLen * 4 + beatLen)
let d = new Float32Array(n)
for (let bar = 0; bar < bars; bar++) {
let base = bar * beatLen * 4
for (let p of pattern) {
let pos = base + p * sixteenth
for (let j = 0; j < 40 && pos + j < n; j++)
d[pos + j] += Math.exp(-j / 6) * Math.sin(2 * Math.PI * 4000 * j / sr) * 0.4
}
}
return d
}
/** Polyrhythm: two pulses at different rates (e.g. 3:2) */
function polyrhythm(bpmSlow, bpmFast, duration, sr = fs) {
let n = Math.floor(duration * sr)
let d = new Float32Array(n)
let intSlow = Math.floor(sr * 60 / bpmSlow)
let intFast = Math.floor(sr * 60 / bpmFast)
for (let i = 0; i < n; i += intSlow) {
for (let j = 0; j < 80 && i + j < n; j++)
d[i + j] += Math.exp(-j / 12) * (j % 2 ? -0.5 : 0.5) // low click
}
for (let i = 0; i < n; i += intFast) {
for (let j = 0; j < 50 && i + j < n; j++)
d[i + j] += Math.exp(-j / 8) * (j % 2 ? -0.3 : 0.3) // high click
}
return d
}
/** Single impulse at the start, rest silence */
function singleImpulse(sr = fs) {
let d = new Float32Array(Math.floor(sr * 2))
for (let j = 0; j < 100 && j < d.length; j++)
d[j] = Math.exp(-j / 10) * (j % 2 ? -1 : 1)
return d
}
// --- Spectral flux ---
test('spectralFlux — silence returns empty', () => {
let { odf, nFrames } = spectralFlux(silence(2), { fs })
is(nFrames, 0)
is(odf.length, 0)
})
test('spectralFlux — clicks produce peaks', () => {
let { odf, nFrames } = spectralFlux(clicks(120, 4, fs), { fs })
ok(nFrames > 0, 'has frames')
ok(odf.length > 0, 'has odf values')
// should have non-zero flux
let sum = 0
for (let i = 0; i < odf.length; i++) sum += odf[i]
ok(sum > 0, 'flux is non-zero')
})
// --- Onset detection ---
test('onsets — detect click positions', () => {
let data = clicks(120, 4, fs)
let ons = onsets(data, { fs })
ok(ons.length > 0, 'finds onsets')
ok(ons.length >= 4, 'enough onsets')
})
test('onsets — silence returns empty', () => {
let ons = onsets(silence(2), { fs })
is(ons.length, 0)
})
// --- Tempo ---
test('tempo — 120 BPM clicks', () => {
let data = clicks(120, 8, fs)
let result = tempo(data, { fs })
ok(result.bpm > 0, 'detects tempo')
almost(result.bpm, 120, 5)
})
test('tempo — 90 BPM clicks', () => {
let data = clicks(90, 8, fs)
let result = tempo(data, { fs })
almost(result.bpm, 90, 5)
})
test('tempo — silence', () => {
let result = tempo(silence(2), { fs })
is(result.bpm, 0)
is(result.confidence, 0)
})
test('tempo — confidence for clicks is high', () => {
let data = clicks(120, 8, fs)
let result = tempo(data, { fs })
ok(result.confidence > 0.5, 'high confidence for clean clicks')
})
// --- Full detection ---
test('detect — 120 BPM full pipeline', () => {
let data = clicks(120, 8, fs)
let result = detect(data, { fs })
ok(result.bpm > 0, 'detects BPM')
almost(result.bpm, 120, 5)
ok(result.beats.length > 0, 'has beat grid')
ok(result.onsets.length > 0, 'has onsets')
})
test('detect — silence', () => {
let result = detect(silence(2), { fs })
is(result.beats.length, 0)
is(result.onsets.length, 0)
})
test('detect — beat grid spacing matches BPM', () => {
let data = clicks(120, 4, fs)
let result = detect(data, { fs })
if (result.beats.length >= 2) {
let interval = result.beats[1] - result.beats[0]
almost(interval, 0.5, 0.05) // 120 BPM = 0.5s per beat
}
})
// --- Energy flux ---
test('energyFlux — silence returns empty', () => {
let { odf, nFrames } = energyFlux(silence(2), { fs })
is(nFrames, 0)
is(odf.length, 0)
})
test('energyFlux — clicks produce peaks', () => {
let { odf } = energyFlux(clicks(120, 4, fs), { fs })
ok(odf.length > 0, 'has odf')
let sum = 0
for (let i = 0; i < odf.length; i++) sum += odf[i]
ok(sum > 0, 'flux is non-zero')
})
// --- Energy onsets ---
test('energyOnsets — detect click positions', () => {
let data = clicks(120, 4, fs)
let ons = energyOnsets(data, { fs })
ok(ons.length > 0, 'finds onsets')
ok(ons.length >= 3, 'enough onsets')
})
test('energyOnsets — silence returns empty', () => {
let ons = energyOnsets(silence(2), { fs })
is(ons.length, 0)
})
// --- Comb-filter tempo ---
test('combTempo — 120 BPM clicks', () => {
let data = clicks(120, 8, fs)
let result = combTempo(data, { fs })
ok(result.bpm > 0, 'detects tempo')
almost(result.bpm, 120, 5)
})
test('combTempo — silence', () => {
let result = combTempo(silence(2), { fs })
is(result.bpm, 0)
})
// --- Beat tracking ---
test('beatTrack — 120 BPM clicks', () => {
let data = clicks(120, 8, fs)
let result = beatTrack(data, { fs })
ok(result.beats.length > 0, 'finds beats')
ok(result.bpm > 0, 'estimates BPM')
})
test('beatTrack — silence', () => {
let result = beatTrack(silence(2), { fs })
is(result.beats.length, 0)
is(result.bpm, 0)
})
// --- Phase onsets ---
test('phaseOnsets — detect click positions', () => {
let data = clicks(120, 4, fs)
let ons = phaseOnsets(data, { fs })
ok(ons.length > 0, 'finds onsets')
ok(ons.length >= 4, 'enough onsets')
})
test('phaseOnsets — silence returns empty', () => {
let ons = phaseOnsets(silence(2), { fs })
is(ons.length, 0)
})
// --- Multi-band onsets ---
test('bandOnsets — detect click positions', () => {
let data = clicks(120, 4, fs)
let ons = bandOnsets(data, { fs })
ok(ons.length > 0, 'finds onsets')
ok(ons.length >= 4, 'enough onsets')
})
test('bandOnsets — silence returns empty', () => {
let ons = bandOnsets(silence(2), { fs })
is(ons.length, 0)
})
// --- Tempo candidates ---
test('tempo — candidates option returns top-N', () => {
let data = clicks(120, 8, fs)
let result = tempo(data, { fs, candidates: 3 })
ok(result.bpm > 0, 'detects tempo')
ok(result.candidates, 'returns candidates')
ok(result.candidates.length <= 3, 'returns at most 3')
ok(result.candidates.length >= 2, 'returns multiple')
almost(result.bpm, 120, 5)
})
test('combTempo — candidates option returns top-N', () => {
let data = clicks(120, 8, fs)
let result = combTempo(data, { fs, candidates: 3 })
ok(result.bpm > 0, 'detects tempo')
ok(result.candidates, 'returns candidates')
ok(result.candidates.length <= 3, 'returns at most 3')
almost(result.bpm, 120, 5)
})
// ═══════════════════════════════════════════
// Noise robustness
// ═══════════════════════════════════════════
test('tempo — clicks in +6dB noise', () => {
let data = clicksInNoise(120, 8, 6)
let result = tempo(data, { fs })
ok(result.bpm > 0, 'detects tempo despite noise')
almost(result.bpm, 120, 10)
})
test('tempo — clicks in 0dB noise (equal energy)', () => {
let data = clicksInNoise(120, 8, 0)
let result = tempo(data, { fs })
ok(result.bpm > 0, 'detects tempo at 0dB SNR')
// wider tolerance — 0dB is borderline
almost(result.bpm, 120, 15)
})
test('tempo — clicks in -3dB noise (noise louder than signal)', () => {
let data = clicksInNoise(120, 8, -3)
let result = tempo(data, { fs })
// may still detect or may fail gracefully
if (result.bpm > 0) {
almost(result.bpm, 120, 20)
} else {
ok(true, 'gracefully returns 0 for buried signal')
}
})
test('onsets — clicks in +6dB noise', () => {
let data = clicksInNoise(120, 4, 6)
let ons = onsets(data, { fs })
ok(ons.length >= 4, 'detects onsets through noise')
})
test('combTempo — clicks in +6dB noise', () => {
let data = clicksInNoise(120, 8, 6)
let result = combTempo(data, { fs })
ok(result.bpm > 0, 'comb filter finds tempo through noise')
// comb filter may shift with noise — accept wider range including 3×
nearBpm(result.bpm, 120, 30)
})
// ═══════════════════════════════════════════
// Percussion patterns
// ═══════════════════════════════════════════
test('tempo — rock beat 120 BPM', () => {
let data = rockBeat(120, 4)
let result = tempo(data, { fs })
ok(result.bpm > 0, 'detects rock tempo')
// octave error (60 vs 120) is acceptable — hi-hat 8th notes can confuse
nearBpm(result.bpm, 120, 10)
})
test('tempo — rock beat 90 BPM', () => {
let data = rockBeat(90, 4)
let result = tempo(data, { fs })
ok(result.bpm > 0, 'detects slower rock tempo')
almost(result.bpm, 90, 10)
})
test('onsets — rock beat detects kick+snare', () => {
let data = rockBeat(120, 2)
let ons = onsets(data, { fs })
// 2 bars × 4 kicks+snare = 8 onsets minimum (plus some hi-hat)
ok(ons.length >= 6, 'detects drum hits')
})
test('detect — rock beat full pipeline', () => {
let data = rockBeat(120, 4)
let result = detect(data, { fs })
ok(result.bpm > 0, 'pipeline detects tempo')
ok(result.beats.length > 0, 'generates beat grid')
ok(result.onsets.length >= 4, 'finds onsets')
})
// ═══════════════════════════════════════════
// Latin percussion
// ═══════════════════════════════════════════
test('tempo — son clave 100 BPM', () => {
let data = sonClave(100, 4)
let result = tempo(data, { fs })
ok(result.bpm > 0, 'detects son clave tempo')
// syncopated clave is challenging — just verify reasonable detection
ok(result.bpm >= 50 && result.bpm <= 200, 'tempo in plausible range')
})
test('tempo — son clave 120 BPM', () => {
let data = sonClave(120, 4)
let result = tempo(data, { fs })
ok(result.bpm > 0, 'detects faster son clave')
almost(result.bpm, 120, 15)
})
test('onsets — son clave detects 5 hits per bar', () => {
let data = sonClave(120, 2)
let ons = onsets(data, { fs })
// 2 bars × 5 hits = 10, but some may merge
ok(ons.length >= 6, 'detects clave hits')
})
test('tempo — rumba clave 100 BPM', () => {
let data = rumbaClave(100, 4)
let result = tempo(data, { fs })
ok(result.bpm > 0, 'detects rumba clave tempo')
almost(result.bpm, 100, 15)
})
test('tempo — cascara 110 BPM', () => {
let data = cascara(110, 4)
let result = tempo(data, { fs })
ok(result.bpm > 0, 'detects cascara tempo')
nearBpm(result.bpm, 110, 20)
})
test('onsets — cascara detects hits', () => {
let data = cascara(120, 2)
let ons = onsets(data, { fs })
ok(ons.length >= 8, 'cascara has many hits')
})
// ═══════════════════════════════════════════
// Edge cases
// ═══════════════════════════════════════════
test('onsets — very short audio (<1 frame)', () => {
let data = clicks(120, 0.01, fs) // ~441 samples, less than frameSize=2048
let ons = onsets(data, { fs })
is(ons.length, 0, 'too short for analysis')
})
test('tempo — very short audio', () => {
let data = clicks(120, 0.5, fs) // half a second, barely 1 beat at 120 BPM
let result = tempo(data, { fs })
// may return 0 or something, just shouldn't crash
ok(result.bpm >= 0, 'does not crash on short input')
})
test('onsets — single impulse', () => {
let data = singleImpulse()
let ons = onsets(data, { fs })
ok(ons.length <= 2, 'finds at most the single impulse')
})
test('onsets — sustained drone (no onsets)', () => {
let data = drone(220, 4)
let ons = onsets(data, { fs })
// pure tone causes spectral flux false positives — verify bounded output
ok(ons.length < 200, 'drone produces bounded onsets')
})
test('tempo — drone (no rhythm)', () => {
let result = tempo(drone(220, 4), { fs })
// drone may produce spurious tempo — just verify it doesn't crash
ok(result.bpm >= 0, 'does not crash on drone')
})
test('onsets — white noise (no structure)', () => {
let data = noise(4)
let ons = onsets(data, { fs })
// noise has no temporal structure, but spectral flux may fire randomly
// just shouldn't crash or return thousands of onsets
ok(ons.length < 100, 'noise produces bounded onsets')
})
test('tempo — white noise', () => {
let result = tempo(noise(4), { fs })
// noise may produce spurious tempo — just verify it doesn't crash
ok(result.bpm >= 0, 'does not crash on noise')
})
test('onsets — polyrhythm 3:2', () => {
// 3:2 polyrhythm = 90 BPM : 135 BPM
let data = polyrhythm(90, 135, 4)
let ons = onsets(data, { fs })
ok(ons.length >= 6, 'detects polyrhythm hits')
})
test('tempo — polyrhythm 3:2', () => {
let data = polyrhythm(90, 135, 8)
let result = tempo(data, { fs })
ok(result.bpm > 0, 'detects some tempo from polyrhythm')
})
test('bandOnsets — rock beat', () => {
let data = rockBeat(120, 2)
let ons = bandOnsets(data, { fs })
ok(ons.length >= 4, 'multi-band detects drum hits')
})
test('phaseOnsets — rock beat', () => {
let data = rockBeat(120, 2)
let ons = phaseOnsets(data, { fs })
ok(ons.length >= 4, 'phase deviation detects drum hits')
})
test('beatTrack — rock beat 120 BPM', () => {
let data = rockBeat(120, 4)
let result = beatTrack(data, { fs })
ok(result.beats.length > 0, 'DP tracker finds beats in rock')
ok(result.bpm > 0, 'estimates BPM')
})
// Musical pattern generators imported from synth.js
// --- Audio loader (Node.js) ---
async function loadAudio(url, startSec = 0, durationSec = 4) {
let decode
try { ({ default: decode } = await import('audio-decode')) } catch { return null }
let resp = await fetch(url)
if (!resp.ok) return null
let buf = Buffer.from(await resp.arrayBuffer())
let audio = await decode(buf)
if (!audio?.channelData?.[0]) return null
let full = audio.channelData[0]
let sr = audio.sampleRate
let start = Math.floor(startSec * sr), len = Math.floor(durationSec * sr)
if (start + len > full.length) len = full.length - start
let d = new Float32Array(len)
d.set(full.subarray(start, start + len))
let fade = Math.min(400, len >> 1)
for (let i = 0; i < fade; i++) { let g = i / fade; d[i] *= g; d[len - 1 - i] *= g }
if (sr !== fs) {
let ratio = fs / sr
let newLen = Math.floor(len * ratio)
let out = new Float32Array(newLen)
for (let i = 0; i < newLen; i++) {
let src = i / ratio
let idx = Math.floor(src)
let frac = src - idx
out[i] = idx + 1 < len ? d[idx] * (1 - frac) + d[idx + 1] * frac : d[idx]
}
return out
}
return d
}
// ═══════════════════════════════════════════
// Musical pattern tests
// ═══════════════════════════════════════════
test('tempo — EDM 128 BPM', () => {
let data = edmBeat(128, 4)
let result = tempo(data, { fs })
ok(result.bpm > 0, 'detects EDM tempo')
nearBpm(result.bpm, 128, 15)
})
test('combTempo — EDM 128 BPM', () => {
let data = edmBeat(128, 4)
let result = combTempo(data, { fs })
ok(result.bpm > 0, 'comb finds EDM tempo')
nearBpm(result.bpm, 128, 15)
})
test('onsets — EDM 4-on-floor kicks', () => {
let data = edmBeat(128, 2)
let ons = onsets(data, { fs })
ok(ons.length >= 8, 'detects EDM kick pattern')
})
test('detect — EDM full pipeline', () => {
let data = edmBeat(128, 4)
let result = detect(data, { fs })
ok(result.bpm > 0, 'pipeline detects EDM tempo')
ok(result.beats.length > 0, 'generates beat grid')
ok(result.onsets.length >= 4, 'finds onsets')
})
test('tempo — hip-hop 90 BPM', () => {
let data = hiphopBeat(90, 4)
let result = tempo(data, { fs })
ok(result.bpm > 0, 'detects hip-hop tempo')
nearBpm(result.bpm, 90, 15)
})
test('onsets — hip-hop boom-bap pattern', () => {
let data = hiphopBeat(90, 2)
let ons = onsets(data, { fs })
ok(ons.length >= 4, 'detects kick-snare pattern')
})
test('tempo — disco 120 BPM', () => {
let data = discoBeat(120, 4)
let result = tempo(data, { fs })
ok(result.bpm > 0, 'detects disco tempo')
nearBpm(result.bpm, 120, 15)
})
test('combTempo — disco 120 BPM', () => {
let data = discoBeat(120, 4)
let result = combTempo(data, { fs })
ok(result.bpm > 0, 'comb finds disco tempo')
nearBpm(result.bpm, 120, 15)
})
test('tempo — jazz swing 140 BPM', () => {
let data = jazzSwing(140, 4)
let result = tempo(data, { fs })
ok(result.bpm > 0, 'detects jazz swing tempo')
nearBpm(result.bpm, 140, 20)
})
test('tempo — reggae 80 BPM', () => {
let data = reggaeBeat(80, 4)
let result = tempo(data, { fs })
ok(result.bpm > 0, 'detects reggae tempo')
nearBpm(result.bpm, 80, 15)
})
test('bandOnsets — EDM 128', () => {
let data = edmBeat(128, 2)
let ons = bandOnsets(data, { fs })
ok(ons.length >= 4, 'multi-band detects EDM beats')
})
test('phaseOnsets — disco 120', () => {
let data = discoBeat(120, 2)
let ons = phaseOnsets(data, { fs })
ok(ons.length >= 4, 'phase detects disco beats')
})
test('beatTrack — hip-hop 90', () => {
let data = hiphopBeat(90, 4)
let result = beatTrack(data, { fs })
ok(result.beats.length > 0, 'DP finds hip-hop beats')
ok(result.bpm > 0, 'estimates BPM')
})
// ═══════════════════════════════════════════
// Real audio (audio-decode, Node.js)
// ═══════════════════════════════════════════
test('onsets — real voice (audio-lena)', async () => {
let samples = await loadAudio('https://cdn.jsdelivr.net/npm/audio-lena@3.0.0/lena.wav', 1.5, 4)
if (!samples) return ok(true, 'skip: audio-decode unavailable')
ok(samples.length > 0, 'decoded audio')
let ons = onsets(samples, { fs })
ok(ons.length >= 3, 'detects voice onsets')
})
test('tempo — real voice (audio-lena)', async () => {
let samples = await loadAudio('https://cdn.jsdelivr.net/npm/audio-lena@3.0.0/lena.wav', 1.5, 4)
if (!samples) return ok(true, 'skip: audio-decode unavailable')
let result = tempo(samples, { fs })
ok(result.bpm >= 0, 'does not crash on voice')
})
test('detect — real voice (audio-lena)', async () => {
let samples = await loadAudio('https://cdn.jsdelivr.net/npm/audio-lena@3.0.0/lena.wav', 1.5, 4)
if (!samples) return ok(true, 'skip: audio-decode unavailable')
let result = detect(samples, { fs })
ok(result.onsets.length >= 2, 'pipeline detects voice onsets')
ok(result.beats.length >= 0, 'pipeline produces output')
})
// ═══════════════════════════════════════════
// Real music track (deterministic, no downloads)
// ═══════════════════════════════════════════
// Dollchan floatbeat — ground-truth BPM declared in the formula itself.
test(`detect — real music (floatbeat ${sanxion.name.toLowerCase()}, ${sanxion.bpm} BPM)`, () => {
let samples = renderFloatbeat(sanxion.code, sanxion.sampleRate, 10)
ok(samples.length === sanxion.sampleRate * 10, 'rendered 10s of audio')
let peak = 0
for (let i = 0; i < samples.length; i++) if (Math.abs(samples[i]) > peak) peak = Math.abs(samples[i])
ok(peak > 0.01, 'renders non-silent signal (peak ' + peak.toFixed(3) + ')')
let result = detect(samples, { fs: sanxion.sampleRate })
ok(result.bpm > 0, 'detects tempo on real track')
ok(result.beats.length > 0, 'produces beat grid')
ok(result.onsets.length >= 8, 'finds onsets (' + result.onsets.length + ')')
nearBpm(result.bpm, sanxion.bpm, 10)
})
// ═══════════════════════════════════════════
// Accuracy benchmark — systematic validation
// ═══════════════════════════════════════════
// Tests all styles × BPM ranges × algorithms.
// Reports: accuracy1 (exact ±5%), accuracy2 (octave-tolerant ±5%),
// mean absolute error, and octave error rate.
test('accuracy benchmark — tempo estimation', () => {
let generators = [
{ name: 'clicks', fn: (bpm) => clicks(bpm, 8) },
{ name: 'rock', fn: (bpm) => rockFn(bpm, 4) },
{ name: 'edm', fn: (bpm) => edmBeat(bpm, 4) },
{ name: 'hiphop', fn: (bpm) => hiphopBeat(bpm, 4) },
{ name: 'disco', fn: (bpm) => discoBeat(bpm, 4) },
{ name: 'jazz', fn: (bpm) => jazzSwing(bpm, 4) },
{ name: 'reggae', fn: (bpm) => reggaeBeat(bpm, 4) },
{ name: 'funk', fn: (bpm) => funkFn(bpm, 4) },
{ name: 'ballad', fn: (bpm) => balladFn(bpm, 4) },
{ name: 'breakbeat', fn: (bpm) => breakbeatFn(bpm, 4) },
]
let bpms = [70, 80, 90, 100, 110, 120, 130, 140, 160, 180]
let methods = [
{ name: 'tempo', fn: (d) => tempo(d, { fs }).bpm },
{ name: 'combTempo', fn: (d) => combTempo(d, { fs }).bpm },
{ name: 'detect', fn: (d) => detect(d, { fs }).bpm },
{ name: 'beatTrack', fn: (d) => beatTrack(d, { fs }).bpm },
]
let results = []
for (let method of methods) {
let exact = 0, octave = 0, total = 0, absErr = 0
for (let gen of generators) {
for (let targetBpm of bpms) {
let data = gen.fn(targetBpm)
let detected = method.fn(data)
total++
let err = Math.abs(detected - targetBpm)
let errPct = err / targetBpm
if (errPct <= 0.05) {
exact++; octave++
} else {
// check octave (half/double)
let halfErr = Math.abs(detected - targetBpm / 2) / targetBpm
let dblErr = Math.abs(detected - targetBpm * 2) / targetBpm
if (halfErr <= 0.05 || dblErr <= 0.05) octave++
}
absErr += err
}
}
results.push({
name: method.name, total,
acc1: (exact / total * 100).toFixed(1),
acc2: (octave / total * 100).toFixed(1),
mae: (absErr / total).toFixed(1),
octaveErrors: octave - exact
})
}
// print results table
console.log('\n ┌─────────────────────────────────────────────────────────┐')
console.log(' │ Accuracy Benchmark: ' + generators.length + ' styles × ' + bpms.length + ' BPMs = ' + (generators.length * bpms.length) + ' cases per method │')
console.log(' ├────────────┬─────────┬──────────┬────────┬─────────────┤')
console.log(' │ Method │ Acc1(%) │ Acc2(%) │ MAE │ Octave errs │')
console.log(' ├────────────┼─────────┼──────────┼────────┼─────────────┤')
for (let r of results) {
console.log(' │ ' + r.name.padEnd(10) + ' │ ' + r.acc1.padStart(5) + ' │ ' + r.acc2.padStart(6) + ' │ ' + r.mae.padStart(4) + ' │ ' + String(r.octaveErrors).padStart(8) + ' │')
}
console.log(' └────────────┴─────────┴──────────┴────────┴─────────────┘')
console.log(' Acc1 = exact ±5%. Acc2 = octave-tolerant ±5%. MAE = mean absolute BPM error.\n')
// assert minimum quality bar
let best = results[0] // tempo method
ok(+best.acc1 >= 60, 'tempo accuracy1 ≥ 60%: ' + best.acc1 + '%')
ok(+best.acc2 >= 80, 'tempo accuracy2 ≥ 80%: ' + best.acc2 + '%')
})