nodejs-audio-visualizer 5.0.0 → 5.2.0

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 (53) hide show
  1. package/LICENSE +21 -21
  2. package/README.md +3 -2
  3. package/dist/audio.d.ts +13 -12
  4. package/dist/audio.js +128 -120
  5. package/dist/audio.js.map +1 -1
  6. package/dist/beats.d.ts +39 -19
  7. package/dist/beats.js +220 -43
  8. package/dist/beats.js.map +1 -1
  9. package/dist/bpmEncoder.d.ts +14 -14
  10. package/dist/bpmEncoder.js +97 -97
  11. package/dist/config.d.ts +78 -77
  12. package/dist/config.js +377 -369
  13. package/dist/config.js.map +1 -1
  14. package/dist/dsp.d.ts +16 -16
  15. package/dist/dsp.js +76 -76
  16. package/dist/highlight.d.ts +34 -32
  17. package/dist/highlight.js +334 -325
  18. package/dist/highlight.js.map +1 -1
  19. package/dist/image.d.ts +97 -79
  20. package/dist/image.js +545 -458
  21. package/dist/image.js.map +1 -1
  22. package/dist/index.d.ts +87 -79
  23. package/dist/index.js +597 -534
  24. package/dist/index.js.map +1 -1
  25. package/dist/subtitleConvert.d.ts +3 -3
  26. package/dist/subtitleConvert.js +61 -61
  27. package/dist/video.d.ts +150 -80
  28. package/dist/video.js +677 -434
  29. package/dist/video.js.map +1 -1
  30. package/dist/waitForEventLoop.d.ts +1 -1
  31. package/dist/waitForEventLoop.js +5 -5
  32. package/package.json +62 -61
  33. package/src/audio.ts +129 -120
  34. package/src/beats.ts +279 -68
  35. package/src/bpmEncoder.ts +97 -97
  36. package/src/config.ts +487 -476
  37. package/src/dsp.ts +95 -95
  38. package/src/highlight.ts +291 -284
  39. package/src/image.ts +707 -602
  40. package/src/index.ts +124 -31
  41. package/src/subtitleConvert.ts +63 -63
  42. package/src/tests/audio.test.ts +102 -96
  43. package/src/tests/beats.test.ts +134 -0
  44. package/src/tests/config.test.ts +534 -496
  45. package/src/tests/dsp.test.ts +24 -24
  46. package/src/tests/highlight.test.ts +178 -178
  47. package/src/tests/image.test.ts +319 -222
  48. package/src/tests/index.test.ts +180 -180
  49. package/src/tests/subtitleConvert.test.ts +23 -23
  50. package/src/tests/video.test.ts +535 -285
  51. package/src/typings.d.ts +28 -6
  52. package/src/video.ts +856 -550
  53. 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
+ });