-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathchart_writer.cpp
More file actions
171 lines (147 loc) · 6.57 KB
/
Copy pathchart_writer.cpp
File metadata and controls
171 lines (147 loc) · 6.57 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
#include "chart_writer.hpp"
#include <iomanip>
#include <sstream>
#include <algorithm>
#include <vector>
namespace NoteGen {
// clone hero/moonscraper format
const char* LINE_END = "\r\n";
const char* INDENT = " "; // two spaces not tabs
void ChartWriter::write(std::ostream& out,
const ChartMetadata& metadata,
const std::vector<SyncTrackEvent>& sync_events,
const std::vector<LoopedSection>& sections,
const std::map<std::pair<SightRead::Instrument, SightRead::Difficulty>,
std::vector<SightRead::Note>>& tracks,
const std::vector<SightRead::StarPower>& sp_phrases) {
write_song_section(out, metadata);
write_sync_track(out, sync_events);
write_events(out, sections);
// write each track
for (const auto& [key, notes] : tracks) {
auto track_name = instrument_difficulty_to_track_name(key.first, key.second);
write_note_track(out, track_name, notes, sp_phrases);
}
}
void ChartWriter::write_song_section(std::ostream& out, const ChartMetadata& metadata) {
out << "[Song]" << LINE_END << "{" << LINE_END;
out << INDENT << "Name = \"" << metadata.name << "\"" << LINE_END;
out << INDENT << "Artist = \"" << metadata.artist << "\"" << LINE_END;
out << INDENT << "Charter = \"" << metadata.charter << "\"" << LINE_END;
out << INDENT << "Offset = 0" << LINE_END;
out << INDENT << "Resolution = " << metadata.resolution << LINE_END;
out << INDENT << "Player2 = bass" << LINE_END;
out << INDENT << "Difficulty = 0" << LINE_END;
out << INDENT << "PreviewStart = 0" << LINE_END;
out << INDENT << "PreviewEnd = 0" << LINE_END;
out << INDENT << "Genre = \"Practice\"" << LINE_END;
out << INDENT << "MediaType = \"cd\"" << LINE_END;
out << INDENT << "MusicStream = \"song.ogg\"" << LINE_END;
out << "}" << LINE_END;
}
void ChartWriter::write_sync_track(std::ostream& out, const std::vector<SyncTrackEvent>& sync_events) {
out << "[SyncTrack]" << LINE_END << "{" << LINE_END;
for (const auto& event : sync_events) {
if (event.is_bpm) {
out << INDENT << event.position.value() << " = B " << event.bpm << LINE_END;
} else {
out << INDENT << event.position.value() << " = TS " << event.ts_num;
if (event.ts_denom != 4) {
int denom_log = 0;
int d = event.ts_denom;
while (d > 1) { d /= 2; denom_log++; }
out << " " << denom_log;
}
out << LINE_END;
}
}
out << "}" << LINE_END;
}
void ChartWriter::write_events(std::ostream& out, const std::vector<LoopedSection>& sections) {
out << "[Events]" << LINE_END << "{" << LINE_END;
for (const auto& section : sections) {
out << INDENT << section.start.value() << " = E \"section " << section.name << "\"" << LINE_END;
}
// end event after last section
if (!sections.empty()) {
out << INDENT << sections.back().end.value() << " = E \"end\"" << LINE_END;
}
out << "}" << LINE_END;
}
void ChartWriter::write_note_track(std::ostream& out,
const std::string& track_name,
const std::vector<SightRead::Note>& notes,
const std::vector<SightRead::StarPower>& sp_phrases) {
out << "[" << track_name << "]" << LINE_END << "{" << LINE_END;
// build sorted event list (clone hero wants notes and sp interspersed)
struct TrackEvent {
int64_t tick;
int order; // 0=note, 1=sp (notes first at same tick)
std::string line;
};
std::vector<TrackEvent> events;
// notes
for (const auto& note : notes) {
// check each fret
for (int fret = 0; fret < 7; ++fret) {
if (note.lengths[fret].value() >= 0) {
std::ostringstream ss;
ss << INDENT << note.position.value() << " = N " << fret << " "
<< note.lengths[fret].value() << LINE_END;
events.push_back({note.position.value(), 0, ss.str()});
}
}
// note flags
// n 5 = force (flip hopo/strum)
if (note.flags & (SightRead::FLAGS_FORCE_FLIP | SightRead::FLAGS_FORCE_HOPO | SightRead::FLAGS_FORCE_STRUM)) {
std::ostringstream ss;
ss << INDENT << note.position.value() << " = N 5 0" << LINE_END;
events.push_back({note.position.value(), 0, ss.str()});
}
// n 6 = tap
if (note.flags & SightRead::FLAGS_TAP) {
std::ostringstream ss;
ss << INDENT << note.position.value() << " = N 6 0" << LINE_END;
events.push_back({note.position.value(), 0, ss.str()});
}
}
// sp phrases
for (const auto& sp : sp_phrases) {
std::ostringstream ss;
ss << INDENT << sp.position.value() << " = S 2 " << sp.length.value() << LINE_END;
events.push_back({sp.position.value(), 1, ss.str()});
}
// sort by tick then order
std::sort(events.begin(), events.end(), [](const TrackEvent& a, const TrackEvent& b) {
if (a.tick != b.tick) return a.tick < b.tick;
return a.order < b.order;
});
// write em
for (const auto& event : events) {
out << event.line;
}
out << "}" << LINE_END;
}
std::string ChartWriter::instrument_difficulty_to_track_name(SightRead::Instrument inst,
SightRead::Difficulty diff) {
std::string diff_str;
switch (diff) {
case SightRead::Difficulty::Easy: diff_str = "Easy"; break;
case SightRead::Difficulty::Medium: diff_str = "Medium"; break;
case SightRead::Difficulty::Hard: diff_str = "Hard"; break;
case SightRead::Difficulty::Expert: diff_str = "Expert"; break;
}
std::string inst_str;
switch (inst) {
case SightRead::Instrument::Guitar: inst_str = "Single"; break;
case SightRead::Instrument::Bass: inst_str = "DoubleBass"; break;
case SightRead::Instrument::Rhythm: inst_str = "DoubleRhythm"; break;
case SightRead::Instrument::Keys: inst_str = "Keyboard"; break;
case SightRead::Instrument::Drums: inst_str = "Drums"; break;
case SightRead::Instrument::GHLGuitar: inst_str = "GHLGuitar"; break;
case SightRead::Instrument::GHLBass: inst_str = "GHLBass"; break;
default: inst_str = "Single"; break;
}
return diff_str + inst_str;
}
} // namespace NoteGen