-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmain.cpp
289 lines (249 loc) · 10.6 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
/*
This file is part of adlib2vgm.
Copyright (C) 2021 ReimuNotMoe <[email protected]>
Copyright (C) 2023 Ivo van poorten <[email protected]>
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as
published by the Free Software Foundation, either version 3 of the
License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
#include "CommonIncludes.h"
#include "VgmOpl.h"
#include "Formats/Formats.h"
#include "cxxopts.h"
bool global_use_opl3;
bool global_verbose;
bool global_debug;
VgmOpl thisopl;
struct format {
std::string name;
std::function<std::unique_ptr<CPlayer>()> player;
};
std::vector<std::string> force_opl3 = {
"PIS", "SNG3", "SOP"
};
std::map<std::string, struct format> format_list = {
{ "CMF", { "Creative Music File Format by Creative Technology",
[]{return std::make_unique<CcmfPlayer>(&thisopl);} } },
{ "RAD", { "Reality ADlib Tracker by Reality",
[]{return std::make_unique<Crad2Player>(&thisopl);} } },
{ "D00", { "EdLib by Vibrants",
[]{return std::make_unique<Cd00Player>(&thisopl);} } },
{ "DRO", { "DOSBox Raw OPL Format",
[]{return std::make_unique<CdroPlayer>(&thisopl);} } },
{ "DRO2",{ "DOSBox Raw OPL Format",
[]{return std::make_unique<Cdro2Player>(&thisopl);} } },
{ "RIX", { "Softstar RIX OPL Music Format",
[]{return std::make_unique<CrixPlayer>(&thisopl);} } },
{ "MKF", { "Softstar RIX OPL Music Format in MKF container",
[]{return std::make_unique<CrixPlayer>(&thisopl);} } },
{ "LAA", { "Lucas Arts Adlib Music",
[]{return std::make_unique<CmidPlayer>(&thisopl);} } },
{ "SCI", { "Sierra AdLib Music",
[]{return std::make_unique<CmidPlayer>(&thisopl);} } },
{ "MID", { "MIDI Audio File Format",
[]{return std::make_unique<CmidPlayer>(&thisopl);} } },
{ "MUS", { "AdLib MIDI Music Format by Ad Lib Inc.",
[]{return std::make_unique<CmusPlayer>(&thisopl);} } },
{ "LDS", { "Loudness Sound System by Andras Molnar",
[]{return std::make_unique<CldsPlayer>(&thisopl);} } },
{ "ROL", { "AdLib Visual Composer by Ad Lib Inc. (needs standard.bnk)",
[]{return std::make_unique<CrolPlayer>(&thisopl);} } },
{ "A2M", { "AdLib Tracker 2 (v2)",
[]{return std::make_unique<Ca2mv2Player>(&thisopl);} } },
{ "AS3M", { "Scream Tracker 3 AdLib",
[]{return std::make_unique<Cs3mPlayer>(&thisopl);} } },
{ "S3M", { "Scream Tracker 3 AdLib",
[]{return std::make_unique<Cs3mPlayer>(&thisopl);} } },
{ "RAW", { "Raw AdLib Capture",
[]{return std::make_unique<CrawPlayer>(&thisopl);} } },
{ "U6M", { "Ultima 6 Music",
[]{return std::make_unique<Cu6mPlayer>(&thisopl);} } },
{ "SA2", { "Surprise AdLib Tracker",
[]{return std::make_unique<Csa2Loader>(&thisopl);} } },
{ "SAT", { "Surprise AdLib Tracker",
[]{return std::make_unique<Csa2Loader>(&thisopl);} } },
{ "AMD", { "AMUSIC AdLib Tracker",
[]{return std::make_unique<CamdLoader>(&thisopl);} } },
{ "IMF", { "Apogee (id/3D Realms) Music File",
[]{return std::make_unique<CimfPlayer>(&thisopl);} } },
{ "WLF", { "Wolfenstein 3D Music File",
[]{return std::make_unique<CimfPlayer>(&thisopl);} } },
{ "SNG", { "AdLib Tracker 1.0",
[]{return std::make_unique<CadtrackLoader>(&thisopl);} } },
{ "BAM", { "Bob's Adlib Music Player",
[]{return std::make_unique<CbamPlayer>(&thisopl);} } },
{ "BMF", { "Easy AdLib 1.0 (aka FMPlay) by The Brain",
[]{return std::make_unique<CxadbmfPlayer>(&thisopl);} } },
{ "CFF", { "Boom Tracker",
[]{return std::make_unique<CcffLoader>(&thisopl);} } },
{ "CMF2", { "SoundFX Macs Opera CMF Player",
[]{return std::make_unique<CcmfmacsoperaPlayer>(&thisopl);} } },
{ "DTM", { "DeFy AdLib Tracker",
[]{return std::make_unique<CdtmLoader>(&thisopl);} } },
{ "XAD", { "Lunatic Flash/Logic Design Player",
[]{return std::make_unique<CxadflashPlayer>(&thisopl);} } },
{ "XAD2", { "Hybrid MiG-29M Player by Domark",
[]{return std::make_unique<CxadhybridPlayer>(&thisopl);} } },
{ "XAD3", { "Hyp LKCC/SAC Player",
[]{return std::make_unique<CxadhypPlayer>(&thisopl);} } },
{ "XAD4", { "Psi/Future Crew PGRID Player",
[]{return std::make_unique<CxadpsiPlayer>(&thisopl);} } },
{ "XAD5", { "Ratt/GRIF Player",
[]{return std::make_unique<CxadratPlayer>(&thisopl);} } },
{ "SNG2", { "Faust Music Tracker",
[]{return std::make_unique<CfmcLoader>(&thisopl);} } },
{ "GOT", { "God Of Thunder Player",
[]{return std::make_unique<CgotPlayer>(&thisopl);} } },
{ "AGD", { "HERAD Herbulot AdLib Player",
[]{return std::make_unique<CheradPlayer>(&thisopl);} } },
{ "SDB", { "HERAD Herbulot AdLib Player",
[]{return std::make_unique<CheradPlayer>(&thisopl);} } },
{ "SQX", { "HERAD Herbulot AdLib Player",
[]{return std::make_unique<CheradPlayer>(&thisopl);} } },
{ "HSC", { "HSC Adlib Composer / HSC-Tracker",
[]{return std::make_unique<ChscPlayer>(&thisopl);} } },
{ "JBM", { "Johannes Bjerregaard's JBM Adlib Music Format",
[]{return std::make_unique<CjbmPlayer>(&thisopl);} } },
{ "KSM", { "Ken Silverman Music (requires insts.dat)",
[]{return std::make_unique<CksmPlayer>(&thisopl);} } },
{ "MAD", { "Mlat Adlib Tracker",
[]{return std::make_unique<CmadLoader>(&thisopl);} } },
{ "MDI", { "AdLib Visual Composer MIDIPlay File",
[]{return std::make_unique<CmdiPlayer>(&thisopl);} } },
{ "PIS", { "Beni Tracker PIS module",
[]{return std::make_unique<CpisPlayer>(&thisopl);} } },
{ "SNG3", { "SNGPlay File Format",
[]{return std::make_unique<CsngPlayer>(&thisopl);} } },
{ "SOP", { "Note Sequencer v1.0 by sopepos",
[]{return std::make_unique<CsopPlayer>(&thisopl);} } },
};
int main(int argc, char **argv) {
cxxopts::Options options("adlib2vgm", "adlib2vgm - Converts adlib file formats to VGM format.");
options.add_options()
("h,help", "Show this help")
("l,list-formats", "List supported formats / file extensions")
("v,verbose", "Verbose output during conversion")
("d,debug", "Show input format debug information")
("m,mode", "Generation mode (opl2/opl3), not supported by all formats", cxxopts::value<std::string>()->default_value("opl2"))
("f,format", "Override auto format detection", cxxopts::value<std::string>()->default_value(""))
("i,in", "Input file", cxxopts::value<std::string>())
("o,out", "Output file", cxxopts::value<std::string>())
("s,subsong", "Select subsong", cxxopts::value<unsigned int>()->default_value("1"))
("L,loop", "Enable looping")
("z,gzipped", "Export gzipped .vgz file")
;
std::string mode, input_file, output_file, format;
unsigned int subsong;
bool loop = false, gzipped = false;
try {
auto cmd = options.parse(argc, argv);
if (cmd.count("help")) {
std::cout << options.help();
return 0;
}
if (cmd.unmatched().size()) {
std::cout << "error: unmatched command line arguments: ";
for (auto u : cmd.unmatched())
std::cout << u << " ";
std::cout << "\n";
return 2;
}
if (cmd.count("verbose")) global_verbose = true;
if (cmd.count("debug")) global_debug = true;
if (cmd.count("loop")) loop = true;
if (cmd.count("gzipped")) gzipped = true;
if (cmd.count("list-formats")) {
puts("Supported formats / file extensions:");
for (auto &it : format_list) {
std::cout << it.first.c_str() << ": "
<< it.second.name << "\n";
}
return 0;
}
if (!cmd.count("in") || !cmd.count("out")) {
puts("error: please supply both input file and output file.");
std::cout << options.help();
return 1;
}
mode = cmd["mode"].as<std::string>();
format = cmd["format"].as<std::string>();
input_file = cmd["in"].as<std::string>();
output_file = cmd["out"].as<std::string>();
subsong = cmd["subsong"].as<unsigned int>();
} catch (std::exception &e) {
std::cout << "Error: " << e.what() << "\n";
std::cout << options.help();
return 1;
}
if (format.empty()) {
auto ext = std::filesystem::path(input_file).extension().string();
if (ext.size() > 1) {
ext.erase(0, 1);
format = ext;
}
}
std::transform(format.begin(), format.end(), format.begin(), ::toupper);
auto force = std::find(force_opl3.begin(), force_opl3.end(), format);
if (force != force_opl3.end()) {
printf("forcing OPL3 for format %s\n", format.c_str());
mode = "opl3";
}
global_use_opl3 = mode == "opl3";
printf("mode: %s\n", mode.c_str());
thisopl = { };
std::unique_ptr<CPlayer> player;
auto it = format_list.find(format);
if (it != format_list.end()) {
printf("using loader %s: %s\n", format.c_str(), it->second.name.c_str());
player = it->second.player();
if (!player->load(input_file, CProvider_Filesystem())) {
puts("error: failed to load file");
return EXIT_FAILURE;
}
} else {
bool success = false;
printf("file format '%s' not found\n", format.c_str());
for (auto detect : format_list) {
printf("trying %s: %s\n", detect.first.c_str(), detect.second.name.c_str());
player = detect.second.player();
if (player->load(input_file, CProvider_Filesystem())) {
puts("success!");
success = true;
break;
}
}
if (!success) {
puts("error: unable to load file");
return EXIT_FAILURE;
}
}
unsigned int subsongs = player->getsubsongs();
if (subsong == 0 || subsong > subsongs) {
printf("error: subsong out of range [1-%d]\n", subsongs);
return EXIT_FAILURE;
}
player->rewind(subsong - 1);
double sleep_samples = 0.0;
do {
if (global_verbose) puts("tick");
double hertz = player->getrefresh();
sleep_samples += 44100 / hertz;
thisopl.insert_sleep(round(sleep_samples));
sleep_samples -= round(sleep_samples);
} while (player->update());
thisopl.set_author(player->getauthor());
thisopl.set_title(player->gettitle());
thisopl.set_desc(player->getdesc());
thisopl.set_loop(loop);
int retval = thisopl.save(output_file, gzipped);
puts("Done.");
return retval;
}
// vi: ts=8 sw=8 noet