-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
executable file
·64 lines (56 loc) · 1.92 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
#!/usr/bin/env node
import { exec } from "child_process";
import { readFileSync } from "fs";
import { mkdtemp } from "fs/promises";
import path from "path";
import wav from "node-wav";
import os from "os";
import Meyda from "meyda";
const CONFIG = {
bufferSize: 1024,
hopSize: 512,
numberOfMFCCCoefficients: 13,
features: ["rms", "mfcc"]
}
const FILE = process.argv[process.argv.length - 1];
// Create a temporary directory to store transcoded audio
const TEMP_DIR = await mkdtemp(path.join(os.tmpdir(), "transcoder-storage-"));
async function transcodeToWav(filename) {
return new Promise((resolve, reject) => {
let output_filename = `${path.join(TEMP_DIR, path.win32.basename(filename))}.wav`;
// "shell out" to ffmpeg
exec(
`ffmpeg -i ${filename} ${output_filename}`,
(error, stdout, stderr) => {
if (error) {
console.log("ERROR: ", error);
reject(error);
}
resolve({ filename: output_filename, stdout, stderr });
}
);
});
}
function chunk(samples, chunkSize, hopSize) {
const chunks = [];
for (let i = 0; i < samples.length; i += hopSize) {
chunks.push(samples.slice(i, i + chunkSize));
}
return chunks;
}
try {
let { filename } = await transcodeToWav(FILE);
// result.filename is the new filename of the transcoded audio.
// We can now use node-wav as described above to read the audio
let buffer = readFileSync(filename);
let decodedAudio = wav.decode(buffer);
const buffers = chunk(decodedAudio.channelData[0], CONFIG.bufferSize, CONFIG.hopSize);
const features = buffers.map(buffer => Meyda.extract(CONFIG.features, buffer));
console.log(features);
console.log(["rms"].concat(new Array(features[0].mfcc.length).fill("mfcc").map((_, i) => `mfcc${i}`)).join(","));
for (var featureFrame of features) {
console.log([featureFrame.rms, ...featureFrame.mfcc].join(","));
}
} catch (error) {
console.log("ERROR: ", error);
}