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.
Files changed (48) 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 -44
  6. package/dist/beats.js +220 -313
  7. package/dist/beats.js.map +1 -1
  8. package/dist/bpmEncoder.d.ts +14 -14
  9. package/dist/bpmEncoder.js +97 -97
  10. package/dist/config.d.ts +81 -80
  11. package/dist/config.js +392 -391
  12. package/dist/config.js.map +1 -1
  13. package/dist/dsp.d.ts +16 -16
  14. package/dist/dsp.js +76 -76
  15. package/dist/highlight.d.ts +91 -91
  16. package/dist/highlight.js +606 -606
  17. package/dist/image.d.ts +107 -97
  18. package/dist/image.js +614 -545
  19. package/dist/image.js.map +1 -1
  20. package/dist/index.d.ts +98 -96
  21. package/dist/index.js +673 -667
  22. package/dist/index.js.map +1 -1
  23. package/dist/subtitleConvert.d.ts +3 -3
  24. package/dist/subtitleConvert.js +61 -61
  25. package/dist/video.d.ts +171 -171
  26. package/dist/video.js +743 -743
  27. package/dist/waitForEventLoop.d.ts +1 -1
  28. package/dist/waitForEventLoop.js +5 -5
  29. package/package.json +62 -61
  30. package/src/audio.ts +129 -129
  31. package/src/beats.ts +279 -396
  32. package/src/bpmEncoder.ts +97 -97
  33. package/src/config.ts +504 -501
  34. package/src/dsp.ts +95 -95
  35. package/src/highlight.ts +617 -617
  36. package/src/image.ts +791 -707
  37. package/src/index.ts +896 -885
  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 +568 -553
  42. package/src/tests/dsp.test.ts +24 -24
  43. package/src/tests/highlight.test.ts +397 -397
  44. package/src/tests/image.test.ts +371 -319
  45. package/src/tests/index.test.ts +180 -180
  46. package/src/tests/subtitleConvert.test.ts +23 -23
  47. package/src/typings.d.ts +28 -6
  48. package/src/waitForEventLoop.ts +2 -2
@@ -1,180 +1,180 @@
1
- import { expect } from 'chai';
2
- import { createSandbox } from 'sinon';
3
- import { renderAudioVisualizer, Config } from '../index';
4
- import path from 'path';
5
- import fs from 'fs';
6
- import { Writable, Readable } from 'stream';
7
- import { ChildProcessWithoutNullStreams } from 'child_process';
8
- import { EventEmitter } from 'events';
9
- import * as image from '../image';
10
- import * as audio from '../audio';
11
- import * as video from '../video';
12
- import * as bpmEncoder from '../bpmEncoder';
13
-
14
- const EXIT_CODE = 0;
15
- const sandbox = createSandbox();
16
-
17
- describe('index', function () {
18
- const createMockWriter = (emitExitOnWrite: boolean): ChildProcessWithoutNullStreams => {
19
- const eventBus = new EventEmitter();
20
- const writable = new Writable({
21
- write(_chunk: Buffer, _enc: string, cb: (error?: Error | null) => void) {
22
- if (emitExitOnWrite) {
23
- eventBus.emit('exit', EXIT_CODE);
24
- }
25
- cb();
26
- },
27
- });
28
- const stderr = new Readable();
29
- stderr._read = () => {};
30
- return {
31
- stdin: writable,
32
- stderr,
33
- on: eventBus.on.bind(eventBus),
34
- once: eventBus.once.bind(eventBus),
35
- removeListener: eventBus.removeListener.bind(eventBus),
36
- emit: eventBus.emit.bind(eventBus),
37
- } as unknown as ChildProcessWithoutNullStreams;
38
- };
39
-
40
- this.beforeEach(function () {
41
- sandbox.stub(fs, 'readFileSync').returns(Buffer.from([1, 1]));
42
- sandbox.stub(audio, 'createAudioBuffer').returns(
43
- Promise.resolve({ audioBuffer: Buffer.from([1, 1]), sampleRate: 2 })
44
- );
45
- sandbox.stub(image, 'getImageColor').returns({ red: 0, green: 0, blue: 0 });
46
-
47
- let childProcessStream = createMockWriter(true);
48
- sandbox.stub(video, 'spawnFfmpegVideoWriter').callsFake(
49
- () => childProcessStream
50
- );
51
- (this as any).setMockWriter = (emitExitOnWrite: boolean) => {
52
- childProcessStream = createMockWriter(emitExitOnWrite);
53
- };
54
- });
55
-
56
- this.afterEach(function () {
57
- sandbox.restore();
58
- });
59
-
60
- it('renderAudioVisualizer', async function () {
61
- const config: Config = {
62
- audio: {
63
- path: 'audioPath'
64
- },
65
- image: {
66
- path: 'example/media/horses.png'
67
- },
68
- outVideo: {
69
- path: 'outVideoPath',
70
- fps: 1
71
- }
72
- };
73
-
74
- const { exitCode, reason, outputVideoFiles } = await renderAudioVisualizer(config);
75
- expect(exitCode).equal(EXIT_CODE);
76
- expect(reason).equal(undefined);
77
- expect(outputVideoFiles).deep.equal([path.resolve('outVideoPath')]);
78
- });
79
-
80
- it('does not render a visualizer when neither spectrum nor polar is specified', async function () {
81
- const polarSpy = sandbox.spy(image, 'createPolarVisualizerFrameGenerator');
82
- const spectrumSpy = sandbox.spy(image, 'createSpectrumVisualizerFrameGenerator');
83
- const config: Config = {
84
- audio: {
85
- path: 'audioPath'
86
- },
87
- image: {
88
- path: 'example/media/horses.png'
89
- },
90
- outVideo: {
91
- path: 'outVideoPath',
92
- fps: 1
93
- }
94
- };
95
-
96
- const { exitCode } = await renderAudioVisualizer(config);
97
- expect(exitCode).equal(EXIT_CODE);
98
- expect(polarSpy.called).equal(false);
99
- expect(spectrumSpy.called).equal(false);
100
- });
101
-
102
- it('renders a polar visualizer when polar is specified', async function () {
103
- const polarSpy = sandbox.spy(image, 'createPolarVisualizerFrameGenerator');
104
- const spectrumSpy = sandbox.spy(image, 'createSpectrumVisualizerFrameGenerator');
105
- const config: Config = {
106
- audio: {
107
- path: 'audioPath'
108
- },
109
- image: {
110
- path: 'example/media/horses.png'
111
- },
112
- outVideo: {
113
- path: 'outVideoPath',
114
- fps: 1,
115
- polar: {}
116
- }
117
- };
118
-
119
- const { exitCode } = await renderAudioVisualizer(config);
120
- expect(exitCode).equal(EXIT_CODE);
121
- expect(polarSpy.calledOnce).equal(true);
122
- expect(spectrumSpy.called).equal(false);
123
- });
124
-
125
- it('renders a spectrum visualizer when spectrum is specified', async function () {
126
- const polarSpy = sandbox.spy(image, 'createPolarVisualizerFrameGenerator');
127
- const spectrumSpy = sandbox.spy(image, 'createSpectrumVisualizerFrameGenerator');
128
- const config: Config = {
129
- audio: {
130
- path: 'audioPath'
131
- },
132
- image: {
133
- path: 'example/media/horses.png'
134
- },
135
- outVideo: {
136
- path: 'outVideoPath',
137
- fps: 1,
138
- spectrum: {}
139
- }
140
- };
141
-
142
- const { exitCode } = await renderAudioVisualizer(config);
143
- expect(exitCode).equal(EXIT_CODE);
144
- expect(spectrumSpy.calledOnce).equal(true);
145
- expect(polarSpy.called).equal(false);
146
- });
147
-
148
- it('resolves and does not report 100 when process completion times out', async function () {
149
- (this as any).setMockWriter(false);
150
- sandbox.stub(video, 'waitForProcessExit').callsFake(
151
- () => Promise.resolve({ exitCode: 1 })
152
- );
153
- const progressValues: number[] = [];
154
- const config: Config = {
155
- audio: {
156
- path: 'audioPath'
157
- },
158
- image: {
159
- path: 'example/media/horses.png'
160
- },
161
- outVideo: {
162
- path: 'outVideoPath',
163
- fps: 1
164
- }
165
- };
166
-
167
- const timeoutPromise = new Promise<never>((_resolve, reject) => {
168
- setTimeout(() => reject(new Error('renderAudioVisualizer hung')), 200);
169
- });
170
-
171
- const { exitCode, outputVideoFiles } = await Promise.race([
172
- renderAudioVisualizer(config, (progress) => progressValues.push(progress)),
173
- timeoutPromise,
174
- ]);
175
-
176
- expect(exitCode).equal(1);
177
- expect(outputVideoFiles).deep.equal([]);
178
- expect(progressValues.some(progress => progress === 100)).equal(false);
179
- });
180
- });
1
+ import { expect } from 'chai';
2
+ import { createSandbox } from 'sinon';
3
+ import { renderAudioVisualizer, Config } from '../index';
4
+ import path from 'path';
5
+ import fs from 'fs';
6
+ import { Writable, Readable } from 'stream';
7
+ import { ChildProcessWithoutNullStreams } from 'child_process';
8
+ import { EventEmitter } from 'events';
9
+ import * as image from '../image';
10
+ import * as audio from '../audio';
11
+ import * as video from '../video';
12
+ import * as bpmEncoder from '../bpmEncoder';
13
+
14
+ const EXIT_CODE = 0;
15
+ const sandbox = createSandbox();
16
+
17
+ describe('index', function () {
18
+ const createMockWriter = (emitExitOnWrite: boolean): ChildProcessWithoutNullStreams => {
19
+ const eventBus = new EventEmitter();
20
+ const writable = new Writable({
21
+ write(_chunk: Buffer, _enc: string, cb: (error?: Error | null) => void) {
22
+ if (emitExitOnWrite) {
23
+ eventBus.emit('exit', EXIT_CODE);
24
+ }
25
+ cb();
26
+ },
27
+ });
28
+ const stderr = new Readable();
29
+ stderr._read = () => {};
30
+ return {
31
+ stdin: writable,
32
+ stderr,
33
+ on: eventBus.on.bind(eventBus),
34
+ once: eventBus.once.bind(eventBus),
35
+ removeListener: eventBus.removeListener.bind(eventBus),
36
+ emit: eventBus.emit.bind(eventBus),
37
+ } as unknown as ChildProcessWithoutNullStreams;
38
+ };
39
+
40
+ this.beforeEach(function () {
41
+ sandbox.stub(fs, 'readFileSync').returns(Buffer.from([1, 1]));
42
+ sandbox.stub(audio, 'createAudioBuffer').returns(
43
+ Promise.resolve({ audioBuffer: Buffer.from([1, 1]), sampleRate: 2 })
44
+ );
45
+ sandbox.stub(image, 'getImageColor').returns({ red: 0, green: 0, blue: 0 });
46
+
47
+ let childProcessStream = createMockWriter(true);
48
+ sandbox.stub(video, 'spawnFfmpegVideoWriter').callsFake(
49
+ () => childProcessStream
50
+ );
51
+ (this as any).setMockWriter = (emitExitOnWrite: boolean) => {
52
+ childProcessStream = createMockWriter(emitExitOnWrite);
53
+ };
54
+ });
55
+
56
+ this.afterEach(function () {
57
+ sandbox.restore();
58
+ });
59
+
60
+ it('renderAudioVisualizer', async function () {
61
+ const config: Config = {
62
+ audio: {
63
+ path: 'audioPath'
64
+ },
65
+ image: {
66
+ path: 'example/media/horses.png'
67
+ },
68
+ outVideo: {
69
+ path: 'outVideoPath',
70
+ fps: 1
71
+ }
72
+ };
73
+
74
+ const { exitCode, reason, outputVideoFiles } = await renderAudioVisualizer(config);
75
+ expect(exitCode).equal(EXIT_CODE);
76
+ expect(reason).equal(undefined);
77
+ expect(outputVideoFiles).deep.equal([path.resolve('outVideoPath')]);
78
+ });
79
+
80
+ it('does not render a visualizer when neither spectrum nor polar is specified', async function () {
81
+ const polarSpy = sandbox.spy(image, 'createPolarVisualizerFrameGenerator');
82
+ const spectrumSpy = sandbox.spy(image, 'createSpectrumVisualizerFrameGenerator');
83
+ const config: Config = {
84
+ audio: {
85
+ path: 'audioPath'
86
+ },
87
+ image: {
88
+ path: 'example/media/horses.png'
89
+ },
90
+ outVideo: {
91
+ path: 'outVideoPath',
92
+ fps: 1
93
+ }
94
+ };
95
+
96
+ const { exitCode } = await renderAudioVisualizer(config);
97
+ expect(exitCode).equal(EXIT_CODE);
98
+ expect(polarSpy.called).equal(false);
99
+ expect(spectrumSpy.called).equal(false);
100
+ });
101
+
102
+ it('renders a polar visualizer when polar is specified', async function () {
103
+ const polarSpy = sandbox.spy(image, 'createPolarVisualizerFrameGenerator');
104
+ const spectrumSpy = sandbox.spy(image, 'createSpectrumVisualizerFrameGenerator');
105
+ const config: Config = {
106
+ audio: {
107
+ path: 'audioPath'
108
+ },
109
+ image: {
110
+ path: 'example/media/horses.png'
111
+ },
112
+ outVideo: {
113
+ path: 'outVideoPath',
114
+ fps: 1,
115
+ polar: {}
116
+ }
117
+ };
118
+
119
+ const { exitCode } = await renderAudioVisualizer(config);
120
+ expect(exitCode).equal(EXIT_CODE);
121
+ expect(polarSpy.calledOnce).equal(true);
122
+ expect(spectrumSpy.called).equal(false);
123
+ });
124
+
125
+ it('renders a spectrum visualizer when spectrum is specified', async function () {
126
+ const polarSpy = sandbox.spy(image, 'createPolarVisualizerFrameGenerator');
127
+ const spectrumSpy = sandbox.spy(image, 'createSpectrumVisualizerFrameGenerator');
128
+ const config: Config = {
129
+ audio: {
130
+ path: 'audioPath'
131
+ },
132
+ image: {
133
+ path: 'example/media/horses.png'
134
+ },
135
+ outVideo: {
136
+ path: 'outVideoPath',
137
+ fps: 1,
138
+ spectrum: {}
139
+ }
140
+ };
141
+
142
+ const { exitCode } = await renderAudioVisualizer(config);
143
+ expect(exitCode).equal(EXIT_CODE);
144
+ expect(spectrumSpy.calledOnce).equal(true);
145
+ expect(polarSpy.called).equal(false);
146
+ });
147
+
148
+ it('resolves and does not report 100 when process completion times out', async function () {
149
+ (this as any).setMockWriter(false);
150
+ sandbox.stub(video, 'waitForProcessExit').callsFake(
151
+ () => Promise.resolve({ exitCode: 1 })
152
+ );
153
+ const progressValues: number[] = [];
154
+ const config: Config = {
155
+ audio: {
156
+ path: 'audioPath'
157
+ },
158
+ image: {
159
+ path: 'example/media/horses.png'
160
+ },
161
+ outVideo: {
162
+ path: 'outVideoPath',
163
+ fps: 1
164
+ }
165
+ };
166
+
167
+ const timeoutPromise = new Promise<never>((_resolve, reject) => {
168
+ setTimeout(() => reject(new Error('renderAudioVisualizer hung')), 200);
169
+ });
170
+
171
+ const { exitCode, outputVideoFiles } = await Promise.race([
172
+ renderAudioVisualizer(config, (progress) => progressValues.push(progress)),
173
+ timeoutPromise,
174
+ ]);
175
+
176
+ expect(exitCode).equal(1);
177
+ expect(outputVideoFiles).deep.equal([]);
178
+ expect(progressValues.some(progress => progress === 100)).equal(false);
179
+ });
180
+ });
@@ -1,23 +1,23 @@
1
- import { expect } from 'chai';
2
- import { lrcToSrt, isLikelyLrc, normalizeInlineSubtitlesToSrt } from '../subtitleConvert';
3
-
4
- describe('subtitleConvert', function () {
5
- it('isLikelyLrc', function () {
6
- expect(isLikelyLrc('[00:01.00]Hello')).equal(true);
7
- expect(isLikelyLrc('1\n00:00:01,000 --> 00:00:02,000\nSRT')).equal(false);
8
- });
9
-
10
- it('lrcToSrt converts basic LRC to SRT', function () {
11
- const lrc = '[00:01.00]Line one\n[00:03.50]Line two';
12
- const srt = lrcToSrt(lrc);
13
- expect(srt).to.include('00:00:01,000 --> 00:00:03,500');
14
- expect(srt).to.include('Line one');
15
- expect(srt).to.include('00:00:03,500 -->');
16
- expect(srt).to.include('Line two');
17
- });
18
-
19
- it('normalizeInlineSubtitlesToSrt leaves SRT unchanged', function () {
20
- const s = '1\n00:00:00,000 --> 00:00:01,000\nX';
21
- expect(normalizeInlineSubtitlesToSrt(s)).equal(s);
22
- });
23
- });
1
+ import { expect } from 'chai';
2
+ import { lrcToSrt, isLikelyLrc, normalizeInlineSubtitlesToSrt } from '../subtitleConvert';
3
+
4
+ describe('subtitleConvert', function () {
5
+ it('isLikelyLrc', function () {
6
+ expect(isLikelyLrc('[00:01.00]Hello')).equal(true);
7
+ expect(isLikelyLrc('1\n00:00:01,000 --> 00:00:02,000\nSRT')).equal(false);
8
+ });
9
+
10
+ it('lrcToSrt converts basic LRC to SRT', function () {
11
+ const lrc = '[00:01.00]Line one\n[00:03.50]Line two';
12
+ const srt = lrcToSrt(lrc);
13
+ expect(srt).to.include('00:00:01,000 --> 00:00:03,500');
14
+ expect(srt).to.include('Line one');
15
+ expect(srt).to.include('00:00:03,500 -->');
16
+ expect(srt).to.include('Line two');
17
+ });
18
+
19
+ it('normalizeInlineSubtitlesToSrt leaves SRT unchanged', function () {
20
+ const s = '1\n00:00:00,000 --> 00:00:01,000\nX';
21
+ expect(normalizeInlineSubtitlesToSrt(s)).equal(s);
22
+ });
23
+ });
package/src/typings.d.ts CHANGED
@@ -1,7 +1,29 @@
1
- declare module 'fft-js' {
2
- export function fft(signal: number[]): [number, number][];
3
- export const util: {
4
- fftFreq(phasors: [number, number][], sampleRate: number): number[];
5
- fftMag(phasors: [number, number][]): number[];
6
- };
1
+ declare module 'fft-js' {
2
+ export function fft(signal: number[]): [number, number][];
3
+ export const util: {
4
+ fftFreq(phasors: [number, number][], sampleRate: number): number[];
5
+ fftMag(phasors: [number, number][]): number[];
6
+ };
7
+ }
8
+
9
+ declare module 'music-tempo' {
10
+ export interface MusicTempoParams {
11
+ bufferSize?: number;
12
+ hopSize?: number;
13
+ timeStep?: number;
14
+ decayRate?: number;
15
+ peakFindingWindow?: number;
16
+ meanWndMultiplier?: number;
17
+ peakThreshold?: number;
18
+ minBeatInterval?: number;
19
+ maxBeatInterval?: number;
20
+ expiryTime?: number;
21
+ }
22
+
23
+ export default class MusicTempo {
24
+ constructor(audioData: number[] | Float32Array, params?: MusicTempoParams);
25
+ tempo: string | number;
26
+ beats: number[];
27
+ beatInterval: number;
28
+ }
7
29
  }
@@ -1,2 +1,2 @@
1
- export const waitForEventLoop = () =>
2
- new Promise<void>(resolve => setImmediate(resolve));
1
+ export const waitForEventLoop = () =>
2
+ new Promise<void>(resolve => setImmediate(resolve));