nodejs-audio-visualizer 5.5.3 → 5.6.4

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 (49) hide show
  1. package/LICENSE +21 -21
  2. package/README.md +84 -83
  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 +81 -80
  10. package/dist/config.js +392 -391
  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 +91 -91
  15. package/dist/highlight.js +606 -606
  16. package/dist/image.d.ts +107 -97
  17. package/dist/image.js +614 -545
  18. package/dist/image.js.map +1 -1
  19. package/dist/index.d.ts +98 -96
  20. package/dist/index.js +673 -667
  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 +171 -171
  25. package/dist/video.js +743 -743
  26. package/dist/waitForEventLoop.d.ts +1 -1
  27. package/dist/waitForEventLoop.js +5 -5
  28. package/package.json +62 -62
  29. package/src/audio.ts +129 -129
  30. package/src/beats.ts +279 -279
  31. package/src/bpmEncoder.ts +97 -97
  32. package/src/config.ts +504 -501
  33. package/src/dsp.ts +95 -95
  34. package/src/highlight.ts +617 -617
  35. package/src/image.ts +791 -707
  36. package/src/index.ts +896 -885
  37. package/src/subtitleConvert.ts +63 -63
  38. package/src/tests/audio.test.ts +102 -102
  39. package/src/tests/beats.test.ts +134 -134
  40. package/src/tests/config.test.ts +568 -553
  41. package/src/tests/dsp.test.ts +24 -24
  42. package/src/tests/highlight.test.ts +397 -397
  43. package/src/tests/image.test.ts +371 -319
  44. package/src/tests/index.test.ts +180 -180
  45. package/src/tests/subtitleConvert.test.ts +23 -23
  46. package/src/tests/video.test.ts +652 -652
  47. package/src/typings.d.ts +28 -28
  48. package/src/video.ts +947 -947
  49. package/src/waitForEventLoop.ts +2 -2
@@ -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;
@@ -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
+ });
@@ -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
+ });