nodejs-audio-visualizer 4.5.0 → 5.0.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 (47) hide show
  1. package/LICENSE +21 -21
  2. package/README.md +80 -80
  3. package/dist/audio.d.ts +12 -12
  4. package/dist/audio.js +120 -120
  5. package/dist/beats.d.ts +19 -19
  6. package/dist/beats.js +43 -43
  7. package/dist/bpmEncoder.d.ts +14 -14
  8. package/dist/bpmEncoder.js +97 -97
  9. package/dist/config.d.ts +77 -77
  10. package/dist/config.js +369 -369
  11. package/dist/dsp.d.ts +16 -16
  12. package/dist/dsp.js +76 -76
  13. package/dist/highlight.d.ts +32 -32
  14. package/dist/highlight.js +325 -325
  15. package/dist/image.d.ts +79 -79
  16. package/dist/image.js +458 -458
  17. package/dist/index.d.ts +79 -79
  18. package/dist/index.js +534 -531
  19. package/dist/index.js.map +1 -1
  20. package/dist/subtitleConvert.d.ts +3 -3
  21. package/dist/subtitleConvert.js +61 -61
  22. package/dist/video.d.ts +80 -80
  23. package/dist/video.js +434 -426
  24. package/dist/video.js.map +1 -1
  25. package/dist/waitForEventLoop.d.ts +1 -1
  26. package/dist/waitForEventLoop.js +5 -5
  27. package/package.json +61 -61
  28. package/src/audio.ts +120 -120
  29. package/src/beats.ts +68 -68
  30. package/src/bpmEncoder.ts +97 -97
  31. package/src/config.ts +476 -476
  32. package/src/dsp.ts +95 -95
  33. package/src/highlight.ts +284 -284
  34. package/src/image.ts +602 -602
  35. package/src/index.ts +718 -713
  36. package/src/subtitleConvert.ts +63 -63
  37. package/src/tests/audio.test.ts +96 -96
  38. package/src/tests/config.test.ts +496 -496
  39. package/src/tests/dsp.test.ts +24 -24
  40. package/src/tests/highlight.test.ts +178 -178
  41. package/src/tests/image.test.ts +222 -222
  42. package/src/tests/index.test.ts +180 -112
  43. package/src/tests/subtitleConvert.test.ts +23 -23
  44. package/src/tests/video.test.ts +285 -216
  45. package/src/typings.d.ts +6 -6
  46. package/src/video.ts +550 -542
  47. package/src/waitForEventLoop.ts +2 -2
@@ -1,112 +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('resolves and does not report 100 when process completion times out', async function () {
81
- (this as any).setMockWriter(false);
82
- sandbox.stub(video, 'waitForProcessExit').callsFake(
83
- () => Promise.resolve({ exitCode: 1 })
84
- );
85
- const progressValues: number[] = [];
86
- const config: Config = {
87
- audio: {
88
- path: 'audioPath'
89
- },
90
- image: {
91
- path: 'example/media/horses.png'
92
- },
93
- outVideo: {
94
- path: 'outVideoPath',
95
- fps: 1
96
- }
97
- };
98
-
99
- const timeoutPromise = new Promise<never>((_resolve, reject) => {
100
- setTimeout(() => reject(new Error('renderAudioVisualizer hung')), 200);
101
- });
102
-
103
- const { exitCode, outputVideoFiles } = await Promise.race([
104
- renderAudioVisualizer(config, (progress) => progressValues.push(progress)),
105
- timeoutPromise,
106
- ]);
107
-
108
- expect(exitCode).equal(1);
109
- expect(outputVideoFiles).deep.equal([]);
110
- expect(progressValues.some(progress => progress === 100)).equal(false);
111
- });
112
- });
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
+ });