nodejs-audio-visualizer 5.1.0 → 5.2.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 -21
- package/README.md +3 -2
- package/dist/audio.d.ts +13 -13
- package/dist/audio.js +128 -128
- package/dist/beats.d.ts +39 -39
- package/dist/beats.js +220 -220
- package/dist/bpmEncoder.d.ts +14 -14
- package/dist/bpmEncoder.js +97 -97
- package/dist/config.d.ts +78 -77
- package/dist/config.js +377 -369
- package/dist/config.js.map +1 -1
- package/dist/dsp.d.ts +16 -16
- package/dist/dsp.js +76 -76
- package/dist/highlight.d.ts +34 -34
- package/dist/highlight.js +334 -334
- package/dist/image.d.ts +97 -92
- package/dist/image.js +545 -538
- package/dist/image.js.map +1 -1
- package/dist/index.d.ts +87 -79
- package/dist/index.js +597 -566
- package/dist/index.js.map +1 -1
- package/dist/subtitleConvert.d.ts +3 -3
- package/dist/subtitleConvert.js +61 -61
- package/dist/video.d.ts +150 -120
- package/dist/video.js +677 -582
- package/dist/video.js.map +1 -1
- package/dist/waitForEventLoop.d.ts +1 -1
- package/dist/waitForEventLoop.js +5 -5
- package/package.json +62 -62
- package/src/audio.ts +129 -129
- package/src/beats.ts +279 -279
- package/src/bpmEncoder.ts +97 -97
- package/src/config.ts +487 -476
- package/src/dsp.ts +95 -95
- package/src/highlight.ts +291 -291
- package/src/image.ts +707 -700
- package/src/index.ts +79 -37
- package/src/subtitleConvert.ts +63 -63
- package/src/tests/audio.test.ts +102 -102
- package/src/tests/beats.test.ts +134 -134
- package/src/tests/config.test.ts +534 -496
- package/src/tests/dsp.test.ts +24 -24
- package/src/tests/highlight.test.ts +178 -178
- package/src/tests/image.test.ts +319 -308
- package/src/tests/index.test.ts +180 -180
- package/src/tests/subtitleConvert.test.ts +23 -23
- package/src/tests/video.test.ts +535 -447
- package/src/typings.d.ts +28 -28
- package/src/video.ts +856 -722
- package/src/waitForEventLoop.ts +2 -2
package/src/tests/beats.test.ts
CHANGED
|
@@ -1,134 +1,134 @@
|
|
|
1
|
-
import { expect } from 'chai';
|
|
2
|
-
import { estimateTempo, beatGridFrameIndices, shiftTempoPhase, tempoForWindow } from '../beats';
|
|
3
|
-
|
|
4
|
-
const clickTrain = (
|
|
5
|
-
sampleRate: number,
|
|
6
|
-
bpm: number,
|
|
7
|
-
durationSec: number,
|
|
8
|
-
phaseSec = 0,
|
|
9
|
-
halfPeriodGhost = 0,
|
|
10
|
-
): number[] => {
|
|
11
|
-
const n = Math.round(sampleRate * durationSec);
|
|
12
|
-
const samples = new Array<number>(n).fill(0);
|
|
13
|
-
const period = 60 / bpm;
|
|
14
|
-
const clickLen = Math.round(sampleRate * 0.01);
|
|
15
|
-
const addClick = (timeSec: number, amplitude: number) => {
|
|
16
|
-
const start = Math.round(timeSec * sampleRate);
|
|
17
|
-
for (let i = 0; i < clickLen && start + i < n; i++) {
|
|
18
|
-
const env = Math.exp(-i / (clickLen * 0.25));
|
|
19
|
-
samples[start + i] += amplitude * env * (i % 2 === 0 ? 1 : -0.7);
|
|
20
|
-
}
|
|
21
|
-
};
|
|
22
|
-
for (let t = phaseSec; t < durationSec; t += period) {
|
|
23
|
-
addClick(t, 1);
|
|
24
|
-
if (halfPeriodGhost > 0) {
|
|
25
|
-
addClick(t + period / 2, halfPeriodGhost);
|
|
26
|
-
}
|
|
27
|
-
}
|
|
28
|
-
return samples;
|
|
29
|
-
};
|
|
30
|
-
|
|
31
|
-
describe('beats', function () {
|
|
32
|
-
this.timeout(10000);
|
|
33
|
-
|
|
34
|
-
it('estimateTempo detects 128 BPM from a click train', function () {
|
|
35
|
-
const sampleRate = 22050;
|
|
36
|
-
const fps = 30;
|
|
37
|
-
const tempo = estimateTempo(clickTrain(sampleRate, 128, 8), sampleRate, fps);
|
|
38
|
-
expect(tempo).not.equal(null);
|
|
39
|
-
expect(tempo!.bpm).closeTo(128, 8);
|
|
40
|
-
expect(tempo!.periodFrames).closeTo(fps * 60 / 128, 1);
|
|
41
|
-
});
|
|
42
|
-
|
|
43
|
-
it('estimateTempo recovers phase of the click train', function () {
|
|
44
|
-
const sampleRate = 22050;
|
|
45
|
-
const fps = 30;
|
|
46
|
-
const phaseSec = 0.1;
|
|
47
|
-
const tempo = estimateTempo(clickTrain(sampleRate, 128, 8, phaseSec), sampleRate, fps);
|
|
48
|
-
expect(tempo).not.equal(null);
|
|
49
|
-
expect(tempo!.phaseFrame).closeTo(phaseSec * fps, 2);
|
|
50
|
-
});
|
|
51
|
-
|
|
52
|
-
it('estimateTempo prefers ~128 BPM when half-period ghosts are present', function () {
|
|
53
|
-
const sampleRate = 22050;
|
|
54
|
-
const fps = 30;
|
|
55
|
-
const tempo = estimateTempo(clickTrain(sampleRate, 128, 8, 0, 0.35), sampleRate, fps);
|
|
56
|
-
expect(tempo).not.equal(null);
|
|
57
|
-
expect(tempo!.bpm).closeTo(128, 8);
|
|
58
|
-
});
|
|
59
|
-
|
|
60
|
-
it('estimateTempo returns null for silence', function () {
|
|
61
|
-
const sampleRate = 22050;
|
|
62
|
-
const samples = new Array<number>(sampleRate * 2).fill(0);
|
|
63
|
-
expect(estimateTempo(samples, sampleRate, 30)).equal(null);
|
|
64
|
-
});
|
|
65
|
-
|
|
66
|
-
it('estimateTempo returns null for empty or invalid input', function () {
|
|
67
|
-
expect(estimateTempo([], 22050, 30)).equal(null);
|
|
68
|
-
expect(estimateTempo(clickTrain(22050, 128, 8), 0, 30)).equal(null);
|
|
69
|
-
expect(estimateTempo(clickTrain(22050, 128, 8), 22050, 0)).equal(null);
|
|
70
|
-
});
|
|
71
|
-
|
|
72
|
-
it('shiftTempoPhase wraps the beat grid into a later window', function () {
|
|
73
|
-
expect(shiftTempoPhase({ bpm: 120, periodFrames: 15, phaseFrame: 3 }, 0)).deep.equal({
|
|
74
|
-
bpm: 120,
|
|
75
|
-
periodFrames: 15,
|
|
76
|
-
phaseFrame: 3,
|
|
77
|
-
});
|
|
78
|
-
expect(shiftTempoPhase({ bpm: 120, periodFrames: 15, phaseFrame: 3 }, 3).phaseFrame).equal(0);
|
|
79
|
-
expect(shiftTempoPhase({ bpm: 120, periodFrames: 15, phaseFrame: 3 }, 18).phaseFrame).equal(0);
|
|
80
|
-
});
|
|
81
|
-
|
|
82
|
-
it('shiftTempoPhase keeps a non-integer period aligned in a late window', function () {
|
|
83
|
-
const tempo = { bpm: 128, periodFrames: 14.0625, phaseFrame: 3 };
|
|
84
|
-
const startFrame = 2000;
|
|
85
|
-
const shifted = shiftTempoPhase(tempo, startFrame);
|
|
86
|
-
const expected: number[] = [];
|
|
87
|
-
for (let n = 0; n < 400; n++) {
|
|
88
|
-
const local = Math.round(tempo.phaseFrame + n * tempo.periodFrames) - startFrame;
|
|
89
|
-
if (local > 0 && local < 60) {
|
|
90
|
-
expected.push(local);
|
|
91
|
-
}
|
|
92
|
-
}
|
|
93
|
-
expect(beatGridFrameIndices(shifted, 60)).deep.equal(expected);
|
|
94
|
-
});
|
|
95
|
-
|
|
96
|
-
it('tempoForWindow locks phase to beat times inside the edited window', function () {
|
|
97
|
-
const fps = 30;
|
|
98
|
-
const periodFrames = 15;
|
|
99
|
-
const startFrame = 90;
|
|
100
|
-
const tempo = {
|
|
101
|
-
bpm: 120,
|
|
102
|
-
periodFrames,
|
|
103
|
-
phaseFrame: 1,
|
|
104
|
-
beatsSec: [0.1, 3.2, 3.7, 4.2, 4.7],
|
|
105
|
-
};
|
|
106
|
-
const windowed = tempoForWindow(tempo, startFrame, fps, 60);
|
|
107
|
-
expect(windowed.phaseFrame).closeTo(6, 1);
|
|
108
|
-
expect(shiftTempoPhase(tempo, startFrame).phaseFrame).closeTo(1, 0.001);
|
|
109
|
-
expect(beatGridFrameIndices(windowed, 50)[0]).closeTo(6, 1);
|
|
110
|
-
expect(tempoForWindow(tempo, startFrame, fps, 60, [1, 16, 31]).phaseFrame).closeTo(6, 1);
|
|
111
|
-
});
|
|
112
|
-
|
|
113
|
-
it('tempoForWindow falls back to local onsets when beat times are outside the window', function () {
|
|
114
|
-
const tempo = {
|
|
115
|
-
bpm: 120,
|
|
116
|
-
periodFrames: 15,
|
|
117
|
-
phaseFrame: 1,
|
|
118
|
-
beatsSec: [0.1, 0.6, 1.1],
|
|
119
|
-
};
|
|
120
|
-
const windowed = tempoForWindow(tempo, 90, 30, 60, [7, 22, 37]);
|
|
121
|
-
expect(windowed.phaseFrame).closeTo(7, 1);
|
|
122
|
-
});
|
|
123
|
-
|
|
124
|
-
it('beatGridFrameIndices emits every beat and skips frame 0', function () {
|
|
125
|
-
expect(beatGridFrameIndices({ periodFrames: 14, phaseFrame: 0 }, 50)).deep.equal([14, 28, 42]);
|
|
126
|
-
expect(beatGridFrameIndices({ periodFrames: 14, phaseFrame: 3 }, 32)).deep.equal([3, 17, 31]);
|
|
127
|
-
expect(beatGridFrameIndices({ periodFrames: 14, phaseFrame: 0 }, 14)).deep.equal([]);
|
|
128
|
-
});
|
|
129
|
-
|
|
130
|
-
it('beatGridFrameIndices stride emits every Nth beat', function () {
|
|
131
|
-
expect(beatGridFrameIndices({ periodFrames: 14, phaseFrame: 0 }, 200, 4)).deep.equal([14, 70, 126, 182]);
|
|
132
|
-
expect(beatGridFrameIndices({ periodFrames: 14, phaseFrame: 3 }, 200, 4)).deep.equal([3, 59, 115, 171]);
|
|
133
|
-
});
|
|
134
|
-
});
|
|
1
|
+
import { expect } from 'chai';
|
|
2
|
+
import { estimateTempo, beatGridFrameIndices, shiftTempoPhase, tempoForWindow } from '../beats';
|
|
3
|
+
|
|
4
|
+
const clickTrain = (
|
|
5
|
+
sampleRate: number,
|
|
6
|
+
bpm: number,
|
|
7
|
+
durationSec: number,
|
|
8
|
+
phaseSec = 0,
|
|
9
|
+
halfPeriodGhost = 0,
|
|
10
|
+
): number[] => {
|
|
11
|
+
const n = Math.round(sampleRate * durationSec);
|
|
12
|
+
const samples = new Array<number>(n).fill(0);
|
|
13
|
+
const period = 60 / bpm;
|
|
14
|
+
const clickLen = Math.round(sampleRate * 0.01);
|
|
15
|
+
const addClick = (timeSec: number, amplitude: number) => {
|
|
16
|
+
const start = Math.round(timeSec * sampleRate);
|
|
17
|
+
for (let i = 0; i < clickLen && start + i < n; i++) {
|
|
18
|
+
const env = Math.exp(-i / (clickLen * 0.25));
|
|
19
|
+
samples[start + i] += amplitude * env * (i % 2 === 0 ? 1 : -0.7);
|
|
20
|
+
}
|
|
21
|
+
};
|
|
22
|
+
for (let t = phaseSec; t < durationSec; t += period) {
|
|
23
|
+
addClick(t, 1);
|
|
24
|
+
if (halfPeriodGhost > 0) {
|
|
25
|
+
addClick(t + period / 2, halfPeriodGhost);
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
return samples;
|
|
29
|
+
};
|
|
30
|
+
|
|
31
|
+
describe('beats', function () {
|
|
32
|
+
this.timeout(10000);
|
|
33
|
+
|
|
34
|
+
it('estimateTempo detects 128 BPM from a click train', function () {
|
|
35
|
+
const sampleRate = 22050;
|
|
36
|
+
const fps = 30;
|
|
37
|
+
const tempo = estimateTempo(clickTrain(sampleRate, 128, 8), sampleRate, fps);
|
|
38
|
+
expect(tempo).not.equal(null);
|
|
39
|
+
expect(tempo!.bpm).closeTo(128, 8);
|
|
40
|
+
expect(tempo!.periodFrames).closeTo(fps * 60 / 128, 1);
|
|
41
|
+
});
|
|
42
|
+
|
|
43
|
+
it('estimateTempo recovers phase of the click train', function () {
|
|
44
|
+
const sampleRate = 22050;
|
|
45
|
+
const fps = 30;
|
|
46
|
+
const phaseSec = 0.1;
|
|
47
|
+
const tempo = estimateTempo(clickTrain(sampleRate, 128, 8, phaseSec), sampleRate, fps);
|
|
48
|
+
expect(tempo).not.equal(null);
|
|
49
|
+
expect(tempo!.phaseFrame).closeTo(phaseSec * fps, 2);
|
|
50
|
+
});
|
|
51
|
+
|
|
52
|
+
it('estimateTempo prefers ~128 BPM when half-period ghosts are present', function () {
|
|
53
|
+
const sampleRate = 22050;
|
|
54
|
+
const fps = 30;
|
|
55
|
+
const tempo = estimateTempo(clickTrain(sampleRate, 128, 8, 0, 0.35), sampleRate, fps);
|
|
56
|
+
expect(tempo).not.equal(null);
|
|
57
|
+
expect(tempo!.bpm).closeTo(128, 8);
|
|
58
|
+
});
|
|
59
|
+
|
|
60
|
+
it('estimateTempo returns null for silence', function () {
|
|
61
|
+
const sampleRate = 22050;
|
|
62
|
+
const samples = new Array<number>(sampleRate * 2).fill(0);
|
|
63
|
+
expect(estimateTempo(samples, sampleRate, 30)).equal(null);
|
|
64
|
+
});
|
|
65
|
+
|
|
66
|
+
it('estimateTempo returns null for empty or invalid input', function () {
|
|
67
|
+
expect(estimateTempo([], 22050, 30)).equal(null);
|
|
68
|
+
expect(estimateTempo(clickTrain(22050, 128, 8), 0, 30)).equal(null);
|
|
69
|
+
expect(estimateTempo(clickTrain(22050, 128, 8), 22050, 0)).equal(null);
|
|
70
|
+
});
|
|
71
|
+
|
|
72
|
+
it('shiftTempoPhase wraps the beat grid into a later window', function () {
|
|
73
|
+
expect(shiftTempoPhase({ bpm: 120, periodFrames: 15, phaseFrame: 3 }, 0)).deep.equal({
|
|
74
|
+
bpm: 120,
|
|
75
|
+
periodFrames: 15,
|
|
76
|
+
phaseFrame: 3,
|
|
77
|
+
});
|
|
78
|
+
expect(shiftTempoPhase({ bpm: 120, periodFrames: 15, phaseFrame: 3 }, 3).phaseFrame).equal(0);
|
|
79
|
+
expect(shiftTempoPhase({ bpm: 120, periodFrames: 15, phaseFrame: 3 }, 18).phaseFrame).equal(0);
|
|
80
|
+
});
|
|
81
|
+
|
|
82
|
+
it('shiftTempoPhase keeps a non-integer period aligned in a late window', function () {
|
|
83
|
+
const tempo = { bpm: 128, periodFrames: 14.0625, phaseFrame: 3 };
|
|
84
|
+
const startFrame = 2000;
|
|
85
|
+
const shifted = shiftTempoPhase(tempo, startFrame);
|
|
86
|
+
const expected: number[] = [];
|
|
87
|
+
for (let n = 0; n < 400; n++) {
|
|
88
|
+
const local = Math.round(tempo.phaseFrame + n * tempo.periodFrames) - startFrame;
|
|
89
|
+
if (local > 0 && local < 60) {
|
|
90
|
+
expected.push(local);
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
expect(beatGridFrameIndices(shifted, 60)).deep.equal(expected);
|
|
94
|
+
});
|
|
95
|
+
|
|
96
|
+
it('tempoForWindow locks phase to beat times inside the edited window', function () {
|
|
97
|
+
const fps = 30;
|
|
98
|
+
const periodFrames = 15;
|
|
99
|
+
const startFrame = 90;
|
|
100
|
+
const tempo = {
|
|
101
|
+
bpm: 120,
|
|
102
|
+
periodFrames,
|
|
103
|
+
phaseFrame: 1,
|
|
104
|
+
beatsSec: [0.1, 3.2, 3.7, 4.2, 4.7],
|
|
105
|
+
};
|
|
106
|
+
const windowed = tempoForWindow(tempo, startFrame, fps, 60);
|
|
107
|
+
expect(windowed.phaseFrame).closeTo(6, 1);
|
|
108
|
+
expect(shiftTempoPhase(tempo, startFrame).phaseFrame).closeTo(1, 0.001);
|
|
109
|
+
expect(beatGridFrameIndices(windowed, 50)[0]).closeTo(6, 1);
|
|
110
|
+
expect(tempoForWindow(tempo, startFrame, fps, 60, [1, 16, 31]).phaseFrame).closeTo(6, 1);
|
|
111
|
+
});
|
|
112
|
+
|
|
113
|
+
it('tempoForWindow falls back to local onsets when beat times are outside the window', function () {
|
|
114
|
+
const tempo = {
|
|
115
|
+
bpm: 120,
|
|
116
|
+
periodFrames: 15,
|
|
117
|
+
phaseFrame: 1,
|
|
118
|
+
beatsSec: [0.1, 0.6, 1.1],
|
|
119
|
+
};
|
|
120
|
+
const windowed = tempoForWindow(tempo, 90, 30, 60, [7, 22, 37]);
|
|
121
|
+
expect(windowed.phaseFrame).closeTo(7, 1);
|
|
122
|
+
});
|
|
123
|
+
|
|
124
|
+
it('beatGridFrameIndices emits every beat and skips frame 0', function () {
|
|
125
|
+
expect(beatGridFrameIndices({ periodFrames: 14, phaseFrame: 0 }, 50)).deep.equal([14, 28, 42]);
|
|
126
|
+
expect(beatGridFrameIndices({ periodFrames: 14, phaseFrame: 3 }, 32)).deep.equal([3, 17, 31]);
|
|
127
|
+
expect(beatGridFrameIndices({ periodFrames: 14, phaseFrame: 0 }, 14)).deep.equal([]);
|
|
128
|
+
});
|
|
129
|
+
|
|
130
|
+
it('beatGridFrameIndices stride emits every Nth beat', function () {
|
|
131
|
+
expect(beatGridFrameIndices({ periodFrames: 14, phaseFrame: 0 }, 200, 4)).deep.equal([14, 70, 126, 182]);
|
|
132
|
+
expect(beatGridFrameIndices({ periodFrames: 14, phaseFrame: 3 }, 200, 4)).deep.equal([3, 59, 115, 171]);
|
|
133
|
+
});
|
|
134
|
+
});
|