-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathmain.cpp
More file actions
executable file
·167 lines (144 loc) · 7.21 KB
/
main.cpp
File metadata and controls
executable file
·167 lines (144 loc) · 7.21 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
#include <vector>
#include <iostream>
#include <string>
#include "lib/midi.h"
#include "lib/midi-parser.h"
#include "lib/frequencyApprox.h"
/* DEFAULT PARAMETER */
const int WINDOW_SIZE = 1000;
const int WINDOW_DISTANCE = 200;
const int ZERO_PADDING = 0;
const int MAX_NOTES = 88; // limit midi messages per time unit to enable playing it with midi hardware interface
const uint32_t TEMPO = 300 * 1000; // microseconds for one quarter note / beat (default = 500 ms = 500 * 1000 us)
const uint16_t PPQ = 10; // parts per quarter note -> with default settings (quarter note = 500 ms) PPQ *2(!) = parts per second
const uchar PROGRAM = 0x0; // MIDI instrument (choose 0 for acoustic piano, see https://de.wikipedia.org/wiki/General_MIDI)
using namespace std;
typedef unsigned char uchar;
MIDIFile demo()
{
const uint32_t pauseUs = TEMPO / 2;
vector<vector<int>> testVector = {
{0, 0x7F, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0x7F, 0, 0, 0, 0, 0, 0, 0, 0, 0},
{pauseUs, 0, 0, 0x7F, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0x7F, 0, 0, 0, 0, 0, 0, 0},
{pau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temp.noteCount = 22;
temp.firstNoteIndex = 60;
return temp.getMidiFile(testVector, false, PROGRAM);
}
int main(int argc, char *argv[])
{
// for demo / testing purposes only
demo().saveAs("./output/demo.mid");
// input validation
if (argc < 2)
{
cerr << "At least one input argument is expected. Please provide a file path..." << endl;
return -1;
}
if (argc > 8)
{
cerr << "Too many input arguments (" << argc << ")" << endl;
return -1;
}
string inputFilepath = "";
int windowSize = WINDOW_SIZE;
int windowDistance = WINDOW_DISTANCE;
int zeroPadding = ZERO_PADDING;
int maxNotes = MAX_NOTES;
uchar minVolume = MIN_VOLUME;
uchar noteSwitchThreshold = NOTE_SWITCH_THRESHOLD;
// read args
for (int i = 1; i < argc; ++i)
{
char *value = argv[i];
switch (i)
{
case 1:
inputFilepath = string(value);
break;
case 2:
windowSize = atoi(value);
break;
case 3:
windowDistance = atoi(value);
break;
case 4:
zeroPadding = atoi(value);
break;
case 5:
maxNotes = atoi(value);
break;
case 6:
minVolume = atoi(value);
break;
case 7:
noteSwitchThreshold = atoi(value);
break;
}
}
cout << "generating: " << inputFilepath << "\nwindowSize " << to_string(windowSize) << ", windowDistance " << to_string(windowDistance);
cout << ", zeroPadding " << to_string(zeroPadding) << ", maxNotes " << to_string(maxNotes);
cout << ", minVolume " << to_string(minVolume) << ", noteSwitchThreshold " << to_string(noteSwitchThreshold) << endl;
// read audio file, perform fast-fourier-transformation (FFT) and aggregate results to MIDI compatible format
short **midiTable;
FrequencyApprox approx{};
int frames, midiTempo;
midiTable = approx.toMIDI(inputFilepath.c_str(), windowSize, windowDistance, zeroPadding, midiTempo, frames);
cout << "read audio file, performed FFT and aggregated MIDI data" << endl;
// parse data to MIDI object
uint32_t tempo = midiTempo;
MIDIParser parser{tempo, PPQ, maxNotes, minVolume, noteSwitchThreshold};
MIDIFile midiFile = parser.getMidiFile(midiTable, frames, PROGRAM);
cout << "generated MIDI file" << endl;
// save as MIDI file
size_t slashIndex = inputFilepath.find_last_of("/\\");
size_t dotIndex = inputFilepath.find_last_of(".");
// get file name without parent directories and file extension
string inputFilename = inputFilepath.substr(slashIndex + 1, dotIndex - slashIndex - 1);
// add parameter information to file names
string filenameSuffix1 = "-" + to_string(windowSize) + "-" + to_string(windowDistance) + "-" + to_string(zeroPadding);
string filenameSuffix2 = "-" + to_string(maxNotes) + "-" + to_string(minVolume) + "-" + to_string(noteSwitchThreshold);
string outputFilename = "./output/" + inputFilename + filenameSuffix1 + filenameSuffix2 + ".mid";
midiFile.saveAs(outputFilename.c_str());
cout << "saved MIDI file" << endl;
}