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.
Files changed (50) hide show
  1. package/LICENSE +21 -21
  2. package/README.md +3 -2
  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 +34 -34
  15. package/dist/highlight.js +334 -334
  16. package/dist/image.d.ts +97 -92
  17. package/dist/image.js +545 -538
  18. package/dist/image.js.map +1 -1
  19. package/dist/index.d.ts +87 -79
  20. package/dist/index.js +597 -566
  21. package/dist/index.js.map +1 -1
  22. package/dist/subtitleConvert.d.ts +3 -3
  23. package/dist/subtitleConvert.js +61 -61
  24. package/dist/video.d.ts +150 -120
  25. package/dist/video.js +677 -582
  26. package/dist/video.js.map +1 -1
  27. package/dist/waitForEventLoop.d.ts +1 -1
  28. package/dist/waitForEventLoop.js +5 -5
  29. package/package.json +62 -62
  30. package/src/audio.ts +129 -129
  31. package/src/beats.ts +279 -279
  32. package/src/bpmEncoder.ts +97 -97
  33. package/src/config.ts +487 -476
  34. package/src/dsp.ts +95 -95
  35. package/src/highlight.ts +291 -291
  36. package/src/image.ts +707 -700
  37. package/src/index.ts +79 -37
  38. package/src/subtitleConvert.ts +63 -63
  39. package/src/tests/audio.test.ts +102 -102
  40. package/src/tests/beats.test.ts +134 -134
  41. package/src/tests/config.test.ts +534 -496
  42. package/src/tests/dsp.test.ts +24 -24
  43. package/src/tests/highlight.test.ts +178 -178
  44. package/src/tests/image.test.ts +319 -308
  45. package/src/tests/index.test.ts +180 -180
  46. package/src/tests/subtitleConvert.test.ts +23 -23
  47. package/src/tests/video.test.ts +535 -447
  48. package/src/typings.d.ts +28 -28
  49. package/src/video.ts +856 -722
  50. package/src/waitForEventLoop.ts +2 -2
package/src/highlight.ts CHANGED
@@ -1,291 +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
- 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
- }
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
+ }