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,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
+ });