nodejs-audio-visualizer 5.5.1 → 5.5.3
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/dist/beats.d.ts +4 -9
- package/dist/beats.js +65 -158
- package/dist/beats.js.map +1 -1
- package/dist/video.js +1 -2
- package/dist/video.js.map +1 -1
- package/package.json +3 -2
- package/src/beats.ts +70 -187
- package/src/tests/video.test.ts +652 -652
- package/src/typings.d.ts +22 -0
- package/src/video.ts +947 -948
package/src/beats.ts
CHANGED
|
@@ -1,3 +1,5 @@
|
|
|
1
|
+
import MusicTempo from 'music-tempo';
|
|
2
|
+
|
|
1
3
|
export interface BeatInfo {
|
|
2
4
|
isBeat: boolean;
|
|
3
5
|
intensity: number;
|
|
@@ -26,26 +28,14 @@ export interface TempoEstimate {
|
|
|
26
28
|
beatsSec?: number[];
|
|
27
29
|
}
|
|
28
30
|
|
|
29
|
-
|
|
30
|
-
export const MIN_TEMPO_BPM = 90;
|
|
31
|
+
export const MIN_TEMPO_BPM = 70;
|
|
31
32
|
export const MAX_TEMPO_BPM = 180;
|
|
33
|
+
export const PREFERRED_TEMPO_BPM_MIN = 90;
|
|
34
|
+
export const PREFERRED_TEMPO_BPM_MAX = 160;
|
|
32
35
|
export const DEFAULT_BASS_END_INDEX = 8;
|
|
33
|
-
/** Longest PCM window
|
|
36
|
+
/** Longest PCM window passed to music-tempo (seconds), taken from the start of the audio. */
|
|
34
37
|
export const MAX_TEMPO_ANALYSIS_SECONDS = 90;
|
|
35
|
-
|
|
36
|
-
const TEMPO_LOW_PASS_HZ = 150;
|
|
37
|
-
const TEMPO_HIGH_PASS_HZ = 100;
|
|
38
|
-
/** Biquad Q in dB, as in the Web Audio BiquadFilterNode. */
|
|
39
|
-
const TEMPO_FILTER_Q_DB = 1;
|
|
40
|
-
/** Audio is split into parts of this length; each part contributes its loudest sample as a peak. */
|
|
41
|
-
const TEMPO_PART_SECONDS = 0.5;
|
|
42
|
-
/** Each peak is compared with this many following peaks to build intervals. */
|
|
43
|
-
const TEMPO_INTERVAL_NEIGHBOURS = 9;
|
|
44
|
-
/** Peaks are moved left by this share of a beat to land on the start of the hit, not its maximum. */
|
|
45
|
-
const TEMPO_PEAK_LEFT_SHIFT_SHARE = 0.05;
|
|
46
|
-
/** Peaks whose beat offset is within this many seconds of the loudest peak's offset set the phase. */
|
|
47
|
-
const TEMPO_OFFSET_TOLERANCE_SEC = 0.05;
|
|
48
|
-
const SILENCE_THRESHOLD = 1e-4;
|
|
38
|
+
const TEMPO_TIME_STEP = 0.01;
|
|
49
39
|
|
|
50
40
|
export const frameBassEnergy = (spectrum: number[], bassEndIndex = DEFAULT_BASS_END_INDEX): number => {
|
|
51
41
|
const end = Math.min(bassEndIndex, spectrum.length);
|
|
@@ -108,6 +98,29 @@ export const createBeatDetector = (fps: number, options?: BeatDetectorOptions) =
|
|
|
108
98
|
};
|
|
109
99
|
};
|
|
110
100
|
|
|
101
|
+
const foldTempoBpm = (bpm: number): number => {
|
|
102
|
+
let folded = bpm;
|
|
103
|
+
while (folded > MAX_TEMPO_BPM && folded / 2 >= MIN_TEMPO_BPM) {
|
|
104
|
+
folded /= 2;
|
|
105
|
+
}
|
|
106
|
+
while (folded < MIN_TEMPO_BPM && folded * 2 <= MAX_TEMPO_BPM) {
|
|
107
|
+
folded *= 2;
|
|
108
|
+
}
|
|
109
|
+
return folded;
|
|
110
|
+
};
|
|
111
|
+
|
|
112
|
+
const samplesToArray = (samples: ArrayLike<number>, maxLength: number): number[] => {
|
|
113
|
+
const n = Math.min(samples.length, maxLength);
|
|
114
|
+
if (Array.isArray(samples) && n === samples.length) {
|
|
115
|
+
return samples;
|
|
116
|
+
}
|
|
117
|
+
const audioData = new Array<number>(n);
|
|
118
|
+
for (let i = 0; i < n; i++) {
|
|
119
|
+
audioData[i] = samples[i];
|
|
120
|
+
}
|
|
121
|
+
return audioData;
|
|
122
|
+
};
|
|
123
|
+
|
|
111
124
|
const wrapIntoPeriod = (value: number, period: number): number => {
|
|
112
125
|
if (!(period > 0) || !isFinite(value)) {
|
|
113
126
|
return 0;
|
|
@@ -137,154 +150,6 @@ const circularMeanPhase = (frames: number[], period: number): number => {
|
|
|
137
150
|
return wrapIntoPeriod(phase, period);
|
|
138
151
|
};
|
|
139
152
|
|
|
140
|
-
/** Signed distance from `value` to `reference` on a circle of length `period`, in [-period/2, period/2). */
|
|
141
|
-
const circularDiff = (value: number, reference: number, period: number): number =>
|
|
142
|
-
wrapIntoPeriod(value - reference + period / 2, period) - period / 2;
|
|
143
|
-
|
|
144
|
-
type BiquadType = 'lowpass' | 'highpass';
|
|
145
|
-
|
|
146
|
-
/** Biquad filter with the Web Audio BiquadFilterNode (RBJ cookbook) coefficients. */
|
|
147
|
-
const biquadFilter = (
|
|
148
|
-
samples: ArrayLike<number>,
|
|
149
|
-
length: number,
|
|
150
|
-
sampleRate: number,
|
|
151
|
-
type: BiquadType,
|
|
152
|
-
frequency: number,
|
|
153
|
-
qDb: number,
|
|
154
|
-
): Float32Array => {
|
|
155
|
-
const w0 = 2 * Math.PI * Math.min(frequency, sampleRate / 2 - 1) / sampleRate;
|
|
156
|
-
const cosW0 = Math.cos(w0);
|
|
157
|
-
const alpha = Math.sin(w0) / (2 * Math.pow(10, qDb / 20));
|
|
158
|
-
const a0 = 1 + alpha;
|
|
159
|
-
const b1 = (type === 'lowpass' ? 1 - cosW0 : -(1 + cosW0)) / a0;
|
|
160
|
-
const b0 = (type === 'lowpass' ? b1 / 2 : -b1 / 2);
|
|
161
|
-
const b2 = b0;
|
|
162
|
-
const a1 = (-2 * cosW0) / a0;
|
|
163
|
-
const a2 = (1 - alpha) / a0;
|
|
164
|
-
|
|
165
|
-
const out = new Float32Array(length);
|
|
166
|
-
let x1 = 0;
|
|
167
|
-
let x2 = 0;
|
|
168
|
-
let y1 = 0;
|
|
169
|
-
let y2 = 0;
|
|
170
|
-
for (let i = 0; i < length; i++) {
|
|
171
|
-
const x = samples[i];
|
|
172
|
-
const y = b0 * x + b1 * x1 + b2 * x2 - a1 * y1 - a2 * y2;
|
|
173
|
-
out[i] = y;
|
|
174
|
-
x2 = x1;
|
|
175
|
-
x1 = x;
|
|
176
|
-
y2 = y1;
|
|
177
|
-
y1 = y;
|
|
178
|
-
}
|
|
179
|
-
return out;
|
|
180
|
-
};
|
|
181
|
-
|
|
182
|
-
interface Peak {
|
|
183
|
-
position: number;
|
|
184
|
-
volume: number;
|
|
185
|
-
}
|
|
186
|
-
|
|
187
|
-
/** Loudest sample of each part (`getVolume` picks how a sample is measured). */
|
|
188
|
-
const partPeaks = (
|
|
189
|
-
data: Float32Array,
|
|
190
|
-
partSize: number,
|
|
191
|
-
getVolume: (sample: number) => number,
|
|
192
|
-
positionShift = 0,
|
|
193
|
-
): Peak[] => {
|
|
194
|
-
const peaks: Peak[] = [];
|
|
195
|
-
for (let start = 0; start < data.length; start += partSize) {
|
|
196
|
-
const end = Math.min(start + partSize, data.length);
|
|
197
|
-
let max: Peak | null = null;
|
|
198
|
-
for (let j = start; j < end; j++) {
|
|
199
|
-
const volume = getVolume(data[j]);
|
|
200
|
-
if (!max || volume > max.volume) {
|
|
201
|
-
max = { position: j - positionShift, volume };
|
|
202
|
-
}
|
|
203
|
-
}
|
|
204
|
-
if (max) {
|
|
205
|
-
peaks.push(max);
|
|
206
|
-
}
|
|
207
|
-
}
|
|
208
|
-
return peaks;
|
|
209
|
-
};
|
|
210
|
-
|
|
211
|
-
/** Loudest half of the per-part peaks, in playback order. */
|
|
212
|
-
const getLoudPeaks = (data: Float32Array, partSize: number): Peak[] => {
|
|
213
|
-
const peaks = partPeaks(data, partSize, Math.abs)
|
|
214
|
-
.sort((a, b) => b.volume - a.volume);
|
|
215
|
-
return peaks
|
|
216
|
-
.slice(0, Math.floor(peaks.length / 2))
|
|
217
|
-
.filter(p => p.volume > SILENCE_THRESHOLD)
|
|
218
|
-
.sort((a, b) => a.position - b.position);
|
|
219
|
-
};
|
|
220
|
-
|
|
221
|
-
const foldTempoBpm = (bpm: number): number => {
|
|
222
|
-
let folded = bpm;
|
|
223
|
-
while (folded <= MIN_TEMPO_BPM) {
|
|
224
|
-
folded *= 2;
|
|
225
|
-
}
|
|
226
|
-
while (folded > MAX_TEMPO_BPM) {
|
|
227
|
-
folded /= 2;
|
|
228
|
-
}
|
|
229
|
-
return folded;
|
|
230
|
-
};
|
|
231
|
-
|
|
232
|
-
/** Most frequent tempo among the intervals between each peak and its following neighbours. */
|
|
233
|
-
const getMostFrequentTempo = (peaks: Peak[], sampleRate: number): number | null => {
|
|
234
|
-
const counts = new Map<number, number>();
|
|
235
|
-
peaks.forEach((peak, index) => {
|
|
236
|
-
for (let i = 1; index + i < peaks.length && i <= TEMPO_INTERVAL_NEIGHBOURS; i++) {
|
|
237
|
-
const distance = peaks[index + i].position - peak.position;
|
|
238
|
-
if (distance <= 0) {
|
|
239
|
-
continue;
|
|
240
|
-
}
|
|
241
|
-
const tempo = Math.round(foldTempoBpm((60 * sampleRate) / distance));
|
|
242
|
-
counts.set(tempo, (counts.get(tempo) ?? 0) + 1);
|
|
243
|
-
}
|
|
244
|
-
});
|
|
245
|
-
let best: number | null = null;
|
|
246
|
-
let bestCount = 0;
|
|
247
|
-
counts.forEach((count, tempo) => {
|
|
248
|
-
if (count > bestCount) {
|
|
249
|
-
best = tempo;
|
|
250
|
-
bestCount = count;
|
|
251
|
-
}
|
|
252
|
-
});
|
|
253
|
-
return best;
|
|
254
|
-
};
|
|
255
|
-
|
|
256
|
-
/**
|
|
257
|
-
* Beat offset in [0, beat) seconds: the loudest peak is taken as a strong beat, and the offsets
|
|
258
|
-
* of peaks that agree with it (within `TEMPO_OFFSET_TOLERANCE_SEC`) are averaged.
|
|
259
|
-
*/
|
|
260
|
-
const getBeatOffsetSec = (data: Float32Array, partSize: number, sampleRate: number, bpm: number): number => {
|
|
261
|
-
const beatSec = 60 / bpm;
|
|
262
|
-
const shift = Math.round(beatSec * TEMPO_PEAK_LEFT_SHIFT_SHARE * sampleRate);
|
|
263
|
-
const peaks = partPeaks(data, partSize, v => v, shift)
|
|
264
|
-
.filter(p => p.volume > SILENCE_THRESHOLD)
|
|
265
|
-
.sort((a, b) => b.volume - a.volume);
|
|
266
|
-
if (peaks.length === 0) {
|
|
267
|
-
return 0;
|
|
268
|
-
}
|
|
269
|
-
const refOffset = wrapIntoPeriod(peaks[0].position / sampleRate, beatSec);
|
|
270
|
-
let diffSum = 0;
|
|
271
|
-
let count = 0;
|
|
272
|
-
for (const peak of peaks) {
|
|
273
|
-
const diff = circularDiff(peak.position / sampleRate, refOffset, beatSec);
|
|
274
|
-
if (Math.abs(diff) < TEMPO_OFFSET_TOLERANCE_SEC) {
|
|
275
|
-
diffSum += diff;
|
|
276
|
-
count++;
|
|
277
|
-
}
|
|
278
|
-
}
|
|
279
|
-
return wrapIntoPeriod(refOffset + diffSum / count, beatSec);
|
|
280
|
-
};
|
|
281
|
-
|
|
282
|
-
/**
|
|
283
|
-
* Estimates the track tempo and beat phase (after BeatDetect.js by Arthur Beaulieu):
|
|
284
|
-
* band-pass the audio around the kick, take the loudest sample of every half second,
|
|
285
|
-
* keep the loudest half of those peaks, and pick the most frequent tempo among the intervals
|
|
286
|
-
* between neighbouring peaks. The beat phase comes from the loudest peak and the peaks aligned with it.
|
|
287
|
-
*/
|
|
288
153
|
export const estimateTempo = (
|
|
289
154
|
samples: ArrayLike<number>,
|
|
290
155
|
sampleRate: number,
|
|
@@ -294,31 +159,49 @@ export const estimateTempo = (
|
|
|
294
159
|
return null;
|
|
295
160
|
}
|
|
296
161
|
|
|
297
|
-
const
|
|
298
|
-
const
|
|
299
|
-
const
|
|
300
|
-
|
|
162
|
+
const maxSamples = Math.max(1, Math.floor(sampleRate * MAX_TEMPO_ANALYSIS_SECONDS));
|
|
163
|
+
const audioData = samplesToArray(samples, maxSamples);
|
|
164
|
+
const hopSize = Math.max(1, Math.round(sampleRate * TEMPO_TIME_STEP));
|
|
165
|
+
|
|
166
|
+
try {
|
|
167
|
+
const mt = new MusicTempo(audioData, {
|
|
168
|
+
hopSize,
|
|
169
|
+
timeStep: TEMPO_TIME_STEP,
|
|
170
|
+
minBeatInterval: 60 / MAX_TEMPO_BPM,
|
|
171
|
+
maxBeatInterval: 60 / MIN_TEMPO_BPM,
|
|
172
|
+
});
|
|
173
|
+
const beatInterval = Number(mt.beatInterval);
|
|
174
|
+
let bpm = Number(mt.tempo);
|
|
175
|
+
if (!(bpm > 0) && beatInterval > 0) {
|
|
176
|
+
bpm = 60 / beatInterval;
|
|
177
|
+
}
|
|
178
|
+
if (!(bpm > 0)) {
|
|
179
|
+
return null;
|
|
180
|
+
}
|
|
301
181
|
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
182
|
+
bpm = foldTempoBpm(bpm);
|
|
183
|
+
if (bpm < MIN_TEMPO_BPM * 0.9 || bpm > MAX_TEMPO_BPM * 1.1) {
|
|
184
|
+
return null;
|
|
185
|
+
}
|
|
306
186
|
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
187
|
+
const periodFrames = fps * 60 / bpm;
|
|
188
|
+
const beatsSec = (mt.beats ?? [])
|
|
189
|
+
.map(t => Number(t))
|
|
190
|
+
.filter(t => t >= 0 && isFinite(t));
|
|
191
|
+
const beatFrames = beatsSec.map(t => t * fps);
|
|
192
|
+
const phaseFrame = beatFrames.length > 0
|
|
193
|
+
? wrapIntoPeriod(circularMeanPhase(beatFrames, periodFrames), periodFrames)
|
|
194
|
+
: 0;
|
|
314
195
|
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
}
|
|
196
|
+
return {
|
|
197
|
+
bpm: Math.round(bpm),
|
|
198
|
+
periodFrames,
|
|
199
|
+
phaseFrame,
|
|
200
|
+
...(beatsSec.length > 0 ? { beatsSec } : {}),
|
|
201
|
+
};
|
|
202
|
+
} catch {
|
|
203
|
+
return null;
|
|
204
|
+
}
|
|
322
205
|
};
|
|
323
206
|
|
|
324
207
|
export const shiftTempoPhase = (
|