Skip to content

Instantly share code, notes, and snippets.

@giulioz
Created April 24, 2021 14:21
Show Gist options
  • Save giulioz/8c5464c2096d47b50ff548c5100ebd1b to your computer and use it in GitHub Desktop.
Save giulioz/8c5464c2096d47b50ff548c5100ebd1b to your computer and use it in GitHub Desktop.
const fs = require("fs").promises;
const FileWriter = require("wav").FileWriter;
const muLawDecompressTable = [
32124,
31100,
30076,
29052,
28028,
27004,
25980,
24956,
23932,
22908,
21884,
20860,
19836,
18812,
17788,
16764,
15996,
15484,
14972,
14460,
13948,
13436,
12924,
12412,
11900,
11388,
10876,
10364,
9852,
9340,
8828,
8316,
7932,
7676,
7420,
7164,
6908,
6652,
6396,
6140,
5884,
5628,
5372,
5116,
4860,
4604,
4348,
4092,
3900,
3772,
3644,
3516,
3388,
3260,
3132,
3004,
2876,
2748,
2620,
2492,
2364,
2236,
2108,
1980,
1884,
1820,
1756,
1692,
1628,
1564,
1500,
1436,
1372,
1308,
1244,
1180,
1116,
1052,
988,
924,
876,
844,
812,
780,
748,
716,
684,
652,
620,
588,
556,
524,
492,
460,
428,
396,
372,
356,
340,
324,
308,
292,
276,
260,
244,
228,
212,
196,
180,
164,
148,
132,
120,
112,
104,
96,
88,
80,
72,
64,
56,
48,
40,
32,
24,
16,
8,
1,
];
const paths = [
// `/Library/Arturia/Samples/Emulator II V/Factory/08 Cello & Violin/`,
// `/Library/Arturia/Samples/Emulator II V/Factory/09 Orchestra Tune/`,
// `/Library/Arturia/Samples/Emulator II V/Factory/10 Stacked Strings/`,
// `/Library/Arturia/Samples/Emulator II V/Factory/11 Acoustic Guitar/`,
`/Library/Arturia/Samples/Emulator II V/Factory/12 Voices/`,
// `/Library/Arturia/Samples/Emulator II V/Factory/22 Loon Garden/`,
];
async function readPaths(path) {
const paths = await fs.readdir(path);
return paths.map((f) => path + f);
}
async function main() {
const fileNames = (await Promise.all(paths.map((p) => fs.readdir(p)))).flat();
const files = (await Promise.all(paths.map(readPaths))).flat();
const fileBuffers = await Promise.all(files.map((file) => fs.readFile(file)));
const parsed = fileBuffers.map((buf, i) => ({
name: fileNames[i],
flags: buf.readInt32LE(0).toString(16),
length: buf.readInt32LE(4),
loopStart: buf.readInt32LE(8),
loopEnd: buf.readInt32LE(12),
midiNote: buf.readUInt32LE(16).toString(16),
waveData: buf.subarray(20),
}));
console.log(parsed);
parsed.forEach((curr) => {
const writer = new FileWriter(`output/${curr.name}.wav`, {
sampleRate: 27777,
channels: 1,
bitDepth: 16,
format: 1,
});
const buff = Buffer.alloc(curr.length * 2);
curr.waveData.forEach((d, i) => {
buff.writeInt16LE(
muLawDecompressTable[(255 - d) % 128] * Math.sign(d - 128),
i * 2
);
});
writer.write(buff);
});
}
main();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment