nodejs-audio-visualizer 1.1.1 → 2.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.
package/src/image.ts CHANGED
@@ -1,3 +1,4 @@
1
+ import { RotationAliasName } from './index';
1
2
  import { decode, encode, BmpDecoder } from 'bmp-js';
2
3
  import Jimp from 'jimp';
3
4
 
@@ -29,9 +30,18 @@ export const drawRect = (imageDstBuffer: BmpDecoder, position: Position, size: S
29
30
  }
30
31
  };
31
32
 
32
- const drawSpectrum = (imageDstBuffer: BmpDecoder, spectrum: number[], size: Size, color: Color) => {
33
- const paddingLeft = Math.trunc(imageDstBuffer.width / 2 - size.width / 2);
33
+ const drawSpectrum = (
34
+ imageDstBuffer: BmpDecoder,
35
+ spectrum: number[],
36
+ size: Size,
37
+ position: Position,
38
+ rotation: RotationAliasName,
39
+ color: Color
40
+ ) => {
34
41
  const busWidth = Math.trunc(size.width / spectrum.length);
42
+ const left = Math.trunc(position.x);
43
+ const top = Math.trunc(position.y);
44
+ const height = Math.trunc(size.height);
35
45
  const margin = 4;
36
46
 
37
47
  for (let spectrumX = 0; spectrumX < spectrum.length; spectrumX++) {
@@ -40,17 +50,18 @@ const drawSpectrum = (imageDstBuffer: BmpDecoder, spectrum: number[], size: Size
40
50
  throw new Error('Spectrum values must be in range from 0 to 1');
41
51
  }
42
52
 
43
- const rectX = paddingLeft + busWidth * spectrumX;
44
- const rectHeight = size.height * spectrumValue;
45
- drawRect(imageDstBuffer, { x: rectX + margin, y: 0 }, { width: busWidth - margin / 2, height: rectHeight }, color);
53
+ const rectX = left + busWidth * spectrumX;
54
+ const rectHeight = Math.trunc(height * spectrumValue);
55
+ const rectY = (rotation === 'up') ? top + height - rectHeight : top;
56
+ drawRect(imageDstBuffer, { x: rectX + margin, y: rectY }, { width: busWidth - margin / 2, height: rectHeight }, color);
46
57
  }
47
58
  };
48
59
 
49
- export const createVisualizerFrame = (backgroundImageBuffer: BmpDecoder, spectrum: number[], size: Size, color: Color | string) => {
60
+ export const createVisualizerFrame = (backgroundImageBuffer: BmpDecoder, spectrum: number[], size: Size, position: Position, rotation: RotationAliasName, color: Color | string) => {
50
61
  const image = Object.assign({}, backgroundImageBuffer);
51
62
  image.data = Buffer.from(image.data);
52
63
  const rgbSpectrumColor = (typeof color === 'string') ? hexToRgb(color) : color;
53
- drawSpectrum(image, spectrum, size, rgbSpectrumColor);
64
+ drawSpectrum(image, spectrum, size, position, rotation, rgbSpectrumColor);
54
65
  return image;
55
66
  };
56
67
 
package/src/index.ts CHANGED
@@ -1,4 +1,19 @@
1
- import path from 'path';
1
+ import {
2
+ getAudioFilePath,
3
+ getBackgroundImagePath,
4
+ getOutVideoPath,
5
+ getFPS,
6
+ getSpectrumWidthAbsolute,
7
+ getSpectrumHeightAbsolute,
8
+ getSpectrumXAbsolute,
9
+ getSpectrumYAbsolute,
10
+ getSpectrumColor,
11
+ getFfmpeg_cfr,
12
+ getFfmpeg_preset,
13
+ getFrame_processing_delay,
14
+ rotationAliasValues,
15
+ getSpectrumRotation
16
+ } from './config';
2
17
  import { createAudioBuffer, bufferToUInt8, createSpectrumsProcessor } from './audio';
3
18
  import { parseImage, createVisualizerFrame, createImageBuffer, getImageColor, invertColor, Color, convertToBmp } from './image';
4
19
  import { spawnFfmpegVideoWriter, getProgress, calculateProgress, waitDrain } from './video';
@@ -21,8 +36,11 @@ export interface Config {
21
36
  path: string;
22
37
  fps?: number;
23
38
  spectrum?: {
24
- width?: number;
25
- height?: number;
39
+ width?: SpectrumSizeValue;
40
+ height?: SpectrumSizeValue;
41
+ x?: number | PositionAliasName;
42
+ y?: number | PositionAliasName;
43
+ rotation?: RotationAliasName;
26
44
  color?: Color | string;
27
45
  }
28
46
  };
@@ -33,14 +51,26 @@ export interface Config {
33
51
  };
34
52
  }
35
53
 
54
+ export type SpectrumSizeValue = number | string;
55
+
56
+ export type PositionAliasName =
57
+ 'left' |
58
+ 'center' |
59
+ 'right' |
60
+ 'top' |
61
+ 'middle' |
62
+ 'bottom';
63
+
64
+ export type RotationAliasName = typeof rotationAliasValues[number];
65
+
36
66
  const sleep = (timeout: number) =>
37
- new Promise(resolve => setTimeout(resolve, 1000));
67
+ new Promise(resolve => setTimeout(resolve, timeout));
38
68
 
39
69
  export const renderAudioVisualizer = (config: Config, onProgress?: (progress: number) => any, shouldStop?: () => boolean) =>
40
70
  new Promise<number>(async (resolve) => {
41
- const audioFilePath = path.resolve(config.audio.path);
42
- const backgroundImagePath = path.resolve(config.image.path);
43
- const outVideoPath = path.resolve(config.outVideo.path);
71
+ const audioFilePath = getAudioFilePath(config);
72
+ const backgroundImagePath = getBackgroundImagePath(config);
73
+ const outVideoPath = getOutVideoPath(config);
44
74
 
45
75
  const backgroundImageBmpBuffer = await convertToBmp(backgroundImagePath);
46
76
  const backgroundImage = parseImage(backgroundImageBmpBuffer);
@@ -51,22 +81,23 @@ export const renderAudioVisualizer = (config: Config, onProgress?: (progress: nu
51
81
  throw new Error('ffmpeg didn\'t show audio sample rate');
52
82
  }
53
83
 
54
- const FPS = config.outVideo.fps || 60;
84
+ const FPS = getFPS(config);
55
85
  const spectrumWidth =
56
- (config.outVideo.spectrum && config.outVideo.spectrum.width) ||
57
- backgroundImage.width * 0.4;
86
+ getSpectrumWidthAbsolute(config, backgroundImage.width);
58
87
  const spectrumHeight =
59
- (config.outVideo.spectrum && config.outVideo.spectrum.height) ||
60
- backgroundImage.height * 0.1;
88
+ getSpectrumHeightAbsolute(config, backgroundImage.height);
89
+ const spectrumX =
90
+ getSpectrumXAbsolute(config, spectrumWidth, backgroundImage.width);
91
+ const spectrumY =
92
+ getSpectrumYAbsolute(config, spectrumHeight, backgroundImage.height);
93
+ const spectrumRotation =
94
+ getSpectrumRotation(config);
61
95
  const spectrumColor =
62
- (config.outVideo.spectrum && config.outVideo.spectrum.color) ||
96
+ getSpectrumColor(config) ||
63
97
  invertColor(getImageColor(backgroundImage));
64
- const ffmpeg_cfr =
65
- config.tweaks && config.tweaks.ffmpeg_cfr;
66
- const ffmpeg_preset =
67
- config.tweaks && config.tweaks.ffmpeg_preset;
68
- const frame_processing_delay =
69
- config.tweaks && config.tweaks.frame_processing_delay;
98
+ const ffmpeg_cfr = getFfmpeg_cfr(config);
99
+ const ffmpeg_preset = getFfmpeg_preset(config);
100
+ const frame_processing_delay = getFrame_processing_delay(config);
70
101
  const spectrumBusesCount = 64;
71
102
 
72
103
  const audioDuration = audioBuffer.length / sampleRate;
@@ -92,6 +123,8 @@ export const renderAudioVisualizer = (config: Config, onProgress?: (progress: nu
92
123
  backgroundImage,
93
124
  spectrum,
94
125
  { width: spectrumWidth, height: spectrumHeight },
126
+ { x: spectrumX, y: spectrumY },
127
+ spectrumRotation,
95
128
  spectrumColor
96
129
  );
97
130
  const frameImageBuffer = createImageBuffer(frameImage);
@@ -0,0 +1,316 @@
1
+ import path from 'path';
2
+ import { expect } from 'chai';
3
+ import { Config } from '../index';
4
+ import {
5
+ defaults,
6
+ getAudioFilePath,
7
+ getBackgroundImagePath,
8
+ getOutVideoPath,
9
+ getFPS,
10
+ getSpectrumWidthAbsolute,
11
+ getSpectrumHeightAbsolute,
12
+ getSpectrumXAbsolute,
13
+ getSpectrumYAbsolute,
14
+ getSpectrumColor,
15
+ getFfmpeg_cfr,
16
+ getFfmpeg_preset,
17
+ getFrame_processing_delay,
18
+ checkIsValidRotationAlias,
19
+ checkIsPercentValue
20
+ } from '../config';
21
+
22
+ describe('config', function() {
23
+ it('getAudioFilePath', function() {
24
+ const result = getAudioFilePath({
25
+ audio: { path: 'test/path' }
26
+ } as Config);
27
+ const expected = path.resolve('test/path');
28
+ expect(result).equal(expected);
29
+ });
30
+
31
+ it('getBackgroundImagePath', function() {
32
+ const result = getBackgroundImagePath({
33
+ image: { path: 'test/path' }
34
+ } as Config);
35
+ const expected = path.resolve('test/path');
36
+ expect(result).equal(expected);
37
+ });
38
+
39
+ it('getOutVideoPath', function() {
40
+ const result = getOutVideoPath({
41
+ outVideo: { path: 'test/path' }
42
+ } as Config);
43
+ const expected = path.resolve('test/path');
44
+ expect(result).equal(expected);
45
+ });
46
+
47
+ it('getFPS', function() {
48
+ const result1 = getFPS({
49
+ outVideo: {}
50
+ } as Config);
51
+ expect(result1).equal(defaults.fps);
52
+
53
+ const result2 = getFPS({
54
+ outVideo: { fps: 15 }
55
+ } as Config);
56
+ expect(result2).equal(15);
57
+ });
58
+
59
+ it('checkIsPercentValue', function() {
60
+ const result1 = checkIsPercentValue('69%');
61
+ expect(result1).equal(true);
62
+
63
+ const result2 = checkIsPercentValue('69');
64
+ expect(result2).equal(false);
65
+
66
+ const result3 = checkIsPercentValue('fake value');
67
+ expect(result3).equal(false);
68
+ });
69
+
70
+ it('getSpectrumWidthAbsolute', function() {
71
+ const backgroundImageWidth = 1366;
72
+ const result1 = getSpectrumWidthAbsolute({
73
+ outVideo: {}
74
+ } as Config, backgroundImageWidth);
75
+ expect(result1).equal(
76
+ backgroundImageWidth * (parseInt(defaults.spectrumWidth) / 100)
77
+ );
78
+
79
+ const result2 = getSpectrumWidthAbsolute({
80
+ outVideo: { spectrum: { width: 420 }}
81
+ } as Config, backgroundImageWidth);
82
+ expect(result2).equal(420);
83
+
84
+ const result3 = getSpectrumWidthAbsolute({
85
+ outVideo: { spectrum: { width: '69%' }}
86
+ } as Config, backgroundImageWidth);
87
+ expect(result3).equal(backgroundImageWidth * 0.69);
88
+
89
+ const result4 = getSpectrumWidthAbsolute.bind(
90
+ undefined,
91
+ {
92
+ outVideo: { spectrum: { width: '69' }}
93
+ } as Config,
94
+ backgroundImageWidth
95
+ );
96
+ expect(result4).to.throw('Invalid spectrum width value: \'69\'. Use number value or percent value in string, for example: \'30%\'.');
97
+ });
98
+
99
+ it('getSpectrumHeightAbsolute', function() {
100
+ const backgroundImageHeight = 768;
101
+ const result1 = getSpectrumHeightAbsolute({
102
+ outVideo: {}
103
+ } as Config, backgroundImageHeight);
104
+ expect(result1).equal(
105
+ backgroundImageHeight * (parseInt(defaults.spectrumHeight) / 100)
106
+ );
107
+
108
+ const result2 = getSpectrumHeightAbsolute({
109
+ outVideo: { spectrum: { height: 420 }}
110
+ } as Config, backgroundImageHeight);
111
+ expect(result2).equal(420);
112
+
113
+ const result3 = getSpectrumHeightAbsolute({
114
+ outVideo: { spectrum: { height: '69%' }}
115
+ } as Config, backgroundImageHeight);
116
+ expect(result3).equal(backgroundImageHeight * 0.69);
117
+
118
+ const result4 = getSpectrumHeightAbsolute.bind(
119
+ undefined,
120
+ {
121
+ outVideo: { spectrum: { height: '69' }}
122
+ } as Config,
123
+ backgroundImageHeight
124
+ );
125
+ expect(result4).to.throw('Invalid spectrum height value: \'69\'. Use number value or percent value in string, for example: \'30%\'.');
126
+ });
127
+
128
+ it('getSpectrumXAbsolute', function() {
129
+ const spectrumWidth = 420;
130
+ const backgroundImageWidth = 1366;
131
+ const result1 = getSpectrumXAbsolute(
132
+ {
133
+ outVideo: {},
134
+ } as Config,
135
+ spectrumWidth,
136
+ backgroundImageWidth
137
+ );
138
+ expect(result1).equal(
139
+ 0.5 * backgroundImageWidth - spectrumWidth / 2
140
+ );
141
+
142
+ const result2 = getSpectrumXAbsolute(
143
+ {
144
+ outVideo: { spectrum: { x: 420 } }
145
+ } as Config,
146
+ spectrumWidth,
147
+ backgroundImageWidth
148
+ );
149
+ expect(result2).equal(420);
150
+
151
+ const result3 = getSpectrumXAbsolute(
152
+ {
153
+ outVideo: { spectrum: { x: 'left' } }
154
+ } as Config,
155
+ spectrumWidth,
156
+ backgroundImageWidth
157
+ );
158
+ expect(result3).equal(0);
159
+
160
+ const result4 = getSpectrumXAbsolute(
161
+ {
162
+ outVideo: { spectrum: { x: 'center' } }
163
+ } as Config,
164
+ spectrumWidth,
165
+ backgroundImageWidth
166
+ );
167
+ expect(result4).equal(
168
+ 0.5 * backgroundImageWidth - spectrumWidth / 2
169
+ );
170
+
171
+ const result5 = getSpectrumXAbsolute(
172
+ {
173
+ outVideo: { spectrum: { x: 'right' } }
174
+ } as Config,
175
+ spectrumWidth,
176
+ backgroundImageWidth
177
+ );
178
+ expect(result5).equal(
179
+ 1 * backgroundImageWidth - spectrumWidth
180
+ );
181
+
182
+ const result6 = getSpectrumXAbsolute.bind(
183
+ undefined,
184
+ {
185
+ outVideo: { spectrum: { x: 'sagewklkg' } }
186
+ } as unknown as Config,
187
+ spectrumWidth,
188
+ backgroundImageWidth
189
+ );
190
+ expect(result6).to.throw('Invalid spectrum x value: sagewklkg. Valid values: left, center, right, top, middle, bottom.');
191
+ });
192
+
193
+ it('getSpectrumYAbsolute', function() {
194
+ const spectrumHeight = 125;
195
+ const backgroundImageHeight = 768;
196
+ const result1 = getSpectrumYAbsolute(
197
+ {
198
+ outVideo: {},
199
+ } as Config,
200
+ spectrumHeight,
201
+ backgroundImageHeight
202
+ );
203
+ expect(result1).equal(
204
+ 0
205
+ );
206
+
207
+ const result2 = getSpectrumYAbsolute(
208
+ {
209
+ outVideo: { spectrum: { y: 69 } }
210
+ } as Config,
211
+ spectrumHeight,
212
+ backgroundImageHeight
213
+ );
214
+ expect(result2).equal(69);
215
+
216
+ const result3 = getSpectrumYAbsolute(
217
+ {
218
+ outVideo: { spectrum: { y: 'top' } }
219
+ } as Config,
220
+ spectrumHeight,
221
+ backgroundImageHeight
222
+ );
223
+ expect(result3).equal(0);
224
+
225
+ const result4 = getSpectrumYAbsolute(
226
+ {
227
+ outVideo: { spectrum: { y: 'middle' } }
228
+ } as Config,
229
+ spectrumHeight,
230
+ backgroundImageHeight
231
+ );
232
+ expect(result4).equal(
233
+ 0.5 * backgroundImageHeight - spectrumHeight / 2
234
+ );
235
+
236
+ const result5 = getSpectrumYAbsolute(
237
+ {
238
+ outVideo: { spectrum: { y: 'bottom' } }
239
+ } as Config,
240
+ spectrumHeight,
241
+ backgroundImageHeight
242
+ );
243
+ expect(result5).equal(
244
+ 1 * backgroundImageHeight - spectrumHeight
245
+ );
246
+
247
+ const result6 = getSpectrumYAbsolute.bind(
248
+ undefined,
249
+ {
250
+ outVideo: { spectrum: { y: 'asokosckm' } }
251
+ } as unknown as Config,
252
+ spectrumHeight,
253
+ backgroundImageHeight
254
+ );
255
+ expect(result6).to.throw('Invalid spectrum y value: asokosckm. Valid values: left, center, right, top, middle, bottom.');
256
+ });
257
+
258
+ it('getSpectrumColor', function() {
259
+ const result1 = getSpectrumColor({
260
+ outVideo: {}
261
+ } as Config);
262
+ expect(result1).equal(undefined);
263
+
264
+ const result2 = getSpectrumColor({
265
+ outVideo: { spectrum: { color: '#cccc99' } }
266
+ } as Config);
267
+ expect(result2).equal('#cccc99');
268
+ });
269
+
270
+ it('getFfmpeg_cfr', function() {
271
+ const result1 = getFfmpeg_cfr({
272
+ tweaks: {}
273
+ } as Config);
274
+ expect(result1).equal(undefined);
275
+
276
+ const result2 = getFfmpeg_cfr({
277
+ tweaks: { ffmpeg_cfr: '69' }
278
+ } as Config);
279
+ expect(result2).equal('69');
280
+ });
281
+
282
+ it('getFfmpeg_preset', function() {
283
+ const result1 = getFfmpeg_preset({
284
+ tweaks: {}
285
+ } as Config);
286
+ expect(result1).equal(undefined);
287
+
288
+ const result2 = getFfmpeg_preset({
289
+ tweaks: { ffmpeg_preset: 'quality' }
290
+ } as Config);
291
+ expect(result2).equal('quality');
292
+ });
293
+
294
+ it('getFrame_processing_delay', function() {
295
+ const result1 = getFrame_processing_delay({
296
+ tweaks: {}
297
+ } as Config);
298
+ expect(result1).equal(undefined);
299
+
300
+ const result2 = getFrame_processing_delay({
301
+ tweaks: { frame_processing_delay: 420 }
302
+ } as Config);
303
+ expect(result2).equal(420);
304
+ });
305
+
306
+ it('checkIsValidRotationAlias', function() {
307
+ const result1 = checkIsValidRotationAlias('up');
308
+ expect(result1).equal(true);
309
+
310
+ const result2 = checkIsValidRotationAlias('down');
311
+ expect(result2).equal(true);
312
+
313
+ const result3 = checkIsValidRotationAlias('fake value');
314
+ expect(result3).equal(false);
315
+ });
316
+ });
@@ -2,6 +2,9 @@ import { expect } from 'chai';
2
2
  import path from 'path';
3
3
  import { drawRect, Color, createVisualizerFrame, parseImage, convertToBmp } from '../image';
4
4
 
5
+ import frameSnapshotSpectrumDown from './snapshots/frameSnapshotSpectrumDown.json';
6
+ import frameSnapshotSpectrumUp from './snapshots/frameSnapshotSpectrumUp.json';
7
+
5
8
  describe('image', function () {
6
9
  it('drawRect', function () {
7
10
  const imageWidth = 10;
@@ -32,12 +35,26 @@ describe('image', function () {
32
35
  const backgroundImagePath = path.resolve('example/media/background.png');
33
36
  const backgroundImageBmpBuffer = await convertToBmp(backgroundImagePath);
34
37
  const backgroundImage = parseImage(backgroundImageBmpBuffer);
35
- const frame = await createVisualizerFrame(
38
+ const frameSpectrumDown = createVisualizerFrame(
36
39
  backgroundImage,
37
40
  [0.5, 0, 1],
38
41
  { width: 15, height: 20 },
42
+ { x: 15, y: 0 },
43
+ 'down',
39
44
  { red: 1, green: 1, blue: 1 }
40
45
  );
41
- expect(frame.data.toJSON().data.length).equal(1090560);
46
+ const resultSpectrumDown = frameSpectrumDown.data.toJSON().data;
47
+ expect(resultSpectrumDown).deep.equal(frameSnapshotSpectrumDown);
48
+
49
+ const frameSpectrumUp = createVisualizerFrame(
50
+ backgroundImage,
51
+ [0.1, 1, 0],
52
+ { width: 25, height: 23 },
53
+ { x: 10, y: 5 },
54
+ 'up',
55
+ { red: 0, green: 123, blue: 69 }
56
+ );
57
+ const resultSpectrumUp = frameSpectrumUp.data.toJSON().data;
58
+ expect(resultSpectrumUp).deep.equal(frameSnapshotSpectrumUp);
42
59
  });
43
60
  });