-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmidiconv.cc
312 lines (296 loc) · 8.05 KB
/
midiconv.cc
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
#include <iostream>
#include <string>
#include <vector>
#include <MidiFile.h> // midifile.sapp.org || github.com/craigsapp/midifile
using namespace std;
class TransformNote
{
public:
TransformNote(int t=0, float _a=1., int _b=0) : track(t), a(_a), b(_b) {}
int track;
float a;
int b;
};
typedef vector<TransformNote> vtfn_t;
class MoveNote
{
public:
MoveNote(int t=0, int e=0, int n=0) : track(t), event_index(e), note(n) {}
int track;
int event_index;
int note;
};
typedef vector<MoveNote> vmvn_t;
class MidiConv
{
public:
MidiConv(
smf::MidiFile& _mf,
const vtfn_t& tnfv,
const vmvn_t& mvns,
bool _verbose=false
) :
mf(_mf),
trans_notes_velocity(tnfv),
move_notes(mvns),
verbose(_verbose),
pxform_note_default(nullptr)
{
pxform_note_default = pxform_note_of_track_index(-1);
}
int run();
private:
const TransformNote* pxform_note_of_track(int track) const
{
const TransformNote *ret = pxform_note_of_track_index(track);
ret = ret ? : pxform_note_default;
return ret;
}
bool verbose;
const TransformNote* pxform_note_of_track_index(int track) const;
void xform_event_velocity(const TransformNote& x, smf::MidiEvent& e);
const MoveNote* find_move_note(int track, int event_index) const;
void move_note(const MoveNote& mvn, smf::MidiEvent& e);
smf::MidiFile& mf;
vtfn_t trans_notes_velocity;
vmvn_t move_notes;
const TransformNote *pxform_note_default;
};
int MidiConv::run()
{
int rc = 0;
int n_tracks = mf.size();
cerr << "#(tracks) = " << n_tracks << '\n';
for (int ti = 0; ti < n_tracks; ++ti)
{
smf::MidiEventList& events = mf[ti];
int n_events = events.size();
int n_notes_on = 0;
const TransformNote *px = pxform_note_of_track(ti);
cerr << "track[" << ti << "] #(events) = " << n_events << '\n';
for (int ei = 0; ei < n_events; ++ei)
{
smf::MidiEvent& e = events[ei];
if (e.isMetaMessage() && e.isTrackName())
{
cout << "trackname(" << ti << "): " << e.getMetaContent() <<
'\n';
}
if (verbose)
{
cout << ti << "/" << ei << ": " <<
(e.isNoteOn() ? "NoteOn " : "NoteOff") <<
", key: " << e.getKeyNumber() <<
", velocity: " << e.getVelocity() <<
", tick: " << e.tick <<
", duration-" << e.getDurationInSeconds() << '\n';
}
if (e.isNoteOn())
{
++n_notes_on;
if (px)
{
xform_event_velocity(*px, e);
}
}
const MoveNote* pmn = find_move_note(ti, ei);
if (pmn)
{
move_note(*pmn, e);
}
}
}
return rc;
}
const TransformNote* MidiConv::pxform_note_of_track_index(int track) const
{
const TransformNote *ret = nullptr;
for (const TransformNote& xform_note: trans_notes_velocity)
{
if (xform_note.track == track)
{
ret = &xform_note;
}
}
return ret;
}
void MidiConv::xform_event_velocity(const TransformNote& x, smf::MidiEvent& e)
{
int v = e.getVelocity();
v = static_cast<int>(x.a * v) + x.b;
if (v < 0) { v = 0; }
if (v > 0x7f) { v = 0x7f; }
e.setVelocity(v);
}
const MoveNote* MidiConv::find_move_note(int track, int event_index) const
{
const MoveNote *ret = nullptr;
for (const MoveNote& mvn: move_notes)
{
if ((mvn.track == track) && (mvn.event_index == event_index))
{
ret = &mvn;
}
}
return ret;
}
void MidiConv::move_note(const MoveNote& move_note, smf::MidiEvent& e)
{
e.setKeyNumber(move_note.note);
}
class UMidiConv
{
public:
UMidiConv(int argc, char **argv);
int run();
int get_rc() const { return rc; }
private:
void usage(const char *p0) const;
int rc;
const char* fn_in;
const char* fn_out;
vtfn_t trans_notes_velocity;
vmvn_t move_notes;
bool verbose;
};
UMidiConv::UMidiConv(int argc, char **argv) :
rc(0),
fn_in(nullptr),
fn_out(nullptr),
verbose(false)
{
int ai = 1;
for ( ; (rc == 0) && (ai < argc) && (argv[ai][0] == '-'); )
{
const string opt(argv[ai++]);
if (opt == string("-tv"))
{
int t = (ai < argc ? strtoul(argv[ai++], 0, 0) : -1);
float a = (ai < argc ? strtof(argv[ai++], 0) : -1);
int b = (ai < argc ? strtoul(argv[ai++], 0, 0) : -1);
if (ai < argc)
{
trans_notes_velocity.push_back(TransformNote(t, a, b));
}
else
{
cerr << "missing -tv values\n";
rc = 1;
}
}
else if (opt == string("-note"))
{
int t = (ai < argc ? strtoul(argv[ai++], 0, 0) : -1);
int ei = (ai < argc ? strtoul(argv[ai++], 0, 0) : -1);
int n = (ai < argc ? strtoul(argv[ai++], 0, 0) : -1);
move_notes.push_back(MoveNote(t, ei, n));
}
else if (opt == string("-v"))
{
verbose = true;
}
else
{
cerr << "Bad optopn: " << opt << '\n';
rc = 1;
}
}
if ((rc == 0) && (ai +2 != argc))
{
cerr << "Missing file name(s)\n";
rc = 1;
}
else
{
fn_in = argv[ai++];
fn_out = argv[ai++];
}
if (rc != 0)
{
usage(argv[0]);
}
}
void UMidiConv::usage(const char *p0) const
{
cerr << "Usage: \n" <<
p0 << '\n' <<
" [-tv <n> <a> <b>] # Change velocity for track <n>, V := aV + b\n"
" # (n = -1 all other tracks) Repeatable\n"
" [-note <m> <n>] # Force the <m>-th note to be <n>\n"
" [-v] # verbose\n"
" <in midi> <out midi>\n";
}
int UMidiConv::run()
{
smf::MidiFile mf;
if (!mf.read(string(fn_in)))
{
cerr << "Failed to read " << fn_in << '\n';
rc = 1;
}
if (rc == 0)
{
MidiConv mc(mf, trans_notes_velocity, move_notes, verbose);
rc = mc.run();
}
if (rc == 0)
{
if (!mf.write(string(fn_out)))
{
cerr << "Failed to write " << fn_out << '\n';
}
}
return rc;
}
int main(int argc, char **argv)
{
UMidiConv p(argc, argv);
int rc = (p.get_rc() ? : p.run());
return rc;
}
#if 0 // old
int midiconv(smf::MidiFile& mf)
{
int rc = 0;
int n_tracks = mf.size();
cout << "n_tracks = " << n_tracks << '\n';
for (int ti = 0; ti < n_tracks; ++ti)
{
smf::MidiEventList& events = mf[ti];
int n_events = events.size();
cout << "track[" << ti << "] n_events = " << n_events << '\n';
for (int ei = 0; ei < n_events; ++ei)
{
smf::MidiEvent& e = events[ei];
if (e.isMetaMessage())
{
string meta = e.getMetaContent();
cout << ti << "/" << ei <<
" meta: type; " << e.getMetaType() <<
", isTrackName: " << e.isTrackName() <<
", isInstrumentName: " << e.isInstrumentName() <<
": content: " << meta << '\n';
}
if (e.isNoteOn() || e.isNoteOff())
{
const char *onoff = (e.isNoteOn() ? "On " : "Off");
cout << ti << "/" << ei << ": Note" << onoff <<
", key: " << e.getKeyNumber() <<
", velocity: " << e.getVelocity() <<
", tick-" << e.tick <<
", duration-" << e.getDurationInSeconds() << '\n';
}
}
}
return rc;
}
int main(int argc, char **argv)
{
int rc = 0;
const char* fn = argv[1];
smf::MidiFile mf;
mf.read(string(fn));
midiconv(mf);
return rc;
}
#endif // old