nodejs-audio-visualizer 1.2.0 → 2.1.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.
@@ -9,10 +9,16 @@ import {
9
9
  getFPS,
10
10
  getSpectrumWidthAbsolute,
11
11
  getSpectrumHeightAbsolute,
12
+ getSpectrumXAbsolute,
13
+ getSpectrumYAbsolute,
12
14
  getSpectrumColor,
15
+ getSpectrumOpacity,
16
+ getSpectrumOpacityParsed,
13
17
  getFfmpeg_cfr,
14
18
  getFfmpeg_preset,
15
- getFrame_processing_delay
19
+ getFrame_processing_delay,
20
+ checkIsValidRotationAlias,
21
+ checkIsPercentValue
16
22
  } from '../config';
17
23
 
18
24
  describe('config', function() {
@@ -52,13 +58,24 @@ describe('config', function() {
52
58
  expect(result2).equal(15);
53
59
  });
54
60
 
61
+ it('checkIsPercentValue', function() {
62
+ const result1 = checkIsPercentValue('69%');
63
+ expect(result1).equal(true);
64
+
65
+ const result2 = checkIsPercentValue('69');
66
+ expect(result2).equal(false);
67
+
68
+ const result3 = checkIsPercentValue('fake value');
69
+ expect(result3).equal(false);
70
+ });
71
+
55
72
  it('getSpectrumWidthAbsolute', function() {
56
73
  const backgroundImageWidth = 1366;
57
74
  const result1 = getSpectrumWidthAbsolute({
58
75
  outVideo: {}
59
76
  } as Config, backgroundImageWidth);
60
77
  expect(result1).equal(
61
- backgroundImageWidth * defaults.spectrumWidth
78
+ backgroundImageWidth * (parseInt(defaults.spectrumWidth) / 100)
62
79
  );
63
80
 
64
81
  const result2 = getSpectrumWidthAbsolute({
@@ -67,9 +84,36 @@ describe('config', function() {
67
84
  expect(result2).equal(420);
68
85
 
69
86
  const result3 = getSpectrumWidthAbsolute({
70
- outVideo: { spectrum: { width: 0.69 }}
87
+ outVideo: { spectrum: { width: '69%' }}
71
88
  } as Config, backgroundImageWidth);
72
89
  expect(result3).equal(backgroundImageWidth * 0.69);
90
+
91
+ const result4 = getSpectrumWidthAbsolute.bind(
92
+ undefined,
93
+ {
94
+ outVideo: { spectrum: { width: '69' }}
95
+ } as Config,
96
+ backgroundImageWidth
97
+ );
98
+ expect(result4).to.throw('Invalid spectrum width value: \'69\'. Use number value or percent value in string, for example: \'30%\'.');
99
+
100
+ const result5 = getSpectrumWidthAbsolute.bind(
101
+ undefined,
102
+ {
103
+ outVideo: { spectrum: { width: 1 }}
104
+ } as Config,
105
+ backgroundImageWidth
106
+ );
107
+ expect(result5).to.throw('Spectrum width \'1\' is too small.');
108
+
109
+ const result6 = getSpectrumWidthAbsolute.bind(
110
+ undefined,
111
+ {
112
+ outVideo: { spectrum: { width: '1%' }}
113
+ } as Config,
114
+ backgroundImageWidth
115
+ );
116
+ expect(result6).to.throw('Spectrum width \'1%\' is too small.');
73
117
  });
74
118
 
75
119
  it('getSpectrumHeightAbsolute', function() {
@@ -78,7 +122,7 @@ describe('config', function() {
78
122
  outVideo: {}
79
123
  } as Config, backgroundImageHeight);
80
124
  expect(result1).equal(
81
- backgroundImageHeight * defaults.spectrumHeight
125
+ backgroundImageHeight * (parseInt(defaults.spectrumHeight) / 100)
82
126
  );
83
127
 
84
128
  const result2 = getSpectrumHeightAbsolute({
@@ -87,9 +131,148 @@ describe('config', function() {
87
131
  expect(result2).equal(420);
88
132
 
89
133
  const result3 = getSpectrumHeightAbsolute({
90
- outVideo: { spectrum: { height: 0.69 }}
134
+ outVideo: { spectrum: { height: '69%' }}
91
135
  } as Config, backgroundImageHeight);
92
136
  expect(result3).equal(backgroundImageHeight * 0.69);
137
+
138
+ const result4 = getSpectrumHeightAbsolute.bind(
139
+ undefined,
140
+ {
141
+ outVideo: { spectrum: { height: '69' }}
142
+ } as Config,
143
+ backgroundImageHeight
144
+ );
145
+ expect(result4).to.throw('Invalid spectrum height value: \'69\'. Use number value or percent value in string, for example: \'30%\'.');
146
+ });
147
+
148
+ it('getSpectrumXAbsolute', function() {
149
+ const spectrumWidth = 420;
150
+ const backgroundImageWidth = 1366;
151
+ const result1 = getSpectrumXAbsolute(
152
+ {
153
+ outVideo: {},
154
+ } as Config,
155
+ spectrumWidth,
156
+ backgroundImageWidth
157
+ );
158
+ expect(result1).equal(
159
+ 0.5 * backgroundImageWidth - spectrumWidth / 2
160
+ );
161
+
162
+ const result2 = getSpectrumXAbsolute(
163
+ {
164
+ outVideo: { spectrum: { x: 420 } }
165
+ } as Config,
166
+ spectrumWidth,
167
+ backgroundImageWidth
168
+ );
169
+ expect(result2).equal(420);
170
+
171
+ const result3 = getSpectrumXAbsolute(
172
+ {
173
+ outVideo: { spectrum: { x: 'left' } }
174
+ } as Config,
175
+ spectrumWidth,
176
+ backgroundImageWidth
177
+ );
178
+ expect(result3).equal(0);
179
+
180
+ const result4 = getSpectrumXAbsolute(
181
+ {
182
+ outVideo: { spectrum: { x: 'center' } }
183
+ } as Config,
184
+ spectrumWidth,
185
+ backgroundImageWidth
186
+ );
187
+ expect(result4).equal(
188
+ 0.5 * backgroundImageWidth - spectrumWidth / 2
189
+ );
190
+
191
+ const result5 = getSpectrumXAbsolute(
192
+ {
193
+ outVideo: { spectrum: { x: 'right' } }
194
+ } as Config,
195
+ spectrumWidth,
196
+ backgroundImageWidth
197
+ );
198
+ expect(result5).equal(
199
+ 1 * backgroundImageWidth - spectrumWidth
200
+ );
201
+
202
+ const result6 = getSpectrumXAbsolute.bind(
203
+ undefined,
204
+ {
205
+ outVideo: { spectrum: { x: 'sagewklkg' } }
206
+ } as unknown as Config,
207
+ spectrumWidth,
208
+ backgroundImageWidth
209
+ );
210
+ expect(result6).to.throw('Invalid spectrum x value: sagewklkg. Valid values: left, center, right, top, middle, bottom.');
211
+ });
212
+
213
+ it('getSpectrumYAbsolute', function() {
214
+ const spectrumHeight = 125;
215
+ const backgroundImageHeight = 768;
216
+ const result1 = getSpectrumYAbsolute(
217
+ {
218
+ outVideo: {},
219
+ } as Config,
220
+ spectrumHeight,
221
+ backgroundImageHeight
222
+ );
223
+ expect(result1).equal(
224
+ 0
225
+ );
226
+
227
+ const result2 = getSpectrumYAbsolute(
228
+ {
229
+ outVideo: { spectrum: { y: 69 } }
230
+ } as Config,
231
+ spectrumHeight,
232
+ backgroundImageHeight
233
+ );
234
+ expect(result2).equal(69);
235
+
236
+ const result3 = getSpectrumYAbsolute(
237
+ {
238
+ outVideo: { spectrum: { y: 'top' } }
239
+ } as Config,
240
+ spectrumHeight,
241
+ backgroundImageHeight
242
+ );
243
+ expect(result3).equal(0);
244
+
245
+ const result4 = getSpectrumYAbsolute(
246
+ {
247
+ outVideo: { spectrum: { y: 'middle' } }
248
+ } as Config,
249
+ spectrumHeight,
250
+ backgroundImageHeight
251
+ );
252
+ expect(result4).equal(
253
+ 0.5 * backgroundImageHeight - spectrumHeight / 2
254
+ );
255
+
256
+ const result5 = getSpectrumYAbsolute(
257
+ {
258
+ outVideo: { spectrum: { y: 'bottom' } }
259
+ } as Config,
260
+ spectrumHeight,
261
+ backgroundImageHeight
262
+ );
263
+ expect(result5).equal(
264
+ 1 * backgroundImageHeight - spectrumHeight
265
+ );
266
+
267
+ const result6 = getSpectrumYAbsolute.bind(
268
+ undefined,
269
+ {
270
+ outVideo: { spectrum: { y: 'asokosckm' } }
271
+ } as unknown as Config,
272
+ spectrumHeight,
273
+ backgroundImageHeight
274
+ );
275
+ expect(result6).to.throw('Invalid spectrum y value: asokosckm. Valid values: left, center, right, top, middle, bottom.');
93
276
  });
94
277
 
95
278
  it('getSpectrumColor', function() {
@@ -104,6 +287,46 @@ describe('config', function() {
104
287
  expect(result2).equal('#cccc99');
105
288
  });
106
289
 
290
+ it('getSpectrumOpacity', function() {
291
+ const result1 = getSpectrumOpacity({
292
+ outVideo: {}
293
+ } as Config);
294
+ expect(result1).equal(defaults.spectrumOpacity);
295
+
296
+ const result2 = getSpectrumOpacity({
297
+ outVideo: { spectrum: { opacity: '69%' } }
298
+ } as Config);
299
+ expect(result2).equal('69%');
300
+ });
301
+
302
+ it('getSpectrumOpacityParsed', function() {
303
+ const result1 = getSpectrumOpacityParsed({
304
+ outVideo: {}
305
+ } as Config);
306
+ expect(result1).equal(parseInt(defaults.spectrumOpacity) / 100);
307
+
308
+ const result2 = getSpectrumOpacityParsed({
309
+ outVideo: { spectrum: { opacity: '69%' } }
310
+ } as Config);
311
+ expect(result2).equal(0.69);
312
+
313
+ const result3 = getSpectrumOpacityParsed.bind(
314
+ undefined,
315
+ {
316
+ outVideo: { spectrum: { opacity: 'fake value' } }
317
+ } as Config
318
+ );
319
+ expect(result3).to.throw('Invalid spectrum opacity value: \'fake value\'. Use string percent value, for example \'80%\'.');
320
+
321
+ const result4 = getSpectrumOpacityParsed.bind(
322
+ undefined,
323
+ {
324
+ outVideo: { spectrum: { opacity: '103%' } }
325
+ } as Config
326
+ );
327
+ expect(result4).to.throw('Invalid spectrum opacity value: \'103%\'. Percent values must be in range from \'0%\' to \'100%\'.');
328
+ });
329
+
107
330
  it('getFfmpeg_cfr', function() {
108
331
  const result1 = getFfmpeg_cfr({
109
332
  tweaks: {}
@@ -139,4 +362,15 @@ describe('config', function() {
139
362
  } as Config);
140
363
  expect(result2).equal(420);
141
364
  });
365
+
366
+ it('checkIsValidRotationAlias', function() {
367
+ const result1 = checkIsValidRotationAlias('up');
368
+ expect(result1).equal(true);
369
+
370
+ const result2 = checkIsValidRotationAlias('down');
371
+ expect(result2).equal(true);
372
+
373
+ const result3 = checkIsValidRotationAlias('fake value');
374
+ expect(result3).equal(false);
375
+ });
142
376
  });
@@ -1,8 +1,50 @@
1
1
  import { expect } from 'chai';
2
2
  import path from 'path';
3
- import { drawRect, Color, createVisualizerFrame, parseImage, convertToBmp } from '../image';
3
+ import fs from 'fs';
4
+ import {
5
+ drawRect,
6
+ Color,
7
+ createVisualizerFrame,
8
+ parseImage,
9
+ convertToBmp,
10
+ mixValues,
11
+ mixColors,
12
+ } from '../image';
13
+
14
+ import frameSnapshotSpectrumDown from './snapshots/frameSnapshotSpectrumDown.json';
15
+ import frameSnapshotSpectrumUp from './snapshots/frameSnapshotSpectrumUp.json';
16
+ import frameSnapshotSpectrumUpOpacity50 from './snapshots/frameSnapshotSpectrumUpOpacity50.json';
4
17
 
5
18
  describe('image', function () {
19
+ it('mixValues', function () {
20
+ expect(mixValues(420, 0.69, 96)).equal(320);
21
+ expect(mixValues(420, 1, 96)).equal(420);
22
+ expect(mixValues(420, 0, 96)).equal(96);
23
+ });
24
+
25
+ it('mixColors', function () {
26
+ const result1 = mixColors({
27
+ color: { red: 123, green: 69, blue: 96 },
28
+ colorOpacity: 0.24,
29
+ backgroundColor: { red: 69, green: 123, blue: 42 },
30
+ });
31
+ expect(result1).deep.equal({ red: 82, green: 110, blue: 55 });
32
+
33
+ const result2 = mixColors({
34
+ color: { red: 123, green: 69, blue: 96 },
35
+ colorOpacity: 0,
36
+ backgroundColor: { red: 69, green: 123, blue: 42 },
37
+ });
38
+ expect(result2).deep.equal({ red: 69, green: 123, blue: 42 });
39
+
40
+ const result3 = mixColors({
41
+ color: { red: 123, green: 69, blue: 96 },
42
+ colorOpacity: 1,
43
+ backgroundColor: { red: 69, green: 123, blue: 42 },
44
+ });
45
+ expect(result3).deep.equal({ red: 123, green: 69, blue: 96 });
46
+ });
47
+
6
48
  it('drawRect', function () {
7
49
  const imageWidth = 10;
8
50
  const imageHeight = 10;
@@ -24,7 +66,13 @@ describe('image', function () {
24
66
  expectedImageData[pixelIndex + 3] = rectColor.red;
25
67
  });
26
68
 
27
- drawRect(imageDstBuffer as any, { x: rectX, y: rectY }, { width: rectWidth, height: rectHeight }, rectColor);
69
+ drawRect({
70
+ imageDstBuffer: imageDstBuffer as any,
71
+ position: { x: rectX, y: rectY },
72
+ size: { width: rectWidth, height: rectHeight },
73
+ color: rectColor,
74
+ opacity: 1
75
+ });
28
76
  expect(imageDstBuffer.data).deep.equal(expectedImageData);
29
77
  });
30
78
 
@@ -32,12 +80,43 @@ describe('image', function () {
32
80
  const backgroundImagePath = path.resolve('example/media/background.png');
33
81
  const backgroundImageBmpBuffer = await convertToBmp(backgroundImagePath);
34
82
  const backgroundImage = parseImage(backgroundImageBmpBuffer);
35
- const frame = await createVisualizerFrame(
36
- backgroundImage,
37
- [0.5, 0, 1],
38
- { width: 15, height: 20 },
39
- { red: 1, green: 1, blue: 1 }
40
- );
41
- expect(frame.data.toJSON().data.length).equal(1090560);
83
+ const frameSpectrumDown = createVisualizerFrame({
84
+ backgroundImageBuffer: backgroundImage,
85
+ spectrum: [0.5, 0, 1],
86
+ size: { width: 15, height: 20 },
87
+ position: { x: 15, y: 0 },
88
+ rotation: 'down',
89
+ margin: 4,
90
+ color: { red: 1, green: 1, blue: 1 },
91
+ opacity: 1,
92
+ });
93
+ const resultSpectrumDown = frameSpectrumDown.data.toJSON().data;
94
+ expect(resultSpectrumDown).deep.equal(frameSnapshotSpectrumDown);
95
+
96
+ const frameSpectrumUp = createVisualizerFrame({
97
+ backgroundImageBuffer: backgroundImage,
98
+ spectrum: [0.1, 1, 0],
99
+ size: { width: 25, height: 23 },
100
+ position: { x: 10, y: 5 },
101
+ rotation: 'up',
102
+ margin: 4,
103
+ color: { red: 0, green: 123, blue: 69 },
104
+ opacity: 1,
105
+ });
106
+ const resultSpectrumUp = frameSpectrumUp.data.toJSON().data;
107
+ expect(resultSpectrumUp).deep.equal(frameSnapshotSpectrumUp);
108
+
109
+ const frameSpectrumUpOpacity50 = createVisualizerFrame({
110
+ backgroundImageBuffer: backgroundImage,
111
+ spectrum: [0.1, 1, 0],
112
+ size: { width: 25, height: 23 },
113
+ position: { x: 10, y: 5 },
114
+ rotation: 'up',
115
+ margin: 4,
116
+ color: { red: 0, green: 123, blue: 69 },
117
+ opacity: 0.5,
118
+ });
119
+ const resultSpectrumUpOpacity50 = frameSpectrumUpOpacity50.data.toJSON().data;
120
+ expect(resultSpectrumUpOpacity50).deep.equal(frameSnapshotSpectrumUpOpacity50);
42
121
  });
43
122
  });