nodejs-audio-visualizer 5.0.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.
Files changed (53) hide show
  1. package/LICENSE +21 -21
  2. package/README.md +3 -2
  3. package/dist/audio.d.ts +13 -12
  4. package/dist/audio.js +128 -120
  5. package/dist/audio.js.map +1 -1
  6. package/dist/beats.d.ts +39 -19
  7. package/dist/beats.js +220 -43
  8. package/dist/beats.js.map +1 -1
  9. package/dist/bpmEncoder.d.ts +14 -14
  10. package/dist/bpmEncoder.js +97 -97
  11. package/dist/config.d.ts +78 -77
  12. package/dist/config.js +377 -369
  13. package/dist/config.js.map +1 -1
  14. package/dist/dsp.d.ts +16 -16
  15. package/dist/dsp.js +76 -76
  16. package/dist/highlight.d.ts +34 -32
  17. package/dist/highlight.js +334 -325
  18. package/dist/highlight.js.map +1 -1
  19. package/dist/image.d.ts +97 -79
  20. package/dist/image.js +545 -458
  21. package/dist/image.js.map +1 -1
  22. package/dist/index.d.ts +87 -79
  23. package/dist/index.js +597 -534
  24. package/dist/index.js.map +1 -1
  25. package/dist/subtitleConvert.d.ts +3 -3
  26. package/dist/subtitleConvert.js +61 -61
  27. package/dist/video.d.ts +150 -80
  28. package/dist/video.js +677 -434
  29. package/dist/video.js.map +1 -1
  30. package/dist/waitForEventLoop.d.ts +1 -1
  31. package/dist/waitForEventLoop.js +5 -5
  32. package/package.json +62 -61
  33. package/src/audio.ts +129 -120
  34. package/src/beats.ts +279 -68
  35. package/src/bpmEncoder.ts +97 -97
  36. package/src/config.ts +487 -476
  37. package/src/dsp.ts +95 -95
  38. package/src/highlight.ts +291 -284
  39. package/src/image.ts +707 -602
  40. package/src/index.ts +124 -31
  41. package/src/subtitleConvert.ts +63 -63
  42. package/src/tests/audio.test.ts +102 -96
  43. package/src/tests/beats.test.ts +134 -0
  44. package/src/tests/config.test.ts +534 -496
  45. package/src/tests/dsp.test.ts +24 -24
  46. package/src/tests/highlight.test.ts +178 -178
  47. package/src/tests/image.test.ts +319 -222
  48. package/src/tests/index.test.ts +180 -180
  49. package/src/tests/subtitleConvert.test.ts +23 -23
  50. package/src/tests/video.test.ts +535 -285
  51. package/src/typings.d.ts +28 -6
  52. package/src/video.ts +856 -550
  53. package/src/waitForEventLoop.ts +2 -2
package/src/highlight.ts CHANGED
@@ -1,284 +1,291 @@
1
- /** Output length per segment when `audio.autoHighlight` is true (seconds). */
2
-
3
- import { waitForEventLoop } from "./waitForEventLoop";
4
-
5
- export const HIGHLIGHT_DURATION_SEC = 15;
6
-
7
- export interface BeatFrameEvent {
8
- frameIndex: number;
9
- intensity: number;
10
- }
11
-
12
- export interface HighlightAudioSegment {
13
- seekSeconds: number;
14
- durationSeconds: number;
15
- }
16
-
17
- /** One auto-highlight segment before concatenation (for separate output files). */
18
- export interface HighlightRun {
19
- startFrame: number;
20
- highlightFrames: number;
21
- spectrums: number[][];
22
- beatFrameIndices: number[];
23
- audioSegment: HighlightAudioSegment;
24
- }
25
-
26
- function buildHighlightRun(
27
- fps: number,
28
- seg: { startFrame: number; highlightFrames: number },
29
- spectrums: number[][],
30
- beatEvents: BeatFrameEvent[],
31
- ): HighlightRun {
32
- const { startFrame, highlightFrames } = seg;
33
- const spectrumsSlice = spectrums.slice(startFrame, startFrame + highlightFrames);
34
- const beatFrameIndices = beatEvents
35
- .map(b => b.frameIndex)
36
- .filter(i => i >= startFrame && i < startFrame + highlightFrames)
37
- .map(i => i - startFrame)
38
- .sort((a, b) => a - b);
39
- return {
40
- startFrame,
41
- highlightFrames,
42
- spectrums: spectrumsSlice,
43
- beatFrameIndices,
44
- audioSegment: {
45
- seekSeconds: startFrame / fps,
46
- durationSeconds: highlightFrames / fps,
47
- },
48
- };
49
- }
50
-
51
- function frameEnergy(spectrum: number[]): number {
52
- let s = 0;
53
- for (let i = 0; i < spectrum.length; i++) {
54
- const v = spectrum[i];
55
- s += v * v;
56
- }
57
- return s;
58
- }
59
-
60
- function rangesOverlap(
61
- aStart: number,
62
- aEndExclusive: number,
63
- ranges: Array<{ start: number; endExclusive: number }>,
64
- ): boolean {
65
- for (const r of ranges) {
66
- if (aStart < r.endExclusive && aEndExclusive > r.start) {
67
- return true;
68
- }
69
- }
70
- return false;
71
- }
72
-
73
- async function buildFrameEnergies(
74
- spectrums: number[][],
75
- totalFrames: number,
76
- ): Promise<Float64Array> {
77
- const energies = new Float64Array(totalFrames);
78
- for (let i = 0; i < totalFrames; i++) {
79
- energies[i] = frameEnergy(spectrums[i] ?? []);
80
- await waitForEventLoop();
81
- }
82
- return energies;
83
- }
84
-
85
- /**
86
- * Chooses the fixed-length window whose frames have the highest summed spectral energy
87
- * (typical chorus / drop vs a single loud intro transient).
88
- * Skips windows that overlap any excluded range.
89
- */
90
- async function findBestHighlightStart(
91
- energies: Float64Array,
92
- totalFrames: number,
93
- windowFrames: number,
94
- excludeRanges: Array<{ start: number; endExclusive: number }>,
95
- ): Promise<number | null> {
96
- if (totalFrames <= windowFrames) {
97
- if (!rangesOverlap(0, totalFrames, excludeRanges)) {
98
- return 0;
99
- }
100
- return null;
101
- }
102
-
103
- let minE = Infinity;
104
- let maxE = -Infinity;
105
- for (let i = 0; i < totalFrames; i++) {
106
- const e = energies[i];
107
- if (e < minE) {
108
- minE = e;
109
- }
110
- if (e > maxE) {
111
- maxE = e;
112
- }
113
- await waitForEventLoop();
114
- }
115
-
116
- const prefix = new Float64Array(totalFrames + 1);
117
- for (let i = 0; i < totalFrames; i++) {
118
- prefix[i + 1] = prefix[i] + energies[i];
119
- await waitForEventLoop();
120
- }
121
-
122
- const maxStart = totalFrames - windowFrames;
123
- let bestStart: number | null = null;
124
- let bestScore = -Infinity;
125
-
126
- for (let start = 0; start <= maxStart; start++) {
127
- if (rangesOverlap(start, start + windowFrames, excludeRanges)) {
128
- await waitForEventLoop();
129
- continue;
130
- }
131
- const score = prefix[start + windowFrames] - prefix[start];
132
- if (score > bestScore) {
133
- bestScore = score;
134
- bestStart = start;
135
- }
136
- await waitForEventLoop();
137
- }
138
-
139
- if (bestStart === null) {
140
- return null;
141
- }
142
-
143
- if (maxE <= minE) {
144
- const center = Math.floor((totalFrames - windowFrames) / 2);
145
- let bestDist = Infinity;
146
- let tieBreak: number | null = null;
147
- for (let start = 0; start <= maxStart; start++) {
148
- if (rangesOverlap(start, start + windowFrames, excludeRanges)) {
149
- await waitForEventLoop();
150
- continue;
151
- }
152
- const dist = Math.abs(start - center);
153
- if (dist < bestDist) {
154
- bestDist = dist;
155
- tieBreak = start;
156
- }
157
- await waitForEventLoop();
158
- }
159
- return tieBreak;
160
- }
161
-
162
- return bestStart;
163
- }
164
-
165
-
166
- /**
167
- * Picks up to `segmentCount` non-overlapping 15s windows with highest summed spectral energy each,
168
- * sorts them chronologically, concatenates spectrums, and remaps beat indices.
169
- */
170
- export async function computeHighlightSlice(
171
- fps: number,
172
- totalFrames: number,
173
- spectrums: number[][],
174
- beatEvents: BeatFrameEvent[],
175
- segmentCount = 1,
176
- ): Promise<{
177
- startFrame: number;
178
- highlightFrames: number;
179
- spectrums: number[][];
180
- beatFrameIndices: number[];
181
- audioSeekSeconds: number;
182
- audioDurationSeconds: number;
183
- audioSegments: HighlightAudioSegment[];
184
- runs: HighlightRun[];
185
- }> {
186
- const highlightFramesTarget = Math.ceil(HIGHLIGHT_DURATION_SEC * fps);
187
-
188
- if (totalFrames <= 0) {
189
- return {
190
- startFrame: 0,
191
- highlightFrames: 0,
192
- spectrums: [],
193
- beatFrameIndices: [],
194
- audioSeekSeconds: 0,
195
- audioDurationSeconds: 0,
196
- audioSegments: [],
197
- runs: [],
198
- };
199
- }
200
-
201
- if (totalFrames <= highlightFramesTarget) {
202
- const audioSegments: HighlightAudioSegment[] = [
203
- {
204
- seekSeconds: 0,
205
- durationSeconds: totalFrames / fps,
206
- },
207
- ];
208
- const fullSeg = { startFrame: 0, highlightFrames: totalFrames };
209
- return {
210
- startFrame: 0,
211
- highlightFrames: totalFrames,
212
- spectrums: spectrums.slice(),
213
- beatFrameIndices: beatEvents.map(b => b.frameIndex).sort((a, b) => a - b),
214
- audioSeekSeconds: 0,
215
- audioDurationSeconds: totalFrames / fps,
216
- audioSegments,
217
- runs: [buildHighlightRun(fps, fullSeg, spectrums, beatEvents)],
218
- };
219
- }
220
-
221
- const highlightFrames = highlightFramesTarget;
222
- const energies = await buildFrameEnergies(spectrums, totalFrames);
223
- const excludeRanges: Array<{ start: number; endExclusive: number }> = [];
224
- const rawSegments: Array<{ startFrame: number; highlightFrames: number }> = [];
225
-
226
- const n = Math.max(1, Math.floor(segmentCount));
227
- for (let k = 0; k < n; k++) {
228
- const startFrame = await findBestHighlightStart(
229
- energies,
230
- totalFrames,
231
- highlightFrames,
232
- excludeRanges,
233
- );
234
- if (startFrame === null) {
235
- break;
236
- }
237
- rawSegments.push({ startFrame, highlightFrames });
238
- excludeRanges.push({ start: startFrame, endExclusive: startFrame + highlightFrames });
239
- }
240
-
241
- rawSegments.sort((a, b) => a.startFrame - b.startFrame);
242
-
243
- const runs = rawSegments.map(seg =>
244
- buildHighlightRun(fps, seg, spectrums, beatEvents),
245
- );
246
-
247
- const slicedSpectrums: number[][] = [];
248
- const beatFrameIndices: number[] = [];
249
- let outOffset = 0;
250
-
251
- for (const seg of rawSegments) {
252
- const chunk = spectrums.slice(seg.startFrame, seg.startFrame + seg.highlightFrames);
253
- slicedSpectrums.push(...chunk);
254
-
255
- for (const b of beatEvents) {
256
- const i = b.frameIndex;
257
- if (i >= seg.startFrame && i < seg.startFrame + seg.highlightFrames) {
258
- beatFrameIndices.push(i - seg.startFrame + outOffset);
259
- }
260
- }
261
- outOffset += seg.highlightFrames;
262
- }
263
-
264
- beatFrameIndices.sort((a, b) => a - b);
265
-
266
- const totalHighlightFrames = slicedSpectrums.length;
267
- const audioSegments: HighlightAudioSegment[] = rawSegments.map(seg => ({
268
- seekSeconds: seg.startFrame / fps,
269
- durationSeconds: seg.highlightFrames / fps,
270
- }));
271
-
272
- const first = audioSegments[0] ?? { seekSeconds: 0, durationSeconds: 0 };
273
-
274
- return {
275
- startFrame: rawSegments[0]?.startFrame ?? 0,
276
- highlightFrames: totalHighlightFrames,
277
- spectrums: slicedSpectrums,
278
- beatFrameIndices,
279
- audioSeekSeconds: first.seekSeconds,
280
- audioDurationSeconds: first.durationSeconds,
281
- audioSegments,
282
- runs,
283
- };
284
- }
1
+ /** Output length per segment when `audio.autoHighlight` is true (seconds). */
2
+
3
+ import { waitForEventLoop } from "./waitForEventLoop";
4
+
5
+ export const HIGHLIGHT_DURATION_SEC = 15;
6
+
7
+ export interface BeatFrameEvent {
8
+ frameIndex: number;
9
+ intensity: number;
10
+ }
11
+
12
+ export interface HighlightAudioSegment {
13
+ seekSeconds: number;
14
+ durationSeconds: number;
15
+ }
16
+
17
+ /** One auto-highlight segment before concatenation (for separate output files). */
18
+ export interface HighlightRun {
19
+ startFrame: number;
20
+ highlightFrames: number;
21
+ spectrums: number[][];
22
+ beatFrameIndices: number[];
23
+ beatIntensities: number[];
24
+ audioSegment: HighlightAudioSegment;
25
+ }
26
+
27
+ function buildHighlightRun(
28
+ fps: number,
29
+ seg: { startFrame: number; highlightFrames: number },
30
+ spectrums: number[][],
31
+ beatEvents: BeatFrameEvent[],
32
+ ): HighlightRun {
33
+ const { startFrame, highlightFrames } = seg;
34
+ const spectrumsSlice = spectrums.slice(startFrame, startFrame + highlightFrames);
35
+ const beatsInWindow = beatEvents
36
+ .filter(b => b.frameIndex >= startFrame && b.frameIndex < startFrame + highlightFrames)
37
+ .sort((a, b) => a.frameIndex - b.frameIndex);
38
+ const beatFrameIndices = beatsInWindow.map(b => b.frameIndex - startFrame);
39
+ const beatIntensities = beatsInWindow.map(b => b.intensity);
40
+ return {
41
+ startFrame,
42
+ highlightFrames,
43
+ spectrums: spectrumsSlice,
44
+ beatFrameIndices,
45
+ beatIntensities,
46
+ audioSegment: {
47
+ seekSeconds: startFrame / fps,
48
+ durationSeconds: highlightFrames / fps,
49
+ },
50
+ };
51
+ }
52
+
53
+ function frameEnergy(spectrum: number[]): number {
54
+ let s = 0;
55
+ for (let i = 0; i < spectrum.length; i++) {
56
+ const v = spectrum[i];
57
+ s += v * v;
58
+ }
59
+ return s;
60
+ }
61
+
62
+ function rangesOverlap(
63
+ aStart: number,
64
+ aEndExclusive: number,
65
+ ranges: Array<{ start: number; endExclusive: number }>,
66
+ ): boolean {
67
+ for (const r of ranges) {
68
+ if (aStart < r.endExclusive && aEndExclusive > r.start) {
69
+ return true;
70
+ }
71
+ }
72
+ return false;
73
+ }
74
+
75
+ async function buildFrameEnergies(
76
+ spectrums: number[][],
77
+ totalFrames: number,
78
+ ): Promise<Float64Array> {
79
+ const energies = new Float64Array(totalFrames);
80
+ for (let i = 0; i < totalFrames; i++) {
81
+ energies[i] = frameEnergy(spectrums[i] ?? []);
82
+ await waitForEventLoop();
83
+ }
84
+ return energies;
85
+ }
86
+
87
+ /**
88
+ * Chooses the fixed-length window whose frames have the highest summed spectral energy
89
+ * (typical chorus / drop vs a single loud intro transient).
90
+ * Skips windows that overlap any excluded range.
91
+ */
92
+ async function findBestHighlightStart(
93
+ energies: Float64Array,
94
+ totalFrames: number,
95
+ windowFrames: number,
96
+ excludeRanges: Array<{ start: number; endExclusive: number }>,
97
+ ): Promise<number | null> {
98
+ if (totalFrames <= windowFrames) {
99
+ if (!rangesOverlap(0, totalFrames, excludeRanges)) {
100
+ return 0;
101
+ }
102
+ return null;
103
+ }
104
+
105
+ let minE = Infinity;
106
+ let maxE = -Infinity;
107
+ for (let i = 0; i < totalFrames; i++) {
108
+ const e = energies[i];
109
+ if (e < minE) {
110
+ minE = e;
111
+ }
112
+ if (e > maxE) {
113
+ maxE = e;
114
+ }
115
+ await waitForEventLoop();
116
+ }
117
+
118
+ const prefix = new Float64Array(totalFrames + 1);
119
+ for (let i = 0; i < totalFrames; i++) {
120
+ prefix[i + 1] = prefix[i] + energies[i];
121
+ await waitForEventLoop();
122
+ }
123
+
124
+ const maxStart = totalFrames - windowFrames;
125
+ let bestStart: number | null = null;
126
+ let bestScore = -Infinity;
127
+
128
+ for (let start = 0; start <= maxStart; start++) {
129
+ if (rangesOverlap(start, start + windowFrames, excludeRanges)) {
130
+ await waitForEventLoop();
131
+ continue;
132
+ }
133
+ const score = prefix[start + windowFrames] - prefix[start];
134
+ if (score > bestScore) {
135
+ bestScore = score;
136
+ bestStart = start;
137
+ }
138
+ await waitForEventLoop();
139
+ }
140
+
141
+ if (bestStart === null) {
142
+ return null;
143
+ }
144
+
145
+ if (maxE <= minE) {
146
+ const center = Math.floor((totalFrames - windowFrames) / 2);
147
+ let bestDist = Infinity;
148
+ let tieBreak: number | null = null;
149
+ for (let start = 0; start <= maxStart; start++) {
150
+ if (rangesOverlap(start, start + windowFrames, excludeRanges)) {
151
+ await waitForEventLoop();
152
+ continue;
153
+ }
154
+ const dist = Math.abs(start - center);
155
+ if (dist < bestDist) {
156
+ bestDist = dist;
157
+ tieBreak = start;
158
+ }
159
+ await waitForEventLoop();
160
+ }
161
+ return tieBreak;
162
+ }
163
+
164
+ return bestStart;
165
+ }
166
+
167
+
168
+ /**
169
+ * Picks up to `segmentCount` non-overlapping 15s windows with highest summed spectral energy each,
170
+ * sorts them chronologically, concatenates spectrums, and remaps beat indices.
171
+ */
172
+ export async function computeHighlightSlice(
173
+ fps: number,
174
+ totalFrames: number,
175
+ spectrums: number[][],
176
+ beatEvents: BeatFrameEvent[],
177
+ segmentCount = 1,
178
+ ): Promise<{
179
+ startFrame: number;
180
+ highlightFrames: number;
181
+ spectrums: number[][];
182
+ beatFrameIndices: number[];
183
+ beatIntensities: number[];
184
+ audioSeekSeconds: number;
185
+ audioDurationSeconds: number;
186
+ audioSegments: HighlightAudioSegment[];
187
+ runs: HighlightRun[];
188
+ }> {
189
+ const highlightFramesTarget = Math.ceil(HIGHLIGHT_DURATION_SEC * fps);
190
+
191
+ if (totalFrames <= 0) {
192
+ return {
193
+ startFrame: 0,
194
+ highlightFrames: 0,
195
+ spectrums: [],
196
+ beatFrameIndices: [],
197
+ beatIntensities: [],
198
+ audioSeekSeconds: 0,
199
+ audioDurationSeconds: 0,
200
+ audioSegments: [],
201
+ runs: [],
202
+ };
203
+ }
204
+
205
+ if (totalFrames <= highlightFramesTarget) {
206
+ const audioSegments: HighlightAudioSegment[] = [
207
+ {
208
+ seekSeconds: 0,
209
+ durationSeconds: totalFrames / fps,
210
+ },
211
+ ];
212
+ const fullSeg = { startFrame: 0, highlightFrames: totalFrames };
213
+ const fullRun = buildHighlightRun(fps, fullSeg, spectrums, beatEvents);
214
+ return {
215
+ startFrame: 0,
216
+ highlightFrames: totalFrames,
217
+ spectrums: spectrums.slice(),
218
+ beatFrameIndices: fullRun.beatFrameIndices,
219
+ beatIntensities: fullRun.beatIntensities,
220
+ audioSeekSeconds: 0,
221
+ audioDurationSeconds: totalFrames / fps,
222
+ audioSegments,
223
+ runs: [fullRun],
224
+ };
225
+ }
226
+
227
+ const highlightFrames = highlightFramesTarget;
228
+ const energies = await buildFrameEnergies(spectrums, totalFrames);
229
+ const excludeRanges: Array<{ start: number; endExclusive: number }> = [];
230
+ const rawSegments: Array<{ startFrame: number; highlightFrames: number }> = [];
231
+
232
+ const n = Math.max(1, Math.floor(segmentCount));
233
+ for (let k = 0; k < n; k++) {
234
+ const startFrame = await findBestHighlightStart(
235
+ energies,
236
+ totalFrames,
237
+ highlightFrames,
238
+ excludeRanges,
239
+ );
240
+ if (startFrame === null) {
241
+ break;
242
+ }
243
+ rawSegments.push({ startFrame, highlightFrames });
244
+ excludeRanges.push({ start: startFrame, endExclusive: startFrame + highlightFrames });
245
+ }
246
+
247
+ rawSegments.sort((a, b) => a.startFrame - b.startFrame);
248
+
249
+ const runs = rawSegments.map(seg =>
250
+ buildHighlightRun(fps, seg, spectrums, beatEvents),
251
+ );
252
+
253
+ const slicedSpectrums: number[][] = [];
254
+ const beatFrameIndices: number[] = [];
255
+ const beatIntensities: number[] = [];
256
+ let outOffset = 0;
257
+
258
+ for (const seg of rawSegments) {
259
+ const chunk = spectrums.slice(seg.startFrame, seg.startFrame + seg.highlightFrames);
260
+ slicedSpectrums.push(...chunk);
261
+
262
+ const beatsInSeg = beatEvents
263
+ .filter(b => b.frameIndex >= seg.startFrame && b.frameIndex < seg.startFrame + seg.highlightFrames)
264
+ .sort((a, b) => a.frameIndex - b.frameIndex);
265
+ for (const b of beatsInSeg) {
266
+ beatFrameIndices.push(b.frameIndex - seg.startFrame + outOffset);
267
+ beatIntensities.push(b.intensity);
268
+ }
269
+ outOffset += seg.highlightFrames;
270
+ }
271
+
272
+ const totalHighlightFrames = slicedSpectrums.length;
273
+ const audioSegments: HighlightAudioSegment[] = rawSegments.map(seg => ({
274
+ seekSeconds: seg.startFrame / fps,
275
+ durationSeconds: seg.highlightFrames / fps,
276
+ }));
277
+
278
+ const first = audioSegments[0] ?? { seekSeconds: 0, durationSeconds: 0 };
279
+
280
+ return {
281
+ startFrame: rawSegments[0]?.startFrame ?? 0,
282
+ highlightFrames: totalHighlightFrames,
283
+ spectrums: slicedSpectrums,
284
+ beatFrameIndices,
285
+ beatIntensities,
286
+ audioSeekSeconds: first.seekSeconds,
287
+ audioDurationSeconds: first.durationSeconds,
288
+ audioSegments,
289
+ runs,
290
+ };
291
+ }