nodejs-audio-visualizer 1.2.0 → 1.3.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/README.md +2 -0
- package/dist/config.d.ts +4 -0
- package/dist/config.js +75 -1
- package/dist/config.js.map +1 -1
- package/dist/image.d.ts +1 -1
- package/dist/image.js +7 -6
- package/dist/image.js.map +1 -1
- package/dist/index.d.ts +3 -0
- package/dist/index.js +4 -2
- package/dist/index.js.map +1 -1
- package/package.json +1 -1
- package/src/config.ts +113 -2
- package/src/image.ts +7 -6
- package/src/index.ts +17 -0
- package/src/tests/config.test.ts +132 -0
- package/src/tests/image.test.ts +2 -1
package/README.md
CHANGED
|
@@ -18,6 +18,8 @@ const config = {
|
|
|
18
18
|
spectrum: { // Audio spectrum configuration. Optional.
|
|
19
19
|
width: 0.3, // 30% of background image width. Default value: 40%. Also you can use absolute values. For example, value 300 for 300 pixels width.
|
|
20
20
|
height: 0.15, // 15% of background image height. Default value: 10%. Also you can use absolute values. For example, value 300 for 300 pixels height.
|
|
21
|
+
x: 'center', // Available values: 'left', 'center', right'. Also you can use absolute number values. For example, value 300 for 300 pixels x.
|
|
22
|
+
y: 'bottom', // Available values: 'top', 'middle', bottom'. Also you can use absolute number values. For example, value 300 for 300 pixels y.
|
|
21
23
|
color: '#cccc99' // Default value: inverted color of background image
|
|
22
24
|
}
|
|
23
25
|
},
|
package/dist/config.d.ts
CHANGED
|
@@ -3,6 +3,8 @@ export declare const defaults: {
|
|
|
3
3
|
fps: number;
|
|
4
4
|
spectrumWidth: number;
|
|
5
5
|
spectrumHeight: number;
|
|
6
|
+
spectrumX: string;
|
|
7
|
+
spectrumY: string;
|
|
6
8
|
};
|
|
7
9
|
export declare const getAudioFilePath: (config: Config) => string;
|
|
8
10
|
export declare const getBackgroundImagePath: (config: Config) => string;
|
|
@@ -10,6 +12,8 @@ export declare const getOutVideoPath: (config: Config) => string;
|
|
|
10
12
|
export declare const getFPS: (config: Config) => number;
|
|
11
13
|
export declare const getSpectrumWidthAbsolute: (config: Config, backgroundImageWidth: number) => number;
|
|
12
14
|
export declare const getSpectrumHeightAbsolute: (config: Config, backgroundImageHeight: number) => number;
|
|
15
|
+
export declare const getSpectrumXAbsolute: (config: Config, spectrumWidth: number, backgroundImageWidth: number) => number;
|
|
16
|
+
export declare const getSpectrumYAbsolute: (config: Config, spectrumHeight: number, backgroundImageHeight: number) => number;
|
|
13
17
|
export declare const getSpectrumColor: (config: Config) => string | import("./image").Color | undefined;
|
|
14
18
|
export declare const getFfmpeg_cfr: (config: Config) => string | undefined;
|
|
15
19
|
export declare const getFfmpeg_preset: (config: Config) => string | undefined;
|
package/dist/config.js
CHANGED
|
@@ -7,8 +7,18 @@ var path_1 = __importDefault(require("path"));
|
|
|
7
7
|
exports.defaults = {
|
|
8
8
|
fps: 60,
|
|
9
9
|
spectrumWidth: 0.4,
|
|
10
|
-
spectrumHeight: 0.1
|
|
10
|
+
spectrumHeight: 0.1,
|
|
11
|
+
spectrumX: 'center',
|
|
12
|
+
spectrumY: 'top'
|
|
11
13
|
};
|
|
14
|
+
var postitionAliases = [
|
|
15
|
+
{ name: 'left', value: 0 },
|
|
16
|
+
{ name: 'center', value: 0.5 },
|
|
17
|
+
{ name: 'right', value: 1 },
|
|
18
|
+
{ name: 'top', value: 0 },
|
|
19
|
+
{ name: 'middle', value: 0.5 },
|
|
20
|
+
{ name: 'bottom', value: 1 },
|
|
21
|
+
];
|
|
12
22
|
var checkIsInt = function (num) {
|
|
13
23
|
return num % 1 === 0;
|
|
14
24
|
};
|
|
@@ -48,6 +58,70 @@ exports.getSpectrumHeightAbsolute = function (config, backgroundImageHeight) {
|
|
|
48
58
|
}
|
|
49
59
|
return backgroundImageHeight * spectrumHeight;
|
|
50
60
|
};
|
|
61
|
+
var getSpectrumX = function (config) {
|
|
62
|
+
return parseNumberIfPossible((config.outVideo.spectrum && config.outVideo.spectrum.x) ||
|
|
63
|
+
exports.defaults.spectrumX);
|
|
64
|
+
};
|
|
65
|
+
var getSpectrumY = function (config) {
|
|
66
|
+
return parseNumberIfPossible((config.outVideo.spectrum && config.outVideo.spectrum.y) ||
|
|
67
|
+
exports.defaults.spectrumY);
|
|
68
|
+
};
|
|
69
|
+
var parseNumberIfPossible = function (value) {
|
|
70
|
+
var parsed = Number(value);
|
|
71
|
+
if (isNaN(parsed)) {
|
|
72
|
+
return value;
|
|
73
|
+
}
|
|
74
|
+
return parsed;
|
|
75
|
+
};
|
|
76
|
+
var parsePositionAlias = function (aliasName) {
|
|
77
|
+
var nameNormalized = aliasName.trim().toLowerCase();
|
|
78
|
+
var alias = postitionAliases.find(function (als) { return als.name === nameNormalized; });
|
|
79
|
+
if (!alias) {
|
|
80
|
+
throw new Error();
|
|
81
|
+
}
|
|
82
|
+
return alias;
|
|
83
|
+
};
|
|
84
|
+
var getValidPositionAliasValues = function () {
|
|
85
|
+
return postitionAliases.map(function (aliasEl) { return aliasEl.name; }).join(', ');
|
|
86
|
+
};
|
|
87
|
+
var getSpectrumAbsolutePosition = function (relativePosition, spectrumSize, backgroundImageSize) {
|
|
88
|
+
switch (relativePosition) {
|
|
89
|
+
case 0:
|
|
90
|
+
return 0;
|
|
91
|
+
case 0.5:
|
|
92
|
+
return relativePosition * backgroundImageSize - spectrumSize / 2;
|
|
93
|
+
case 1:
|
|
94
|
+
return relativePosition * backgroundImageSize - spectrumSize;
|
|
95
|
+
default:
|
|
96
|
+
throw new Error();
|
|
97
|
+
}
|
|
98
|
+
};
|
|
99
|
+
exports.getSpectrumXAbsolute = function (config, spectrumWidth, backgroundImageWidth) {
|
|
100
|
+
var spectrumX = getSpectrumX(config);
|
|
101
|
+
if (typeof spectrumX !== 'string') {
|
|
102
|
+
return spectrumX;
|
|
103
|
+
}
|
|
104
|
+
try {
|
|
105
|
+
var spectrumXParsed = parsePositionAlias(spectrumX);
|
|
106
|
+
return getSpectrumAbsolutePosition(spectrumXParsed.value, spectrumWidth, backgroundImageWidth);
|
|
107
|
+
}
|
|
108
|
+
catch (_a) {
|
|
109
|
+
throw new Error("Invalid spectrum x value: " + spectrumX + ". Valid values: " + getValidPositionAliasValues() + ".");
|
|
110
|
+
}
|
|
111
|
+
};
|
|
112
|
+
exports.getSpectrumYAbsolute = function (config, spectrumHeight, backgroundImageHeight) {
|
|
113
|
+
var spectrumY = getSpectrumY(config);
|
|
114
|
+
if (typeof spectrumY !== 'string') {
|
|
115
|
+
return spectrumY;
|
|
116
|
+
}
|
|
117
|
+
try {
|
|
118
|
+
var spectrumXParsed = parsePositionAlias(spectrumY);
|
|
119
|
+
return getSpectrumAbsolutePosition(spectrumXParsed.value, spectrumHeight, backgroundImageHeight);
|
|
120
|
+
}
|
|
121
|
+
catch (_a) {
|
|
122
|
+
throw new Error("Invalid spectrum y value: " + spectrumY + ". Valid values: " + getValidPositionAliasValues() + ".");
|
|
123
|
+
}
|
|
124
|
+
};
|
|
51
125
|
exports.getSpectrumColor = function (config) {
|
|
52
126
|
return (config.outVideo.spectrum && config.outVideo.spectrum.color);
|
|
53
127
|
};
|
package/dist/config.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"config.js","sourceRoot":"","sources":["../src/config.ts"],"names":[],"mappings":";;;;;AAAA,8CAAwB;AAGX,QAAA,QAAQ,GAAG;IACtB,GAAG,EAAE,EAAE;IACP,aAAa,EAAE,GAAG;IAClB,cAAc,EAAE,GAAG;
|
|
1
|
+
{"version":3,"file":"config.js","sourceRoot":"","sources":["../src/config.ts"],"names":[],"mappings":";;;;;AAAA,8CAAwB;AAGX,QAAA,QAAQ,GAAG;IACtB,GAAG,EAAE,EAAE;IACP,aAAa,EAAE,GAAG;IAClB,cAAc,EAAE,GAAG;IACnB,SAAS,EAAE,QAAQ;IACnB,SAAS,EAAE,KAAK;CACjB,CAAC;AASF,IAAM,gBAAgB,GAAoB;IACxC,EAAE,IAAI,EAAE,MAAM,EAAE,KAAK,EAAE,CAAC,EAAE;IAC1B,EAAE,IAAI,EAAE,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
|
}
|
|
16
16
|
export declare const drawRect: (imageDstBuffer: BmpDecoder, position: Position, size: Size, color: Color) => void;
|
|
17
|
-
export declare const createVisualizerFrame: (backgroundImageBuffer: BmpDecoder, spectrum: number[], size: Size, color: string | Color) => BmpDecoder;
|
|
17
|
+
export declare const createVisualizerFrame: (backgroundImageBuffer: BmpDecoder, spectrum: number[], size: Size, position: Position, color: string | Color) => BmpDecoder;
|
|
18
18
|
export declare const convertToBmp: (filePath: string) => Promise<Buffer>;
|
|
19
19
|
export declare const parseImage: (buffer: Buffer) => BmpDecoder;
|
|
20
20
|
export declare const createImageBuffer: (image: BmpDecoder) => Buffer;
|
package/dist/image.js
CHANGED
|
@@ -51,25 +51,26 @@ exports.drawRect = function (imageDstBuffer, position, size, color) {
|
|
|
51
51
|
}
|
|
52
52
|
}
|
|
53
53
|
};
|
|
54
|
-
var drawSpectrum = function (imageDstBuffer, spectrum, size, color) {
|
|
55
|
-
var paddingLeft = Math.trunc(imageDstBuffer.width / 2 - size.width / 2);
|
|
54
|
+
var drawSpectrum = function (imageDstBuffer, spectrum, size, position, color) {
|
|
56
55
|
var busWidth = Math.trunc(size.width / spectrum.length);
|
|
56
|
+
var left = Math.trunc(position.x);
|
|
57
|
+
var top = Math.trunc(position.y);
|
|
57
58
|
var margin = 4;
|
|
58
59
|
for (var spectrumX = 0; spectrumX < spectrum.length; spectrumX++) {
|
|
59
60
|
var spectrumValue = spectrum[spectrumX];
|
|
60
61
|
if ((spectrumValue > 1) || (spectrumValue < 0)) {
|
|
61
62
|
throw new Error('Spectrum values must be in range from 0 to 1');
|
|
62
63
|
}
|
|
63
|
-
var rectX =
|
|
64
|
+
var rectX = left + busWidth * spectrumX;
|
|
64
65
|
var rectHeight = size.height * spectrumValue;
|
|
65
|
-
exports.drawRect(imageDstBuffer, { x: rectX + margin, y:
|
|
66
|
+
exports.drawRect(imageDstBuffer, { x: rectX + margin, y: top }, { width: busWidth - margin / 2, height: rectHeight }, color);
|
|
66
67
|
}
|
|
67
68
|
};
|
|
68
|
-
exports.createVisualizerFrame = function (backgroundImageBuffer, spectrum, size, color) {
|
|
69
|
+
exports.createVisualizerFrame = function (backgroundImageBuffer, spectrum, size, position, color) {
|
|
69
70
|
var image = Object.assign({}, backgroundImageBuffer);
|
|
70
71
|
image.data = Buffer.from(image.data);
|
|
71
72
|
var rgbSpectrumColor = (typeof color === 'string') ? hexToRgb(color) : color;
|
|
72
|
-
drawSpectrum(image, spectrum, size, rgbSpectrumColor);
|
|
73
|
+
drawSpectrum(image, spectrum, size, position, rgbSpectrumColor);
|
|
73
74
|
return image;
|
|
74
75
|
};
|
|
75
76
|
exports.convertToBmp = function (filePath) { return __awaiter(void 0, void 0, void 0, function () {
|
package/dist/image.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"image.js","sourceRoot":"","sources":["../src/image.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA,iCAAoD;AACpD,8CAAwB;AAkBX,QAAA,QAAQ,GAAG,UAAC,cAA0B,EAAE,QAAkB,EAAE,IAAU,EAAE,KAAY;IAC/F,KAAK,IAAI,KAAK,GAAG,QAAQ,CAAC,CAAC,EAAE,KAAK,GAAG,QAAQ,CAAC,CAAC,GAAG,IAAI,CAAC,MAAM,EAAE,KAAK,EAAE,EAAE;QACtE,KAAK,IAAI,KAAK,GAAG,QAAQ,CAAC,CAAC,EAAE,KAAK,GAAG,QAAQ,CAAC,CAAC,GAAG,IAAI,CAAC,KAAK,EAAE,KAAK,EAAE,EAAE;YACrE,IAAI,GAAG,GAAG,CAAC,cAAc,CAAC,KAAK,GAAG,KAAK,GAAG,KAAK,CAAC,IAAI,CAAC,CAAC;YAEtD,cAAc,CAAC,IAAI,CAAC,GAAG,GAAG,CAAC,CAAC,GAAG,KAAK,CAAC,IAAI,CAAC;YAC1C,cAAc,CAAC,IAAI,CAAC,GAAG,GAAG,CAAC,CAAC,GAAG,KAAK,CAAC,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|
|
19
19
|
width?: number;
|
|
20
20
|
height?: number;
|
|
21
|
+
x?: number | PositionAliasName;
|
|
22
|
+
y?: number | PositionAliasName;
|
|
21
23
|
color?: Color | string;
|
|
22
24
|
};
|
|
23
25
|
};
|
|
@@ -27,4 +29,5 @@ export interface Config {
|
|
|
27
29
|
frame_processing_delay?: number;
|
|
28
30
|
};
|
|
29
31
|
}
|
|
32
|
+
export declare type PositionAliasName = 'left' | 'center' | 'right' | 'top' | 'middle' | 'bottom';
|
|
30
33
|
export declare const renderAudioVisualizer: (config: Config, onProgress?: ((progress: number) => any) | undefined, shouldStop?: (() => boolean) | undefined) => Promise<number>;
|
package/dist/index.js
CHANGED
|
@@ -62,7 +62,7 @@ var sleep = function (timeout) {
|
|
|
62
62
|
};
|
|
63
63
|
exports.renderAudioVisualizer = function (config, onProgress, shouldStop) {
|
|
64
64
|
return new Promise(function (resolve) { return __awaiter(void 0, void 0, void 0, function () {
|
|
65
|
-
var audioFilePath, backgroundImagePath, outVideoPath, backgroundImageBmpBuffer, backgroundImage, audioReader, audioBuffer, sampleRate, FPS, spectrumWidth, spectrumHeight, spectrumColor, ffmpeg_cfr, ffmpeg_preset, frame_processing_delay, spectrumBusesCount, audioDuration, framesCount, audioDataStep, ffmpegVideoWriter, processSpectrum, _loop_1, i, state_1;
|
|
65
|
+
var audioFilePath, backgroundImagePath, outVideoPath, backgroundImageBmpBuffer, backgroundImage, audioReader, audioBuffer, sampleRate, FPS, spectrumWidth, spectrumHeight, spectrumX, spectrumY, spectrumColor, ffmpeg_cfr, ffmpeg_preset, frame_processing_delay, spectrumBusesCount, audioDuration, framesCount, audioDataStep, ffmpegVideoWriter, processSpectrum, _loop_1, i, state_1;
|
|
66
66
|
return __generator(this, function (_a) {
|
|
67
67
|
switch (_a.label) {
|
|
68
68
|
case 0:
|
|
@@ -84,6 +84,8 @@ exports.renderAudioVisualizer = function (config, onProgress, shouldStop) {
|
|
|
84
84
|
FPS = config_1.getFPS(config);
|
|
85
85
|
spectrumWidth = config_1.getSpectrumWidthAbsolute(config, backgroundImage.width);
|
|
86
86
|
spectrumHeight = config_1.getSpectrumHeightAbsolute(config, backgroundImage.height);
|
|
87
|
+
spectrumX = config_1.getSpectrumXAbsolute(config, spectrumWidth, backgroundImage.width);
|
|
88
|
+
spectrumY = config_1.getSpectrumYAbsolute(config, spectrumHeight, backgroundImage.height);
|
|
87
89
|
spectrumColor = config_1.getSpectrumColor(config) ||
|
|
88
90
|
image_1.invertColor(image_1.getImageColor(backgroundImage));
|
|
89
91
|
ffmpeg_cfr = config_1.getFfmpeg_cfr(config);
|
|
@@ -105,7 +107,7 @@ exports.renderAudioVisualizer = function (config, onProgress, shouldStop) {
|
|
|
105
107
|
return exports.PCM_FORMAT.parseFunction(audioBuffer, i * audioDataStep, i * audioDataStep + audioDataStep);
|
|
106
108
|
};
|
|
107
109
|
spectrum = processSpectrum(i, audioDataParser);
|
|
108
|
-
frameImage = image_1.createVisualizerFrame(backgroundImage, spectrum, { width: spectrumWidth, height: spectrumHeight }, spectrumColor);
|
|
110
|
+
frameImage = image_1.createVisualizerFrame(backgroundImage, spectrum, { width: spectrumWidth, height: spectrumHeight }, { x: spectrumX, y: spectrumY }, spectrumColor);
|
|
109
111
|
frameImageBuffer = image_1.createImageBuffer(frameImage);
|
|
110
112
|
isFrameProcessed = ffmpegVideoWriter.stdin.write(frameImageBuffer);
|
|
111
113
|
if (!!isFrameProcessed) return [3 /*break*/, 2];
|
package/dist/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA,
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA,mCAakB;AAClB,iCAAqF;AACrF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AC,kBAAkB,CAAC,CAAC;wCAC5D,CAAC;;;;;oCACF,eAAe,GAAG;wCACtB,OAAA,kBAAU,CAAC,aAAa,CAAC,WAAW,EAAE,CAAC,GAAG,aAAa,EAAE,CAAC,GAAG,aAAa,GAAG,aAAa,CAAC;oCAA3F,CAA2F,CAAC;oCACxF,QAAQ,GAAG,eAAe,CAAC,CAAC,EAAE,eAAe,CAAC,CAAC;oCAC/C,UAAU,GAAG,6BAAqB,CACtC,eAAe,EACf,QAAQ,EACR,EAAE,KAAK,EAAE,aAAa,EAAE,MAAM,EAAE,cAAc,EAAE,EAChD,EAAE,CAAC,EAAE,SAAS,EAAE,CAAC,EAAE,SAAS,EAAE,EAC9B,aAAa,CACd,CAAC;oCACI,gBAAgB,GAAG,yBAAiB,CAAC,UAAU,CAAC,CAAC;oCACjD,gBAAgB,GAAG,iBAAiB,CAAC,KAAK,CAAC,KAAK,CAAC,gBAAgB,CAAC,CAAC;yCACrE,CAAC,gBAAgB,EAAjB,wBAAiB;oCACnB,qBAAM,iBAAS,CAAC,iBAAiB,CAAC,KAAK,CAAC,EAAA;;oCAAxC,SAAwC,CAAC;;;oCAE3C,IAAI,UAAU,IAAI,UAAU,EAAE,EAAE;;qCAE/B;yCACG,sBAAsB,EAAtB,wBAAsB;oCACxB,qBAAM,KAAK,CAAC,sBAAsB,CAAC,EAAA;;oCAAnC,SAAmC,CAAC;;;;;;oBApB/B,CAAC,GAAG,CAAC;;;yBAAE,CAAA,CAAC,GAAG,WAAW,CAAA;kDAAtB,CAAC;;;;;;;oBAAuB,CAAC,EAAE,CAAA;;;oBAwBpC,iBAAiB,CAAC,KAAK,CAAC,GAAG,EAAE,CAAC;;;;SAC/B,CAAC;AAvEF,CAuEE,CAAC"}
|
package/package.json
CHANGED
package/src/config.ts
CHANGED
|
@@ -1,12 +1,30 @@
|
|
|
1
1
|
import path from 'path';
|
|
2
|
-
import { Config } from './index';
|
|
2
|
+
import { Config, PositionAliasName } from './index';
|
|
3
3
|
|
|
4
4
|
export const defaults = {
|
|
5
5
|
fps: 60,
|
|
6
6
|
spectrumWidth: 0.4,
|
|
7
|
-
spectrumHeight: 0.1
|
|
7
|
+
spectrumHeight: 0.1,
|
|
8
|
+
spectrumX: 'center',
|
|
9
|
+
spectrumY: 'top'
|
|
8
10
|
};
|
|
9
11
|
|
|
12
|
+
type RelativePositionValue = 0 | 0.5 | 1;
|
|
13
|
+
|
|
14
|
+
interface PositionAlias {
|
|
15
|
+
name: PositionAliasName;
|
|
16
|
+
value: RelativePositionValue;
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
const postitionAliases: PositionAlias[] = [
|
|
20
|
+
{ name: 'left', value: 0 },
|
|
21
|
+
{ name: 'center', value: 0.5 },
|
|
22
|
+
{ name: 'right', value: 1 },
|
|
23
|
+
{ name: 'top', value: 0 },
|
|
24
|
+
{ name: 'middle', value: 0.5 },
|
|
25
|
+
{ name: 'bottom', value: 1 },
|
|
26
|
+
];
|
|
27
|
+
|
|
10
28
|
const checkIsInt = (num: number) =>
|
|
11
29
|
num % 1 === 0;
|
|
12
30
|
|
|
@@ -52,6 +70,99 @@ export const getSpectrumHeightAbsolute = (
|
|
|
52
70
|
return backgroundImageHeight * spectrumHeight;
|
|
53
71
|
};
|
|
54
72
|
|
|
73
|
+
const getSpectrumX = (config: Config) =>
|
|
74
|
+
parseNumberIfPossible(
|
|
75
|
+
(config.outVideo.spectrum && config.outVideo.spectrum.x) ||
|
|
76
|
+
defaults.spectrumX
|
|
77
|
+
);
|
|
78
|
+
|
|
79
|
+
const getSpectrumY = (config: Config) =>
|
|
80
|
+
parseNumberIfPossible(
|
|
81
|
+
(config.outVideo.spectrum && config.outVideo.spectrum.y) ||
|
|
82
|
+
defaults.spectrumY
|
|
83
|
+
);
|
|
84
|
+
|
|
85
|
+
const parseNumberIfPossible = (value: string | number) => {
|
|
86
|
+
const parsed = Number(value);
|
|
87
|
+
if (isNaN(parsed)) {
|
|
88
|
+
return value;
|
|
89
|
+
}
|
|
90
|
+
return parsed;
|
|
91
|
+
};
|
|
92
|
+
|
|
93
|
+
const parsePositionAlias = (aliasName: string): PositionAlias => {
|
|
94
|
+
const nameNormalized = aliasName.trim().toLowerCase();
|
|
95
|
+
const alias = postitionAliases.find(
|
|
96
|
+
als => als.name === nameNormalized
|
|
97
|
+
);
|
|
98
|
+
if (!alias) {
|
|
99
|
+
throw new Error();
|
|
100
|
+
}
|
|
101
|
+
return alias;
|
|
102
|
+
};
|
|
103
|
+
|
|
104
|
+
const getValidPositionAliasValues = () =>
|
|
105
|
+
postitionAliases.map(aliasEl => aliasEl.name).join(', ');
|
|
106
|
+
|
|
107
|
+
const getSpectrumAbsolutePosition = (
|
|
108
|
+
relativePosition: RelativePositionValue,
|
|
109
|
+
spectrumSize: number,
|
|
110
|
+
backgroundImageSize: number
|
|
111
|
+
) => {
|
|
112
|
+
switch (relativePosition) {
|
|
113
|
+
case 0:
|
|
114
|
+
return 0;
|
|
115
|
+
case 0.5:
|
|
116
|
+
return relativePosition * backgroundImageSize - spectrumSize / 2;
|
|
117
|
+
case 1:
|
|
118
|
+
return relativePosition * backgroundImageSize - spectrumSize;
|
|
119
|
+
default:
|
|
120
|
+
throw new Error();
|
|
121
|
+
}
|
|
122
|
+
};
|
|
123
|
+
|
|
124
|
+
export const getSpectrumXAbsolute = (
|
|
125
|
+
config: Config,
|
|
126
|
+
spectrumWidth: number,
|
|
127
|
+
backgroundImageWidth: number
|
|
128
|
+
) => {
|
|
129
|
+
const spectrumX = getSpectrumX(config);
|
|
130
|
+
if (typeof spectrumX !== 'string') {
|
|
131
|
+
return spectrumX;
|
|
132
|
+
}
|
|
133
|
+
try {
|
|
134
|
+
const spectrumXParsed = parsePositionAlias(spectrumX);
|
|
135
|
+
return getSpectrumAbsolutePosition(
|
|
136
|
+
spectrumXParsed.value,
|
|
137
|
+
spectrumWidth,
|
|
138
|
+
backgroundImageWidth
|
|
139
|
+
);
|
|
140
|
+
} catch {
|
|
141
|
+
throw new Error(`Invalid spectrum x value: ${spectrumX}. Valid values: ${getValidPositionAliasValues()}.`);
|
|
142
|
+
}
|
|
143
|
+
};
|
|
144
|
+
|
|
145
|
+
export const getSpectrumYAbsolute = (
|
|
146
|
+
config: Config,
|
|
147
|
+
spectrumHeight: number,
|
|
148
|
+
backgroundImageHeight: number
|
|
149
|
+
) => {
|
|
150
|
+
const spectrumY = getSpectrumY(config);
|
|
151
|
+
if (typeof spectrumY !== 'string') {
|
|
152
|
+
return spectrumY;
|
|
153
|
+
}
|
|
154
|
+
try {
|
|
155
|
+
const spectrumXParsed = parsePositionAlias(spectrumY);
|
|
156
|
+
return getSpectrumAbsolutePosition(
|
|
157
|
+
spectrumXParsed.value,
|
|
158
|
+
spectrumHeight,
|
|
159
|
+
backgroundImageHeight
|
|
160
|
+
);
|
|
161
|
+
} catch {
|
|
162
|
+
throw new Error(`Invalid spectrum y value: ${spectrumY}. Valid values: ${getValidPositionAliasValues()}.`);
|
|
163
|
+
}
|
|
164
|
+
};
|
|
165
|
+
|
|
55
166
|
export const getSpectrumColor = (config: Config) =>
|
|
56
167
|
(config.outVideo.spectrum && config.outVideo.spectrum.color);
|
|
57
168
|
|
package/src/image.ts
CHANGED
|
@@ -29,9 +29,10 @@ export const drawRect = (imageDstBuffer: BmpDecoder, position: Position, size: S
|
|
|
29
29
|
}
|
|
30
30
|
};
|
|
31
31
|
|
|
32
|
-
const drawSpectrum = (imageDstBuffer: BmpDecoder, spectrum: number[], size: Size, color: Color) => {
|
|
33
|
-
const paddingLeft = Math.trunc(imageDstBuffer.width / 2 - size.width / 2);
|
|
32
|
+
const drawSpectrum = (imageDstBuffer: BmpDecoder, spectrum: number[], size: Size, position: Position, color: Color) => {
|
|
34
33
|
const busWidth = Math.trunc(size.width / spectrum.length);
|
|
34
|
+
const left = Math.trunc(position.x);
|
|
35
|
+
const top = Math.trunc(position.y);
|
|
35
36
|
const margin = 4;
|
|
36
37
|
|
|
37
38
|
for (let spectrumX = 0; spectrumX < spectrum.length; spectrumX++) {
|
|
@@ -40,17 +41,17 @@ const drawSpectrum = (imageDstBuffer: BmpDecoder, spectrum: number[], size: Size
|
|
|
40
41
|
throw new Error('Spectrum values must be in range from 0 to 1');
|
|
41
42
|
}
|
|
42
43
|
|
|
43
|
-
const rectX =
|
|
44
|
+
const rectX = left + busWidth * spectrumX;
|
|
44
45
|
const rectHeight = size.height * spectrumValue;
|
|
45
|
-
drawRect(imageDstBuffer, { x: rectX + margin, y:
|
|
46
|
+
drawRect(imageDstBuffer, { x: rectX + margin, y: top }, { width: busWidth - margin / 2, height: rectHeight }, color);
|
|
46
47
|
}
|
|
47
48
|
};
|
|
48
49
|
|
|
49
|
-
export const createVisualizerFrame = (backgroundImageBuffer: BmpDecoder, spectrum: number[], size: Size, color: Color | string) => {
|
|
50
|
+
export const createVisualizerFrame = (backgroundImageBuffer: BmpDecoder, spectrum: number[], size: Size, position: Position, color: Color | string) => {
|
|
50
51
|
const image = Object.assign({}, backgroundImageBuffer);
|
|
51
52
|
image.data = Buffer.from(image.data);
|
|
52
53
|
const rgbSpectrumColor = (typeof color === 'string') ? hexToRgb(color) : color;
|
|
53
|
-
drawSpectrum(image, spectrum, size, rgbSpectrumColor);
|
|
54
|
+
drawSpectrum(image, spectrum, size, position, rgbSpectrumColor);
|
|
54
55
|
return image;
|
|
55
56
|
};
|
|
56
57
|
|
package/src/index.ts
CHANGED
|
@@ -5,6 +5,8 @@ import {
|
|
|
5
5
|
getFPS,
|
|
6
6
|
getSpectrumWidthAbsolute,
|
|
7
7
|
getSpectrumHeightAbsolute,
|
|
8
|
+
getSpectrumXAbsolute,
|
|
9
|
+
getSpectrumYAbsolute,
|
|
8
10
|
getSpectrumColor,
|
|
9
11
|
getFfmpeg_cfr,
|
|
10
12
|
getFfmpeg_preset,
|
|
@@ -34,6 +36,8 @@ export interface Config {
|
|
|
34
36
|
spectrum?: {
|
|
35
37
|
width?: number;
|
|
36
38
|
height?: number;
|
|
39
|
+
x?: number | PositionAliasName;
|
|
40
|
+
y?: number | PositionAliasName;
|
|
37
41
|
color?: Color | string;
|
|
38
42
|
}
|
|
39
43
|
};
|
|
@@ -44,6 +48,14 @@ export interface Config {
|
|
|
44
48
|
};
|
|
45
49
|
}
|
|
46
50
|
|
|
51
|
+
export type PositionAliasName =
|
|
52
|
+
'left' |
|
|
53
|
+
'center' |
|
|
54
|
+
'right' |
|
|
55
|
+
'top' |
|
|
56
|
+
'middle' |
|
|
57
|
+
'bottom';
|
|
58
|
+
|
|
47
59
|
const sleep = (timeout: number) =>
|
|
48
60
|
new Promise(resolve => setTimeout(resolve, timeout));
|
|
49
61
|
|
|
@@ -67,6 +79,10 @@ export const renderAudioVisualizer = (config: Config, onProgress?: (progress: nu
|
|
|
67
79
|
getSpectrumWidthAbsolute(config, backgroundImage.width);
|
|
68
80
|
const spectrumHeight =
|
|
69
81
|
getSpectrumHeightAbsolute(config, backgroundImage.height);
|
|
82
|
+
const spectrumX =
|
|
83
|
+
getSpectrumXAbsolute(config, spectrumWidth, backgroundImage.width);
|
|
84
|
+
const spectrumY =
|
|
85
|
+
getSpectrumYAbsolute(config, spectrumHeight, backgroundImage.height);
|
|
70
86
|
const spectrumColor =
|
|
71
87
|
getSpectrumColor(config) ||
|
|
72
88
|
invertColor(getImageColor(backgroundImage));
|
|
@@ -98,6 +114,7 @@ export const renderAudioVisualizer = (config: Config, onProgress?: (progress: nu
|
|
|
98
114
|
backgroundImage,
|
|
99
115
|
spectrum,
|
|
100
116
|
{ width: spectrumWidth, height: spectrumHeight },
|
|
117
|
+
{ x: spectrumX, y: spectrumY },
|
|
101
118
|
spectrumColor
|
|
102
119
|
);
|
|
103
120
|
const frameImageBuffer = createImageBuffer(frameImage);
|
package/src/tests/config.test.ts
CHANGED
|
@@ -9,6 +9,8 @@ import {
|
|
|
9
9
|
getFPS,
|
|
10
10
|
getSpectrumWidthAbsolute,
|
|
11
11
|
getSpectrumHeightAbsolute,
|
|
12
|
+
getSpectrumXAbsolute,
|
|
13
|
+
getSpectrumYAbsolute,
|
|
12
14
|
getSpectrumColor,
|
|
13
15
|
getFfmpeg_cfr,
|
|
14
16
|
getFfmpeg_preset,
|
|
@@ -92,6 +94,136 @@ describe('config', function() {
|
|
|
92
94
|
expect(result3).equal(backgroundImageHeight * 0.69);
|
|
93
95
|
});
|
|
94
96
|
|
|
97
|
+
it('getSpectrumXAbsolute', function() {
|
|
98
|
+
const spectrumWidth = 420;
|
|
99
|
+
const backgroundImageWidth = 1366;
|
|
100
|
+
const result1 = getSpectrumXAbsolute(
|
|
101
|
+
{
|
|
102
|
+
outVideo: {},
|
|
103
|
+
} as Config,
|
|
104
|
+
spectrumWidth,
|
|
105
|
+
backgroundImageWidth
|
|
106
|
+
);
|
|
107
|
+
expect(result1).equal(
|
|
108
|
+
0.5 * backgroundImageWidth - spectrumWidth / 2
|
|
109
|
+
);
|
|
110
|
+
|
|
111
|
+
const result2 = getSpectrumXAbsolute(
|
|
112
|
+
{
|
|
113
|
+
outVideo: { spectrum: { x: 420 } }
|
|
114
|
+
} as Config,
|
|
115
|
+
spectrumWidth,
|
|
116
|
+
backgroundImageWidth
|
|
117
|
+
);
|
|
118
|
+
expect(result2).equal(420);
|
|
119
|
+
|
|
120
|
+
const result3 = getSpectrumXAbsolute(
|
|
121
|
+
{
|
|
122
|
+
outVideo: { spectrum: { x: 'left' } }
|
|
123
|
+
} as Config,
|
|
124
|
+
spectrumWidth,
|
|
125
|
+
backgroundImageWidth
|
|
126
|
+
);
|
|
127
|
+
expect(result3).equal(0);
|
|
128
|
+
|
|
129
|
+
const result4 = getSpectrumXAbsolute(
|
|
130
|
+
{
|
|
131
|
+
outVideo: { spectrum: { x: 'center' } }
|
|
132
|
+
} as Config,
|
|
133
|
+
spectrumWidth,
|
|
134
|
+
backgroundImageWidth
|
|
135
|
+
);
|
|
136
|
+
expect(result4).equal(
|
|
137
|
+
0.5 * backgroundImageWidth - spectrumWidth / 2
|
|
138
|
+
);
|
|
139
|
+
|
|
140
|
+
const result5 = getSpectrumXAbsolute(
|
|
141
|
+
{
|
|
142
|
+
outVideo: { spectrum: { x: 'right' } }
|
|
143
|
+
} as Config,
|
|
144
|
+
spectrumWidth,
|
|
145
|
+
backgroundImageWidth
|
|
146
|
+
);
|
|
147
|
+
expect(result5).equal(
|
|
148
|
+
1 * backgroundImageWidth - spectrumWidth
|
|
149
|
+
);
|
|
150
|
+
|
|
151
|
+
const result6 = getSpectrumXAbsolute.bind(
|
|
152
|
+
undefined,
|
|
153
|
+
{
|
|
154
|
+
outVideo: { spectrum: { x: 'sagewklkg' } }
|
|
155
|
+
} as unknown as Config,
|
|
156
|
+
spectrumWidth,
|
|
157
|
+
backgroundImageWidth
|
|
158
|
+
);
|
|
159
|
+
expect(result6).to.throw('Invalid spectrum x value: sagewklkg. Valid values: left, center, right, top, middle, bottom.');
|
|
160
|
+
});
|
|
161
|
+
|
|
162
|
+
it('getSpectrumYAbsolute', function() {
|
|
163
|
+
const spectrumHeight = 125;
|
|
164
|
+
const backgroundImageHeight = 768;
|
|
165
|
+
const result1 = getSpectrumYAbsolute(
|
|
166
|
+
{
|
|
167
|
+
outVideo: {},
|
|
168
|
+
} as Config,
|
|
169
|
+
spectrumHeight,
|
|
170
|
+
backgroundImageHeight
|
|
171
|
+
);
|
|
172
|
+
expect(result1).equal(
|
|
173
|
+
0
|
|
174
|
+
);
|
|
175
|
+
|
|
176
|
+
const result2 = getSpectrumYAbsolute(
|
|
177
|
+
{
|
|
178
|
+
outVideo: { spectrum: { y: 69 } }
|
|
179
|
+
} as Config,
|
|
180
|
+
spectrumHeight,
|
|
181
|
+
backgroundImageHeight
|
|
182
|
+
);
|
|
183
|
+
expect(result2).equal(69);
|
|
184
|
+
|
|
185
|
+
const result3 = getSpectrumYAbsolute(
|
|
186
|
+
{
|
|
187
|
+
outVideo: { spectrum: { y: 'top' } }
|
|
188
|
+
} as Config,
|
|
189
|
+
spectrumHeight,
|
|
190
|
+
backgroundImageHeight
|
|
191
|
+
);
|
|
192
|
+
expect(result3).equal(0);
|
|
193
|
+
|
|
194
|
+
const result4 = getSpectrumYAbsolute(
|
|
195
|
+
{
|
|
196
|
+
outVideo: { spectrum: { y: 'middle' } }
|
|
197
|
+
} as Config,
|
|
198
|
+
spectrumHeight,
|
|
199
|
+
backgroundImageHeight
|
|
200
|
+
);
|
|
201
|
+
expect(result4).equal(
|
|
202
|
+
0.5 * backgroundImageHeight - spectrumHeight / 2
|
|
203
|
+
);
|
|
204
|
+
|
|
205
|
+
const result5 = getSpectrumYAbsolute(
|
|
206
|
+
{
|
|
207
|
+
outVideo: { spectrum: { y: 'bottom' } }
|
|
208
|
+
} as Config,
|
|
209
|
+
spectrumHeight,
|
|
210
|
+
backgroundImageHeight
|
|
211
|
+
);
|
|
212
|
+
expect(result5).equal(
|
|
213
|
+
1 * backgroundImageHeight - spectrumHeight
|
|
214
|
+
);
|
|
215
|
+
|
|
216
|
+
const result6 = getSpectrumYAbsolute.bind(
|
|
217
|
+
undefined,
|
|
218
|
+
{
|
|
219
|
+
outVideo: { spectrum: { y: 'asokosckm' } }
|
|
220
|
+
} as unknown as Config,
|
|
221
|
+
spectrumHeight,
|
|
222
|
+
backgroundImageHeight
|
|
223
|
+
);
|
|
224
|
+
expect(result6).to.throw('Invalid spectrum y value: asokosckm. Valid values: left, center, right, top, middle, bottom.');
|
|
225
|
+
});
|
|
226
|
+
|
|
95
227
|
it('getSpectrumColor', function() {
|
|
96
228
|
const result1 = getSpectrumColor({
|
|
97
229
|
outVideo: {}
|
package/src/tests/image.test.ts
CHANGED
|
@@ -32,10 +32,11 @@ describe('image', function () {
|
|
|
32
32
|
const backgroundImagePath = path.resolve('example/media/background.png');
|
|
33
33
|
const backgroundImageBmpBuffer = await convertToBmp(backgroundImagePath);
|
|
34
34
|
const backgroundImage = parseImage(backgroundImageBmpBuffer);
|
|
35
|
-
const frame =
|
|
35
|
+
const frame = createVisualizerFrame(
|
|
36
36
|
backgroundImage,
|
|
37
37
|
[0.5, 0, 1],
|
|
38
38
|
{ width: 15, height: 20 },
|
|
39
|
+
{ x: 15, y: 0 },
|
|
39
40
|
{ red: 1, green: 1, blue: 1 }
|
|
40
41
|
);
|
|
41
42
|
expect(frame.data.toJSON().data.length).equal(1090560);
|