nodejs-audio-visualizer 5.2.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.
- package/README.md +81 -81
- package/dist/highlight.d.ts +35 -3
- package/dist/highlight.js +152 -15
- package/dist/highlight.js.map +1 -1
- package/dist/index.d.ts +2 -2
- package/dist/index.js +14 -13
- package/dist/index.js.map +1 -1
- package/package.json +1 -1
- package/src/highlight.ts +164 -4
- package/src/index.ts +813 -811
- package/src/tests/highlight.test.ts +86 -3
|
@@ -1,5 +1,11 @@
|
|
|
1
1
|
import { expect } from 'chai';
|
|
2
|
-
import {
|
|
2
|
+
import {
|
|
3
|
+
computeHighlightSlice,
|
|
4
|
+
HIGHLIGHT_DURATION_SEC,
|
|
5
|
+
MAX_BEAT_SNAP_SEC,
|
|
6
|
+
buildBeatCandidates,
|
|
7
|
+
snapSegmentToBeats,
|
|
8
|
+
} from '../highlight';
|
|
3
9
|
|
|
4
10
|
const dummySpectrums = (n: number) => Array.from({ length: n }, () => [0]);
|
|
5
11
|
|
|
@@ -113,9 +119,10 @@ describe('computeHighlightSlice', function() {
|
|
|
113
119
|
]);
|
|
114
120
|
|
|
115
121
|
expect(result.startFrame).equal(10);
|
|
116
|
-
|
|
122
|
+
// The end moves from frame 460 onto the nearby beat at 500.
|
|
123
|
+
expect(result.highlightFrames).equal(490);
|
|
117
124
|
expect(result.audioSegments).deep.equal([
|
|
118
|
-
{ seekSeconds: 10 / fps, durationSeconds:
|
|
125
|
+
{ seekSeconds: 10 / fps, durationSeconds: 490 / fps },
|
|
119
126
|
]);
|
|
120
127
|
expect(result.runs).have.length(1);
|
|
121
128
|
});
|
|
@@ -175,4 +182,80 @@ describe('computeHighlightSlice', function() {
|
|
|
175
182
|
expect(result.audioSegments).have.length(1);
|
|
176
183
|
expect(result.runs).have.length(1);
|
|
177
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
|
+
});
|
|
178
261
|
});
|