-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathO2BConverter.cpp
More file actions
351 lines (338 loc) · 14 KB
/
O2BConverter.cpp
File metadata and controls
351 lines (338 loc) · 14 KB
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
#include "O2BConverter.hpp"
#include <algorithm>
#include <cmath>
#include <limits>
#include <memory>
#include <vector>
#include "_Detail/Utilities.hpp"
namespace Osu2Bms {
O2BConverter::O2BConverter(const O2BConvertionOptions &options)
: _Options(options) {}
Bms::BmsBeatmap O2BConverter::operator()(const Osu::OsuBeatmap &osuBeatmap) throw(O2BException) {
using namespace std;
cout << "Generating BPMs..." << endl;
auto bpms = _GenerateBpms(osuBeatmap);
cout << "Generating WAVs..." << endl;
auto wavs = _GenerateWavs(osuBeatmap);
cout << "Generating BMPs..." << endl;
auto bmps = _GenerateBmps(osuBeatmap);
cout << "Generating notes..." << endl;
auto notes = _GenerateNotes(osuBeatmap, bpms, wavs, bmps);
cout << "Converting time to position..." << endl;
_ConvertTimeToPosition(osuBeatmap, notes, bpms);
cout << "Generating BMS beatmap..." << endl;
return _GenerateBmsBeatmap(osuBeatmap, notes, bpms, wavs, bmps);
}
std::vector<double> O2BConverter::_GenerateBpms(
const Osu::OsuBeatmap &osuBeatmap) {
using namespace std;
vector<double> bpms;
if (_Options.WithTimingPoints) {
if (osuBeatmap.TimingPoints.size() == 0 || osuBeatmap.TimingPoints.front().Inherited) {
throw O2BException(
std::string("in ") + OSU_2_BMS_FUNCTION_SIGNATURE
+ ": First TimingPoint should be non-inherited");
}
double last = nan(nullptr);
for (const auto &tp : osuBeatmap.TimingPoints) {
if (!tp.Inherited || _Options.WithInheritedTimingPoints) {
double bpm = last;
if (tp.Inherited) {
bpm /= tp.Ratio();
} else {
last = bpm = tp.BeatsPerMinute();
}
auto it = lower_bound(bpms.begin(), bpms.end(), bpm);
if (it == bpms.end() || *it != bpm) {
bpms.insert(it, bpm);
}
}
}
}
return bpms;
}
std::vector<std::string> O2BConverter::_GenerateWavs(
const Osu::OsuBeatmap &osuBeatmap) {
using namespace std;
vector<string> wavs;
auto addWav = [&wavs](const std::string &wav) {
auto it = lower_bound(wavs.begin(), wavs.end(), wav);
if (it == wavs.end() || *it != wav) {
wavs.insert(it, wav);
}
};
addWav(osuBeatmap.AudioFilename);
if (_Options.WithKeySounds) {
for (auto &object : osuBeatmap.HitObjects) {
auto wav = object->StartPoint.CustomHitSound;
if (wav) {
addWav(*wav);
}
}
}
if (_Options.WithEventSounds) {
for (auto &event : osuBeatmap.Events) {
if (std::dynamic_pointer_cast<Osu::OsuSoundEffectEvent>(event)) {
addWav(event->FilePath);
}
}
}
return wavs;
}
std::vector<std::string> O2BConverter::_GenerateBmps(
const Osu::OsuBeatmap &osuBeatmap) {
using namespace std;
vector<string> bmps;
if (_Options.WithBga) {
for (auto &event : osuBeatmap.Events) {
if (std::dynamic_pointer_cast<Osu::OsuVideoEvent>(event)) {
const auto &bmp = event->FilePath;
auto it = lower_bound(bmps.begin(), bmps.end(), bmp);
if (it == bmps.end() || *it != bmp) {
bmps.insert(it, bmp);
}
}
}
}
return bmps;
}
std::vector<O2BConverter::_Note> O2BConverter::_GenerateNotes(
const Osu::OsuBeatmap &osuBeatmap,
const std::vector<double> &bpms,
const std::vector<std::string> &wavs,
const std::vector<std::string> &bmps) {
using namespace std;
using namespace Bms;
using namespace Osu;
vector<_Note> notes;
double last = nan(nullptr);
if (_Options.WithTimingPoints) {
for (size_t i = 0; i < osuBeatmap.TimingPoints.size(); ++i) {
const auto &tp = osuBeatmap.TimingPoints[i];
if (!tp.Inherited || _Options.WithInheritedTimingPoints) {
double bpm = last;
if (tp.Inherited) {
bpm /= tp.Ratio();
} else {
last = bpm = tp.BeatsPerMinute();
}
_Note note;
note.Channel = BmsChannelId::Bpm2;
note.ReferenceId =
lower_bound(bpms.begin(), bpms.end(), bpm)
- bpms.begin() + 1;
note.Time = tp.Time;
note.AssociatedObjectType = _Note::TimingPoint;
note.AssociatedObjectIndex = i;
notes.push_back(note);
}
}
}
_Note bgmNote;
bgmNote.Time = osuBeatmap.AudioLeadIn;
bgmNote.Channel = BmsChannelId::Bgm;
bgmNote.ReferenceId = lower_bound(wavs.begin(), wavs.end(), osuBeatmap.AudioFilename) - wavs.begin() + 1;
notes.push_back(bgmNote);
for (size_t i = 0; i < osuBeatmap.Events.size(); ++i) {
const auto &event = osuBeatmap.Events[i];
string fileName = event->FilePath;
if (auto sound = dynamic_pointer_cast<OsuSoundEffectEvent>(event)) {
if (_Options.WithEventSounds) {
_Note note;
note.Time = sound->Time;
note.Channel = BmsChannelId::Bgm;
note.ReferenceId = lower_bound(wavs.begin(), wavs.end(), fileName) - wavs.begin() + 1;
note.AssociatedObjectType = _Note::Event;
note.AssociatedObjectIndex = i;
notes.push_back(note);
}
} else if (auto video = dynamic_pointer_cast<OsuVideoEvent>(event)) {
if (_Options.WithBga) {
_Note note;
note.Time = video->Time;
note.Channel = BmsChannelId::Bga;
note.ReferenceId = lower_bound(bmps.begin(), bmps.end(), fileName) - bmps.begin() + 1;
note.AssociatedObjectType = _Note::Event;
note.AssociatedObjectIndex = i;
notes.push_back(note);
}
}
}
auto keyCount = osuBeatmap.ManiaKeyCount();
if (_Options.KeyMap.size() != keyCount) {
throw O2BException(
std::string("in ") + OSU_2_BMS_FUNCTION_SIGNATURE
+ ": Key map size mismatched");
}
for (size_t i = 0; i < osuBeatmap.HitObjects.size(); ++i) {
const auto &o = osuBeatmap.HitObjects[i];
auto column = o->StartPoint.ManiaColumn(keyCount);
BmsReferenceId ref("ZZ");
if (_Options.WithKeySounds) {
if (const auto &wav = o->StartPoint.CustomHitSound) {
ref = lower_bound(wavs.begin(), wavs.end(), *wav) - wavs.begin() + 1;
}
}
BmsChannelId channel = _Options.KeyMap[column];
if (auto ln = dynamic_pointer_cast<OsuHold>(o)) {
_Note endNote;
channel = static_cast<BmsChannelId>(
static_cast<underlying_type_t<BmsChannelId>>(
_Options.KeyMap[column]) + 40);
endNote.Channel = channel;
endNote.ReferenceId = ref;
endNote.Time = ln->EndTime;
endNote.AssociatedObjectType = _Note::HitObject;
endNote.AssociatedObjectIndex = i;
notes.push_back(endNote);
}
_Note note;
note.Channel = channel;
note.ReferenceId = ref;
note.Time = o->StartTime;
note.AssociatedObjectType = _Note::HitObject;
note.AssociatedObjectIndex = i;
notes.push_back(note);
}
sort(notes.begin(), notes.end(), [](const _Note &lhs, const _Note &rhs) {
return lhs.Time < rhs.Time;
});
return notes;
}
void O2BConverter::_ConvertTimeToPosition(
const Osu::OsuBeatmap &osuBeatmap,
std::vector<_Note> ¬es,
const std::vector<double> &bpms) {
using namespace std;
using namespace Bms;
if (notes.size() == 0) {
return;
}
double bpm;
if (_Options.WithTimingPoints) {
const auto &firstTp = osuBeatmap.TimingPoints.front();
bpm = firstTp.BeatsPerMinute() / firstTp.Meter;
} else {
bpm = _Options.CustomBpm / _Options.CustomMeter;
}
double position = _Options.Offset / _Options.GridSize;
auto time = notes.front().Time;
for (auto ¬e : notes) {
auto newPosition = position + static_cast<double>(note.Time - time) / 60000.0 * bpm;
if (note.Channel == BmsChannelId::Bpm2) {
const auto &tp = osuBeatmap.TimingPoints[note.AssociatedObjectIndex];
bpm = bpms[note.ReferenceId.UnderlyingValue() - 1] / tp.Meter;
position = newPosition;
time = note.Time;
}
note.Position = newPosition;
}
}
Bms::BmsBeatmap O2BConverter::_GenerateBmsBeatmap(
const Osu::OsuBeatmap &osuBeatmap,
const std::vector<_Note> ¬es,
const std::vector<double> &bpms,
const std::vector<std::string> &wavs,
const std::vector<std::string> &bmps) {
using namespace std;
using namespace Bms;
BmsBeatmap bmsBeatmap;
uint16_t section = 0;
map<BmsChannelId, map<uint8_t, BmsReferenceId>> data;
multimap<uint8_t, BmsReferenceId> bgmNotes;
for (const auto ¬e : notes) {
uint16_t noteSection = (uint16_t)floor(note.Position);
if (noteSection != section) {
_PushBackSectionData(bmsBeatmap, section, data, bgmNotes);
data.clear();
bgmNotes.clear();
section = noteSection;
}
uint8_t index = static_cast<uint8_t>((note.Position - noteSection) * _Options.GridSize);
if (note.Channel == BmsChannelId::Bgm) {
bgmNotes.insert({index, note.ReferenceId});
} else {
data[note.Channel][index] = note.ReferenceId;
}
}
_PushBackSectionData(bmsBeatmap, section, data, bgmNotes);
bmsBeatmap.Artist = osuBeatmap.ArtistUnicode;
bmsBeatmap.Bpm = _Options.WithTimingPoints ? osuBeatmap.TimingPoints.front().BeatsPerMinute() : _Options.CustomBpm;
bmsBeatmap.Title = osuBeatmap.TitleUnicode;
bmsBeatmap.LongNoteType = BmsLongNoteType::NotePair;
for (const auto &event : osuBeatmap.Events) {
if (auto backgroundEvent = dynamic_pointer_cast<Osu::OsuBackgroundEvent>(event)) {
bmsBeatmap.Cover = backgroundEvent->FilePath;
break;
}
}
for (size_t i = 0; i < bpms.size(); ++i) {
bmsBeatmap.BpmMap[i + 1] = bpms[i];
}
for (size_t i = 0; i < wavs.size(); ++i) {
bmsBeatmap.WavMap[i + 1] = wavs[i];
}
if (_Options.WithBga) {
for (size_t i = 0; i < bmps.size(); ++i) {
bmsBeatmap.BmpMap[i + 1] = bmps[i];
}
}
return bmsBeatmap;
}
void O2BConverter::_PushBackSectionData(
Bms::BmsBeatmap &bmsBeatmap,
const uint16_t §ion,
const std::map<Bms::BmsChannelId, std::map<uint8_t, Bms::BmsReferenceId>> &data,
const std::multimap<uint8_t, Bms::BmsReferenceId> &bgmNotes) {
using namespace std;
using namespace Bms;
auto it = bgmNotes.cbegin();
vector<shared_ptr<BmsReferenceListDataUnit>> units;
const BmsDataUnitId bgmUnitId(section, BmsChannelId::Bgm);
for (uint8_t i = 0; i < _Options.GridSize; ++i) {
if (it != bgmNotes.cend() && it->first == i) {
if (units.empty()) {
units.push_back(make_shared<BmsReferenceListDataUnit>(bgmUnitId));
units.back()->Value = BmsReferenceListDataUnit::ValueType(_Options.GridSize, 0);
}
while (it != bgmNotes.cend() && it->first == i) {
bool inserted = false;
for (auto &unit : units) {
if (unit->Value[i].IsPlaceholder()) {
unit->Value[i] = it->second;
inserted = true;
break;
}
}
if (!inserted) {
units.push_back(make_shared<BmsReferenceListDataUnit>(bgmUnitId));
units.back()->Value = BmsReferenceListDataUnit::ValueType(_Options.GridSize, 0);
units.back()->Value[i] = it->second;
}
++it;
}
}
}
for (auto &unit : units) {
unit->Shrink();
}
for (const auto &unit : units) {
bmsBeatmap.MainData.push_back(unit);
}
for (const auto &field : data) {
auto &channel = field.first;
auto ¬es = field.second;
auto it = notes.cbegin();
auto unit = make_shared<BmsReferenceListDataUnit>(BmsDataUnitId(section, channel));
unit->Value = BmsReferenceListDataUnit::ValueType(_Options.GridSize, 0);
for (uint8_t i = 0; i < _Options.GridSize; ++i) {
if (it != notes.cend() && it->first == i) {
unit->Value[i] = it->second;
++it;
}
}
unit->Shrink();
bmsBeatmap.MainData.push_back(unit);
}
}
}