nodejs-audio-visualizer 4.3.7 → 4.3.9

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 +75 -75
  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 +67 -67
  10. package/dist/config.js +356 -356
  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 +72 -72
  18. package/dist/index.js +539 -541
  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 +77 -72
  23. package/dist/video.js +407 -338
  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 +462 -462
  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 +693 -678
  36. package/src/subtitleConvert.ts +63 -63
  37. package/src/tests/audio.test.ts +96 -96
  38. package/src/tests/config.test.ts +475 -475
  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 +156 -63
  43. package/src/tests/subtitleConvert.test.ts +23 -23
  44. package/src/tests/video.test.ts +214 -138
  45. package/src/typings.d.ts +6 -6
  46. package/src/video.ts +517 -437
  47. package/src/waitForEventLoop.ts +2 -2
@@ -1,222 +1,222 @@
1
- import { expect } from 'chai';
2
- import path from 'path';
3
- import {
4
- drawRect,
5
- Color,
6
- createSpectrumVisualizerFrameGenerator,
7
- createPolarVisualizerFrameGenerator,
8
- parseImage,
9
- convertToBmp,
10
- mixValues,
11
- mixColors,
12
- } from '../image';
13
- import { createBpmEncoder } from '../bpmEncoder';
14
-
15
- describe('image', function () {
16
- it('mixValues', function () {
17
- expect(mixValues(420, 0.69, 96)).equal(320);
18
- expect(mixValues(420, 1, 96)).equal(420);
19
- expect(mixValues(420, 0, 96)).equal(96);
20
- });
21
-
22
- it('mixColors', function () {
23
- const result1 = mixColors({
24
- color: { red: 123, green: 69, blue: 96 },
25
- colorOpacity: 0.24,
26
- backgroundColor: { red: 69, green: 123, blue: 42 },
27
- });
28
- expect(result1).deep.equal({ red: 82, green: 110, blue: 55 });
29
-
30
- const result2 = mixColors({
31
- color: { red: 123, green: 69, blue: 96 },
32
- colorOpacity: 0,
33
- backgroundColor: { red: 69, green: 123, blue: 42 },
34
- });
35
- expect(result2).deep.equal({ red: 69, green: 123, blue: 42 });
36
-
37
- const result3 = mixColors({
38
- color: { red: 123, green: 69, blue: 96 },
39
- colorOpacity: 1,
40
- backgroundColor: { red: 69, green: 123, blue: 42 },
41
- });
42
- expect(result3).deep.equal({ red: 123, green: 69, blue: 96 });
43
- });
44
-
45
- it('drawRect', function () {
46
- const imageWidth = 10;
47
- const imageHeight = 10;
48
- const extraBytes = imageWidth % 4;
49
- const rowBytes = 3 * imageWidth + extraBytes;
50
- const imageDstBuffer = {
51
- shiftPos: 54,
52
- rowBytes,
53
- width: imageWidth,
54
- data: Array.from({ length: imageWidth * imageHeight * 4 }).fill(0)
55
- };
56
- const rectX = 4;
57
- const rectY = 3;
58
- const rectWidth = 4;
59
- const rectHeight = 3;
60
- const rectColor: Color = { red: 111, green: 112, blue: 123 };
61
-
62
- const expectedImageData = Array.from({ length: imageWidth * imageWidth * 4 }).fill(0);
63
- const imagePixelIndices = [162, 165, 168, 171, 194, 197, 200, 203, 226, 229, 232, 235];
64
- imagePixelIndices.forEach((pixelIndex) => {
65
- expectedImageData[pixelIndex] = rectColor.blue;
66
- expectedImageData[pixelIndex + 1] = rectColor.green;
67
- expectedImageData[pixelIndex + 2] = rectColor.red;
68
- });
69
-
70
- drawRect({
71
- imageDstBuffer: imageDstBuffer as any,
72
- position: { x: rectX, y: rectY },
73
- size: { width: rectWidth, height: rectHeight },
74
- color: rectColor,
75
- opacity: 1
76
- });
77
- expect(imageDstBuffer.data).deep.equal(expectedImageData);
78
- });
79
-
80
- it('createSpectrumVisualizerFrameGenerator', async function () {
81
- const expectedImageLength = 6220854;
82
- const backgroundImagePath = path.resolve('example/media/horses.png');
83
- const backgroundImageBmpBuffer = await convertToBmp(backgroundImagePath);
84
- const backgroundImage = parseImage(backgroundImageBmpBuffer);
85
- const bpmEncoder = createBpmEncoder({ width: backgroundImage.width, height: backgroundImage.height });
86
- const backgroundImageBuffer = bpmEncoder(backgroundImage.data);
87
- const createVisualizerFrame = createSpectrumVisualizerFrameGenerator();
88
- const frameSpectrumDown = createVisualizerFrame({
89
- backgroundImageBuffer,
90
- spectrum: [0.5, 0, 1],
91
- size: { width: 15, height: 20 },
92
- position: { x: 15, y: 0 },
93
- rotation: 'down',
94
- margin: 4,
95
- color: { red: 1, green: 1, blue: 1 },
96
- opacity: 1,
97
- });
98
- const resultSpectrumDown = frameSpectrumDown.data.toJSON().data;
99
- expect(resultSpectrumDown).to.have.length(expectedImageLength);
100
-
101
- const frameSpectrumUp = createVisualizerFrame({
102
- backgroundImageBuffer,
103
- spectrum: [0.1, 1, 0],
104
- size: { width: 25, height: 23 },
105
- position: { x: 10, y: 5 },
106
- rotation: 'up',
107
- margin: 4,
108
- color: { red: 0, green: 123, blue: 69 },
109
- opacity: 1,
110
- });
111
- const resultSpectrumUp = frameSpectrumUp.data.toJSON().data;
112
- expect(resultSpectrumUp).to.have.length(expectedImageLength);
113
-
114
- const frameSpectrumMirror = createVisualizerFrame({
115
- backgroundImageBuffer,
116
- spectrum: [0.1, 1, 0],
117
- size: { width: 25, height: 23 },
118
- position: { x: 10, y: 5 },
119
- rotation: 'mirror',
120
- margin: 4,
121
- color: { red: 0, green: 123, blue: 69 },
122
- opacity: 1,
123
- });
124
- const resultSpectrumMirror = frameSpectrumMirror.data.toJSON().data;
125
- expect(resultSpectrumMirror).to.have.length(expectedImageLength);
126
-
127
- const frameSpectrumUpOpacity50 = createVisualizerFrame({
128
- backgroundImageBuffer,
129
- spectrum: [0.1, 1, 0],
130
- size: { width: 25, height: 23 },
131
- position: { x: 10, y: 5 },
132
- rotation: 'up',
133
- margin: 4,
134
- color: { red: 0, green: 123, blue: 69 },
135
- opacity: 0.5,
136
- });
137
- const resultSpectrumUpOpacity50 = frameSpectrumUpOpacity50.data.toJSON().data;
138
- expect(resultSpectrumUpOpacity50).to.have.length(expectedImageLength);
139
- });
140
-
141
- it('createPolarVisualizerFrameGenerator', async function () {
142
- const expectedImageLength = 6220854;
143
- const backgroundImagePath = path.resolve('example/media/horses.png');
144
- const backgroundImageBmpBuffer = await convertToBmp(backgroundImagePath);
145
- const backgroundImage = parseImage(backgroundImageBmpBuffer);
146
- const bpmEncoder = createBpmEncoder({ width: backgroundImage.width, height: backgroundImage.height });
147
- const backgroundImageBuffer = bpmEncoder(backgroundImage.data);
148
- const createPolarVisualizerFrame = createPolarVisualizerFrameGenerator();
149
-
150
- const framePolarBasic = createPolarVisualizerFrame({
151
- backgroundImageBuffer,
152
- spectrum: [0.5, 0.8, 0.3, 0.9, 0.6, 0.4, 0.7, 0.2],
153
- centerX: 500,
154
- centerY: 400,
155
- innerRadius: 100,
156
- maxBarLength: 150,
157
- barWidth: 5,
158
- color: { red: 255, green: 100, blue: 50 },
159
- opacity: 1,
160
- });
161
- const resultPolarBasic = framePolarBasic.data.toJSON().data;
162
- expect(resultPolarBasic).to.have.length(expectedImageLength);
163
-
164
- const framePolarVolume = createPolarVisualizerFrame({
165
- backgroundImageBuffer,
166
- spectrum: [0.2, 0.5, 0.8, 0.3, 0.6, 0.9, 0.4, 0.7],
167
- centerX: 500,
168
- centerY: 400,
169
- innerRadius: 80,
170
- maxBarLength: 120,
171
- barWidth: 4,
172
- color: { red: 100, green: 200, blue: 255 },
173
- opacity: 1,
174
- spectrumEffect: 'volume',
175
- });
176
- const resultPolarVolume = framePolarVolume.data.toJSON().data;
177
- expect(resultPolarVolume).to.have.length(expectedImageLength);
178
-
179
- const framePolarSmooth = createPolarVisualizerFrame({
180
- backgroundImageBuffer,
181
- spectrum: [0.7, 0.4, 0.9, 0.2, 0.5, 0.8, 0.3, 0.6],
182
- centerX: 500,
183
- centerY: 400,
184
- innerRadius: 90,
185
- maxBarLength: 130,
186
- barWidth: 6,
187
- color: { red: 255, green: 255, blue: 0 },
188
- opacity: 1,
189
- spectrumEffect: 'smooth',
190
- });
191
- const resultPolarSmooth = framePolarSmooth.data.toJSON().data;
192
- expect(resultPolarSmooth).to.have.length(expectedImageLength);
193
-
194
- const framePolarOpacity50 = createPolarVisualizerFrame({
195
- backgroundImageBuffer,
196
- spectrum: [0.6, 0.3, 0.8, 0.5, 0.4, 0.7, 0.2, 0.9],
197
- centerX: 500,
198
- centerY: 400,
199
- innerRadius: 100,
200
- maxBarLength: 140,
201
- barWidth: 5,
202
- color: { red: 0, green: 255, blue: 128 },
203
- opacity: 0.5,
204
- });
205
- const resultPolarOpacity50 = framePolarOpacity50.data.toJSON().data;
206
- expect(resultPolarOpacity50).to.have.length(expectedImageLength);
207
-
208
- const framePolarHexColor = createPolarVisualizerFrame({
209
- backgroundImageBuffer,
210
- spectrum: [0.5, 0.6, 0.7, 0.8, 0.9, 0.4, 0.3, 0.2],
211
- centerX: 500,
212
- centerY: 400,
213
- innerRadius: 110,
214
- maxBarLength: 160,
215
- barWidth: 7,
216
- color: '#FF5733',
217
- opacity: 1,
218
- });
219
- const resultPolarHexColor = framePolarHexColor.data.toJSON().data;
220
- expect(resultPolarHexColor).to.have.length(expectedImageLength);
221
- });
222
- });
1
+ import { expect } from 'chai';
2
+ import path from 'path';
3
+ import {
4
+ drawRect,
5
+ Color,
6
+ createSpectrumVisualizerFrameGenerator,
7
+ createPolarVisualizerFrameGenerator,
8
+ parseImage,
9
+ convertToBmp,
10
+ mixValues,
11
+ mixColors,
12
+ } from '../image';
13
+ import { createBpmEncoder } from '../bpmEncoder';
14
+
15
+ describe('image', function () {
16
+ it('mixValues', function () {
17
+ expect(mixValues(420, 0.69, 96)).equal(320);
18
+ expect(mixValues(420, 1, 96)).equal(420);
19
+ expect(mixValues(420, 0, 96)).equal(96);
20
+ });
21
+
22
+ it('mixColors', function () {
23
+ const result1 = mixColors({
24
+ color: { red: 123, green: 69, blue: 96 },
25
+ colorOpacity: 0.24,
26
+ backgroundColor: { red: 69, green: 123, blue: 42 },
27
+ });
28
+ expect(result1).deep.equal({ red: 82, green: 110, blue: 55 });
29
+
30
+ const result2 = mixColors({
31
+ color: { red: 123, green: 69, blue: 96 },
32
+ colorOpacity: 0,
33
+ backgroundColor: { red: 69, green: 123, blue: 42 },
34
+ });
35
+ expect(result2).deep.equal({ red: 69, green: 123, blue: 42 });
36
+
37
+ const result3 = mixColors({
38
+ color: { red: 123, green: 69, blue: 96 },
39
+ colorOpacity: 1,
40
+ backgroundColor: { red: 69, green: 123, blue: 42 },
41
+ });
42
+ expect(result3).deep.equal({ red: 123, green: 69, blue: 96 });
43
+ });
44
+
45
+ it('drawRect', function () {
46
+ const imageWidth = 10;
47
+ const imageHeight = 10;
48
+ const extraBytes = imageWidth % 4;
49
+ const rowBytes = 3 * imageWidth + extraBytes;
50
+ const imageDstBuffer = {
51
+ shiftPos: 54,
52
+ rowBytes,
53
+ width: imageWidth,
54
+ data: Array.from({ length: imageWidth * imageHeight * 4 }).fill(0)
55
+ };
56
+ const rectX = 4;
57
+ const rectY = 3;
58
+ const rectWidth = 4;
59
+ const rectHeight = 3;
60
+ const rectColor: Color = { red: 111, green: 112, blue: 123 };
61
+
62
+ const expectedImageData = Array.from({ length: imageWidth * imageWidth * 4 }).fill(0);
63
+ const imagePixelIndices = [162, 165, 168, 171, 194, 197, 200, 203, 226, 229, 232, 235];
64
+ imagePixelIndices.forEach((pixelIndex) => {
65
+ expectedImageData[pixelIndex] = rectColor.blue;
66
+ expectedImageData[pixelIndex + 1] = rectColor.green;
67
+ expectedImageData[pixelIndex + 2] = rectColor.red;
68
+ });
69
+
70
+ drawRect({
71
+ imageDstBuffer: imageDstBuffer as any,
72
+ position: { x: rectX, y: rectY },
73
+ size: { width: rectWidth, height: rectHeight },
74
+ color: rectColor,
75
+ opacity: 1
76
+ });
77
+ expect(imageDstBuffer.data).deep.equal(expectedImageData);
78
+ });
79
+
80
+ it('createSpectrumVisualizerFrameGenerator', async function () {
81
+ const expectedImageLength = 6220854;
82
+ const backgroundImagePath = path.resolve('example/media/horses.png');
83
+ const backgroundImageBmpBuffer = await convertToBmp(backgroundImagePath);
84
+ const backgroundImage = parseImage(backgroundImageBmpBuffer);
85
+ const bpmEncoder = createBpmEncoder({ width: backgroundImage.width, height: backgroundImage.height });
86
+ const backgroundImageBuffer = bpmEncoder(backgroundImage.data);
87
+ const createVisualizerFrame = createSpectrumVisualizerFrameGenerator();
88
+ const frameSpectrumDown = createVisualizerFrame({
89
+ backgroundImageBuffer,
90
+ spectrum: [0.5, 0, 1],
91
+ size: { width: 15, height: 20 },
92
+ position: { x: 15, y: 0 },
93
+ rotation: 'down',
94
+ margin: 4,
95
+ color: { red: 1, green: 1, blue: 1 },
96
+ opacity: 1,
97
+ });
98
+ const resultSpectrumDown = frameSpectrumDown.data.toJSON().data;
99
+ expect(resultSpectrumDown).to.have.length(expectedImageLength);
100
+
101
+ const frameSpectrumUp = createVisualizerFrame({
102
+ backgroundImageBuffer,
103
+ spectrum: [0.1, 1, 0],
104
+ size: { width: 25, height: 23 },
105
+ position: { x: 10, y: 5 },
106
+ rotation: 'up',
107
+ margin: 4,
108
+ color: { red: 0, green: 123, blue: 69 },
109
+ opacity: 1,
110
+ });
111
+ const resultSpectrumUp = frameSpectrumUp.data.toJSON().data;
112
+ expect(resultSpectrumUp).to.have.length(expectedImageLength);
113
+
114
+ const frameSpectrumMirror = createVisualizerFrame({
115
+ backgroundImageBuffer,
116
+ spectrum: [0.1, 1, 0],
117
+ size: { width: 25, height: 23 },
118
+ position: { x: 10, y: 5 },
119
+ rotation: 'mirror',
120
+ margin: 4,
121
+ color: { red: 0, green: 123, blue: 69 },
122
+ opacity: 1,
123
+ });
124
+ const resultSpectrumMirror = frameSpectrumMirror.data.toJSON().data;
125
+ expect(resultSpectrumMirror).to.have.length(expectedImageLength);
126
+
127
+ const frameSpectrumUpOpacity50 = createVisualizerFrame({
128
+ backgroundImageBuffer,
129
+ spectrum: [0.1, 1, 0],
130
+ size: { width: 25, height: 23 },
131
+ position: { x: 10, y: 5 },
132
+ rotation: 'up',
133
+ margin: 4,
134
+ color: { red: 0, green: 123, blue: 69 },
135
+ opacity: 0.5,
136
+ });
137
+ const resultSpectrumUpOpacity50 = frameSpectrumUpOpacity50.data.toJSON().data;
138
+ expect(resultSpectrumUpOpacity50).to.have.length(expectedImageLength);
139
+ });
140
+
141
+ it('createPolarVisualizerFrameGenerator', async function () {
142
+ const expectedImageLength = 6220854;
143
+ const backgroundImagePath = path.resolve('example/media/horses.png');
144
+ const backgroundImageBmpBuffer = await convertToBmp(backgroundImagePath);
145
+ const backgroundImage = parseImage(backgroundImageBmpBuffer);
146
+ const bpmEncoder = createBpmEncoder({ width: backgroundImage.width, height: backgroundImage.height });
147
+ const backgroundImageBuffer = bpmEncoder(backgroundImage.data);
148
+ const createPolarVisualizerFrame = createPolarVisualizerFrameGenerator();
149
+
150
+ const framePolarBasic = createPolarVisualizerFrame({
151
+ backgroundImageBuffer,
152
+ spectrum: [0.5, 0.8, 0.3, 0.9, 0.6, 0.4, 0.7, 0.2],
153
+ centerX: 500,
154
+ centerY: 400,
155
+ innerRadius: 100,
156
+ maxBarLength: 150,
157
+ barWidth: 5,
158
+ color: { red: 255, green: 100, blue: 50 },
159
+ opacity: 1,
160
+ });
161
+ const resultPolarBasic = framePolarBasic.data.toJSON().data;
162
+ expect(resultPolarBasic).to.have.length(expectedImageLength);
163
+
164
+ const framePolarVolume = createPolarVisualizerFrame({
165
+ backgroundImageBuffer,
166
+ spectrum: [0.2, 0.5, 0.8, 0.3, 0.6, 0.9, 0.4, 0.7],
167
+ centerX: 500,
168
+ centerY: 400,
169
+ innerRadius: 80,
170
+ maxBarLength: 120,
171
+ barWidth: 4,
172
+ color: { red: 100, green: 200, blue: 255 },
173
+ opacity: 1,
174
+ spectrumEffect: 'volume',
175
+ });
176
+ const resultPolarVolume = framePolarVolume.data.toJSON().data;
177
+ expect(resultPolarVolume).to.have.length(expectedImageLength);
178
+
179
+ const framePolarSmooth = createPolarVisualizerFrame({
180
+ backgroundImageBuffer,
181
+ spectrum: [0.7, 0.4, 0.9, 0.2, 0.5, 0.8, 0.3, 0.6],
182
+ centerX: 500,
183
+ centerY: 400,
184
+ innerRadius: 90,
185
+ maxBarLength: 130,
186
+ barWidth: 6,
187
+ color: { red: 255, green: 255, blue: 0 },
188
+ opacity: 1,
189
+ spectrumEffect: 'smooth',
190
+ });
191
+ const resultPolarSmooth = framePolarSmooth.data.toJSON().data;
192
+ expect(resultPolarSmooth).to.have.length(expectedImageLength);
193
+
194
+ const framePolarOpacity50 = createPolarVisualizerFrame({
195
+ backgroundImageBuffer,
196
+ spectrum: [0.6, 0.3, 0.8, 0.5, 0.4, 0.7, 0.2, 0.9],
197
+ centerX: 500,
198
+ centerY: 400,
199
+ innerRadius: 100,
200
+ maxBarLength: 140,
201
+ barWidth: 5,
202
+ color: { red: 0, green: 255, blue: 128 },
203
+ opacity: 0.5,
204
+ });
205
+ const resultPolarOpacity50 = framePolarOpacity50.data.toJSON().data;
206
+ expect(resultPolarOpacity50).to.have.length(expectedImageLength);
207
+
208
+ const framePolarHexColor = createPolarVisualizerFrame({
209
+ backgroundImageBuffer,
210
+ spectrum: [0.5, 0.6, 0.7, 0.8, 0.9, 0.4, 0.3, 0.2],
211
+ centerX: 500,
212
+ centerY: 400,
213
+ innerRadius: 110,
214
+ maxBarLength: 160,
215
+ barWidth: 7,
216
+ color: '#FF5733',
217
+ opacity: 1,
218
+ });
219
+ const resultPolarHexColor = framePolarHexColor.data.toJSON().data;
220
+ expect(resultPolarHexColor).to.have.length(expectedImageLength);
221
+ });
222
+ });
@@ -1,63 +1,156 @@
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 * as image from '../image';
9
- import * as audio from '../audio';
10
- import * as video from '../video';
11
-
12
- const EXIT_CODE = 0;
13
- const sandbox = createSandbox();
14
-
15
- describe('index', function () {
16
-
17
- this.beforeAll(function () {
18
- sandbox.stub(fs, 'readFileSync').returns(Buffer.from([1, 1]));
19
- sandbox.stub(audio, 'createAudioBuffer').returns(
20
- new Promise(resolve => resolve({ audioBuffer: Buffer.from([1, 1]), sampleRate: 2 }))
21
- );
22
- sandbox.stub(image, 'getImageColor').returns({ red: 0, green: 0, blue: 0 });
23
-
24
- let exitCallback: Function = () => { };
25
- const stdin = new Writable({
26
- write(_chunk: Buffer, _enc: string, cb: (error?: Error | null) => void) {
27
- exitCallback(EXIT_CODE);
28
- cb();
29
- },
30
- });
31
- let childProcessStream: unknown = {
32
- stdin,
33
- stderr: new Readable(),
34
- on: function (_: string, callback: Function) {
35
- exitCallback = callback;
36
- },
37
- };
38
- sandbox.stub(video, 'spawnFfmpegVideoWriter').returns(childProcessStream as ChildProcessWithoutNullStreams);
39
- });
40
-
41
- this.afterAll(function () {
42
- sandbox.restore();
43
- });
44
-
45
- it('renderAudioVisualizer', async function () {
46
- const config: Config = {
47
- audio: {
48
- path: 'audioPath'
49
- },
50
- image: {
51
- path: 'example/media/horses.png'
52
- },
53
- outVideo: {
54
- path: 'outVideoPath',
55
- fps: 1
56
- }
57
- };
58
-
59
- const { exitCode, outputVideoFiles } = await renderAudioVisualizer(config);
60
- expect(exitCode).equal(EXIT_CODE);
61
- expect(outputVideoFiles).deep.equal([path.resolve('outVideoPath')]);
62
- });
63
- });
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, outputVideoFiles } = await renderAudioVisualizer(config);
75
+ expect(exitCode).equal(EXIT_CODE);
76
+ expect(outputVideoFiles).deep.equal([path.resolve('outVideoPath')]);
77
+ });
78
+
79
+ it('resolves and does not report 100 when process completion times out', async function () {
80
+ (this as any).setMockWriter(false);
81
+ sandbox.stub(video, 'waitForProcessExit').callsFake(
82
+ () => Promise.resolve(1)
83
+ );
84
+ const progressValues: number[] = [];
85
+ const config: Config = {
86
+ audio: {
87
+ path: 'audioPath'
88
+ },
89
+ image: {
90
+ path: 'example/media/horses.png'
91
+ },
92
+ outVideo: {
93
+ path: 'outVideoPath',
94
+ fps: 1
95
+ }
96
+ };
97
+
98
+ const timeoutPromise = new Promise<never>((_resolve, reject) => {
99
+ setTimeout(() => reject(new Error('renderAudioVisualizer hung')), 200);
100
+ });
101
+
102
+ const { exitCode, outputVideoFiles } = await Promise.race([
103
+ renderAudioVisualizer(config, (progress) => progressValues.push(progress)),
104
+ timeoutPromise,
105
+ ]);
106
+
107
+ expect(exitCode).equal(1);
108
+ expect(outputVideoFiles).deep.equal([]);
109
+ expect(progressValues.some(progress => progress === 100)).equal(false);
110
+ });
111
+
112
+ it('aborts render when video frame reads fail repeatedly', async function () {
113
+ const videoFrameReaderOutput = new Readable();
114
+ videoFrameReaderOutput._read = () => {};
115
+ sandbox.stub(video, 'getVideoInfo').resolves({
116
+ width: 1,
117
+ height: 1,
118
+ duration: 1,
119
+ });
120
+ sandbox.stub(video, 'writeConcatFile').returns('fake-concat-path.txt');
121
+ sandbox.stub(video, 'spawnConcatVideoFrameReader').returns({
122
+ stdout: videoFrameReaderOutput,
123
+ kill: () => {},
124
+ } as any);
125
+ sandbox.stub(image, 'getVideoFrameColor').returns({ red: 0, green: 0, blue: 0 });
126
+ sandbox.stub(bpmEncoder, 'createBgrFrameEncoder').returns(
127
+ () => ({ shiftPos: 0, rowBytes: 0, data: Buffer.from([1, 2, 3]) }),
128
+ );
129
+ let frameReadCount = 0;
130
+ sandbox.stub(video, 'readVideoFrame').callsFake(async () => {
131
+ frameReadCount += 1;
132
+ if (frameReadCount === 1) {
133
+ return Buffer.from([1, 2, 3]);
134
+ }
135
+ return null;
136
+ });
137
+
138
+ const config: Config = {
139
+ audio: {
140
+ path: 'audioPath'
141
+ },
142
+ video: {
143
+ path: 'example/media/test-blur.mp4',
144
+ },
145
+ outVideo: {
146
+ path: 'outVideoPath',
147
+ fps: 10,
148
+ }
149
+ };
150
+
151
+ const { exitCode, outputVideoFiles } = await renderAudioVisualizer(config);
152
+ expect(exitCode).equal(1);
153
+ expect(outputVideoFiles).deep.equal([]);
154
+ expect(frameReadCount).greaterThan(1);
155
+ });
156
+ });