nodejs-audio-visualizer 5.1.0 → 5.3.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 (51) hide show
  1. package/LICENSE +21 -21
  2. package/README.md +81 -80
  3. package/dist/audio.d.ts +13 -13
  4. package/dist/audio.js +128 -128
  5. package/dist/beats.d.ts +39 -39
  6. package/dist/beats.js +220 -220
  7. package/dist/bpmEncoder.d.ts +14 -14
  8. package/dist/bpmEncoder.js +97 -97
  9. package/dist/config.d.ts +78 -77
  10. package/dist/config.js +377 -369
  11. package/dist/config.js.map +1 -1
  12. package/dist/dsp.d.ts +16 -16
  13. package/dist/dsp.js +76 -76
  14. package/dist/highlight.d.ts +66 -34
  15. package/dist/highlight.js +471 -334
  16. package/dist/highlight.js.map +1 -1
  17. package/dist/image.d.ts +97 -92
  18. package/dist/image.js +545 -538
  19. package/dist/image.js.map +1 -1
  20. package/dist/index.d.ts +87 -79
  21. package/dist/index.js +598 -566
  22. package/dist/index.js.map +1 -1
  23. package/dist/subtitleConvert.d.ts +3 -3
  24. package/dist/subtitleConvert.js +61 -61
  25. package/dist/video.d.ts +150 -120
  26. package/dist/video.js +677 -582
  27. package/dist/video.js.map +1 -1
  28. package/dist/waitForEventLoop.d.ts +1 -1
  29. package/dist/waitForEventLoop.js +5 -5
  30. package/package.json +62 -62
  31. package/src/audio.ts +129 -129
  32. package/src/beats.ts +279 -279
  33. package/src/bpmEncoder.ts +97 -97
  34. package/src/config.ts +487 -476
  35. package/src/dsp.ts +95 -95
  36. package/src/highlight.ts +451 -291
  37. package/src/image.ts +707 -700
  38. package/src/index.ts +813 -769
  39. package/src/subtitleConvert.ts +63 -63
  40. package/src/tests/audio.test.ts +102 -102
  41. package/src/tests/beats.test.ts +134 -134
  42. package/src/tests/config.test.ts +534 -496
  43. package/src/tests/dsp.test.ts +24 -24
  44. package/src/tests/highlight.test.ts +261 -178
  45. package/src/tests/image.test.ts +319 -308
  46. package/src/tests/index.test.ts +180 -180
  47. package/src/tests/subtitleConvert.test.ts +23 -23
  48. package/src/tests/video.test.ts +535 -447
  49. package/src/typings.d.ts +28 -28
  50. package/src/video.ts +856 -722
  51. package/src/waitForEventLoop.ts +2 -2
@@ -1,24 +1,24 @@
1
- import { expect } from 'chai';
2
- import { brutForceFFTSignalLength, getSpectrum } from '../dsp';
3
-
4
- describe('fft', function () {
5
- it('brutForceFFTSignalLength', function () {
6
- const expectedResultMap = {
7
- 1: 2,
8
- 4: 4,
9
- 10: 8,
10
- 123: 64
11
- };
12
- Object.entries(expectedResultMap).forEach(
13
- ([argument, expectedResult]) =>
14
- expect(brutForceFFTSignalLength(+argument)).equal(expectedResult)
15
- );
16
- });
17
-
18
- it('getSpectrum', function () {
19
- const signal = Array(Math.pow(2, 12)).fill(1);
20
-
21
- const result = getSpectrum(signal, 44100);
22
- expect(result).to.have.length(24);
23
- });
24
- });
1
+ import { expect } from 'chai';
2
+ import { brutForceFFTSignalLength, getSpectrum } from '../dsp';
3
+
4
+ describe('fft', function () {
5
+ it('brutForceFFTSignalLength', function () {
6
+ const expectedResultMap = {
7
+ 1: 2,
8
+ 4: 4,
9
+ 10: 8,
10
+ 123: 64
11
+ };
12
+ Object.entries(expectedResultMap).forEach(
13
+ ([argument, expectedResult]) =>
14
+ expect(brutForceFFTSignalLength(+argument)).equal(expectedResult)
15
+ );
16
+ });
17
+
18
+ it('getSpectrum', function () {
19
+ const signal = Array(Math.pow(2, 12)).fill(1);
20
+
21
+ const result = getSpectrum(signal, 44100);
22
+ expect(result).to.have.length(24);
23
+ });
24
+ });
@@ -1,178 +1,261 @@
1
- import { expect } from 'chai';
2
- import { computeHighlightSlice, HIGHLIGHT_DURATION_SEC } from '../highlight';
3
-
4
- const dummySpectrums = (n: number) => Array.from({ length: n }, () => [0]);
5
-
6
- /** Non-zero spectrum only on [rangeStart, rangeEnd) so frame energy is concentrated there. */
7
- function spectrumsWithEnergyInRange(
8
- n: number,
9
- rangeStart: number,
10
- rangeEndExclusive: number,
11
- binValue = 1,
12
- ): number[][] {
13
- return Array.from({ length: n }, (_, i) =>
14
- i >= rangeStart && i < rangeEndExclusive ? [binValue] : [0],
15
- );
16
- }
17
-
18
- describe('computeHighlightSlice', function() {
19
- it('centers window when energy is flat (e.g. silence)', async function() {
20
- const fps = 30;
21
- const totalFrames = 1000;
22
- const spectrums = dummySpectrums(totalFrames);
23
- const highlightFrames = Math.ceil(HIGHLIGHT_DURATION_SEC * fps);
24
- const expectedStart = Math.floor((totalFrames - highlightFrames) / 2);
25
-
26
- const result = await computeHighlightSlice(fps, totalFrames, spectrums, []);
27
-
28
- expect(result.highlightFrames).equal(highlightFrames);
29
- expect(result.startFrame).equal(expectedStart);
30
- expect(result.spectrums.length).equal(highlightFrames);
31
- expect(result.audioSeekSeconds).equal(expectedStart / fps);
32
- expect(result.audioDurationSeconds).equal(highlightFrames / fps);
33
- expect(result.audioSegments).deep.equal([
34
- { seekSeconds: expectedStart / fps, durationSeconds: highlightFrames / fps },
35
- ]);
36
- expect(result.runs).have.length(1);
37
- expect(result.runs[0].highlightFrames).equal(highlightFrames);
38
- });
39
-
40
- it('clamps to track end when the loudest stretch is only in the final window', async function() {
41
- const fps = 30;
42
- const totalFrames = 1000;
43
- const highlightFrames = Math.ceil(HIGHLIGHT_DURATION_SEC * fps);
44
- const maxStart = totalFrames - highlightFrames;
45
- const spectrums = spectrumsWithEnergyInRange(totalFrames, maxStart, totalFrames);
46
-
47
- const result = await computeHighlightSlice(fps, totalFrames, spectrums, [
48
- { frameIndex: 950, intensity: 1 },
49
- ]);
50
-
51
- expect(result.startFrame).equal(maxStart);
52
- expect(result.highlightFrames).equal(highlightFrames);
53
- expect(result.spectrums.length).equal(highlightFrames);
54
- expect(result.audioSegments).deep.equal([
55
- { seekSeconds: maxStart / fps, durationSeconds: highlightFrames / fps },
56
- ]);
57
- expect(result.runs).have.length(1);
58
- });
59
-
60
- it('uses full track when shorter than highlight duration', async function() {
61
- const fps = 30;
62
- const totalFrames = 400;
63
- const spectrums = dummySpectrums(totalFrames);
64
-
65
- const result = await computeHighlightSlice(fps, totalFrames, spectrums, [
66
- { frameIndex: 100, intensity: 1 },
67
- ]);
68
-
69
- expect(result.startFrame).equal(0);
70
- expect(result.highlightFrames).equal(totalFrames);
71
- expect(result.spectrums.length).equal(totalFrames);
72
- expect(result.beatFrameIndices).deep.equal([100]);
73
- expect(result.audioSeekSeconds).equal(0);
74
- expect(result.audioDurationSeconds).equal(totalFrames / fps);
75
- expect(result.audioSegments).deep.equal([
76
- { seekSeconds: 0, durationSeconds: totalFrames / fps },
77
- ]);
78
- expect(result.runs).have.length(1);
79
- expect(result.runs[0].spectrums.length).equal(totalFrames);
80
- });
81
-
82
- it('remaps beat indices into the highlight window', async function() {
83
- const fps = 30;
84
- const totalFrames = 1000;
85
- const highlightFrames = Math.ceil(HIGHLIGHT_DURATION_SEC * fps);
86
- const spectrums = spectrumsWithEnergyInRange(totalFrames, 300, 300 + highlightFrames);
87
-
88
- const result = await computeHighlightSlice(fps, totalFrames, spectrums, [
89
- { frameIndex: 300, intensity: 1 },
90
- { frameIndex: 400, intensity: 0.5 },
91
- { frameIndex: 800, intensity: 0.2 },
92
- ]);
93
-
94
- expect(result.startFrame).equal(300);
95
- expect(result.beatFrameIndices).deep.equal([0, 100]);
96
- expect(result.audioSegments).deep.equal([
97
- { seekSeconds: 300 / fps, durationSeconds: highlightFrames / fps },
98
- ]);
99
- expect(result.runs).have.length(1);
100
- });
101
-
102
- it('picks the window with the highest summed spectral energy', async function() {
103
- const fps = 30;
104
- const totalFrames = 1000;
105
- const highlightFrames = Math.ceil(HIGHLIGHT_DURATION_SEC * fps);
106
- const loud = 10;
107
- const spectrums = spectrumsWithEnergyInRange(totalFrames, 10, 10 + highlightFrames, loud);
108
-
109
- const result = await computeHighlightSlice(fps, totalFrames, spectrums, [
110
- { frameIndex: 10, intensity: 99 },
111
- { frameIndex: 200, intensity: 0.4 },
112
- { frameIndex: 500, intensity: 0.9 },
113
- ]);
114
-
115
- expect(result.startFrame).equal(10);
116
- expect(result.highlightFrames).equal(highlightFrames);
117
- expect(result.audioSegments).deep.equal([
118
- { seekSeconds: 10 / fps, durationSeconds: highlightFrames / fps },
119
- ]);
120
- expect(result.runs).have.length(1);
121
- });
122
-
123
- it('returns empty slice for zero frames', async function() {
124
- const result = await computeHighlightSlice(30, 0, [], []);
125
- expect(result.highlightFrames).equal(0);
126
- expect(result.spectrums).deep.equal([]);
127
- expect(result.beatFrameIndices).deep.equal([]);
128
- expect(result.audioSegments).deep.equal([]);
129
- expect(result.runs).deep.equal([]);
130
- });
131
-
132
- it('concatenates two non-overlapping highlights chronologically with segmentCount 2', async function() {
133
- const fps = 30;
134
- const totalFrames = 1000;
135
- const highlightFrames = Math.ceil(HIGHLIGHT_DURATION_SEC * fps);
136
- const loudA = 10;
137
- const loudB = 5;
138
- const spectrumsA = spectrumsWithEnergyInRange(totalFrames, 0, 450, loudA);
139
- const spectrumsB = spectrumsWithEnergyInRange(totalFrames, 500, 950, loudB);
140
- const spectrums = spectrumsA.map((row, i) => {
141
- const b = spectrumsB[i];
142
- const v = (row[0] || 0) + (b[0] || 0);
143
- return v > 0 ? [v] : [0];
144
- });
145
-
146
- const result = await computeHighlightSlice(fps, totalFrames, spectrums, [
147
- { frameIndex: 50, intensity: 1 },
148
- { frameIndex: 550, intensity: 1 },
149
- ], 2);
150
-
151
- expect(result.highlightFrames).equal(highlightFrames * 2);
152
- expect(result.spectrums.length).equal(highlightFrames * 2);
153
- expect(result.startFrame).equal(0);
154
- expect(result.audioSegments).deep.equal([
155
- { seekSeconds: 0, durationSeconds: highlightFrames / fps },
156
- { seekSeconds: 500 / fps, durationSeconds: highlightFrames / fps },
157
- ]);
158
- expect(result.beatFrameIndices).deep.equal([50, 500]);
159
- expect(result.runs).have.length(2);
160
- expect(result.runs[0].startFrame).equal(0);
161
- expect(result.runs[1].startFrame).equal(500);
162
- expect(result.runs[0].spectrums.length).equal(highlightFrames);
163
- expect(result.runs[1].spectrums.length).equal(highlightFrames);
164
- });
165
-
166
- it('ignores extra segmentCount when only one full highlight window fits', async function() {
167
- const fps = 30;
168
- const totalFrames = 600;
169
- const highlightFrames = Math.ceil(HIGHLIGHT_DURATION_SEC * fps);
170
- const spectrums = spectrumsWithEnergyInRange(totalFrames, 0, highlightFrames, 1);
171
-
172
- const result = await computeHighlightSlice(fps, totalFrames, spectrums, [], 3);
173
-
174
- expect(result.highlightFrames).equal(highlightFrames);
175
- expect(result.audioSegments).have.length(1);
176
- expect(result.runs).have.length(1);
177
- });
178
- });
1
+ import { expect } from 'chai';
2
+ import {
3
+ computeHighlightSlice,
4
+ HIGHLIGHT_DURATION_SEC,
5
+ MAX_BEAT_SNAP_SEC,
6
+ buildBeatCandidates,
7
+ snapSegmentToBeats,
8
+ } from '../highlight';
9
+
10
+ const dummySpectrums = (n: number) => Array.from({ length: n }, () => [0]);
11
+
12
+ /** Non-zero spectrum only on [rangeStart, rangeEnd) so frame energy is concentrated there. */
13
+ function spectrumsWithEnergyInRange(
14
+ n: number,
15
+ rangeStart: number,
16
+ rangeEndExclusive: number,
17
+ binValue = 1,
18
+ ): number[][] {
19
+ return Array.from({ length: n }, (_, i) =>
20
+ i >= rangeStart && i < rangeEndExclusive ? [binValue] : [0],
21
+ );
22
+ }
23
+
24
+ describe('computeHighlightSlice', function() {
25
+ it('centers window when energy is flat (e.g. silence)', async function() {
26
+ const fps = 30;
27
+ const totalFrames = 1000;
28
+ const spectrums = dummySpectrums(totalFrames);
29
+ const highlightFrames = Math.ceil(HIGHLIGHT_DURATION_SEC * fps);
30
+ const expectedStart = Math.floor((totalFrames - highlightFrames) / 2);
31
+
32
+ const result = await computeHighlightSlice(fps, totalFrames, spectrums, []);
33
+
34
+ expect(result.highlightFrames).equal(highlightFrames);
35
+ expect(result.startFrame).equal(expectedStart);
36
+ expect(result.spectrums.length).equal(highlightFrames);
37
+ expect(result.audioSeekSeconds).equal(expectedStart / fps);
38
+ expect(result.audioDurationSeconds).equal(highlightFrames / fps);
39
+ expect(result.audioSegments).deep.equal([
40
+ { seekSeconds: expectedStart / fps, durationSeconds: highlightFrames / fps },
41
+ ]);
42
+ expect(result.runs).have.length(1);
43
+ expect(result.runs[0].highlightFrames).equal(highlightFrames);
44
+ });
45
+
46
+ it('clamps to track end when the loudest stretch is only in the final window', async function() {
47
+ const fps = 30;
48
+ const totalFrames = 1000;
49
+ const highlightFrames = Math.ceil(HIGHLIGHT_DURATION_SEC * fps);
50
+ const maxStart = totalFrames - highlightFrames;
51
+ const spectrums = spectrumsWithEnergyInRange(totalFrames, maxStart, totalFrames);
52
+
53
+ const result = await computeHighlightSlice(fps, totalFrames, spectrums, [
54
+ { frameIndex: 950, intensity: 1 },
55
+ ]);
56
+
57
+ expect(result.startFrame).equal(maxStart);
58
+ expect(result.highlightFrames).equal(highlightFrames);
59
+ expect(result.spectrums.length).equal(highlightFrames);
60
+ expect(result.audioSegments).deep.equal([
61
+ { seekSeconds: maxStart / fps, durationSeconds: highlightFrames / fps },
62
+ ]);
63
+ expect(result.runs).have.length(1);
64
+ });
65
+
66
+ it('uses full track when shorter than highlight duration', async function() {
67
+ const fps = 30;
68
+ const totalFrames = 400;
69
+ const spectrums = dummySpectrums(totalFrames);
70
+
71
+ const result = await computeHighlightSlice(fps, totalFrames, spectrums, [
72
+ { frameIndex: 100, intensity: 1 },
73
+ ]);
74
+
75
+ expect(result.startFrame).equal(0);
76
+ expect(result.highlightFrames).equal(totalFrames);
77
+ expect(result.spectrums.length).equal(totalFrames);
78
+ expect(result.beatFrameIndices).deep.equal([100]);
79
+ expect(result.audioSeekSeconds).equal(0);
80
+ expect(result.audioDurationSeconds).equal(totalFrames / fps);
81
+ expect(result.audioSegments).deep.equal([
82
+ { seekSeconds: 0, durationSeconds: totalFrames / fps },
83
+ ]);
84
+ expect(result.runs).have.length(1);
85
+ expect(result.runs[0].spectrums.length).equal(totalFrames);
86
+ });
87
+
88
+ it('remaps beat indices into the highlight window', async function() {
89
+ const fps = 30;
90
+ const totalFrames = 1000;
91
+ const highlightFrames = Math.ceil(HIGHLIGHT_DURATION_SEC * fps);
92
+ const spectrums = spectrumsWithEnergyInRange(totalFrames, 300, 300 + highlightFrames);
93
+
94
+ const result = await computeHighlightSlice(fps, totalFrames, spectrums, [
95
+ { frameIndex: 300, intensity: 1 },
96
+ { frameIndex: 400, intensity: 0.5 },
97
+ { frameIndex: 800, intensity: 0.2 },
98
+ ]);
99
+
100
+ expect(result.startFrame).equal(300);
101
+ expect(result.beatFrameIndices).deep.equal([0, 100]);
102
+ expect(result.audioSegments).deep.equal([
103
+ { seekSeconds: 300 / fps, durationSeconds: highlightFrames / fps },
104
+ ]);
105
+ expect(result.runs).have.length(1);
106
+ });
107
+
108
+ it('picks the window with the highest summed spectral energy', async function() {
109
+ const fps = 30;
110
+ const totalFrames = 1000;
111
+ const highlightFrames = Math.ceil(HIGHLIGHT_DURATION_SEC * fps);
112
+ const loud = 10;
113
+ const spectrums = spectrumsWithEnergyInRange(totalFrames, 10, 10 + highlightFrames, loud);
114
+
115
+ const result = await computeHighlightSlice(fps, totalFrames, spectrums, [
116
+ { frameIndex: 10, intensity: 99 },
117
+ { frameIndex: 200, intensity: 0.4 },
118
+ { frameIndex: 500, intensity: 0.9 },
119
+ ]);
120
+
121
+ expect(result.startFrame).equal(10);
122
+ // The end moves from frame 460 onto the nearby beat at 500.
123
+ expect(result.highlightFrames).equal(490);
124
+ expect(result.audioSegments).deep.equal([
125
+ { seekSeconds: 10 / fps, durationSeconds: 490 / fps },
126
+ ]);
127
+ expect(result.runs).have.length(1);
128
+ });
129
+
130
+ it('returns empty slice for zero frames', async function() {
131
+ const result = await computeHighlightSlice(30, 0, [], []);
132
+ expect(result.highlightFrames).equal(0);
133
+ expect(result.spectrums).deep.equal([]);
134
+ expect(result.beatFrameIndices).deep.equal([]);
135
+ expect(result.audioSegments).deep.equal([]);
136
+ expect(result.runs).deep.equal([]);
137
+ });
138
+
139
+ it('concatenates two non-overlapping highlights chronologically with segmentCount 2', async function() {
140
+ const fps = 30;
141
+ const totalFrames = 1000;
142
+ const highlightFrames = Math.ceil(HIGHLIGHT_DURATION_SEC * fps);
143
+ const loudA = 10;
144
+ const loudB = 5;
145
+ const spectrumsA = spectrumsWithEnergyInRange(totalFrames, 0, 450, loudA);
146
+ const spectrumsB = spectrumsWithEnergyInRange(totalFrames, 500, 950, loudB);
147
+ const spectrums = spectrumsA.map((row, i) => {
148
+ const b = spectrumsB[i];
149
+ const v = (row[0] || 0) + (b[0] || 0);
150
+ return v > 0 ? [v] : [0];
151
+ });
152
+
153
+ const result = await computeHighlightSlice(fps, totalFrames, spectrums, [
154
+ { frameIndex: 50, intensity: 1 },
155
+ { frameIndex: 550, intensity: 1 },
156
+ ], 2);
157
+
158
+ expect(result.highlightFrames).equal(highlightFrames * 2);
159
+ expect(result.spectrums.length).equal(highlightFrames * 2);
160
+ expect(result.startFrame).equal(0);
161
+ expect(result.audioSegments).deep.equal([
162
+ { seekSeconds: 0, durationSeconds: highlightFrames / fps },
163
+ { seekSeconds: 500 / fps, durationSeconds: highlightFrames / fps },
164
+ ]);
165
+ expect(result.beatFrameIndices).deep.equal([50, 500]);
166
+ expect(result.runs).have.length(2);
167
+ expect(result.runs[0].startFrame).equal(0);
168
+ expect(result.runs[1].startFrame).equal(500);
169
+ expect(result.runs[0].spectrums.length).equal(highlightFrames);
170
+ expect(result.runs[1].spectrums.length).equal(highlightFrames);
171
+ });
172
+
173
+ it('ignores extra segmentCount when only one full highlight window fits', async function() {
174
+ const fps = 30;
175
+ const totalFrames = 600;
176
+ const highlightFrames = Math.ceil(HIGHLIGHT_DURATION_SEC * fps);
177
+ const spectrums = spectrumsWithEnergyInRange(totalFrames, 0, highlightFrames, 1);
178
+
179
+ const result = await computeHighlightSlice(fps, totalFrames, spectrums, [], 3);
180
+
181
+ expect(result.highlightFrames).equal(highlightFrames);
182
+ expect(result.audioSegments).have.length(1);
183
+ expect(result.runs).have.length(1);
184
+ });
185
+
186
+ it('snaps highlight start and end onto detected beats', async function() {
187
+ const fps = 30;
188
+ const totalFrames = 1000;
189
+ const highlightFrames = Math.ceil(HIGHLIGHT_DURATION_SEC * fps);
190
+ const spectrums = spectrumsWithEnergyInRange(totalFrames, 300, 300 + highlightFrames);
191
+
192
+ const result = await computeHighlightSlice(fps, totalFrames, spectrums, [
193
+ { frameIndex: 290, intensity: 1 },
194
+ { frameIndex: 600, intensity: 1 },
195
+ { frameIndex: 760, intensity: 1 },
196
+ ]);
197
+
198
+ expect(result.startFrame).equal(290);
199
+ expect(result.highlightFrames).equal(760 - 290);
200
+ expect(result.audioSegments).deep.equal([
201
+ { seekSeconds: 290 / fps, durationSeconds: (760 - 290) / fps },
202
+ ]);
203
+ expect(result.beatFrameIndices).deep.equal([0, 310]);
204
+ });
205
+
206
+ it('snaps to a tempo grid and prefers a whole number of bars', async function() {
207
+ const fps = 30;
208
+ const totalFrames = 2000;
209
+ const highlightFrames = Math.ceil(HIGHLIGHT_DURATION_SEC * fps);
210
+ const spectrums = spectrumsWithEnergyInRange(totalFrames, 505, 505 + highlightFrames);
211
+ // 120 BPM at 30 fps: a beat every 15 frames, a bar every 60.
212
+ const beatGrid = { periodFrames: 15, phaseFrame: 5 };
213
+
214
+ const result = await computeHighlightSlice(fps, totalFrames, spectrums, [], 1, beatGrid);
215
+
216
+ expect((result.startFrame - 5) % 15).equal(0);
217
+ expect(result.highlightFrames % 60).equal(0);
218
+ expect(Math.abs(result.startFrame - 505)).lte(MAX_BEAT_SNAP_SEC * fps);
219
+ expect(Math.abs(result.highlightFrames - highlightFrames)).lte(2 * MAX_BEAT_SNAP_SEC * fps);
220
+ });
221
+
222
+ it('keeps several snapped highlights from overlapping', async function() {
223
+ const fps = 30;
224
+ const totalFrames = 3000;
225
+ const spectrums = Array.from({ length: totalFrames }, (_, i) => [1 + Math.sin(i / 50)]);
226
+ const beatGrid = { periodFrames: 14, phaseFrame: 3 };
227
+
228
+ const result = await computeHighlightSlice(fps, totalFrames, spectrums, [], 4, beatGrid);
229
+
230
+ for (let i = 1; i < result.runs.length; i++) {
231
+ const prev = result.runs[i - 1];
232
+ expect(result.runs[i].startFrame).gte(prev.startFrame + prev.highlightFrames);
233
+ }
234
+ });
235
+ });
236
+
237
+ describe('buildBeatCandidates', function() {
238
+ it('uses onsets when no tempo grid is given', function() {
239
+ expect(buildBeatCandidates(30, 100, [
240
+ { frameIndex: 40, intensity: 1 },
241
+ { frameIndex: 10, intensity: 1 },
242
+ { frameIndex: 10, intensity: 1 },
243
+ ])).deep.equal([10, 40]);
244
+ });
245
+
246
+ it('uses tracked beats, then the grid nudged onto nearby onsets', function() {
247
+ const frames = buildBeatCandidates(10, 60, [{ frameIndex: 41, intensity: 1 }], {
248
+ periodFrames: 10,
249
+ phaseFrame: 0,
250
+ beatsSec: [0.1, 1.1, 2.1],
251
+ });
252
+ expect(frames).deep.equal([1, 11, 21, 30, 41, 50, 60]);
253
+ });
254
+ });
255
+
256
+ describe('snapSegmentToBeats', function() {
257
+ it('leaves a segment unchanged when no beat is close enough', function() {
258
+ const seg = { startFrame: 100, highlightFrames: 200 };
259
+ expect(snapSegmentToBeats(seg, 1000, [10, 900], 20)).deep.equal(seg);
260
+ });
261
+ });