-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmidi_io.cpp
More file actions
204 lines (180 loc) · 6.73 KB
/
Copy pathmidi_io.cpp
File metadata and controls
204 lines (180 loc) · 6.73 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
#include "midi_io.h"
#include <cmath>
#include <cstdio>
#include <cstring>
#include <fstream>
#include <stdexcept>
namespace muscriptor {
namespace {
void write_be16(std::vector<uint8_t>& out, uint16_t v) {
out.push_back(static_cast<uint8_t>((v >> 8) & 0xff));
out.push_back(static_cast<uint8_t>(v & 0xff));
}
void write_be32(std::vector<uint8_t>& out, uint32_t v) {
out.push_back(static_cast<uint8_t>((v >> 24) & 0xff));
out.push_back(static_cast<uint8_t>((v >> 16) & 0xff));
out.push_back(static_cast<uint8_t>((v >> 8) & 0xff));
out.push_back(static_cast<uint8_t>(v & 0xff));
}
void write_vlq(std::vector<uint8_t>& out, uint32_t value) {
uint8_t buf[5];
int n = 0;
buf[n++] = value & 0x7f;
while (value >>= 7) {
buf[n++] = static_cast<uint8_t>((value & 0x7f) | 0x80);
}
while (n--) out.push_back(buf[n]);
}
int second2tick(float seconds, int ticks_per_beat, int tempo_us) {
// tick = second * (1e6 / tempo) * ticks_per_beat
double ticks = static_cast<double>(seconds) * (1000000.0 / tempo_us) * ticks_per_beat;
return static_cast<int>(std::lround(ticks));
}
struct MidiMsg {
int abs_tick = 0;
int channel = 0;
int pitch = 0;
int velocity = 0;
bool note_on = true;
};
std::vector<uint8_t> encode_track(const std::vector<uint8_t>& events) {
std::vector<uint8_t> track;
track.insert(track.end(), {'M', 'T', 'r', 'k'});
write_be32(track, static_cast<uint32_t>(events.size()));
track.insert(track.end(), events.begin(), events.end());
return track;
}
} // namespace
std::vector<uint8_t> notes_to_midi_bytes(
const std::vector<Note>& notes,
int velocity,
int tempo_bpm,
const std::map<int, std::string>* program_names) {
auto note_events = note2note_event(notes);
int ticks_per_beat = 480;
int tempo_us = static_cast<int>(60'000'000 / tempo_bpm);
// Add drum offsets (0.01s) like Python note_event2midi
std::vector<NoteEvent> all_events = note_events;
for (const auto& ne : note_events) {
if (ne.is_drum) {
all_events.push_back({true, ne.program, ne.time + 0.01f, 0, ne.pitch});
}
}
sort_note_events(all_events);
std::map<int, int> program_to_channel;
std::vector<int> available_channels;
for (int c = 0; c < 9; ++c) available_channels.push_back(c);
for (int c = 10; c < 16; ++c) available_channels.push_back(c);
std::map<int, std::vector<uint8_t>> track_events;
std::map<int, int> track_ticks;
int current_tick = 0;
// Meta track with tempo
std::vector<uint8_t> meta;
write_vlq(meta, 0);
meta.push_back(0xff);
meta.push_back(0x51);
meta.push_back(0x03);
meta.push_back(static_cast<uint8_t>((tempo_us >> 16) & 0xff));
meta.push_back(static_cast<uint8_t>((tempo_us >> 8) & 0xff));
meta.push_back(static_cast<uint8_t>(tempo_us & 0xff));
write_vlq(meta, 0);
meta.push_back(0xff);
meta.push_back(0x2f);
meta.push_back(0x00);
for (const auto& ne : all_events) {
int absolute_tick = second2tick(ne.time, ticks_per_beat, tempo_us);
if (absolute_tick < current_tick) {
throw std::runtime_error("MIDI tick went backwards");
}
current_tick = absolute_tick;
int key = (ne.is_drum || ne.program == DRUM_PROGRAM) ? DRUM_PROGRAM : ne.program;
if (!track_events.count(key)) {
track_events[key] = {};
track_ticks[key] = 0;
int ne_channel;
std::string name;
int gm_program;
if (key == DRUM_PROGRAM) {
ne_channel = 9;
name = (program_names && program_names->count(key))
? program_names->at(key)
: "drums";
gm_program = 0;
} else {
if (!available_channels.empty()) {
ne_channel = available_channels.front();
available_channels.erase(available_channels.begin());
} else {
ne_channel = 15;
}
name = (program_names && program_names->count(key))
? program_names->at(key)
: ("program " + std::to_string(key));
gm_program = ne.program;
}
program_to_channel[key] = ne_channel;
auto& te = track_events[key];
// track name
write_vlq(te, 0);
te.push_back(0xff);
te.push_back(0x03);
te.push_back(static_cast<uint8_t>(name.size()));
te.insert(te.end(), name.begin(), name.end());
// program change
write_vlq(te, 0);
te.push_back(static_cast<uint8_t>(0xc0 | (ne_channel & 0x0f)));
te.push_back(static_cast<uint8_t>(gm_program & 0x7f));
}
auto& te = track_events[key];
int ne_channel = program_to_channel[key];
int delta = absolute_tick - track_ticks[key];
track_ticks[key] = absolute_tick;
write_vlq(te, static_cast<uint32_t>(delta));
if (ne.velocity > 0) {
te.push_back(static_cast<uint8_t>(0x90 | (ne_channel & 0x0f)));
te.push_back(static_cast<uint8_t>(ne.pitch & 0x7f));
te.push_back(static_cast<uint8_t>(velocity & 0x7f));
} else {
te.push_back(static_cast<uint8_t>(0x80 | (ne_channel & 0x0f)));
te.push_back(static_cast<uint8_t>(ne.pitch & 0x7f));
te.push_back(0);
}
}
// End-of-track for each program track
for (auto& [key, te] : track_events) {
(void)key;
write_vlq(te, 0);
te.push_back(0xff);
te.push_back(0x2f);
te.push_back(0x00);
}
// Assemble SMF
std::vector<uint8_t> midi;
midi.insert(midi.end(), {'M', 'T', 'h', 'd'});
write_be32(midi, 6);
write_be16(midi, 1); // type 1
write_be16(midi, static_cast<uint16_t>(1 + track_events.size()));
write_be16(midi, static_cast<uint16_t>(ticks_per_beat));
auto meta_track = encode_track(meta);
midi.insert(midi.end(), meta_track.begin(), meta_track.end());
for (const auto& [key, te] : track_events) {
(void)key;
auto trk = encode_track(te);
midi.insert(midi.end(), trk.begin(), trk.end());
}
return midi;
}
bool save_midi(
const std::vector<Note>& notes,
const std::string& path,
int velocity,
int tempo_bpm,
const std::map<int, std::string>* program_names) {
auto bytes = notes_to_midi_bytes(notes, velocity, tempo_bpm, program_names);
std::ofstream f(path, std::ios::binary);
if (!f) return false;
f.write(reinterpret_cast<const char*>(bytes.data()),
static_cast<std::streamsize>(bytes.size()));
return static_cast<bool>(f);
}
} // namespace muscriptor