-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathencode_helpers.cpp
More file actions
208 lines (190 loc) · 7.58 KB
/
Copy pathencode_helpers.cpp
File metadata and controls
208 lines (190 loc) · 7.58 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
#include "encode_helpers.h"
#include <algorithm>
#include <cmath>
#include <optional>
#include <set>
#include <stdexcept>
namespace muscriptor {
std::vector<Event> note_event2event(
std::vector<NoteEvent> note_events,
const std::vector<TieNoteEvent>* tie_note_events,
float start_time,
int frame_rate) {
std::vector<TieNoteEvent> ties;
if (tie_note_events) {
ties = *tie_note_events;
sort_tie_note_events(ties);
}
std::sort(note_events.begin(), note_events.end(),
[frame_rate](const NoteEvent& a, const NoteEvent& b) {
int ta = static_cast<int>(std::lround(a.time * frame_rate));
int tb = static_cast<int>(std::lround(b.time * frame_rate));
if (ta != tb) return ta < tb;
if (a.is_drum != b.is_drum) return a.is_drum < b.is_drum;
if (a.program != b.program) return a.program < b.program;
if (a.velocity != b.velocity) return a.velocity < b.velocity;
return a.pitch < b.pitch;
});
std::vector<Event> events;
int start_tick = static_cast<int>(std::lround(start_time * frame_rate));
int tick_state = start_tick;
std::optional<int> program_state;
for (const auto& tne : ties) {
if (!program_state.has_value() || tne.program != *program_state) {
events.push_back({"program", tne.program});
program_state = tne.program;
}
events.push_back({"pitch", tne.pitch});
}
events.push_back({"tie", 0});
std::optional<int> velocity_state;
for (const auto& ne : note_events) {
if (ne.is_drum && ne.velocity == 0) continue;
int ne_tick = static_cast<int>(std::lround(ne.time * frame_rate));
if (ne_tick > tick_state) {
int shift_ticks = ne_tick - start_tick;
events.push_back({"shift", shift_ticks});
tick_state = ne_tick;
} else if (ne_tick < tick_state) {
throw std::runtime_error("NoteEvent tick is smaller than tick_state");
}
if (ne.is_drum && ne.velocity == 1) {
if (!velocity_state.has_value() || *velocity_state != 1) {
events.push_back({"velocity", 1});
velocity_state = 1;
}
events.push_back({"drum", ne.pitch});
} else {
if (!program_state.has_value() || ne.program != *program_state) {
events.push_back({"program", ne.program});
program_state = ne.program;
}
if (!velocity_state.has_value() || ne.velocity != *velocity_state) {
events.push_back({"velocity", ne.velocity});
velocity_state = ne.velocity;
}
events.push_back({"pitch", ne.pitch});
}
}
return events;
}
std::map<std::pair<std::string, int>, int> encode_index_map(int max_shift_steps) {
std::map<std::pair<std::string, int>, int> index;
auto vocab = build_event_vocab(max_shift_steps);
for (int i = 0; i < static_cast<int>(vocab.size()); ++i) {
index[{vocab[i].type, vocab[i].value}] = i;
}
return index;
}
std::vector<int> encode_note_events(
const std::vector<NoteEvent>& note_events,
int max_shift_steps,
const std::vector<TieNoteEvent>* tie_note_events,
float start_time,
int frame_rate) {
auto events = note_event2event(note_events, tie_note_events, start_time, frame_rate);
auto index = encode_index_map(max_shift_steps);
std::vector<int> tokens;
tokens.reserve(events.size());
for (const auto& e : events) {
tokens.push_back(index.at({e.type, e.value}));
}
return tokens;
}
DecodeTokensResult event2note_event(
const std::vector<Event>& events_in,
float start_time,
int frame_rate) {
DecodeTokensResult result;
std::optional<int> tie_index;
std::optional<int> program_state;
std::set<std::pair<int, int>> last_activity;
for (int i = 0; i < static_cast<int>(events_in.size()); ++i) {
const auto& e = events_in[i];
if (e.type == "tie") {
tie_index = i;
break;
}
if (e.type == "shift") break;
if (e.type == "program") {
program_state = e.value;
} else if (e.type == "pitch") {
if (!program_state.has_value()) {
++result.err_cnt;
continue;
}
result.tie_note_events.push_back({*program_state, e.value});
last_activity.insert({*program_state, e.value});
}
}
if (!tie_index.has_value()) {
++result.err_cnt;
return result;
}
std::vector<Event> events(events_in.begin() + *tie_index + 1, events_in.end());
std::optional<int> velocity_state;
int start_tick = static_cast<int>(std::lround(start_time * frame_rate));
int tick_state = start_tick;
for (const auto& e : events) {
try {
if (e.type == "shift") {
if (e.value <= 0) throw std::runtime_error("Err/Negative shift");
int prev = tick_state;
tick_state = start_tick + e.value;
if (tick_state <= prev) throw std::runtime_error("Err/Shift not strictly monotonic");
} else if (e.type == "drum") {
result.note_events.push_back(
{true, DRUM_PROGRAM, static_cast<float>(tick_state) / frame_rate, 1, e.value});
} else if (e.type == "program") {
program_state = e.value;
} else if (e.type == "velocity") {
velocity_state = e.value;
} else if (e.type == "pitch") {
if (!program_state.has_value()) throw std::runtime_error("Err/Missing prg");
if (!velocity_state.has_value()) throw std::runtime_error("Err/Missing vel");
if (*velocity_state > 0) {
last_activity.insert({*program_state, e.value});
} else if (*velocity_state == 0 && last_activity.count({*program_state, e.value})) {
last_activity.erase({*program_state, e.value});
} else {
throw std::runtime_error("Err/Note off without note on");
}
result.note_events.push_back(
{false, *program_state, static_cast<float>(tick_state) / frame_rate,
*velocity_state, e.value});
} else if (e.type == "EOS") {
break;
} else if (e.type == "PAD" || e.type == "UNK") {
continue;
} else if (e.type == "tie") {
throw std::runtime_error(tick_state == start_tick ? "Err/Multi-tie type 1"
: "Err/Multi-tie type 2");
} else {
throw std::runtime_error("Err/Unknown event");
}
} catch (...) {
++result.err_cnt;
}
}
sort_note_events(result.note_events);
sort_tie_note_events(result.tie_note_events);
for (const auto& p : last_activity) {
result.last_activity.emplace_back(p.first, p.second);
}
return result;
}
DecodeTokensResult decode_tokens(
const std::vector<int>& tokens,
const std::vector<Event>& vocab,
float start_time,
int frame_rate) {
std::vector<Event> events;
for (int idx : tokens) {
if (idx < 0 || idx >= static_cast<int>(vocab.size())) {
throw std::runtime_error("Unknown event index: " + std::to_string(idx));
}
events.push_back(vocab[idx]);
}
return event2note_event(events, start_time, frame_rate);
}
} // namespace muscriptor