nodejs-audio-visualizer 4.2.4 → 4.2.6
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 +70 -70
- package/dist/audio.d.ts +12 -12
- package/dist/audio.js +120 -120
- package/dist/beats.d.ts +19 -19
- package/dist/beats.js +43 -43
- package/dist/bpmEncoder.d.ts +14 -14
- package/dist/bpmEncoder.js +97 -97
- package/dist/config.d.ts +54 -54
- package/dist/config.js +299 -299
- package/dist/dsp.d.ts +16 -16
- package/dist/dsp.js +76 -76
- package/dist/highlight.d.ts +32 -32
- package/dist/highlight.js +325 -325
- package/dist/image.d.ts +79 -79
- package/dist/image.js +458 -458
- package/dist/index.d.ts +67 -67
- package/dist/index.js +497 -466
- package/dist/index.js.map +1 -1
- package/dist/video.d.ts +67 -64
- package/dist/video.js +317 -293
- package/dist/video.js.map +1 -1
- package/dist/waitForEventLoop.d.ts +1 -1
- package/dist/waitForEventLoop.js +5 -5
- package/package.json +61 -61
- package/src/audio.ts +120 -120
- package/src/beats.ts +68 -68
- package/src/bpmEncoder.ts +97 -97
- package/src/config.ts +382 -382
- package/src/dsp.ts +95 -95
- package/src/highlight.ts +284 -284
- package/src/image.ts +602 -602
- package/src/index.ts +631 -606
- package/src/tests/audio.test.ts +96 -96
- package/src/tests/config.test.ts +406 -406
- package/src/tests/dsp.test.ts +24 -24
- package/src/tests/highlight.test.ts +178 -178
- package/src/tests/image.test.ts +222 -222
- package/src/tests/index.test.ts +63 -63
- package/src/tests/video.test.ts +71 -71
- package/src/typings.d.ts +6 -6
- package/src/video.ts +403 -375
- package/src/waitForEventLoop.ts +2 -2
package/src/highlight.ts
CHANGED
|
@@ -1,284 +1,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
|
-
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
|
+
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
|
+
}
|