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 +1 @@
1
- export declare const waitForEventLoop: () => Promise<void>;
1
+ export declare const waitForEventLoop: () => Promise<void>;
@@ -1,6 +1,6 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.waitForEventLoop = function () {
4
- return new Promise(function (resolve) { return setImmediate(resolve); });
5
- };
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.waitForEventLoop = function () {
4
+ return new Promise(function (resolve) { return setImmediate(resolve); });
5
+ };
6
6
  //# sourceMappingURL=waitForEventLoop.js.map
package/package.json CHANGED
@@ -1,62 +1,62 @@
1
- {
2
- "name": "nodejs-audio-visualizer",
3
- "version": "5.5.3",
4
- "description": "",
5
- "main": "dist/index.js",
6
- "types": "dist/index.d.ts",
7
- "files": [
8
- "dist/",
9
- "src/"
10
- ],
11
- "scripts": {
12
- "test": "ts-mocha -p tsconfig.json src/**/*.test.ts",
13
- "dev": "ts-node-dev --respawn --transpileOnly ./example/example.ts",
14
- "prebuild": "rimraf dist",
15
- "build": "tsc -m CommonJS",
16
- "preversion": "npm test",
17
- "version": "npm run build && git add .",
18
- "postversion": "git push && git push --tags",
19
- "prepublishOnly": "npm run build"
20
- },
21
- "repository": {
22
- "type": "git",
23
- "url": "git+https://github.com/VladislavPetyukevich/audio-visualizer.git"
24
- },
25
- "homepage": "https://github.com/VladislavPetyukevich/audio-visualizer#readme",
26
- "keywords": [
27
- "audio",
28
- "visualizer"
29
- ],
30
- "author": "",
31
- "license": "MIT",
32
- "devDependencies": {
33
- "@types/chai": "^4.2.4",
34
- "@types/mocha": "^5.2.7",
35
- "chai": "^4.2.0",
36
- "husky": "^3.1.0",
37
- "mocha": "^6.2.3",
38
- "rimraf": "^3.0.2",
39
- "sinon": "^7.5.0",
40
- "ts-mocha": "^11.1.0",
41
- "ts-node": "^8.5.0",
42
- "ts-node-dev": "^1.0.0-pre.43",
43
- "typescript": "^3.7.2"
44
- },
45
- "husky": {
46
- "hooks": {
47
- "pre-commit": "npm test"
48
- }
49
- },
50
- "dependencies": {
51
- "@jimp/bmp": "^0.9.3",
52
- "@jimp/png": "^0.9.3",
53
- "@types/bmp-js": "^0.1.0",
54
- "@types/ffmpeg-static": "^3.0.3",
55
- "@types/sinon": "^7.5.0",
56
- "bmp-js": "^0.1.0",
57
- "ffmpeg-static": "^5.2.0",
58
- "fft-js": "^0.0.12",
59
- "jimp": "^0.9.3",
60
- "music-tempo": "^1.0.3"
61
- }
62
- }
1
+ {
2
+ "name": "nodejs-audio-visualizer",
3
+ "version": "5.6.4",
4
+ "description": "",
5
+ "main": "dist/index.js",
6
+ "types": "dist/index.d.ts",
7
+ "files": [
8
+ "dist/",
9
+ "src/"
10
+ ],
11
+ "scripts": {
12
+ "test": "ts-mocha -p tsconfig.json src/**/*.test.ts",
13
+ "dev": "ts-node-dev --respawn --transpileOnly ./example/example.ts",
14
+ "prebuild": "rimraf dist",
15
+ "build": "tsc -m CommonJS",
16
+ "preversion": "npm test",
17
+ "version": "npm run build && git add .",
18
+ "postversion": "git push && git push --tags",
19
+ "prepublishOnly": "npm run build"
20
+ },
21
+ "repository": {
22
+ "type": "git",
23
+ "url": "git+https://github.com/VladislavPetyukevich/audio-visualizer.git"
24
+ },
25
+ "homepage": "https://github.com/VladislavPetyukevich/audio-visualizer#readme",
26
+ "keywords": [
27
+ "audio",
28
+ "visualizer"
29
+ ],
30
+ "author": "",
31
+ "license": "MIT",
32
+ "devDependencies": {
33
+ "@types/chai": "^4.2.4",
34
+ "@types/mocha": "^5.2.7",
35
+ "chai": "^4.2.0",
36
+ "husky": "^3.1.0",
37
+ "mocha": "^6.2.3",
38
+ "rimraf": "^3.0.2",
39
+ "sinon": "^7.5.0",
40
+ "ts-mocha": "^11.1.0",
41
+ "ts-node": "^8.5.0",
42
+ "ts-node-dev": "^1.0.0-pre.43",
43
+ "typescript": "^3.7.2"
44
+ },
45
+ "husky": {
46
+ "hooks": {
47
+ "pre-commit": "npm test"
48
+ }
49
+ },
50
+ "dependencies": {
51
+ "@jimp/bmp": "^0.9.3",
52
+ "@jimp/png": "^0.9.3",
53
+ "@types/bmp-js": "^0.1.0",
54
+ "@types/ffmpeg-static": "^3.0.3",
55
+ "@types/sinon": "^7.5.0",
56
+ "bmp-js": "^0.1.0",
57
+ "ffmpeg-static": "^5.2.0",
58
+ "fft-js": "^0.0.12",
59
+ "jimp": "^0.9.3",
60
+ "music-tempo": "^1.0.3"
61
+ }
62
+ }
package/src/audio.ts CHANGED
@@ -1,129 +1,129 @@
1
- import { spawn } from 'child_process';
2
- import ffmpegPath from 'ffmpeg-static';
3
- import { FREQUENCY_BANDS, getSpectrum } from './dsp';
4
-
5
- export const skipEvery = <T>(skipIndex: number) => (element: T, index: number) =>
6
- index % skipIndex === 0;
7
-
8
- export const getPeaks = (spectrums: number[], prevPeaks?: number[]) => {
9
- if (!prevPeaks) {
10
- return spectrums;
11
- }
12
- const resultPeaks: number[] = [];
13
- for (let i = 0; i < spectrums.length; i++) {
14
- const currValue = spectrums[i];
15
- const currPrevPeak = prevPeaks[i] || 0;
16
- resultPeaks.push(
17
- (currValue > currPrevPeak) ? currValue : currPrevPeak
18
- );
19
- }
20
- return resultPeaks;
21
- };
22
-
23
- export const correctPeaks = (spectrums: number[], peaks: number[]) => {
24
- const resultSpectrum: number[] = [];
25
- for (let i = 0; i < spectrums.length; i++) {
26
- const value = spectrums[i];
27
- const peakValue = peaks[i] || 0;
28
- if (value < 3) {
29
- resultSpectrum.push(value / 3);
30
- } else {
31
- resultSpectrum.push(value / peakValue);
32
- }
33
- }
34
- return resultSpectrum;
35
- };
36
-
37
- const oldMax = 1;
38
- const newMin = 0.6;
39
- const getInterpolationFactor = (skippedFramesCount: number, skipFramesCount: number) => {
40
- const interpolationFactor = 1 - (skippedFramesCount / skipFramesCount);
41
- const interpolationFactorInRange = (interpolationFactor * (oldMax - newMin)) + newMin;
42
- return interpolationFactorInRange;
43
- };
44
-
45
- export const createSpectrumsProcessor = (sampleRate: number, skipFramesCount: number) => {
46
- let prevAudioDataNormalized: number[] = [];
47
- let prevPeaks: number[] = [];
48
- let skippedFramesCount = 0;
49
- let targetSpectrum: number[] = [];
50
- let previousSpectrum: number[] = [];
51
-
52
- return (parseAudioData: () => number[]) => {
53
- if (skippedFramesCount === 0) {
54
- skippedFramesCount = skipFramesCount;
55
- const audioDataNomrmalized = normalizeAudioData(parseAudioData());
56
- prevAudioDataNormalized = audioDataNomrmalized;
57
-
58
- const spectrum = getSpectrum(audioDataNomrmalized, sampleRate);
59
- const peaks = getPeaks(spectrum, prevPeaks);
60
- const correctedSpectrum =
61
- prevPeaks.length === 0 ?
62
- Array(FREQUENCY_BANDS.length).fill(0) :
63
- correctPeaks(spectrum, peaks);
64
-
65
- prevPeaks = peaks;
66
- previousSpectrum = [...targetSpectrum];
67
- targetSpectrum = correctedSpectrum;
68
- } else {
69
- skippedFramesCount--;
70
- }
71
- const interpolationFactor = getInterpolationFactor(skippedFramesCount, skipFramesCount);
72
- const interpolatedSpectrum = targetSpectrum.map((target, i) => {
73
- const prev = previousSpectrum[i] || target;
74
- return prev + (target - prev) * interpolationFactor;
75
- });
76
- return interpolatedSpectrum;
77
- };
78
- };
79
-
80
- export const bufferToUInt8 = (buffer: Buffer, start: number, end: number) => {
81
- const numbers = [];
82
- for (let i = start; i < end; i += 1) {
83
- numbers.push(buffer.readUInt8(i));
84
- }
85
- return numbers;
86
- };
87
-
88
- export const normalizeAudioData = (PCMData: number[]) =>
89
- PCMData.map(num => (num - 128) / 128);
90
-
91
- export const pcmU8ToFloatSamples = (buffer: Buffer): number[] => {
92
- const n = buffer.length;
93
- const samples = new Array<number>(n);
94
- for (let i = 0; i < n; i++) {
95
- samples[i] = (buffer[i] - 128) / 128;
96
- }
97
- return samples;
98
- };
99
-
100
- export const spawnFfmpegAudioReader = (filename: string, format: string) => {
101
- if (!ffmpegPath) {
102
- throw new Error('ffmpeg path not found');
103
- }
104
- const ffmpegProcess = spawn(ffmpegPath, ['-i', filename, '-f', format, '-ac', '1', '-']);
105
- return ffmpegProcess;
106
- };
107
-
108
- export const createAudioBuffer = (filename: string, format: string) =>
109
- new Promise<{ audioBuffer: Buffer, sampleRate: number }>((resolve, reject) => {
110
- let sampleRate: number;
111
- const sampleRateRegExp = /(\d+) Hz/m;
112
- const audioBuffers: Buffer[] = [];
113
- const ffmpegAudioReader = spawnFfmpegAudioReader(filename, format);
114
-
115
- ffmpegAudioReader.stderr.on('data', function (data) {
116
- const match = data.toString().match(sampleRateRegExp);
117
- if (!sampleRate && match) {
118
- sampleRate = match[1];
119
- }
120
- });
121
- ffmpegAudioReader.stdout.on('data', function (chunkBuffer) {
122
- audioBuffers.push(chunkBuffer);
123
- });
124
- ffmpegAudioReader.stdout.on('end', function () {
125
- const audioBuffer = Buffer.concat(audioBuffers);
126
- resolve({ audioBuffer, sampleRate });
127
- });
128
- });
129
-
1
+ import { spawn } from 'child_process';
2
+ import ffmpegPath from 'ffmpeg-static';
3
+ import { FREQUENCY_BANDS, getSpectrum } from './dsp';
4
+
5
+ export const skipEvery = <T>(skipIndex: number) => (element: T, index: number) =>
6
+ index % skipIndex === 0;
7
+
8
+ export const getPeaks = (spectrums: number[], prevPeaks?: number[]) => {
9
+ if (!prevPeaks) {
10
+ return spectrums;
11
+ }
12
+ const resultPeaks: number[] = [];
13
+ for (let i = 0; i < spectrums.length; i++) {
14
+ const currValue = spectrums[i];
15
+ const currPrevPeak = prevPeaks[i] || 0;
16
+ resultPeaks.push(
17
+ (currValue > currPrevPeak) ? currValue : currPrevPeak
18
+ );
19
+ }
20
+ return resultPeaks;
21
+ };
22
+
23
+ export const correctPeaks = (spectrums: number[], peaks: number[]) => {
24
+ const resultSpectrum: number[] = [];
25
+ for (let i = 0; i < spectrums.length; i++) {
26
+ const value = spectrums[i];
27
+ const peakValue = peaks[i] || 0;
28
+ if (value < 3) {
29
+ resultSpectrum.push(value / 3);
30
+ } else {
31
+ resultSpectrum.push(value / peakValue);
32
+ }
33
+ }
34
+ return resultSpectrum;
35
+ };
36
+
37
+ const oldMax = 1;
38
+ const newMin = 0.6;
39
+ const getInterpolationFactor = (skippedFramesCount: number, skipFramesCount: number) => {
40
+ const interpolationFactor = 1 - (skippedFramesCount / skipFramesCount);
41
+ const interpolationFactorInRange = (interpolationFactor * (oldMax - newMin)) + newMin;
42
+ return interpolationFactorInRange;
43
+ };
44
+
45
+ export const createSpectrumsProcessor = (sampleRate: number, skipFramesCount: number) => {
46
+ let prevAudioDataNormalized: number[] = [];
47
+ let prevPeaks: number[] = [];
48
+ let skippedFramesCount = 0;
49
+ let targetSpectrum: number[] = [];
50
+ let previousSpectrum: number[] = [];
51
+
52
+ return (parseAudioData: () => number[]) => {
53
+ if (skippedFramesCount === 0) {
54
+ skippedFramesCount = skipFramesCount;
55
+ const audioDataNomrmalized = normalizeAudioData(parseAudioData());
56
+ prevAudioDataNormalized = audioDataNomrmalized;
57
+
58
+ const spectrum = getSpectrum(audioDataNomrmalized, sampleRate);
59
+ const peaks = getPeaks(spectrum, prevPeaks);
60
+ const correctedSpectrum =
61
+ prevPeaks.length === 0 ?
62
+ Array(FREQUENCY_BANDS.length).fill(0) :
63
+ correctPeaks(spectrum, peaks);
64
+
65
+ prevPeaks = peaks;
66
+ previousSpectrum = [...targetSpectrum];
67
+ targetSpectrum = correctedSpectrum;
68
+ } else {
69
+ skippedFramesCount--;
70
+ }
71
+ const interpolationFactor = getInterpolationFactor(skippedFramesCount, skipFramesCount);
72
+ const interpolatedSpectrum = targetSpectrum.map((target, i) => {
73
+ const prev = previousSpectrum[i] || target;
74
+ return prev + (target - prev) * interpolationFactor;
75
+ });
76
+ return interpolatedSpectrum;
77
+ };
78
+ };
79
+
80
+ export const bufferToUInt8 = (buffer: Buffer, start: number, end: number) => {
81
+ const numbers = [];
82
+ for (let i = start; i < end; i += 1) {
83
+ numbers.push(buffer.readUInt8(i));
84
+ }
85
+ return numbers;
86
+ };
87
+
88
+ export const normalizeAudioData = (PCMData: number[]) =>
89
+ PCMData.map(num => (num - 128) / 128);
90
+
91
+ export const pcmU8ToFloatSamples = (buffer: Buffer): number[] => {
92
+ const n = buffer.length;
93
+ const samples = new Array<number>(n);
94
+ for (let i = 0; i < n; i++) {
95
+ samples[i] = (buffer[i] - 128) / 128;
96
+ }
97
+ return samples;
98
+ };
99
+
100
+ export const spawnFfmpegAudioReader = (filename: string, format: string) => {
101
+ if (!ffmpegPath) {
102
+ throw new Error('ffmpeg path not found');
103
+ }
104
+ const ffmpegProcess = spawn(ffmpegPath, ['-i', filename, '-f', format, '-ac', '1', '-']);
105
+ return ffmpegProcess;
106
+ };
107
+
108
+ export const createAudioBuffer = (filename: string, format: string) =>
109
+ new Promise<{ audioBuffer: Buffer, sampleRate: number }>((resolve, reject) => {
110
+ let sampleRate: number;
111
+ const sampleRateRegExp = /(\d+) Hz/m;
112
+ const audioBuffers: Buffer[] = [];
113
+ const ffmpegAudioReader = spawnFfmpegAudioReader(filename, format);
114
+
115
+ ffmpegAudioReader.stderr.on('data', function (data) {
116
+ const match = data.toString().match(sampleRateRegExp);
117
+ if (!sampleRate && match) {
118
+ sampleRate = match[1];
119
+ }
120
+ });
121
+ ffmpegAudioReader.stdout.on('data', function (chunkBuffer) {
122
+ audioBuffers.push(chunkBuffer);
123
+ });
124
+ ffmpegAudioReader.stdout.on('end', function () {
125
+ const audioBuffer = Buffer.concat(audioBuffers);
126
+ resolve({ audioBuffer, sampleRate });
127
+ });
128
+ });
129
+