-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.cpp
463 lines (414 loc) · 14.8 KB
/
main.cpp
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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
#include <algorithm>
#include <filesystem>
#include <fstream>
#include <iostream>
#include <map>
#include <set>
#include <string>
#include <vector>
#include "pxtnService.h"
#include "sndfile.h"
#pragma pack(1)
// #define SAMPLE_RATE 48000
#define SAMPLE_RATE 44100
#define CHANNEL_COUNT 2
#ifdef _WIN32
#define PLATFORM_SF_OPEN(a, b, c) sf_wchar_open(a, b, c)
#else
#define PLATFORM_SF_OPEN(a, b, c) sf_open(a, b, c)
#endif
// clang-format off
constexpr char usage[] =
"Usage: pxtone-renderer [options] file(s)...\n"
"By default, the provided files will be rendered as .wav to your working directory.\n"
"Options:\n"
" --format, -f [OGG, WAV, FLAC] Encode data to this format.\n"
// " --vbr, -v [0.0 - 1.0] FLAC/OGG only; Set VBR quality.\n"
// " --compression, -c [0.0 - 1.0] FLAC/OGG only; Set compression level.\n"
" --fadein [seconds] Specify song fade in time.\n"
" --loop, -l Loop the song this many times.\n"
" --loop-separately Separate the song into 'intro' and 'loop' files.\n"
"\n"
" --output, -o If 1 file is being rendered, place the resulting file here.\n"
" If multiple are being rendered, put them in this directory.\n"
"\n"
" --help, -h Show this dialog.\n"
" --quiet, -q Omit info & warning messages from console output.\n"
;
// clang-format on
struct GetError {
static std::string pxtone(std::string err) { return "pxtone: " + err; }
static std::string pxtone(pxtnERR err) {
return pxtone(std::string(pxtnError_get_string(err)));
}
static std::string generic(std::string err) { return "generic: " + err; }
static std::string file(std::string err) { return "file: " + err; }
static std::string encoder(std::string err) { return "encoder: " + err; }
static std::string encoder(SNDFILE *err) {
return encoder(std::string(sf_strerror(err)));
}
};
struct Config {
enum Format {
OGG = SF_FORMAT_OGG | SF_FORMAT_VORBIS,
WAV = SF_FORMAT_WAV | SF_FORMAT_PCM_16,
FLAC = SF_FORMAT_FLAC | SF_FORMAT_PCM_16
};
Format format = WAV;
int loopCount = 1;
bool loopSeparately = false, quiet = true, singleFile = true,
outputToDirectory = false;
double fadeInTime = 0 /*, vbrRate = 0, compressionRate = 0*/;
std::string formatSuffix = "wav", fileName;
std::filesystem::path outputDirectory;
} static config;
enum LogState : unsigned char { Error, Warning, Info };
bool help() {
std::cout << usage << std::endl;
return false;
}
bool logToConsole(std::string text, LogState warning = Error) {
std::string str;
if (warning == Error)
str += "Error: ";
else if (warning == Warning)
str += "Warning: ";
else
str += "Info: ";
str += text;
if (warning != Error && config.quiet)
return false;
else {
std::cout << str << std::endl;
if (warning == Error) help();
}
return false; // returns so that errors can be one-liners (return
// logToConsole(...))
}
typedef std::pair<std::string, std::string> Arg;
struct KnownArg {
bool empty() {
for (auto it : keyMatches)
if (!it.empty()) return false;
return true;
}
std::set<std::string> keyMatches;
bool takesParameter = false;
};
static const KnownArg
argFormat = {{"--format", "-f"}, true},
// argVbr{{"--vbr", "-v"}, true},
// argCompression{{"--compression", "-c"}, true},
//
argOutput = {{"--output", "-o"}, true}, argHelp = {{"--help", "-h"}},
argQuiet{{"--quiet", "-q"}}, argFadeIn{{"--fadein"}, true},
argLoop{{"--loop", "-l"}, true}, argLoopSeparately{{"--loop-separately"}};
static const std::vector<KnownArg> knownArguments = {
argFormat, /*argVbr, argCompression, */ argOutput,
argHelp, argQuiet,
argFadeIn, argLoop,
argLoopSeparately};
KnownArg findArgument(const std::string &key) {
KnownArg match;
for (auto it : knownArguments) {
auto arg = it.keyMatches.find(key);
if (arg != it.keyMatches.end()) match = it;
}
return match;
}
static std::set<std::filesystem::path> files;
bool parseArguments(const std::vector<std::string> &args) {
std::map<std::string, std::string> argData;
bool waitingForSecond = true;
for (auto it = args.begin(); it != args.end(); ++it) {
Arg arg;
while (waitingForSecond) {
if (arg.first.empty()) {
if (*it->begin() == '-') {
auto match = findArgument(*it);
if (!match.empty()) {
arg.first = *it;
waitingForSecond = match.takesParameter;
} else
return logToConsole("Unknown argument '" + *it + "'");
} else {
files.insert(*it);
waitingForSecond = false;
}
} else {
bool argSupplied = true;
if (++it != args.end()) {
if (*it->begin() == '-')
argSupplied = false;
else {
arg.second = *it;
waitingForSecond = false;
}
} else
argSupplied = false;
if (!argSupplied)
return logToConsole("Argument '" + arg.first +
"' requires a parameter.");
}
}
waitingForSecond = true;
argData.insert(arg);
}
for (auto it : argHelp.keyMatches) {
auto helpFound = argData.find(it);
if (helpFound != argData.end()) return help();
}
switch (files.size()) {
case 0:
return logToConsole("At least 1 .ptcop file is required.");
case 1:
config.singleFile = true;
break;
default:
config.singleFile = false;
}
for (auto it : argFadeIn.keyMatches) {
auto fadeInFound = argData.find(it);
if (fadeInFound != argData.end())
config.fadeInTime = std::stod(fadeInFound->second);
}
for (auto it : argQuiet.keyMatches) {
auto quietFound = argData.find(it);
if (quietFound == argData.end()) config.quiet = false;
}
// for (auto it : argVbr.keyMatches) {
// auto vbrFound = argData.find(it);
// if (vbrFound != argData.end()) config.vbrRate =
// std::stod(vbrFound->second);
// }
// for (auto it : argCompression.keyMatches) {
// auto compressionFound = argData.find(it);
// if (compressionFound != argData.end())
// config.compressionRate = std::stod(compressionFound->second);
// }
for (auto it : argLoopSeparately.keyMatches) {
auto loopSeparatelyFound = argData.find(it);
if (loopSeparatelyFound != argData.end()) config.loopSeparately = true;
}
for (auto it : argLoop.keyMatches) {
auto loopFound = argData.find(it);
if (loopFound != argData.end()) {
config.loopCount = std::abs(std::stoi(loopFound->second));
}
}
std::filesystem::path path =
std::filesystem::absolute(std::filesystem::current_path());
for (auto it : argOutput.keyMatches) {
auto outputFound = argData.find(it);
if (outputFound != argData.end())
path = std::filesystem::absolute(outputFound->second);
}
config.outputToDirectory = std::filesystem::is_directory(path);
if (config.singleFile && !config.outputToDirectory)
config.fileName = path.filename().string();
config.outputDirectory = std::filesystem::absolute(path);
for (auto it : argFormat.keyMatches) {
auto formatFound = argData.find(it);
if (formatFound == argData.end() || formatFound->second.empty())
continue;
else {
auto str = formatFound->second;
std::transform(str.begin(), str.end(), str.begin(),
[](unsigned char c) { return std::tolower(c); });
auto setFormat = [](Config::Format format, const char *suffix) {
config.format = format;
config.formatSuffix = suffix;
};
str == "ogg" ? setFormat(Config::OGG, "ogg")
: str == "wav" ? setFormat(Config::WAV, "wav")
: str == "flac"
? setFormat(Config::FLAC, "flac")
: void(logToConsole("Unknown format type '" + formatFound->second +
"'; Resorting to .wav",
LogState::Warning));
}
}
return true;
}
static bool ioRead(void *source, void *destination, int size, int num) {
auto f = static_cast<FILE *>(source);
int i = fread(destination, size, num, f);
if (i < num) return false;
if (_is_big_endian()) {
pxtnData::_correct_endian(static_cast<unsigned char *>(destination), size,
num);
}
return true;
}
static bool ioWrite(void *source, const void *destination, int size, int num) {
auto f = static_cast<FILE *>(source);
const void *realData;
if (_is_big_endian()) {
void *wData = malloc(num * size);
memcpy(wData, destination, num * size);
pxtnData::_correct_endian(static_cast<unsigned char *>(wData), size, num);
realData = wData;
} else {
realData = destination;
}
int i = fwrite(realData, size, num, f);
if (i < num) return false;
return true;
}
static bool ioSeek(void *source, int mode, int size) {
auto f = static_cast<FILE *>(source);
if (fseek(f, size, mode)) return false;
return true;
}
static bool ioTell(void *source, int32_t *p_pos) {
auto f = static_cast<FILE *>(source);
int i = ftell(f);
if (i < 0) return false;
*p_pos = i;
return true;
}
void convert(std::filesystem::path file) {
FILE *fp = fopen(file.string().c_str(), "rb");
if (fp == nullptr)
throw GetError::file("Error opening file " + file.string() +
". The file may not be readable to your user.");
// thanks to pxtone update 220910a, we can get rid of pxtnDescriptor entirely
// & just implement I/O callbacks
pxtnService *pxtn = new pxtnService(ioRead, ioWrite, ioSeek, ioTell);
auto err = pxtn->init();
if (err != pxtnOK) throw GetError::pxtone(err);
if (!pxtn->set_destination_quality(CHANNEL_COUNT, SAMPLE_RATE))
throw GetError::pxtone(
"Could not set destination quality: " + std::to_string(CHANNEL_COUNT) +
" channels, " + std::to_string(SAMPLE_RATE) + "Hz.");
err = pxtn->read(fp);
if (err != pxtnOK) throw GetError::pxtone(err);
err = pxtn->tones_ready();
if (err != pxtnOK) throw GetError::pxtone(err);
SF_INFO info;
info.samplerate = SAMPLE_RATE;
info.channels = CHANNEL_COUNT;
info.format = config.format;
if (!sf_format_check(&info))
throw GetError::encoder("Invalid encoder format.");
std::filesystem::path introPath = config.outputDirectory;
if (config.outputToDirectory) {
if (!std::filesystem::exists(introPath))
if (!std::filesystem::create_directory(introPath))
throw GetError::file("Unable to create destination path.");
if (config.singleFile && !config.fileName.empty())
introPath += "/" + config.fileName;
else
introPath +=
"/" + file.filename().replace_extension(config.formatSuffix).string();
}
auto finalize = [](SNDFILE *pcmFile) {
sf_write_sync(pcmFile);
sf_close(pcmFile);
};
auto render = [&pxtn](int measureCount, int startMeas, SNDFILE *pcmFile,
bool loop) {
int sampleCount =
SAMPLE_RATE * (measureCount * pxtn->master->get_beat_num() /
pxtn->master->get_beat_tempo() * 60);
int renderSize = sampleCount * CHANNEL_COUNT * 16 / 8;
pxtnVOMITPREPARATION prep = {};
prep.flags |= pxtnVOMITPREPFLAG_loop; // TODO: figure this out
prep.start_pos_meas = startMeas;
prep.master_volume = 0.8f; // this is probably good
prep.fadein_sec = loop ? 0 : static_cast<float>(config.fadeInTime);
if (!pxtn->moo_preparation(&prep))
throw GetError::pxtone("I Have No Mouth, and I Must Moo");
if (pcmFile == nullptr) throw GetError::encoder(pcmFile);
// sf_command(pcmFile, SFC_SET_COMPRESSION_LEVEL,
// &config.compressionRate,
// sizeof(double));
// sf_command(pcmFile, SFC_SET_VBR_ENCODING_QUALITY, &config.vbrRate,
// sizeof(double));
sf_command(pcmFile, SFC_UPDATE_HEADER_NOW, nullptr, 0);
int16_t *buf = static_cast<int16_t *>(malloc(static_cast<size_t>(renderSize)));
// char *buf =
// static_cast<char *>(malloc(static_cast<size_t>(renderSize + 1)));
// buf[renderSize + 1] = '\0';
int written = 0;
auto mooSection = [&](int len) {
while (written < len) {
int mooedLength = 0;
if (!pxtn->Moo(buf, len - written, &mooedLength))
throw "Moo error during rendering. Bytes written so far: " +
std::to_string(written);
written += mooedLength;
}
};
int loopCount = loop ? config.loopCount : 1;
for (int i = loopCount; i > 0; i--) {
mooSection(renderSize);
sf_write_short(pcmFile, buf, renderSize / 2);
}
};
int lastMeasure = pxtn->master->get_meas_num();
if (pxtn->master->get_last_meas())
lastMeasure = pxtn->master->get_last_meas();
SNDFILE *introFile;
SNDFILE *loopFile;
if (config.loopSeparately) {
std::filesystem::path loopPath = introPath;
introPath.replace_filename(introPath.filename().stem().string() + "_intro" +
introPath.extension().string());
loopPath.replace_filename(loopPath.filename().stem().string() + "_loop" +
loopPath.extension().string());
introFile = PLATFORM_SF_OPEN(introPath.c_str(), SFM_WRITE, &info);
loopFile = PLATFORM_SF_OPEN(loopPath.c_str(), SFM_WRITE, &info);
} else {
introFile = PLATFORM_SF_OPEN(introPath.c_str(), SFM_WRITE, &info);
loopFile = introFile;
}
render(pxtn->master->get_repeat_meas(), 0, introFile, false);
render((lastMeasure - pxtn->master->get_repeat_meas()),
pxtn->master->get_last_meas(), loopFile, true);
finalize(introFile);
finalize(loopFile);
pxtn->evels->Release();
}
int main(int argc, char *argv[]) {
if (argc <= 1) return logToConsole("Expected at least 1 parameter.");
std::vector<std::string> args;
std::string str;
for (int i = 1; i < argc; i++) {
std::string arg = argv[i];
auto parseArg = [&]() -> bool {
size_t x = arg.find('=');
if (x != arg.npos) {
args.push_back(arg.substr(0, x));
arg.erase(0, x + 1);
args.push_back(arg);
return arg.find('=') != arg.npos;
} else {
args.push_back(arg);
return false;
}
};
if (parseArg()) parseArg();
}
if (!parseArguments(args)) return 0;
for (auto it : files) {
auto absolute = std::filesystem::absolute(it);
if (std::filesystem::exists(it)) try {
convert(std::filesystem::absolute(it));
} catch (std::string err) {
return logToConsole(err);
}
else
logToConsole("File " + it.string() + " not found.", LogState::Warning);
}
return 0;
}
/* TODO:
* - Figure out why different WAVs are produced on different
*platforms/compilers
* - Figure out why sometimes loop points are messed up
* - Compression level
* - Metadata
* - Remove argument parsing boilerplate
* - Implement & test other libsndfile supported formats such as MP3 and Opus
**/