forked from craigsapp/midifile
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmidimixup.cpp
221 lines (177 loc) · 5.4 KB
/
midimixup.cpp
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
//
// Programmer: Craig Stuart Sapp <[email protected]>
// Creation Date: Thu Dec 2 12:45:43 PST 1999
// Last Modified: Fri Feb 20 20:34:39 PST 2015
// Filename: midimixup.cpp
// Syntax: C++11
//
// Description: Reads a standard MIDI file, move the pitches around
// into a random order.
//
#include "MidiFile.h"
#include "Options.h"
#include <iostream>
#include <random>
#include <algorithm>
#include <iterator>
using namespace std;
// function declarations:
void checkOptions (Options& opts);
void example (void);
void usage (const char* command);
void randomizeNotes (vector<MidiEvent*>& notes);
void reverseNotes (vector<MidiEvent*>& notes);
void swapNotes (vector<MidiEvent*>& notes, int index1, int index2);
class pairing {
public:
int index;
double value;
};
// variables related to command-line options
int reverseQ = 0; // used with -r option: reverse notes
///////////////////////////////////////////////////////////////////////////
int main(int argc, char** argv) {
Options options(argc, argv);
checkOptions(options);
MidiFile midifile;
if (options.getArgCount() == 2) {
midifile.read(options.getArg(1));
} else if (options.getArgCount() == 1) {
midifile.read(options.getArg(1));
} else {
cerr << "Need one optional MIDI file (or standard input), ";
cerr << "and one output" << endl;
exit(1);
}
midifile.linkNotePairs();
vector<MidiEvent*> notes;
notes.reserve(123456);
int track, event;
for (track=0; track<midifile.getNumTracks(); track++) {
for (event=0; event<midifile.getNumEvents(track); event++) {
if (midifile[track][event].isNoteOn()) {
if (midifile[track][event].isLinked()) {
notes.push_back(&midifile[track][event]);
}
}
}
}
if (reverseQ) {
reverseNotes(notes);
} else {
randomizeNotes(notes);
}
if (options.getArgCount() == 2) {
midifile.write(options.getArg(2));
} else {
midifile.write(options.getArg(1));
}
return 0;
}
///////////////////////////////////////////////////////////////////////////
//////////////////////////////
//
// reverseNotes -- Reverse the order of the notes in the file.
//
void reverseNotes(vector<MidiEvent*>& notes) {
int count = notes.size();
for (int i=0; i<count/2; i++) {
swapNotes(notes, i, count-1-i);
}
}
//////////////////////////////
//
// randomizeNotes -- adjust an absolute MIDI time. If the time
// becomes negative, then the time will be set to zero.
//
void randomizeNotes(vector<MidiEvent*>& notes) {
random_device rd;
mt19937 md(rd());
uniform_real_distribution<double> dist(0, 100);
int count = notes.size();
vector<pairing> neworder;
neworder.resize(notes.size());
int i;
for (i=0; i<(int)neworder.size(); i++) {
neworder[i].index = i;
neworder[i].value = dist(md);
}
sort(neworder.begin(), neworder.end(),
[](const pairing& a, const pairing& b) {
return a.value > b.value;
});
for (i=0; i<count; i++) {
swapNotes(notes, i, neworder[i].index);
}
}
//////////////////////////////
//
// swapNotes -- move a note from one location to another.
//
void swapNotes(vector<MidiEvent*>& notes, int index1, int index2) {
MidiEvent* noteon1 = notes[index1];
MidiEvent* noteon2 = notes[index2];
MidiEvent* noteoff1 = notes[index1]->getLinkedEvent();
MidiEvent* noteoff2 = notes[index2]->getLinkedEvent();
if (noteon1 == NULL) { return; }
if (noteon2 == NULL) { return; }
if (noteoff1 == NULL) { return; }
if (noteoff2 == NULL) { return; }
int pitch1 = noteon1->getKeyNumber();
int pitch2 = noteon2->getKeyNumber();
if (pitch1 == pitch2) { return; }
if (pitch1 < 0) { return; }
if (pitch2 < 0) { return; }
noteon1->setKeyNumber(pitch2);
noteoff1->setKeyNumber(pitch2);
noteon2->setKeyNumber(pitch1);
noteoff2->setKeyNumber(pitch1);
}
//////////////////////////////
//
// checkOptions -- handle command-line options.
//
void checkOptions(Options& opts) {
opts.define("r|reverse=b", "Reverse the order of notes");
opts.define("author=b", "Author of the program");
opts.define("version=b", "Print version of the program");
opts.define("example=b", "Display example use of the program");
opts.define("help=b", "Dispay help for the program");
opts.process();
if (opts.getBoolean("author")) {
cout << "Written by Craig Stuart Sapp, "
<< "[email protected], 2 December 1999" << endl;
exit(0);
}
if (opts.getBoolean("version")) {
cout << "midimixup version 2.0" << endl;
cout << "compiled: " << __DATE__ << endl;
}
if (opts.getBoolean("help")) {
usage(opts.getCommand().data());
exit(0);
}
if (opts.getBoolean("example")) {
example();
exit(0);
}
reverseQ = opts.getBoolean("reverse");
}
//////////////////////////////
//
// example -- gives example calls to the midiplay program.
//
void example(void) {
cout <<
" \n"
<< endl;
}
//////////////////////////////
//
// usage -- how to run the textmidi program on the command line.
//
void usage(const char* command) {
cout <<
" \n"
<< endl;
}