-
Notifications
You must be signed in to change notification settings - Fork 0
/
vadpcmdecode.js
executable file
·100 lines (89 loc) · 2.6 KB
/
vadpcmdecode.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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
#!/usr/bin/env node
const fs = require('fs');
const util = require('util');
const exec = util.promisify(require('child_process').exec);
const AIFF = require('./aiff');
const arg = require('arg');
const args = arg({
// Types
'--verbose': Boolean,
'--bin': String, // where to find vadpcm_dec
'--help': Boolean,
// Aliases
'-v': '--verbose',
'-h': '--help',
});
if (args['--help']) {
console.log(`vadpcmdecode [--bin vadpcm_dec] files to decode`);
process.exit(0);
}
const vadpcmDecCmd = args['--cmd'] || 'vadpcm_dec';
const files = args._;
if (!files.length) throw new Error(`missing argument`);
async function asyncPool(poolLimit, array, iteratorFn) {
const ret = [];
const executing = [];
for (const item of array) {
const p = Promise.resolve().then(() => iteratorFn(item, array));
ret.push(p);
if (poolLimit <= array.length) {
const e = p.then(() => executing.splice(executing.indexOf(e), 1));
executing.push(e);
if (executing.length >= poolLimit) {
await Promise.race(executing);
}
}
}
return Promise.all(ret);
}
function awaitAll(_, items, iteratorFn) {
return Promise.all(items.map(iteratorFn));
}
async function run() {
await awaitAll(10, files, async (file) => {
const outName = file.replace(/\.aifc$/, '.aiff');
try {
const decoded = await exec(`${vadpcmDecCmd} "${file}"`, {
encoding: 'buffer',
});
const aiff = AIFF.serialize({
soundData: decoded.stdout,
numChannels: 1,
sampleSize: 16,
sampleRate: 22050,
});
await fs.promises.writeFile(outName, aiff);
if (args['--verbose']) {
console.log(file, 'converted');
}
} catch (err) {
if (args['--verbose']) {
console.error(file, err, err.stderr && err.stderr.toString('utf8'));
} else {
console.error(file, err.message.trim());
}
try {
const fileContents = await fs.promises.readFile(file);
const parsed = AIFF.parse(fileContents); // this will throw if it's not an aiff
if (parsed.form === 'AIFF') {
console.log(file, 'is an uncompressed aiff, just copying');
} else {
console.log(
file,
`not a suitable aiff file`,
parsed.form,
parsed.compressionName
);
throw new Error(`not a suitable aiff file`);
}
await fs.promises.writeFile(outName, fileContents);
} catch {
console.error(file, `doesn't seem to be a suitable aiff, skipping`);
}
}
});
}
run().catch((err) => {
console.error(err);
process.exit(1);
});