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,319 +1,371 @@
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
- applyCameraShake,
13
- getCutShakeOffset,
14
- buildCutShakeAmplitudes,
15
- CUT_SHAKE_AMPLITUDE_BY_OFFSET,
16
- } from '../image';
17
- import { createBpmEncoder } from '../bpmEncoder';
18
-
19
- describe('image', function () {
20
- it('mixValues', function () {
21
- expect(mixValues(420, 0.69, 96)).equal(320);
22
- expect(mixValues(420, 1, 96)).equal(420);
23
- expect(mixValues(420, 0, 96)).equal(96);
24
- });
25
-
26
- it('mixColors', function () {
27
- const result1 = mixColors({
28
- color: { red: 123, green: 69, blue: 96 },
29
- colorOpacity: 0.24,
30
- backgroundColor: { red: 69, green: 123, blue: 42 },
31
- });
32
- expect(result1).deep.equal({ red: 82, green: 110, blue: 55 });
33
-
34
- const result2 = mixColors({
35
- color: { red: 123, green: 69, blue: 96 },
36
- colorOpacity: 0,
37
- backgroundColor: { red: 69, green: 123, blue: 42 },
38
- });
39
- expect(result2).deep.equal({ red: 69, green: 123, blue: 42 });
40
-
41
- const result3 = mixColors({
42
- color: { red: 123, green: 69, blue: 96 },
43
- colorOpacity: 1,
44
- backgroundColor: { red: 69, green: 123, blue: 42 },
45
- });
46
- expect(result3).deep.equal({ red: 123, green: 69, blue: 96 });
47
- });
48
-
49
- it('drawRect', function () {
50
- const imageWidth = 10;
51
- const imageHeight = 10;
52
- const extraBytes = imageWidth % 4;
53
- const rowBytes = 3 * imageWidth + extraBytes;
54
- const imageDstBuffer = {
55
- shiftPos: 54,
56
- rowBytes,
57
- width: imageWidth,
58
- data: Array.from({ length: imageWidth * imageHeight * 4 }).fill(0)
59
- };
60
- const rectX = 4;
61
- const rectY = 3;
62
- const rectWidth = 4;
63
- const rectHeight = 3;
64
- const rectColor: Color = { red: 111, green: 112, blue: 123 };
65
-
66
- const expectedImageData = Array.from({ length: imageWidth * imageWidth * 4 }).fill(0);
67
- const imagePixelIndices = [162, 165, 168, 171, 194, 197, 200, 203, 226, 229, 232, 235];
68
- imagePixelIndices.forEach((pixelIndex) => {
69
- expectedImageData[pixelIndex] = rectColor.blue;
70
- expectedImageData[pixelIndex + 1] = rectColor.green;
71
- expectedImageData[pixelIndex + 2] = rectColor.red;
72
- });
73
-
74
- drawRect({
75
- imageDstBuffer: imageDstBuffer as any,
76
- position: { x: rectX, y: rectY },
77
- size: { width: rectWidth, height: rectHeight },
78
- color: rectColor,
79
- opacity: 1
80
- });
81
- expect(imageDstBuffer.data).deep.equal(expectedImageData);
82
- });
83
-
84
- it('getCutShakeOffset peaks on the cut and decays on the next frames', function () {
85
- const cutFrames = new Set([10, 20]);
86
- const magnitude = (offset: { x: number; y: number }) => Math.hypot(offset.x, offset.y);
87
- expect(getCutShakeOffset(9, cutFrames)).deep.equal({ x: 0, y: 0 });
88
- expect(magnitude(getCutShakeOffset(10, cutFrames))).greaterThan(magnitude(getCutShakeOffset(11, cutFrames)));
89
- expect(magnitude(getCutShakeOffset(11, cutFrames))).greaterThan(magnitude(getCutShakeOffset(12, cutFrames)));
90
- expect(magnitude(getCutShakeOffset(12, cutFrames))).greaterThan(magnitude(getCutShakeOffset(13, cutFrames)));
91
- expect(getCutShakeOffset(15, cutFrames)).deep.equal({ x: 0, y: 0 });
92
- expect(magnitude(getCutShakeOffset(20, cutFrames))).greaterThan(0);
93
- });
94
-
95
- it('getCutShakeOffset uses the closer overlapping cut', function () {
96
- const cutFrames = new Set([10, 11]);
97
- expect(getCutShakeOffset(11, cutFrames)).deep.equal(getCutShakeOffset(11, new Set([11])));
98
- });
99
-
100
- it('buildCutShakeAmplitudes lasts about one-third of a beat', function () {
101
- const at128 = buildCutShakeAmplitudes(14);
102
- expect(at128.length).equal(5);
103
- expect(at128[0]).equal(14);
104
- expect(at128[at128.length - 1]).equal(1);
105
- expect(buildCutShakeAmplitudes(30).length).equal(10);
106
- expect(buildCutShakeAmplitudes(6).length).equal(3);
107
- });
108
-
109
- it('getCutShakeOffset uses a longer envelope when amplitudes are passed', function () {
110
- const cutFrames = new Set([10]);
111
- const longAmp = buildCutShakeAmplitudes(30);
112
- const magnitude = (offset: { x: number; y: number }) => Math.hypot(offset.x, offset.y);
113
- expect(getCutShakeOffset(15, cutFrames)).deep.equal({ x: 0, y: 0 });
114
- expect(magnitude(getCutShakeOffset(15, cutFrames, longAmp))).greaterThan(0);
115
- expect(getCutShakeOffset(10 + longAmp.length, cutFrames, longAmp)).deep.equal({ x: 0, y: 0 });
116
- expect(magnitude(getCutShakeOffset(10, cutFrames, longAmp))).greaterThan(
117
- magnitude(getCutShakeOffset(12, cutFrames, longAmp)),
118
- );
119
- });
120
-
121
- it('getCutShakeOffset delays the shake start by delayFrames', function () {
122
- const cutFrames = new Set([10]);
123
- const magnitude = (offset: { x: number; y: number }) => Math.hypot(offset.x, offset.y);
124
- expect(getCutShakeOffset(10, cutFrames, CUT_SHAKE_AMPLITUDE_BY_OFFSET, 9)).deep.equal({ x: 0, y: 0 });
125
- expect(magnitude(getCutShakeOffset(19, cutFrames, CUT_SHAKE_AMPLITUDE_BY_OFFSET, 9))).greaterThan(0);
126
- expect(getCutShakeOffset(19, cutFrames, CUT_SHAKE_AMPLITUDE_BY_OFFSET, 9)).deep.equal(
127
- getCutShakeOffset(10, cutFrames, CUT_SHAKE_AMPLITUDE_BY_OFFSET),
128
- );
129
- });
130
-
131
- it('applyCameraShake shifts pixels and clamps edges', function () {
132
- const width = 3;
133
- const height = 2;
134
- const extraBytes = width % 4;
135
- const rowBytes = 3 * width + extraBytes;
136
- const shiftPos = 4;
137
- const data = Buffer.alloc(shiftPos + height * rowBytes, 0);
138
- data[0] = 9;
139
- data[1] = 8;
140
- data[2] = 7;
141
- data[3] = 6;
142
- let value = 1;
143
- for (let y = 0; y < height; y++) {
144
- for (let x = 0; x < width; x++) {
145
- const pixelIndex = shiftPos + y * rowBytes + x * 3;
146
- data[pixelIndex] = value;
147
- data[pixelIndex + 1] = value + 1;
148
- data[pixelIndex + 2] = value + 2;
149
- value += 3;
150
- }
151
- }
152
-
153
- applyCameraShake({ shiftPos, rowBytes, data }, width, height, 1, 0);
154
-
155
- expect(data[0]).equal(9);
156
- expect(data[1]).equal(8);
157
- expect(data[2]).equal(7);
158
- expect(data[3]).equal(6);
159
- const pixelAt = (x: number, y: number) => {
160
- const pixelIndex = shiftPos + y * rowBytes + x * 3;
161
- return [data[pixelIndex], data[pixelIndex + 1], data[pixelIndex + 2]];
162
- };
163
- expect(pixelAt(0, 0)).deep.equal([1, 2, 3]);
164
- expect(pixelAt(1, 0)).deep.equal([1, 2, 3]);
165
- expect(pixelAt(2, 0)).deep.equal([4, 5, 6]);
166
- expect(pixelAt(0, 1)).deep.equal([10, 11, 12]);
167
- expect(pixelAt(1, 1)).deep.equal([10, 11, 12]);
168
- expect(pixelAt(2, 1)).deep.equal([13, 14, 15]);
169
- });
170
-
171
- it('applyCameraShake is a no-op at zero offset', function () {
172
- const data = Buffer.from([1, 2, 3, 10, 20, 30]);
173
- applyCameraShake({ shiftPos: 3, rowBytes: 3, data }, 1, 1, 0, 0);
174
- expect(Array.from(data)).deep.equal([1, 2, 3, 10, 20, 30]);
175
- });
176
-
177
- it('createSpectrumVisualizerFrameGenerator', async function () {
178
- const expectedImageLength = 6220854;
179
- const backgroundImagePath = path.resolve('example/media/horses.png');
180
- const backgroundImageBmpBuffer = await convertToBmp(backgroundImagePath);
181
- const backgroundImage = parseImage(backgroundImageBmpBuffer);
182
- const bpmEncoder = createBpmEncoder({ width: backgroundImage.width, height: backgroundImage.height });
183
- const backgroundImageBuffer = bpmEncoder(backgroundImage.data);
184
- const createVisualizerFrame = createSpectrumVisualizerFrameGenerator();
185
- const frameSpectrumDown = createVisualizerFrame({
186
- backgroundImageBuffer,
187
- spectrum: [0.5, 0, 1],
188
- size: { width: 15, height: 20 },
189
- position: { x: 15, y: 0 },
190
- rotation: 'down',
191
- margin: 4,
192
- color: { red: 1, green: 1, blue: 1 },
193
- opacity: 1,
194
- });
195
- const resultSpectrumDown = frameSpectrumDown.data.toJSON().data;
196
- expect(resultSpectrumDown).to.have.length(expectedImageLength);
197
-
198
- const frameSpectrumUp = createVisualizerFrame({
199
- backgroundImageBuffer,
200
- spectrum: [0.1, 1, 0],
201
- size: { width: 25, height: 23 },
202
- position: { x: 10, y: 5 },
203
- rotation: 'up',
204
- margin: 4,
205
- color: { red: 0, green: 123, blue: 69 },
206
- opacity: 1,
207
- });
208
- const resultSpectrumUp = frameSpectrumUp.data.toJSON().data;
209
- expect(resultSpectrumUp).to.have.length(expectedImageLength);
210
-
211
- const frameSpectrumMirror = createVisualizerFrame({
212
- backgroundImageBuffer,
213
- spectrum: [0.1, 1, 0],
214
- size: { width: 25, height: 23 },
215
- position: { x: 10, y: 5 },
216
- rotation: 'mirror',
217
- margin: 4,
218
- color: { red: 0, green: 123, blue: 69 },
219
- opacity: 1,
220
- });
221
- const resultSpectrumMirror = frameSpectrumMirror.data.toJSON().data;
222
- expect(resultSpectrumMirror).to.have.length(expectedImageLength);
223
-
224
- const frameSpectrumUpOpacity50 = createVisualizerFrame({
225
- backgroundImageBuffer,
226
- spectrum: [0.1, 1, 0],
227
- size: { width: 25, height: 23 },
228
- position: { x: 10, y: 5 },
229
- rotation: 'up',
230
- margin: 4,
231
- color: { red: 0, green: 123, blue: 69 },
232
- opacity: 0.5,
233
- });
234
- const resultSpectrumUpOpacity50 = frameSpectrumUpOpacity50.data.toJSON().data;
235
- expect(resultSpectrumUpOpacity50).to.have.length(expectedImageLength);
236
- });
237
-
238
- it('createPolarVisualizerFrameGenerator', async function () {
239
- const expectedImageLength = 6220854;
240
- const backgroundImagePath = path.resolve('example/media/horses.png');
241
- const backgroundImageBmpBuffer = await convertToBmp(backgroundImagePath);
242
- const backgroundImage = parseImage(backgroundImageBmpBuffer);
243
- const bpmEncoder = createBpmEncoder({ width: backgroundImage.width, height: backgroundImage.height });
244
- const backgroundImageBuffer = bpmEncoder(backgroundImage.data);
245
- const createPolarVisualizerFrame = createPolarVisualizerFrameGenerator();
246
-
247
- const framePolarBasic = createPolarVisualizerFrame({
248
- backgroundImageBuffer,
249
- spectrum: [0.5, 0.8, 0.3, 0.9, 0.6, 0.4, 0.7, 0.2],
250
- centerX: 500,
251
- centerY: 400,
252
- innerRadius: 100,
253
- maxBarLength: 150,
254
- barWidth: 5,
255
- color: { red: 255, green: 100, blue: 50 },
256
- opacity: 1,
257
- });
258
- const resultPolarBasic = framePolarBasic.data.toJSON().data;
259
- expect(resultPolarBasic).to.have.length(expectedImageLength);
260
-
261
- const framePolarVolume = createPolarVisualizerFrame({
262
- backgroundImageBuffer,
263
- spectrum: [0.2, 0.5, 0.8, 0.3, 0.6, 0.9, 0.4, 0.7],
264
- centerX: 500,
265
- centerY: 400,
266
- innerRadius: 80,
267
- maxBarLength: 120,
268
- barWidth: 4,
269
- color: { red: 100, green: 200, blue: 255 },
270
- opacity: 1,
271
- spectrumEffect: 'volume',
272
- });
273
- const resultPolarVolume = framePolarVolume.data.toJSON().data;
274
- expect(resultPolarVolume).to.have.length(expectedImageLength);
275
-
276
- const framePolarSmooth = createPolarVisualizerFrame({
277
- backgroundImageBuffer,
278
- spectrum: [0.7, 0.4, 0.9, 0.2, 0.5, 0.8, 0.3, 0.6],
279
- centerX: 500,
280
- centerY: 400,
281
- innerRadius: 90,
282
- maxBarLength: 130,
283
- barWidth: 6,
284
- color: { red: 255, green: 255, blue: 0 },
285
- opacity: 1,
286
- spectrumEffect: 'smooth',
287
- });
288
- const resultPolarSmooth = framePolarSmooth.data.toJSON().data;
289
- expect(resultPolarSmooth).to.have.length(expectedImageLength);
290
-
291
- const framePolarOpacity50 = createPolarVisualizerFrame({
292
- backgroundImageBuffer,
293
- spectrum: [0.6, 0.3, 0.8, 0.5, 0.4, 0.7, 0.2, 0.9],
294
- centerX: 500,
295
- centerY: 400,
296
- innerRadius: 100,
297
- maxBarLength: 140,
298
- barWidth: 5,
299
- color: { red: 0, green: 255, blue: 128 },
300
- opacity: 0.5,
301
- });
302
- const resultPolarOpacity50 = framePolarOpacity50.data.toJSON().data;
303
- expect(resultPolarOpacity50).to.have.length(expectedImageLength);
304
-
305
- const framePolarHexColor = createPolarVisualizerFrame({
306
- backgroundImageBuffer,
307
- spectrum: [0.5, 0.6, 0.7, 0.8, 0.9, 0.4, 0.3, 0.2],
308
- centerX: 500,
309
- centerY: 400,
310
- innerRadius: 110,
311
- maxBarLength: 160,
312
- barWidth: 7,
313
- color: '#FF5733',
314
- opacity: 1,
315
- });
316
- const resultPolarHexColor = framePolarHexColor.data.toJSON().data;
317
- expect(resultPolarHexColor).to.have.length(expectedImageLength);
318
- });
319
- });
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
+ applyCameraShake,
13
+ getCutShakeOffset,
14
+ buildCutShakeAmplitudes,
15
+ CUT_SHAKE_AMPLITUDE_BY_OFFSET,
16
+ applyCutZoom,
17
+ getCutZoomScale,
18
+ buildCutZoomScales,
19
+ CUT_ZOOM_PEAK_SCALE,
20
+ DEFAULT_CUT_ZOOM_FRAMES,
21
+ } from '../image';
22
+ import { createBpmEncoder } from '../bpmEncoder';
23
+
24
+ describe('image', function () {
25
+ it('mixValues', function () {
26
+ expect(mixValues(420, 0.69, 96)).equal(320);
27
+ expect(mixValues(420, 1, 96)).equal(420);
28
+ expect(mixValues(420, 0, 96)).equal(96);
29
+ });
30
+
31
+ it('mixColors', function () {
32
+ const result1 = mixColors({
33
+ color: { red: 123, green: 69, blue: 96 },
34
+ colorOpacity: 0.24,
35
+ backgroundColor: { red: 69, green: 123, blue: 42 },
36
+ });
37
+ expect(result1).deep.equal({ red: 82, green: 110, blue: 55 });
38
+
39
+ const result2 = mixColors({
40
+ color: { red: 123, green: 69, blue: 96 },
41
+ colorOpacity: 0,
42
+ backgroundColor: { red: 69, green: 123, blue: 42 },
43
+ });
44
+ expect(result2).deep.equal({ red: 69, green: 123, blue: 42 });
45
+
46
+ const result3 = mixColors({
47
+ color: { red: 123, green: 69, blue: 96 },
48
+ colorOpacity: 1,
49
+ backgroundColor: { red: 69, green: 123, blue: 42 },
50
+ });
51
+ expect(result3).deep.equal({ red: 123, green: 69, blue: 96 });
52
+ });
53
+
54
+ it('drawRect', function () {
55
+ const imageWidth = 10;
56
+ const imageHeight = 10;
57
+ const extraBytes = imageWidth % 4;
58
+ const rowBytes = 3 * imageWidth + extraBytes;
59
+ const imageDstBuffer = {
60
+ shiftPos: 54,
61
+ rowBytes,
62
+ width: imageWidth,
63
+ data: Array.from({ length: imageWidth * imageHeight * 4 }).fill(0)
64
+ };
65
+ const rectX = 4;
66
+ const rectY = 3;
67
+ const rectWidth = 4;
68
+ const rectHeight = 3;
69
+ const rectColor: Color = { red: 111, green: 112, blue: 123 };
70
+
71
+ const expectedImageData = Array.from({ length: imageWidth * imageWidth * 4 }).fill(0);
72
+ const imagePixelIndices = [162, 165, 168, 171, 194, 197, 200, 203, 226, 229, 232, 235];
73
+ imagePixelIndices.forEach((pixelIndex) => {
74
+ expectedImageData[pixelIndex] = rectColor.blue;
75
+ expectedImageData[pixelIndex + 1] = rectColor.green;
76
+ expectedImageData[pixelIndex + 2] = rectColor.red;
77
+ });
78
+
79
+ drawRect({
80
+ imageDstBuffer: imageDstBuffer as any,
81
+ position: { x: rectX, y: rectY },
82
+ size: { width: rectWidth, height: rectHeight },
83
+ color: rectColor,
84
+ opacity: 1
85
+ });
86
+ expect(imageDstBuffer.data).deep.equal(expectedImageData);
87
+ });
88
+
89
+ it('getCutShakeOffset peaks on the cut and decays on the next frames', function () {
90
+ const cutFrames = new Set([10, 20]);
91
+ const magnitude = (offset: { x: number; y: number }) => Math.hypot(offset.x, offset.y);
92
+ expect(getCutShakeOffset(9, cutFrames)).deep.equal({ x: 0, y: 0 });
93
+ expect(magnitude(getCutShakeOffset(10, cutFrames))).greaterThan(magnitude(getCutShakeOffset(11, cutFrames)));
94
+ expect(magnitude(getCutShakeOffset(11, cutFrames))).greaterThan(magnitude(getCutShakeOffset(12, cutFrames)));
95
+ expect(magnitude(getCutShakeOffset(12, cutFrames))).greaterThan(magnitude(getCutShakeOffset(13, cutFrames)));
96
+ expect(getCutShakeOffset(15, cutFrames)).deep.equal({ x: 0, y: 0 });
97
+ expect(magnitude(getCutShakeOffset(20, cutFrames))).greaterThan(0);
98
+ });
99
+
100
+ it('getCutShakeOffset uses the closer overlapping cut', function () {
101
+ const cutFrames = new Set([10, 11]);
102
+ expect(getCutShakeOffset(11, cutFrames)).deep.equal(getCutShakeOffset(11, new Set([11])));
103
+ });
104
+
105
+ it('buildCutShakeAmplitudes lasts about one-third of a beat', function () {
106
+ const at128 = buildCutShakeAmplitudes(14);
107
+ expect(at128.length).equal(5);
108
+ expect(at128[0]).equal(14);
109
+ expect(at128[at128.length - 1]).equal(1);
110
+ expect(buildCutShakeAmplitudes(30).length).equal(10);
111
+ expect(buildCutShakeAmplitudes(6).length).equal(3);
112
+ });
113
+
114
+ it('getCutShakeOffset uses a longer envelope when amplitudes are passed', function () {
115
+ const cutFrames = new Set([10]);
116
+ const longAmp = buildCutShakeAmplitudes(30);
117
+ const magnitude = (offset: { x: number; y: number }) => Math.hypot(offset.x, offset.y);
118
+ expect(getCutShakeOffset(15, cutFrames)).deep.equal({ x: 0, y: 0 });
119
+ expect(magnitude(getCutShakeOffset(15, cutFrames, longAmp))).greaterThan(0);
120
+ expect(getCutShakeOffset(10 + longAmp.length, cutFrames, longAmp)).deep.equal({ x: 0, y: 0 });
121
+ expect(magnitude(getCutShakeOffset(10, cutFrames, longAmp))).greaterThan(
122
+ magnitude(getCutShakeOffset(12, cutFrames, longAmp)),
123
+ );
124
+ });
125
+
126
+ it('getCutShakeOffset delays the shake start by delayFrames', function () {
127
+ const cutFrames = new Set([10]);
128
+ const magnitude = (offset: { x: number; y: number }) => Math.hypot(offset.x, offset.y);
129
+ expect(getCutShakeOffset(10, cutFrames, CUT_SHAKE_AMPLITUDE_BY_OFFSET, 9)).deep.equal({ x: 0, y: 0 });
130
+ expect(magnitude(getCutShakeOffset(19, cutFrames, CUT_SHAKE_AMPLITUDE_BY_OFFSET, 9))).greaterThan(0);
131
+ expect(getCutShakeOffset(19, cutFrames, CUT_SHAKE_AMPLITUDE_BY_OFFSET, 9)).deep.equal(
132
+ getCutShakeOffset(10, cutFrames, CUT_SHAKE_AMPLITUDE_BY_OFFSET),
133
+ );
134
+ });
135
+
136
+ it('applyCameraShake shifts pixels and clamps edges', function () {
137
+ const width = 3;
138
+ const height = 2;
139
+ const extraBytes = width % 4;
140
+ const rowBytes = 3 * width + extraBytes;
141
+ const shiftPos = 4;
142
+ const data = Buffer.alloc(shiftPos + height * rowBytes, 0);
143
+ data[0] = 9;
144
+ data[1] = 8;
145
+ data[2] = 7;
146
+ data[3] = 6;
147
+ let value = 1;
148
+ for (let y = 0; y < height; y++) {
149
+ for (let x = 0; x < width; x++) {
150
+ const pixelIndex = shiftPos + y * rowBytes + x * 3;
151
+ data[pixelIndex] = value;
152
+ data[pixelIndex + 1] = value + 1;
153
+ data[pixelIndex + 2] = value + 2;
154
+ value += 3;
155
+ }
156
+ }
157
+
158
+ applyCameraShake({ shiftPos, rowBytes, data }, width, height, 1, 0);
159
+
160
+ expect(data[0]).equal(9);
161
+ expect(data[1]).equal(8);
162
+ expect(data[2]).equal(7);
163
+ expect(data[3]).equal(6);
164
+ const pixelAt = (x: number, y: number) => {
165
+ const pixelIndex = shiftPos + y * rowBytes + x * 3;
166
+ return [data[pixelIndex], data[pixelIndex + 1], data[pixelIndex + 2]];
167
+ };
168
+ expect(pixelAt(0, 0)).deep.equal([1, 2, 3]);
169
+ expect(pixelAt(1, 0)).deep.equal([1, 2, 3]);
170
+ expect(pixelAt(2, 0)).deep.equal([4, 5, 6]);
171
+ expect(pixelAt(0, 1)).deep.equal([10, 11, 12]);
172
+ expect(pixelAt(1, 1)).deep.equal([10, 11, 12]);
173
+ expect(pixelAt(2, 1)).deep.equal([13, 14, 15]);
174
+ });
175
+
176
+ it('applyCameraShake is a no-op at zero offset', function () {
177
+ const data = Buffer.from([1, 2, 3, 10, 20, 30]);
178
+ applyCameraShake({ shiftPos: 3, rowBytes: 3, data }, 1, 1, 0, 0);
179
+ expect(Array.from(data)).deep.equal([1, 2, 3, 10, 20, 30]);
180
+ });
181
+
182
+ it('buildCutZoomScales starts at peak and eases out towards 1', function () {
183
+ const scales = buildCutZoomScales(18);
184
+ expect(scales).to.have.length(6);
185
+ expect(scales[0]).equal(CUT_ZOOM_PEAK_SCALE);
186
+ for (let i = 1; i < scales.length; i++) {
187
+ expect(scales[i]).lessThan(scales[i - 1]);
188
+ expect(scales[i]).greaterThan(1);
189
+ }
190
+ expect(buildCutZoomScales(0)).to.have.length(DEFAULT_CUT_ZOOM_FRAMES);
191
+ expect(buildCutZoomScales(3)).to.have.length(3);
192
+ });
193
+
194
+ it('getCutZoomScale uses the most recent cut', function () {
195
+ const scales = [1.3, 1.2, 1.1];
196
+ const cutFrames = new Set([10, 11]);
197
+ expect(getCutZoomScale(9, cutFrames, scales)).equal(1);
198
+ expect(getCutZoomScale(10, cutFrames, scales)).equal(1.3);
199
+ expect(getCutZoomScale(11, cutFrames, scales)).equal(1.3);
200
+ expect(getCutZoomScale(13, cutFrames, scales)).equal(1.1);
201
+ expect(getCutZoomScale(14, cutFrames, scales)).equal(1);
202
+ });
203
+
204
+ it('applyCutZoom scales around the center and keeps the header', function () {
205
+ const width = 4;
206
+ const height = 1;
207
+ const rowBytes = 3 * width;
208
+ const shiftPos = 2;
209
+ const data = Buffer.alloc(shiftPos + rowBytes, 0);
210
+ data[0] = 9;
211
+ data[1] = 8;
212
+ [0, 100, 200, 250].forEach((value, x) => data.fill(value, shiftPos + x * 3, shiftPos + x * 3 + 3));
213
+
214
+ applyCutZoom({ shiftPos, rowBytes, data }, width, height, 2);
215
+
216
+ expect(data[0]).equal(9);
217
+ expect(data[1]).equal(8);
218
+ const channel = (x: number) => data[shiftPos + x * 3];
219
+ // srcX = 1.5 + (x - 1.5) / 2 -> 0.75, 1.25, 1.75, 2.25
220
+ expect([0, 1, 2, 3].map(channel)).deep.equal([75, 125, 175, 213]);
221
+ });
222
+
223
+ it('applyCutZoom is a no-op at scale 1', function () {
224
+ const data = Buffer.from([1, 2, 3, 10, 20, 30]);
225
+ applyCutZoom({ shiftPos: 0, rowBytes: 6, data }, 2, 1, 1);
226
+ expect(Array.from(data)).deep.equal([1, 2, 3, 10, 20, 30]);
227
+ });
228
+
229
+ it('createSpectrumVisualizerFrameGenerator', async function () {
230
+ const expectedImageLength = 6220854;
231
+ const backgroundImagePath = path.resolve('example/media/horses.png');
232
+ const backgroundImageBmpBuffer = await convertToBmp(backgroundImagePath);
233
+ const backgroundImage = parseImage(backgroundImageBmpBuffer);
234
+ const bpmEncoder = createBpmEncoder({ width: backgroundImage.width, height: backgroundImage.height });
235
+ const backgroundImageBuffer = bpmEncoder(backgroundImage.data);
236
+ const createVisualizerFrame = createSpectrumVisualizerFrameGenerator();
237
+ const frameSpectrumDown = createVisualizerFrame({
238
+ backgroundImageBuffer,
239
+ spectrum: [0.5, 0, 1],
240
+ size: { width: 15, height: 20 },
241
+ position: { x: 15, y: 0 },
242
+ rotation: 'down',
243
+ margin: 4,
244
+ color: { red: 1, green: 1, blue: 1 },
245
+ opacity: 1,
246
+ });
247
+ const resultSpectrumDown = frameSpectrumDown.data.toJSON().data;
248
+ expect(resultSpectrumDown).to.have.length(expectedImageLength);
249
+
250
+ const frameSpectrumUp = createVisualizerFrame({
251
+ backgroundImageBuffer,
252
+ spectrum: [0.1, 1, 0],
253
+ size: { width: 25, height: 23 },
254
+ position: { x: 10, y: 5 },
255
+ rotation: 'up',
256
+ margin: 4,
257
+ color: { red: 0, green: 123, blue: 69 },
258
+ opacity: 1,
259
+ });
260
+ const resultSpectrumUp = frameSpectrumUp.data.toJSON().data;
261
+ expect(resultSpectrumUp).to.have.length(expectedImageLength);
262
+
263
+ const frameSpectrumMirror = createVisualizerFrame({
264
+ backgroundImageBuffer,
265
+ spectrum: [0.1, 1, 0],
266
+ size: { width: 25, height: 23 },
267
+ position: { x: 10, y: 5 },
268
+ rotation: 'mirror',
269
+ margin: 4,
270
+ color: { red: 0, green: 123, blue: 69 },
271
+ opacity: 1,
272
+ });
273
+ const resultSpectrumMirror = frameSpectrumMirror.data.toJSON().data;
274
+ expect(resultSpectrumMirror).to.have.length(expectedImageLength);
275
+
276
+ const frameSpectrumUpOpacity50 = createVisualizerFrame({
277
+ backgroundImageBuffer,
278
+ spectrum: [0.1, 1, 0],
279
+ size: { width: 25, height: 23 },
280
+ position: { x: 10, y: 5 },
281
+ rotation: 'up',
282
+ margin: 4,
283
+ color: { red: 0, green: 123, blue: 69 },
284
+ opacity: 0.5,
285
+ });
286
+ const resultSpectrumUpOpacity50 = frameSpectrumUpOpacity50.data.toJSON().data;
287
+ expect(resultSpectrumUpOpacity50).to.have.length(expectedImageLength);
288
+ });
289
+
290
+ it('createPolarVisualizerFrameGenerator', async function () {
291
+ const expectedImageLength = 6220854;
292
+ const backgroundImagePath = path.resolve('example/media/horses.png');
293
+ const backgroundImageBmpBuffer = await convertToBmp(backgroundImagePath);
294
+ const backgroundImage = parseImage(backgroundImageBmpBuffer);
295
+ const bpmEncoder = createBpmEncoder({ width: backgroundImage.width, height: backgroundImage.height });
296
+ const backgroundImageBuffer = bpmEncoder(backgroundImage.data);
297
+ const createPolarVisualizerFrame = createPolarVisualizerFrameGenerator();
298
+
299
+ const framePolarBasic = createPolarVisualizerFrame({
300
+ backgroundImageBuffer,
301
+ spectrum: [0.5, 0.8, 0.3, 0.9, 0.6, 0.4, 0.7, 0.2],
302
+ centerX: 500,
303
+ centerY: 400,
304
+ innerRadius: 100,
305
+ maxBarLength: 150,
306
+ barWidth: 5,
307
+ color: { red: 255, green: 100, blue: 50 },
308
+ opacity: 1,
309
+ });
310
+ const resultPolarBasic = framePolarBasic.data.toJSON().data;
311
+ expect(resultPolarBasic).to.have.length(expectedImageLength);
312
+
313
+ const framePolarVolume = createPolarVisualizerFrame({
314
+ backgroundImageBuffer,
315
+ spectrum: [0.2, 0.5, 0.8, 0.3, 0.6, 0.9, 0.4, 0.7],
316
+ centerX: 500,
317
+ centerY: 400,
318
+ innerRadius: 80,
319
+ maxBarLength: 120,
320
+ barWidth: 4,
321
+ color: { red: 100, green: 200, blue: 255 },
322
+ opacity: 1,
323
+ spectrumEffect: 'volume',
324
+ });
325
+ const resultPolarVolume = framePolarVolume.data.toJSON().data;
326
+ expect(resultPolarVolume).to.have.length(expectedImageLength);
327
+
328
+ const framePolarSmooth = createPolarVisualizerFrame({
329
+ backgroundImageBuffer,
330
+ spectrum: [0.7, 0.4, 0.9, 0.2, 0.5, 0.8, 0.3, 0.6],
331
+ centerX: 500,
332
+ centerY: 400,
333
+ innerRadius: 90,
334
+ maxBarLength: 130,
335
+ barWidth: 6,
336
+ color: { red: 255, green: 255, blue: 0 },
337
+ opacity: 1,
338
+ spectrumEffect: 'smooth',
339
+ });
340
+ const resultPolarSmooth = framePolarSmooth.data.toJSON().data;
341
+ expect(resultPolarSmooth).to.have.length(expectedImageLength);
342
+
343
+ const framePolarOpacity50 = createPolarVisualizerFrame({
344
+ backgroundImageBuffer,
345
+ spectrum: [0.6, 0.3, 0.8, 0.5, 0.4, 0.7, 0.2, 0.9],
346
+ centerX: 500,
347
+ centerY: 400,
348
+ innerRadius: 100,
349
+ maxBarLength: 140,
350
+ barWidth: 5,
351
+ color: { red: 0, green: 255, blue: 128 },
352
+ opacity: 0.5,
353
+ });
354
+ const resultPolarOpacity50 = framePolarOpacity50.data.toJSON().data;
355
+ expect(resultPolarOpacity50).to.have.length(expectedImageLength);
356
+
357
+ const framePolarHexColor = createPolarVisualizerFrame({
358
+ backgroundImageBuffer,
359
+ spectrum: [0.5, 0.6, 0.7, 0.8, 0.9, 0.4, 0.3, 0.2],
360
+ centerX: 500,
361
+ centerY: 400,
362
+ innerRadius: 110,
363
+ maxBarLength: 160,
364
+ barWidth: 7,
365
+ color: '#FF5733',
366
+ opacity: 1,
367
+ });
368
+ const resultPolarHexColor = framePolarHexColor.data.toJSON().data;
369
+ expect(resultPolarHexColor).to.have.length(expectedImageLength);
370
+ });
371
+ });