-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathdecode.js
163 lines (143 loc) · 4.5 KB
/
decode.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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
/**
* @title Node PCK Audio Extractor
* @description Extract .pck files
* @author Drew Hutton <Yoroshi>
* When modifying or redistributing this project, do not modify this notice.
*/
const os = require('os');
const fs = require('fs');
const util = require('util');
const rimraf = util.promisify(require('rimraf'));
const path = require('path');
const mkdirp = require('mkdirp');
const args = require('minimist')(process.argv.slice(2));
const { StaticPool } = require('node-worker-threads-pool');
const { pck2wem } = require('./helpers/pck2wem');
const { wem2wav } = require('./helpers/wem2wav');
const { wav2flac } = require('./helpers/wav2flac');
const { wav2mp3 } = require('./helpers/wav2mp3');
const cpuCount = os.cpus().length;
const main = async () => {
try {
const pckFiles = fs
.readdirSync(path.join('.', 'input'))
.filter((f) => f.toLowerCase().endsWith('.pck'));
console.info(`Found ${pckFiles.length} pck files`);
const { e, extraEncode, bmsScript, b } = args;
const encodeFlac =
(extraEncode && extraEncode.includes('flac')) ||
(e && e.includes('flac'));
const encodeMp3 =
(extraEncode && extraEncode.includes('mp3')) || (e && e.includes('mp3'));
const pck2wemPool = new StaticPool({ size: cpuCount, task: pck2wem });
const wem2wavPool = new StaticPool({ size: cpuCount, task: wem2wav });
const wav2flacPool = new StaticPool({ size: cpuCount, task: wav2flac });
const wav2mp3Pool = new StaticPool({ size: cpuCount, task: wav2mp3 });
await Promise.all(
pckFiles.map(async (pckFile) => {
const processingDir = path.join(
__dirname,
'processing',
pckFile.split('.')[0],
);
await mkdirp(processingDir);
await pck2wemPool.exec({ pckFile, processingDir });
const subWavOutputDir = path.join(
__dirname,
'output',
'WAV',
pckFile.split('.')[0],
);
const subFlacOutputDir = path.join(
__dirname,
'output',
'FLAC',
pckFile.split('.')[0],
);
const subMp3OutputDir = path.join(
__dirname,
'output',
'MP3',
pckFile.split('.')[0],
);
await mkdirp(subWavOutputDir);
if (encodeFlac) {
await mkdirp(subFlacOutputDir);
}
if (encodeMp3) {
await mkdirp(subMp3OutputDir);
}
const createdFiles = fs.readdirSync(processingDir);
await Promise.all(
createdFiles.map(async (createdFile) => {
await wem2wavPool.exec({
outputDir: subWavOutputDir,
createdFile,
processingDir,
});
}),
);
// TODO: This logic can be better
const extraEncodeArg =
encodeFlac && encodeMp3
? 'flacandmp3'
: encodeFlac
? 'flac'
: encodeMp3
? 'mp3'
: null;
switch (extraEncodeArg) {
case 'flac':
await Promise.all(
createdFiles.map(async (createdFile) => {
await wav2flacPool.exec({
inputDir: subWavOutputDir,
outputDir: subFlacOutputDir,
createdFile,
});
}),
);
break;
case 'mp3':
await Promise.all(
createdFiles.map(async (createdFile) => {
await wav2mp3Pool.exec({
inputDir: subWavOutputDir,
outputDir: subMp3OutputDir,
createdFile,
});
}),
);
break;
case 'flacandmp3':
await Promise.all([
...createdFiles.map(async (createdFile) => {
await wav2flacPool.exec({
inputDir: subWavOutputDir,
outputDir: subFlacOutputDir,
createdFile,
});
}),
...createdFiles.map(async (createdFile) => {
await wav2mp3Pool.exec({
inputDir: subWavOutputDir,
outputDir: subMp3OutputDir,
createdFile,
});
}),
]);
break;
default:
break;
}
}),
);
} catch (err) {
console.error(err.message);
console.error(err.stack);
} finally {
await rimraf(path.join(__dirname, 'processing'));
process.exit();
}
};
main();