Repository navigation
Expand file tree
/
Copy pathsource.cpp
More file actions
357 lines (347 loc) · 11.4 KB
/
Copy pathsource.cpp
File metadata and controls
357 lines (347 loc) · 11.4 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
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
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
#include <iostream>
#include <windows.h>
#include <mmeapi.h>
#include <vector>
#include <string>
#include <sstream>
#include<fstream>
#include<map>
#include <iterator>
#include <stdexcept>
#pragma comment(lib, "winmm.lib")
int loop_max = 3;
int sleep = 167;
void sendMIDI(HMIDIOUT hMidi, BYTE status, BYTE data1, BYTE data2) {
DWORD msg = status | (data1 << 8) | (data2 << 16);
midiOutShortMsg(hMidi, msg);
}
std::vector<int> getMelodyNotes() {
std::vector<int> notes;
std::string input;
std::cout << "=== Welcome to the Housemade Melody Maker! [by lilias -winmm]" << std::endl;
std::cout << "Skip manually enter to play default sound track, or pass in a file. use -help for infos!" << std::endl;
std::cout << "Melody >> ";
std::getline(std::cin, input);
std::stringstream ss(input);
int note;
while (ss >> note) {
notes.push_back(note);
}
if (notes.empty()) {
notes = {
// Teil 1: Düsteres Intro
60, 63, 65, 67, 60, 63, 65, 67,
62, 65, 67, 68, 62, 65, 67, 68,
72, 70, 68, 67, 68, 67, 65, 63,
65, 67, 68, 70, 72, 74, 75, 79
};
}
return notes;
}
std::vector<int> getBassNotes() {
std::vector<int> notes;
std::string input;
std::cout << "Bass >> ";
std::getline(std::cin, input);
std::stringstream ss(input);
int note;
while (ss >> note) {
notes.push_back(note);
}
if (notes.empty()) {
notes = {
// Passt zu Teil 1
36, 36, 36, 36, 39, 39, 39, 39,
38, 38, 38, 38, 41, 41, 41, 41,
// Passt zu Teil 2
43, 43, 43, 43, 41, 41, 41, 41,
39, 39, 39, 39, 46, 46, 47, 48
};
}
return notes;
}
enum EnumIdent {
MELODY,
BASS
};
enum FLAGS {
SLEEP,
LOOP
};
bool has{};
std::vector<int> cache;
bool hasNumber(const std::string& s) {
return s.find_first_of("0123456789") != std::string::npos;
}
bool isNumeric(const std::string& s) {
size_t i = (s.size() > 1 && (s[0] == '-' || s[0] == '+')) ? 1 : 0;
if (i >= s.size()) {
return false;
}
return s.find_first_not_of("0123456789", i) == std::string::npos;
}
std::vector<int> getMelody(std::string file, std::string targetheader, EnumIdent action) {
if (has) {
return cache;
}
bool found_header{};
bool found_melody{};
bool found_bass{};
bool found_flags{};
bool readFlags{};
int init_counter{};
int sec_counter{};
std::string strmel{};
std::vector<int> melody_;
std::vector<int> bass_;
std::string tmplit;
char svlit{};
for (const char lit : file) {
if (found_bass) {
// finish
std::cout << "loaded melody!" << std::endl;
break;
}
/*
Bass handling
*/
if (found_melody) {
// parse bass
if (lit == '\n') {
if (hasNumber(strmel)) {
bass_.push_back(std::stoi(strmel));
}
strmel.clear();
if (bass_.empty()) {
init_counter++;
continue;
}
if (bass_.empty()) {
throw "melody cant be empty!";
} else {
found_bass = true;
init_counter++;
continue;
}
}
if (lit == ' ') {
if (hasNumber(strmel)) {
bass_.push_back(std::stoi(strmel));
}
strmel.clear();
}
strmel += lit;
init_counter++;
continue;
}
/*
flag handling
*/
if (readFlags) {
if (lit == ' ' || lit == '\n') {
if (svlit == 's' || svlit == 'l') {
if (tmplit.empty()) {
std::cout << "why is your " << svlit << " flag empty? usage: "
<< svlit << "170 or " << svlit << "1" << std::endl;
} else if (!isNumeric(tmplit)) {
std::cout << "you serious? u need to do <f><num>, only "
<< svlit << "2 or " << svlit << "23 etc. got: "
<< svlit << tmplit << std::endl;
} else if (svlit == 's') {
sleep = std::stoi(tmplit);
} else {
loop_max = std::stoi(tmplit);
}
}
tmplit.clear();
svlit = {};
if (lit == '\n') {
readFlags = false;
}
init_counter++;
continue;
}
if (svlit == 0) {
if (lit == 's' || lit == 'l') {
svlit = lit;
}
init_counter++;
continue;
}
tmplit += lit;
init_counter++;
continue;
}
/*
after Header Handling
*/
if (found_header) {
if (lit == 's' || lit == 'l') { // flag: sleep
svlit = lit;
readFlags = true;
init_counter++;
continue;
}
/*
Melody Handling
*/
if (lit == '\n') {
if (hasNumber(strmel)) {
melody_.push_back(std::stoi(strmel));
}
strmel.clear();
if (melody_.empty()) {
init_counter++;
continue;
}
if (melody_.empty()) {
throw "melody cant be empty!";
} else {
found_melody = true;
init_counter++;
continue;
}
}
if (lit == ' ') {
if (hasNumber(strmel)) {
melody_.push_back(std::stoi(strmel));
}
strmel.clear();
}
strmel += lit;
init_counter++;
continue;
}
/* Search Header Quotes *[*]* */
if (lit == '[') {
sec_counter = init_counter + 1;
}
if (lit == ']') {
if (sec_counter == 0) {
throw "error in for(const char lit : file) {\n ... undefined error\n}";
} else {
int offset = init_counter - sec_counter;
std::string header = file.substr(sec_counter, offset);
if (header == targetheader) {
found_header = true;
}
}
}
init_counter++;
}
if (!found_header) {
throw "the provided header was not found in the sound library. did you spelled it right?\nFormat is:\n[HEADER]\n...\n[HEADER2]\n...";
}
if (found_melody && !found_bass && hasNumber(strmel)) { // /flag or opt
bass_.push_back(std::stoi(strmel));
}
if (melody_.empty() || bass_.empty()) {
std::cout << "collected: f-s" << sleep << ".l" << loop_max << std::endl;
throw "error collecting tones in function getMelody!";
}
switch (action) {
case MELODY:
cache = bass_;
has = true;
return melody_;
case BASS:
cache = melody_;
has = true;
return bass_;
break;
default:
std::cout << "error in getMelody(file, targetheader, action)\n /\\ HERE" << std::endl;
break;
}
return {};
}
int main(int argc, char** argv) {
// main globals
bool file_used{};
std::string filename{};
std::string titlename{};
// General Flag checks for flag
for (int i = 0; i < argc; i++) {
std::string c = argv[i];
if (c == "-f") {
filename = argv[i + 1];
titlename = argv[i + 2];
file_used = true;
}
if (c == "-help") {
std::cout << "This is My Personal Music Maker based on 2 integar arrays of MELODY and BASS!" << std::endl;
std::cout << "Usage:" << std::endl;
std::cout << " run the program normally to type in your own custom Melody/Bass logic" << std::endl;
std::cout << " pass argumentes -f <file> <title> to use file-written melodys: " << std::endl;
std::cout << " mymelody.txt " << std::endl;
std::cout << " [MY_TITLE]" << std::endl;
std::cout << " l10 s170 ; optional flags that behave like the -s, -l flags" << std::endl;
std::cout << " 74 29 46 284 83 89 46 -34 -23 3" << std::endl;
std::cout << " 29 47 29 47 29 43 20 26" << std::endl;
std::cout << " [OTHER]" << std::endl;
std::cout << " Then u do \"./melody mymelodie.txt\" \"MY_TITLE\"" << std::endl;
std::cout << " For setting the loop the sound will be played. use the -l flag followed by an 4 byte integer" << std::endl;
std::cout << " For setting the sleep duration between sound-samples use the -s flag followed by an 4 byte integer" << std::endl;
std::cout << " For using a own file with predefined melodies, use -f PATH TITLE" << std::endl;
std::exit(0);
}
if (c == "-l") {
loop_max = std::stol(argv[i + 1]);
std::cout << "set -l (loop) flag to " << loop_max << std::endl;
}
if (c == "-s") {
sleep = std::stol(argv[i + 1]);
std::cout << "set -s (sleep) flag to " << sleep << std::endl;
}
}
std::vector<int> melody{};
std::vector<int> bass{};
std::string file;
std::string openquote = "[";
std::string closequote = "]";
if (file_used) {
std::ifstream samples(filename);
std::string target = titlename;
if (!samples.is_open()) {
std::cout << "file not found!" << std::endl;
return 1;
}
file.assign((std::istreambuf_iterator<char>(samples)), std::istreambuf_iterator<char>());
try {
melody = getMelody(file, target, MELODY);
bass = getMelody(file, target, BASS);
} catch (const char* err) {
std::cerr << err << std::endl;
return 1;
} catch (const std::exception& err) {
std::cerr << "parse error: " << err.what() << std::endl;
return 1;
}
} else {
melody = getMelodyNotes();
bass = getBassNotes();
}
HMIDIOUT hMidi;
if (midiOutOpen(&hMidi, MIDI_MAPPER, 0, 0, CALLBACK_NULL) != MMSYSERR_NOERROR) {
std::cerr << "Could not open Windows MIDI device!" << std::endl;
return 1;
}
std::cout << "Playing..." << std::endl;
sendMIDI(hMidi, 0xC0, 80, 0);
sendMIDI(hMidi, 0xC1, 38, 0);
size_t length = melody.size();
for (int loop = 0; loop < loop_max; loop++) {
for (size_t i = 0; i < length; i++) {
int currentMelody = melody[i];
int currentBass = bass[i % bass.size()];
sendMIDI(hMidi, 0x90, currentMelody, 127);
sendMIDI(hMidi, 0x91, currentBass, 110);
Sleep(sleep);
sendMIDI(hMidi, 0x80, currentMelody, 0);
sendMIDI(hMidi, 0x81, currentBass, 0);
}
}
midiOutClose(hMidi);
std::cout << "finished...!" << std::endl;
return 0;
}