-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
executable file
·65 lines (65 loc) · 2.13 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
65
#!/usr/bin/node
console.log("[wtb] WaveToBin");
const ffmpeg = require("./ffmpeg");
const pth = require("path");
(async function () {
try {
if(process.argv.length <= 2){
return console.log("No argument given. Try -h")
}
// Import library for handling wav files.
const { WaveFile } = require("wavefile");
// Load filesystem API
const fs = require("fs");
// Initialize output file as an wave file
const wf = new WaveFile();
// Procure argyments
const cf = require("./argp")(process.argv);
// Read the file.
if (cf.help) {
console.log(fs.readFileSync(pth.resolve(__dirname,"help.txt")).toString("utf8"));
return;
}
let inputFilePath = cf.file;
if(!(cf.useFFmpeg && inputFilePath.endsWith(".wav"))){
console.log("[wtb] Warning: --ffmpeg isn't implicitly specified")
cf.useFFmpeg = true
}
if (cf.useFFmpeg) {
console.log("[wtb] Waiting for FFmpeg");
inputFilePath = (await ffmpeg(inputFilePath,cf.ffmpegArg,process.stdout,cf)).path;
}
const fd = fs.readFileSync(inputFilePath);
// Load input file into wf.
wf.fromBuffer(fd);
/*do important conversions
*this will convert the sample-type into PCM u8
*changes the sample rate to the desired rate otherwise 32 Khz (good for html5bytebeat)
* */
if (wf.fmt.sampleRate != cf.sampleRate) wf.toSampleRate(cf.sampleRate);
if (wf.bitDepth !== "8") wf.toBitDepth("8");
// Obtain "samples" from audio
let smp = wf.getSamples();
let n; // contains the PCM buffer for output file
// properly interleave the channels
if(cf.plugin == false){
if (wf.fmt.numChannels == 2) {
let [a, b] = smp;
// Use average to mix stereo channels.
n = a.map((v, i) => (v + b[i]) / 2);
} else {
n = smp;
}
} else {
n = require(pth.resolve(process.cwd(),cf.pluginPath))(smp,wf)
}
smp = undefined;
n = new Uint8Array(n);
fs.writeFileSync(cf.output, Buffer.from(n)); // Write output.
} catch (err) {
// Definitely helpful error handling.
console.log("[wtb] an error occurred:");
throw err;
process.exit(1);
}
})();