nodejs-audio-visualizer 4.5.0 → 5.0.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 (47) hide show
  1. package/LICENSE +21 -21
  2. package/README.md +80 -80
  3. package/dist/audio.d.ts +12 -12
  4. package/dist/audio.js +120 -120
  5. package/dist/beats.d.ts +19 -19
  6. package/dist/beats.js +43 -43
  7. package/dist/bpmEncoder.d.ts +14 -14
  8. package/dist/bpmEncoder.js +97 -97
  9. package/dist/config.d.ts +77 -77
  10. package/dist/config.js +369 -369
  11. package/dist/dsp.d.ts +16 -16
  12. package/dist/dsp.js +76 -76
  13. package/dist/highlight.d.ts +32 -32
  14. package/dist/highlight.js +325 -325
  15. package/dist/image.d.ts +79 -79
  16. package/dist/image.js +458 -458
  17. package/dist/index.d.ts +79 -79
  18. package/dist/index.js +534 -531
  19. package/dist/index.js.map +1 -1
  20. package/dist/subtitleConvert.d.ts +3 -3
  21. package/dist/subtitleConvert.js +61 -61
  22. package/dist/video.d.ts +80 -80
  23. package/dist/video.js +434 -426
  24. package/dist/video.js.map +1 -1
  25. package/dist/waitForEventLoop.d.ts +1 -1
  26. package/dist/waitForEventLoop.js +5 -5
  27. package/package.json +61 -61
  28. package/src/audio.ts +120 -120
  29. package/src/beats.ts +68 -68
  30. package/src/bpmEncoder.ts +97 -97
  31. package/src/config.ts +476 -476
  32. package/src/dsp.ts +95 -95
  33. package/src/highlight.ts +284 -284
  34. package/src/image.ts +602 -602
  35. package/src/index.ts +718 -713
  36. package/src/subtitleConvert.ts +63 -63
  37. package/src/tests/audio.test.ts +96 -96
  38. package/src/tests/config.test.ts +496 -496
  39. package/src/tests/dsp.test.ts +24 -24
  40. package/src/tests/highlight.test.ts +178 -178
  41. package/src/tests/image.test.ts +222 -222
  42. package/src/tests/index.test.ts +180 -112
  43. package/src/tests/subtitleConvert.test.ts +23 -23
  44. package/src/tests/video.test.ts +285 -216
  45. package/src/typings.d.ts +6 -6
  46. package/src/video.ts +550 -542
  47. 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,178 @@
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 { 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
+ });