nodejs-audio-visualizer 5.5.2 → 5.6.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/LICENSE +21 -21
- package/README.md +84 -83
- package/dist/audio.d.ts +13 -13
- package/dist/audio.js +128 -128
- package/dist/beats.d.ts +39 -44
- package/dist/beats.js +220 -313
- package/dist/beats.js.map +1 -1
- package/dist/bpmEncoder.d.ts +14 -14
- package/dist/bpmEncoder.js +97 -97
- package/dist/config.d.ts +81 -80
- package/dist/config.js +392 -391
- package/dist/config.js.map +1 -1
- package/dist/dsp.d.ts +16 -16
- package/dist/dsp.js +76 -76
- package/dist/highlight.d.ts +91 -91
- package/dist/highlight.js +606 -606
- package/dist/image.d.ts +107 -97
- package/dist/image.js +614 -545
- package/dist/image.js.map +1 -1
- package/dist/index.d.ts +98 -96
- package/dist/index.js +673 -667
- package/dist/index.js.map +1 -1
- package/dist/subtitleConvert.d.ts +3 -3
- package/dist/subtitleConvert.js +61 -61
- package/dist/video.d.ts +171 -171
- package/dist/video.js +743 -743
- package/dist/waitForEventLoop.d.ts +1 -1
- package/dist/waitForEventLoop.js +5 -5
- package/package.json +62 -61
- package/src/audio.ts +129 -129
- package/src/beats.ts +279 -396
- package/src/bpmEncoder.ts +97 -97
- package/src/config.ts +504 -501
- package/src/dsp.ts +95 -95
- package/src/highlight.ts +617 -617
- package/src/image.ts +791 -707
- package/src/index.ts +896 -885
- package/src/subtitleConvert.ts +63 -63
- package/src/tests/audio.test.ts +102 -102
- package/src/tests/beats.test.ts +134 -134
- package/src/tests/config.test.ts +568 -553
- package/src/tests/dsp.test.ts +24 -24
- package/src/tests/highlight.test.ts +397 -397
- package/src/tests/image.test.ts +371 -319
- package/src/tests/index.test.ts +180 -180
- package/src/tests/subtitleConvert.test.ts +23 -23
- package/src/typings.d.ts +28 -6
- package/src/waitForEventLoop.ts +2 -2
package/src/subtitleConvert.ts
CHANGED
|
@@ -1,63 +1,63 @@
|
|
|
1
|
-
/** LRC timestamp line: [mm:ss.xx] text — hundredths optional after dot. */
|
|
2
|
-
const LRC_LINE = /^\[(\d+):(\d+)(?:\.(\d+))?\]\s*(.*)$/;
|
|
3
|
-
|
|
4
|
-
export const isLikelyLrc = (text: string): boolean => {
|
|
5
|
-
const body = text.replace(/^\uFEFF/, '');
|
|
6
|
-
for (const line of body.split(/\r?\n/)) {
|
|
7
|
-
const t = line.trim();
|
|
8
|
-
if (t.length === 0) {
|
|
9
|
-
continue;
|
|
10
|
-
}
|
|
11
|
-
return LRC_LINE.test(t);
|
|
12
|
-
}
|
|
13
|
-
return false;
|
|
14
|
-
};
|
|
15
|
-
|
|
16
|
-
const msToSrtTime = (ms: number): string => {
|
|
17
|
-
const h = Math.floor(ms / 3600000);
|
|
18
|
-
const m = Math.floor((ms % 3600000) / 60000);
|
|
19
|
-
const s = Math.floor((ms % 60000) / 1000);
|
|
20
|
-
const milli = ms % 1000;
|
|
21
|
-
const pad = (n: number, w: number) => String(n).padStart(w, '0');
|
|
22
|
-
return `${pad(h, 2)}:${pad(m, 2)}:${pad(s, 2)},${pad(milli, 3)}`;
|
|
23
|
-
};
|
|
24
|
-
|
|
25
|
-
const DEFAULT_TAIL_MS = 2000;
|
|
26
|
-
|
|
27
|
-
export const lrcToSrt = (lrc: string): string => {
|
|
28
|
-
const body = lrc.replace(/^\uFEFF/, '');
|
|
29
|
-
const entries: { startMs: number; text: string }[] = [];
|
|
30
|
-
for (const line of body.split(/\r?\n/)) {
|
|
31
|
-
const match = line.trim().match(LRC_LINE);
|
|
32
|
-
if (!match) {
|
|
33
|
-
continue;
|
|
34
|
-
}
|
|
35
|
-
const min = parseInt(match[1], 10);
|
|
36
|
-
const sec = parseInt(match[2], 10);
|
|
37
|
-
const centisec =
|
|
38
|
-
match[3] !== undefined
|
|
39
|
-
? parseInt(match[3].padEnd(2, '0').slice(0, 2), 10)
|
|
40
|
-
: 0;
|
|
41
|
-
const startMs = (min * 60 + sec) * 1000 + centisec * 10;
|
|
42
|
-
const text = (match[4] || '').trim();
|
|
43
|
-
if (text.length > 0) {
|
|
44
|
-
entries.push({ startMs, text });
|
|
45
|
-
}
|
|
46
|
-
}
|
|
47
|
-
entries.sort((a, b) => a.startMs - b.startMs);
|
|
48
|
-
let out = '';
|
|
49
|
-
for (let i = 0; i < entries.length; i++) {
|
|
50
|
-
const start = entries[i].startMs;
|
|
51
|
-
const end =
|
|
52
|
-
i + 1 < entries.length
|
|
53
|
-
? Math.max(entries[i + 1].startMs, start + 1)
|
|
54
|
-
: start + DEFAULT_TAIL_MS;
|
|
55
|
-
out += `${i + 1}\n`;
|
|
56
|
-
out += `${msToSrtTime(start)} --> ${msToSrtTime(end)}\n`;
|
|
57
|
-
out += `${entries[i].text}\n\n`;
|
|
58
|
-
}
|
|
59
|
-
return out.length > 0 ? out.replace(/\n*$/, '\n') : '';
|
|
60
|
-
};
|
|
61
|
-
|
|
62
|
-
export const normalizeInlineSubtitlesToSrt = (text: string): string =>
|
|
63
|
-
isLikelyLrc(text) ? lrcToSrt(text) : text;
|
|
1
|
+
/** LRC timestamp line: [mm:ss.xx] text — hundredths optional after dot. */
|
|
2
|
+
const LRC_LINE = /^\[(\d+):(\d+)(?:\.(\d+))?\]\s*(.*)$/;
|
|
3
|
+
|
|
4
|
+
export const isLikelyLrc = (text: string): boolean => {
|
|
5
|
+
const body = text.replace(/^\uFEFF/, '');
|
|
6
|
+
for (const line of body.split(/\r?\n/)) {
|
|
7
|
+
const t = line.trim();
|
|
8
|
+
if (t.length === 0) {
|
|
9
|
+
continue;
|
|
10
|
+
}
|
|
11
|
+
return LRC_LINE.test(t);
|
|
12
|
+
}
|
|
13
|
+
return false;
|
|
14
|
+
};
|
|
15
|
+
|
|
16
|
+
const msToSrtTime = (ms: number): string => {
|
|
17
|
+
const h = Math.floor(ms / 3600000);
|
|
18
|
+
const m = Math.floor((ms % 3600000) / 60000);
|
|
19
|
+
const s = Math.floor((ms % 60000) / 1000);
|
|
20
|
+
const milli = ms % 1000;
|
|
21
|
+
const pad = (n: number, w: number) => String(n).padStart(w, '0');
|
|
22
|
+
return `${pad(h, 2)}:${pad(m, 2)}:${pad(s, 2)},${pad(milli, 3)}`;
|
|
23
|
+
};
|
|
24
|
+
|
|
25
|
+
const DEFAULT_TAIL_MS = 2000;
|
|
26
|
+
|
|
27
|
+
export const lrcToSrt = (lrc: string): string => {
|
|
28
|
+
const body = lrc.replace(/^\uFEFF/, '');
|
|
29
|
+
const entries: { startMs: number; text: string }[] = [];
|
|
30
|
+
for (const line of body.split(/\r?\n/)) {
|
|
31
|
+
const match = line.trim().match(LRC_LINE);
|
|
32
|
+
if (!match) {
|
|
33
|
+
continue;
|
|
34
|
+
}
|
|
35
|
+
const min = parseInt(match[1], 10);
|
|
36
|
+
const sec = parseInt(match[2], 10);
|
|
37
|
+
const centisec =
|
|
38
|
+
match[3] !== undefined
|
|
39
|
+
? parseInt(match[3].padEnd(2, '0').slice(0, 2), 10)
|
|
40
|
+
: 0;
|
|
41
|
+
const startMs = (min * 60 + sec) * 1000 + centisec * 10;
|
|
42
|
+
const text = (match[4] || '').trim();
|
|
43
|
+
if (text.length > 0) {
|
|
44
|
+
entries.push({ startMs, text });
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
entries.sort((a, b) => a.startMs - b.startMs);
|
|
48
|
+
let out = '';
|
|
49
|
+
for (let i = 0; i < entries.length; i++) {
|
|
50
|
+
const start = entries[i].startMs;
|
|
51
|
+
const end =
|
|
52
|
+
i + 1 < entries.length
|
|
53
|
+
? Math.max(entries[i + 1].startMs, start + 1)
|
|
54
|
+
: start + DEFAULT_TAIL_MS;
|
|
55
|
+
out += `${i + 1}\n`;
|
|
56
|
+
out += `${msToSrtTime(start)} --> ${msToSrtTime(end)}\n`;
|
|
57
|
+
out += `${entries[i].text}\n\n`;
|
|
58
|
+
}
|
|
59
|
+
return out.length > 0 ? out.replace(/\n*$/, '\n') : '';
|
|
60
|
+
};
|
|
61
|
+
|
|
62
|
+
export const normalizeInlineSubtitlesToSrt = (text: string): string =>
|
|
63
|
+
isLikelyLrc(text) ? lrcToSrt(text) : text;
|
package/src/tests/audio.test.ts
CHANGED
|
@@ -1,102 +1,102 @@
|
|
|
1
|
-
import { expect } from 'chai';
|
|
2
|
-
import { Readable, Writable, Pipe } from 'stream';
|
|
3
|
-
import child_process, { ChildProcessWithoutNullStreams } from 'child_process';
|
|
4
|
-
import {
|
|
5
|
-
bufferToUInt8,
|
|
6
|
-
normalizeAudioData,
|
|
7
|
-
pcmU8ToFloatSamples,
|
|
8
|
-
spawnFfmpegAudioReader,
|
|
9
|
-
skipEvery,
|
|
10
|
-
getPeaks,
|
|
11
|
-
correctPeaks,
|
|
12
|
-
createSpectrumsProcessor,
|
|
13
|
-
} from '../audio';
|
|
14
|
-
import { createSandbox } from 'sinon';
|
|
15
|
-
|
|
16
|
-
let childProcessStream = {
|
|
17
|
-
stdin: new Writable(),
|
|
18
|
-
stderr: new Readable(),
|
|
19
|
-
};
|
|
20
|
-
|
|
21
|
-
const videoSandbox = createSandbox();
|
|
22
|
-
|
|
23
|
-
describe('audio', function () {
|
|
24
|
-
|
|
25
|
-
this.beforeAll(function () {
|
|
26
|
-
videoSandbox.stub(child_process, 'spawn').returns(childProcessStream as ChildProcessWithoutNullStreams);
|
|
27
|
-
});
|
|
28
|
-
|
|
29
|
-
this.afterAll(function () {
|
|
30
|
-
videoSandbox.restore();
|
|
31
|
-
});
|
|
32
|
-
|
|
33
|
-
it('bufferToUInt8', function () {
|
|
34
|
-
const expected = [1, 2, 3, 4, 5];
|
|
35
|
-
const buffer = Buffer.from(new Uint8Array(expected));
|
|
36
|
-
const result = bufferToUInt8(buffer, 0, 5);
|
|
37
|
-
expect(result).deep.equal(expected);
|
|
38
|
-
});
|
|
39
|
-
|
|
40
|
-
it('normalizeAudioData', function () {
|
|
41
|
-
const result = normalizeAudioData([4, 5, 23, 78, 2]);
|
|
42
|
-
const expected = [-0.96875, -0.9609375, -0.8203125, -0.390625, -0.984375];
|
|
43
|
-
expect(result).deep.equal(expected);
|
|
44
|
-
});
|
|
45
|
-
|
|
46
|
-
it('pcmU8ToFloatSamples', function () {
|
|
47
|
-
const buffer = Buffer.from([128, 255, 0]);
|
|
48
|
-
expect(pcmU8ToFloatSamples(buffer)).deep.equal([0, 127 / 128, -1]);
|
|
49
|
-
});
|
|
50
|
-
|
|
51
|
-
it('spawnFfmpegAudioReader', function (done) {
|
|
52
|
-
const childProcessReadableStream = new Readable();
|
|
53
|
-
const childProcessWritableStream = new Writable();
|
|
54
|
-
childProcessReadableStream._read = () => { done(); };
|
|
55
|
-
(<Pipe>childProcessWritableStream.pipe) = () => childProcessWritableStream;
|
|
56
|
-
|
|
57
|
-
childProcessStream.stdin = childProcessWritableStream;
|
|
58
|
-
childProcessStream.stderr = childProcessReadableStream;
|
|
59
|
-
|
|
60
|
-
const ffmpegProcess = spawnFfmpegAudioReader('filename', 'format');
|
|
61
|
-
ffmpegProcess.stderr.on('data', (data) => {
|
|
62
|
-
expect(data).equal('some data');
|
|
63
|
-
});
|
|
64
|
-
|
|
65
|
-
childProcessStream.stderr.emit('data', 'some data');
|
|
66
|
-
});
|
|
67
|
-
|
|
68
|
-
it('skipEvery', () => {
|
|
69
|
-
const sampleData = [1, 2, 3, 4];
|
|
70
|
-
const result = sampleData.filter(skipEvery(2));
|
|
71
|
-
const expected = [1, 3];
|
|
72
|
-
expect(result).deep.equal(expected);
|
|
73
|
-
});
|
|
74
|
-
|
|
75
|
-
it('getPeaks', () => {
|
|
76
|
-
const sampleData = [1, 2, 3, 4];
|
|
77
|
-
const prevPeaks = [2, 2, 2, 5];
|
|
78
|
-
const resultNoPrevPeaks = getPeaks(sampleData);
|
|
79
|
-
expect(resultNoPrevPeaks).deep.equal(sampleData);
|
|
80
|
-
const resultWithPrevPeaks = getPeaks(sampleData, prevPeaks);
|
|
81
|
-
const expectedWithPrevPeaks = [2, 2, 3, 5];
|
|
82
|
-
expect(resultWithPrevPeaks).deep.equal(expectedWithPrevPeaks);
|
|
83
|
-
});
|
|
84
|
-
|
|
85
|
-
it('correctPeaks', () => {
|
|
86
|
-
const spectrums = [1, 2, 3, 4];
|
|
87
|
-
const peaks = [2, 2, 3, 5];
|
|
88
|
-
const result = correctPeaks(spectrums, peaks);
|
|
89
|
-
const expected = [1 / 3, 2 / 3, 1, 0.8];
|
|
90
|
-
expect(result).deep.equal(expected);
|
|
91
|
-
});
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
it('createSpectrumsProcessor', () => {
|
|
95
|
-
const spectrumsProcessor = createSpectrumsProcessor(44100);
|
|
96
|
-
|
|
97
|
-
const frame0 = spectrumsProcessor(() => ([1, 2, 3, 4]));
|
|
98
|
-
expect(frame0).to.have.length(24);
|
|
99
|
-
const frame1 = spectrumsProcessor(() => ([2, 2, 6, 2]));
|
|
100
|
-
expect(frame1).to.have.length(24);
|
|
101
|
-
});
|
|
102
|
-
});
|
|
1
|
+
import { expect } from 'chai';
|
|
2
|
+
import { Readable, Writable, Pipe } from 'stream';
|
|
3
|
+
import child_process, { ChildProcessWithoutNullStreams } from 'child_process';
|
|
4
|
+
import {
|
|
5
|
+
bufferToUInt8,
|
|
6
|
+
normalizeAudioData,
|
|
7
|
+
pcmU8ToFloatSamples,
|
|
8
|
+
spawnFfmpegAudioReader,
|
|
9
|
+
skipEvery,
|
|
10
|
+
getPeaks,
|
|
11
|
+
correctPeaks,
|
|
12
|
+
createSpectrumsProcessor,
|
|
13
|
+
} from '../audio';
|
|
14
|
+
import { createSandbox } from 'sinon';
|
|
15
|
+
|
|
16
|
+
let childProcessStream = {
|
|
17
|
+
stdin: new Writable(),
|
|
18
|
+
stderr: new Readable(),
|
|
19
|
+
};
|
|
20
|
+
|
|
21
|
+
const videoSandbox = createSandbox();
|
|
22
|
+
|
|
23
|
+
describe('audio', function () {
|
|
24
|
+
|
|
25
|
+
this.beforeAll(function () {
|
|
26
|
+
videoSandbox.stub(child_process, 'spawn').returns(childProcessStream as ChildProcessWithoutNullStreams);
|
|
27
|
+
});
|
|
28
|
+
|
|
29
|
+
this.afterAll(function () {
|
|
30
|
+
videoSandbox.restore();
|
|
31
|
+
});
|
|
32
|
+
|
|
33
|
+
it('bufferToUInt8', function () {
|
|
34
|
+
const expected = [1, 2, 3, 4, 5];
|
|
35
|
+
const buffer = Buffer.from(new Uint8Array(expected));
|
|
36
|
+
const result = bufferToUInt8(buffer, 0, 5);
|
|
37
|
+
expect(result).deep.equal(expected);
|
|
38
|
+
});
|
|
39
|
+
|
|
40
|
+
it('normalizeAudioData', function () {
|
|
41
|
+
const result = normalizeAudioData([4, 5, 23, 78, 2]);
|
|
42
|
+
const expected = [-0.96875, -0.9609375, -0.8203125, -0.390625, -0.984375];
|
|
43
|
+
expect(result).deep.equal(expected);
|
|
44
|
+
});
|
|
45
|
+
|
|
46
|
+
it('pcmU8ToFloatSamples', function () {
|
|
47
|
+
const buffer = Buffer.from([128, 255, 0]);
|
|
48
|
+
expect(pcmU8ToFloatSamples(buffer)).deep.equal([0, 127 / 128, -1]);
|
|
49
|
+
});
|
|
50
|
+
|
|
51
|
+
it('spawnFfmpegAudioReader', function (done) {
|
|
52
|
+
const childProcessReadableStream = new Readable();
|
|
53
|
+
const childProcessWritableStream = new Writable();
|
|
54
|
+
childProcessReadableStream._read = () => { done(); };
|
|
55
|
+
(<Pipe>childProcessWritableStream.pipe) = () => childProcessWritableStream;
|
|
56
|
+
|
|
57
|
+
childProcessStream.stdin = childProcessWritableStream;
|
|
58
|
+
childProcessStream.stderr = childProcessReadableStream;
|
|
59
|
+
|
|
60
|
+
const ffmpegProcess = spawnFfmpegAudioReader('filename', 'format');
|
|
61
|
+
ffmpegProcess.stderr.on('data', (data) => {
|
|
62
|
+
expect(data).equal('some data');
|
|
63
|
+
});
|
|
64
|
+
|
|
65
|
+
childProcessStream.stderr.emit('data', 'some data');
|
|
66
|
+
});
|
|
67
|
+
|
|
68
|
+
it('skipEvery', () => {
|
|
69
|
+
const sampleData = [1, 2, 3, 4];
|
|
70
|
+
const result = sampleData.filter(skipEvery(2));
|
|
71
|
+
const expected = [1, 3];
|
|
72
|
+
expect(result).deep.equal(expected);
|
|
73
|
+
});
|
|
74
|
+
|
|
75
|
+
it('getPeaks', () => {
|
|
76
|
+
const sampleData = [1, 2, 3, 4];
|
|
77
|
+
const prevPeaks = [2, 2, 2, 5];
|
|
78
|
+
const resultNoPrevPeaks = getPeaks(sampleData);
|
|
79
|
+
expect(resultNoPrevPeaks).deep.equal(sampleData);
|
|
80
|
+
const resultWithPrevPeaks = getPeaks(sampleData, prevPeaks);
|
|
81
|
+
const expectedWithPrevPeaks = [2, 2, 3, 5];
|
|
82
|
+
expect(resultWithPrevPeaks).deep.equal(expectedWithPrevPeaks);
|
|
83
|
+
});
|
|
84
|
+
|
|
85
|
+
it('correctPeaks', () => {
|
|
86
|
+
const spectrums = [1, 2, 3, 4];
|
|
87
|
+
const peaks = [2, 2, 3, 5];
|
|
88
|
+
const result = correctPeaks(spectrums, peaks);
|
|
89
|
+
const expected = [1 / 3, 2 / 3, 1, 0.8];
|
|
90
|
+
expect(result).deep.equal(expected);
|
|
91
|
+
});
|
|
92
|
+
|
|
93
|
+
|
|
94
|
+
it('createSpectrumsProcessor', () => {
|
|
95
|
+
const spectrumsProcessor = createSpectrumsProcessor(44100);
|
|
96
|
+
|
|
97
|
+
const frame0 = spectrumsProcessor(() => ([1, 2, 3, 4]));
|
|
98
|
+
expect(frame0).to.have.length(24);
|
|
99
|
+
const frame1 = spectrumsProcessor(() => ([2, 2, 6, 2]));
|
|
100
|
+
expect(frame1).to.have.length(24);
|
|
101
|
+
});
|
|
102
|
+
});
|
package/src/tests/beats.test.ts
CHANGED
|
@@ -1,134 +1,134 @@
|
|
|
1
|
-
import { expect } from 'chai';
|
|
2
|
-
import { estimateTempo, beatGridFrameIndices, shiftTempoPhase, tempoForWindow } from '../beats';
|
|
3
|
-
|
|
4
|
-
const clickTrain = (
|
|
5
|
-
sampleRate: number,
|
|
6
|
-
bpm: number,
|
|
7
|
-
durationSec: number,
|
|
8
|
-
phaseSec = 0,
|
|
9
|
-
halfPeriodGhost = 0,
|
|
10
|
-
): number[] => {
|
|
11
|
-
const n = Math.round(sampleRate * durationSec);
|
|
12
|
-
const samples = new Array<number>(n).fill(0);
|
|
13
|
-
const period = 60 / bpm;
|
|
14
|
-
const clickLen = Math.round(sampleRate * 0.01);
|
|
15
|
-
const addClick = (timeSec: number, amplitude: number) => {
|
|
16
|
-
const start = Math.round(timeSec * sampleRate);
|
|
17
|
-
for (let i = 0; i < clickLen && start + i < n; i++) {
|
|
18
|
-
const env = Math.exp(-i / (clickLen * 0.25));
|
|
19
|
-
samples[start + i] += amplitude * env * (i % 2 === 0 ? 1 : -0.7);
|
|
20
|
-
}
|
|
21
|
-
};
|
|
22
|
-
for (let t = phaseSec; t < durationSec; t += period) {
|
|
23
|
-
addClick(t, 1);
|
|
24
|
-
if (halfPeriodGhost > 0) {
|
|
25
|
-
addClick(t + period / 2, halfPeriodGhost);
|
|
26
|
-
}
|
|
27
|
-
}
|
|
28
|
-
return samples;
|
|
29
|
-
};
|
|
30
|
-
|
|
31
|
-
describe('beats', function () {
|
|
32
|
-
this.timeout(10000);
|
|
33
|
-
|
|
34
|
-
it('estimateTempo detects 128 BPM from a click train', function () {
|
|
35
|
-
const sampleRate = 22050;
|
|
36
|
-
const fps = 30;
|
|
37
|
-
const tempo = estimateTempo(clickTrain(sampleRate, 128, 8), sampleRate, fps);
|
|
38
|
-
expect(tempo).not.equal(null);
|
|
39
|
-
expect(tempo!.bpm).closeTo(128, 8);
|
|
40
|
-
expect(tempo!.periodFrames).closeTo(fps * 60 / 128, 1);
|
|
41
|
-
});
|
|
42
|
-
|
|
43
|
-
it('estimateTempo recovers phase of the click train', function () {
|
|
44
|
-
const sampleRate = 22050;
|
|
45
|
-
const fps = 30;
|
|
46
|
-
const phaseSec = 0.1;
|
|
47
|
-
const tempo = estimateTempo(clickTrain(sampleRate, 128, 8, phaseSec), sampleRate, fps);
|
|
48
|
-
expect(tempo).not.equal(null);
|
|
49
|
-
expect(tempo!.phaseFrame).closeTo(phaseSec * fps, 2);
|
|
50
|
-
});
|
|
51
|
-
|
|
52
|
-
it('estimateTempo prefers ~128 BPM when half-period ghosts are present', function () {
|
|
53
|
-
const sampleRate = 22050;
|
|
54
|
-
const fps = 30;
|
|
55
|
-
const tempo = estimateTempo(clickTrain(sampleRate, 128, 8, 0, 0.35), sampleRate, fps);
|
|
56
|
-
expect(tempo).not.equal(null);
|
|
57
|
-
expect(tempo!.bpm).closeTo(128, 8);
|
|
58
|
-
});
|
|
59
|
-
|
|
60
|
-
it('estimateTempo returns null for silence', function () {
|
|
61
|
-
const sampleRate = 22050;
|
|
62
|
-
const samples = new Array<number>(sampleRate * 2).fill(0);
|
|
63
|
-
expect(estimateTempo(samples, sampleRate, 30)).equal(null);
|
|
64
|
-
});
|
|
65
|
-
|
|
66
|
-
it('estimateTempo returns null for empty or invalid input', function () {
|
|
67
|
-
expect(estimateTempo([], 22050, 30)).equal(null);
|
|
68
|
-
expect(estimateTempo(clickTrain(22050, 128, 8), 0, 30)).equal(null);
|
|
69
|
-
expect(estimateTempo(clickTrain(22050, 128, 8), 22050, 0)).equal(null);
|
|
70
|
-
});
|
|
71
|
-
|
|
72
|
-
it('shiftTempoPhase wraps the beat grid into a later window', function () {
|
|
73
|
-
expect(shiftTempoPhase({ bpm: 120, periodFrames: 15, phaseFrame: 3 }, 0)).deep.equal({
|
|
74
|
-
bpm: 120,
|
|
75
|
-
periodFrames: 15,
|
|
76
|
-
phaseFrame: 3,
|
|
77
|
-
});
|
|
78
|
-
expect(shiftTempoPhase({ bpm: 120, periodFrames: 15, phaseFrame: 3 }, 3).phaseFrame).equal(0);
|
|
79
|
-
expect(shiftTempoPhase({ bpm: 120, periodFrames: 15, phaseFrame: 3 }, 18).phaseFrame).equal(0);
|
|
80
|
-
});
|
|
81
|
-
|
|
82
|
-
it('shiftTempoPhase keeps a non-integer period aligned in a late window', function () {
|
|
83
|
-
const tempo = { bpm: 128, periodFrames: 14.0625, phaseFrame: 3 };
|
|
84
|
-
const startFrame = 2000;
|
|
85
|
-
const shifted = shiftTempoPhase(tempo, startFrame);
|
|
86
|
-
const expected: number[] = [];
|
|
87
|
-
for (let n = 0; n < 400; n++) {
|
|
88
|
-
const local = Math.round(tempo.phaseFrame + n * tempo.periodFrames) - startFrame;
|
|
89
|
-
if (local > 0 && local < 60) {
|
|
90
|
-
expected.push(local);
|
|
91
|
-
}
|
|
92
|
-
}
|
|
93
|
-
expect(beatGridFrameIndices(shifted, 60)).deep.equal(expected);
|
|
94
|
-
});
|
|
95
|
-
|
|
96
|
-
it('tempoForWindow locks phase to beat times inside the edited window', function () {
|
|
97
|
-
const fps = 30;
|
|
98
|
-
const periodFrames = 15;
|
|
99
|
-
const startFrame = 90;
|
|
100
|
-
const tempo = {
|
|
101
|
-
bpm: 120,
|
|
102
|
-
periodFrames,
|
|
103
|
-
phaseFrame: 1,
|
|
104
|
-
beatsSec: [0.1, 3.2, 3.7, 4.2, 4.7],
|
|
105
|
-
};
|
|
106
|
-
const windowed = tempoForWindow(tempo, startFrame, fps, 60);
|
|
107
|
-
expect(windowed.phaseFrame).closeTo(6, 1);
|
|
108
|
-
expect(shiftTempoPhase(tempo, startFrame).phaseFrame).closeTo(1, 0.001);
|
|
109
|
-
expect(beatGridFrameIndices(windowed, 50)[0]).closeTo(6, 1);
|
|
110
|
-
expect(tempoForWindow(tempo, startFrame, fps, 60, [1, 16, 31]).phaseFrame).closeTo(6, 1);
|
|
111
|
-
});
|
|
112
|
-
|
|
113
|
-
it('tempoForWindow falls back to local onsets when beat times are outside the window', function () {
|
|
114
|
-
const tempo = {
|
|
115
|
-
bpm: 120,
|
|
116
|
-
periodFrames: 15,
|
|
117
|
-
phaseFrame: 1,
|
|
118
|
-
beatsSec: [0.1, 0.6, 1.1],
|
|
119
|
-
};
|
|
120
|
-
const windowed = tempoForWindow(tempo, 90, 30, 60, [7, 22, 37]);
|
|
121
|
-
expect(windowed.phaseFrame).closeTo(7, 1);
|
|
122
|
-
});
|
|
123
|
-
|
|
124
|
-
it('beatGridFrameIndices emits every beat and skips frame 0', function () {
|
|
125
|
-
expect(beatGridFrameIndices({ periodFrames: 14, phaseFrame: 0 }, 50)).deep.equal([14, 28, 42]);
|
|
126
|
-
expect(beatGridFrameIndices({ periodFrames: 14, phaseFrame: 3 }, 32)).deep.equal([3, 17, 31]);
|
|
127
|
-
expect(beatGridFrameIndices({ periodFrames: 14, phaseFrame: 0 }, 14)).deep.equal([]);
|
|
128
|
-
});
|
|
129
|
-
|
|
130
|
-
it('beatGridFrameIndices stride emits every Nth beat', function () {
|
|
131
|
-
expect(beatGridFrameIndices({ periodFrames: 14, phaseFrame: 0 }, 200, 4)).deep.equal([14, 70, 126, 182]);
|
|
132
|
-
expect(beatGridFrameIndices({ periodFrames: 14, phaseFrame: 3 }, 200, 4)).deep.equal([3, 59, 115, 171]);
|
|
133
|
-
});
|
|
134
|
-
});
|
|
1
|
+
import { expect } from 'chai';
|
|
2
|
+
import { estimateTempo, beatGridFrameIndices, shiftTempoPhase, tempoForWindow } from '../beats';
|
|
3
|
+
|
|
4
|
+
const clickTrain = (
|
|
5
|
+
sampleRate: number,
|
|
6
|
+
bpm: number,
|
|
7
|
+
durationSec: number,
|
|
8
|
+
phaseSec = 0,
|
|
9
|
+
halfPeriodGhost = 0,
|
|
10
|
+
): number[] => {
|
|
11
|
+
const n = Math.round(sampleRate * durationSec);
|
|
12
|
+
const samples = new Array<number>(n).fill(0);
|
|
13
|
+
const period = 60 / bpm;
|
|
14
|
+
const clickLen = Math.round(sampleRate * 0.01);
|
|
15
|
+
const addClick = (timeSec: number, amplitude: number) => {
|
|
16
|
+
const start = Math.round(timeSec * sampleRate);
|
|
17
|
+
for (let i = 0; i < clickLen && start + i < n; i++) {
|
|
18
|
+
const env = Math.exp(-i / (clickLen * 0.25));
|
|
19
|
+
samples[start + i] += amplitude * env * (i % 2 === 0 ? 1 : -0.7);
|
|
20
|
+
}
|
|
21
|
+
};
|
|
22
|
+
for (let t = phaseSec; t < durationSec; t += period) {
|
|
23
|
+
addClick(t, 1);
|
|
24
|
+
if (halfPeriodGhost > 0) {
|
|
25
|
+
addClick(t + period / 2, halfPeriodGhost);
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
return samples;
|
|
29
|
+
};
|
|
30
|
+
|
|
31
|
+
describe('beats', function () {
|
|
32
|
+
this.timeout(10000);
|
|
33
|
+
|
|
34
|
+
it('estimateTempo detects 128 BPM from a click train', function () {
|
|
35
|
+
const sampleRate = 22050;
|
|
36
|
+
const fps = 30;
|
|
37
|
+
const tempo = estimateTempo(clickTrain(sampleRate, 128, 8), sampleRate, fps);
|
|
38
|
+
expect(tempo).not.equal(null);
|
|
39
|
+
expect(tempo!.bpm).closeTo(128, 8);
|
|
40
|
+
expect(tempo!.periodFrames).closeTo(fps * 60 / 128, 1);
|
|
41
|
+
});
|
|
42
|
+
|
|
43
|
+
it('estimateTempo recovers phase of the click train', function () {
|
|
44
|
+
const sampleRate = 22050;
|
|
45
|
+
const fps = 30;
|
|
46
|
+
const phaseSec = 0.1;
|
|
47
|
+
const tempo = estimateTempo(clickTrain(sampleRate, 128, 8, phaseSec), sampleRate, fps);
|
|
48
|
+
expect(tempo).not.equal(null);
|
|
49
|
+
expect(tempo!.phaseFrame).closeTo(phaseSec * fps, 2);
|
|
50
|
+
});
|
|
51
|
+
|
|
52
|
+
it('estimateTempo prefers ~128 BPM when half-period ghosts are present', function () {
|
|
53
|
+
const sampleRate = 22050;
|
|
54
|
+
const fps = 30;
|
|
55
|
+
const tempo = estimateTempo(clickTrain(sampleRate, 128, 8, 0, 0.35), sampleRate, fps);
|
|
56
|
+
expect(tempo).not.equal(null);
|
|
57
|
+
expect(tempo!.bpm).closeTo(128, 8);
|
|
58
|
+
});
|
|
59
|
+
|
|
60
|
+
it('estimateTempo returns null for silence', function () {
|
|
61
|
+
const sampleRate = 22050;
|
|
62
|
+
const samples = new Array<number>(sampleRate * 2).fill(0);
|
|
63
|
+
expect(estimateTempo(samples, sampleRate, 30)).equal(null);
|
|
64
|
+
});
|
|
65
|
+
|
|
66
|
+
it('estimateTempo returns null for empty or invalid input', function () {
|
|
67
|
+
expect(estimateTempo([], 22050, 30)).equal(null);
|
|
68
|
+
expect(estimateTempo(clickTrain(22050, 128, 8), 0, 30)).equal(null);
|
|
69
|
+
expect(estimateTempo(clickTrain(22050, 128, 8), 22050, 0)).equal(null);
|
|
70
|
+
});
|
|
71
|
+
|
|
72
|
+
it('shiftTempoPhase wraps the beat grid into a later window', function () {
|
|
73
|
+
expect(shiftTempoPhase({ bpm: 120, periodFrames: 15, phaseFrame: 3 }, 0)).deep.equal({
|
|
74
|
+
bpm: 120,
|
|
75
|
+
periodFrames: 15,
|
|
76
|
+
phaseFrame: 3,
|
|
77
|
+
});
|
|
78
|
+
expect(shiftTempoPhase({ bpm: 120, periodFrames: 15, phaseFrame: 3 }, 3).phaseFrame).equal(0);
|
|
79
|
+
expect(shiftTempoPhase({ bpm: 120, periodFrames: 15, phaseFrame: 3 }, 18).phaseFrame).equal(0);
|
|
80
|
+
});
|
|
81
|
+
|
|
82
|
+
it('shiftTempoPhase keeps a non-integer period aligned in a late window', function () {
|
|
83
|
+
const tempo = { bpm: 128, periodFrames: 14.0625, phaseFrame: 3 };
|
|
84
|
+
const startFrame = 2000;
|
|
85
|
+
const shifted = shiftTempoPhase(tempo, startFrame);
|
|
86
|
+
const expected: number[] = [];
|
|
87
|
+
for (let n = 0; n < 400; n++) {
|
|
88
|
+
const local = Math.round(tempo.phaseFrame + n * tempo.periodFrames) - startFrame;
|
|
89
|
+
if (local > 0 && local < 60) {
|
|
90
|
+
expected.push(local);
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
expect(beatGridFrameIndices(shifted, 60)).deep.equal(expected);
|
|
94
|
+
});
|
|
95
|
+
|
|
96
|
+
it('tempoForWindow locks phase to beat times inside the edited window', function () {
|
|
97
|
+
const fps = 30;
|
|
98
|
+
const periodFrames = 15;
|
|
99
|
+
const startFrame = 90;
|
|
100
|
+
const tempo = {
|
|
101
|
+
bpm: 120,
|
|
102
|
+
periodFrames,
|
|
103
|
+
phaseFrame: 1,
|
|
104
|
+
beatsSec: [0.1, 3.2, 3.7, 4.2, 4.7],
|
|
105
|
+
};
|
|
106
|
+
const windowed = tempoForWindow(tempo, startFrame, fps, 60);
|
|
107
|
+
expect(windowed.phaseFrame).closeTo(6, 1);
|
|
108
|
+
expect(shiftTempoPhase(tempo, startFrame).phaseFrame).closeTo(1, 0.001);
|
|
109
|
+
expect(beatGridFrameIndices(windowed, 50)[0]).closeTo(6, 1);
|
|
110
|
+
expect(tempoForWindow(tempo, startFrame, fps, 60, [1, 16, 31]).phaseFrame).closeTo(6, 1);
|
|
111
|
+
});
|
|
112
|
+
|
|
113
|
+
it('tempoForWindow falls back to local onsets when beat times are outside the window', function () {
|
|
114
|
+
const tempo = {
|
|
115
|
+
bpm: 120,
|
|
116
|
+
periodFrames: 15,
|
|
117
|
+
phaseFrame: 1,
|
|
118
|
+
beatsSec: [0.1, 0.6, 1.1],
|
|
119
|
+
};
|
|
120
|
+
const windowed = tempoForWindow(tempo, 90, 30, 60, [7, 22, 37]);
|
|
121
|
+
expect(windowed.phaseFrame).closeTo(7, 1);
|
|
122
|
+
});
|
|
123
|
+
|
|
124
|
+
it('beatGridFrameIndices emits every beat and skips frame 0', function () {
|
|
125
|
+
expect(beatGridFrameIndices({ periodFrames: 14, phaseFrame: 0 }, 50)).deep.equal([14, 28, 42]);
|
|
126
|
+
expect(beatGridFrameIndices({ periodFrames: 14, phaseFrame: 3 }, 32)).deep.equal([3, 17, 31]);
|
|
127
|
+
expect(beatGridFrameIndices({ periodFrames: 14, phaseFrame: 0 }, 14)).deep.equal([]);
|
|
128
|
+
});
|
|
129
|
+
|
|
130
|
+
it('beatGridFrameIndices stride emits every Nth beat', function () {
|
|
131
|
+
expect(beatGridFrameIndices({ periodFrames: 14, phaseFrame: 0 }, 200, 4)).deep.equal([14, 70, 126, 182]);
|
|
132
|
+
expect(beatGridFrameIndices({ periodFrames: 14, phaseFrame: 3 }, 200, 4)).deep.equal([3, 59, 115, 171]);
|
|
133
|
+
});
|
|
134
|
+
});
|