-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathUI.cpp
528 lines (488 loc) · 18.2 KB
/
UI.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
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
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
#include "Score.hpp"
#include "letterConvert.hpp"
#include <cstring>
#include "WebCpp-Interaction-Lib/DOM.hpp"
#include <emscripten.h>
#include "Player.hpp"
#include "UI.hpp"
#include "ChordTable.hpp"
#include <thread>
extern "C" {
void EMSCRIPTEN_KEEPALIVE setup();
void EMSCRIPTEN_KEEPALIVE optimizeChordButtonClick();
void EMSCRIPTEN_KEEPALIVE nextChordButtonClick();
void EMSCRIPTEN_KEEPALIVE previousChordButtonClick();
void EMSCRIPTEN_KEEPALIVE onNoteChange();
void EMSCRIPTEN_KEEPALIVE playOptimizedChord();
void EMSCRIPTEN_KEEPALIVE sortChord1();
void EMSCRIPTEN_KEEPALIVE sortChord2();
void EMSCRIPTEN_KEEPALIVE parallelFithWeightChange();
void EMSCRIPTEN_KEEPALIVE parallelOctiveWeightChange();
void EMSCRIPTEN_KEEPALIVE arpegiateChordChange();
void EMSCRIPTEN_KEEPALIVE memorizeOptimizedChord();
void EMSCRIPTEN_KEEPALIVE memorizeChord1();
void EMSCRIPTEN_KEEPALIVE memorizeChord2();
void EMSCRIPTEN_KEEPALIVE memorizeOptimizedChordStart();
void EMSCRIPTEN_KEEPALIVE memorizeChord1Start();
void EMSCRIPTEN_KEEPALIVE memorizeChord2Start();
void EMSCRIPTEN_KEEPALIVE clearMemory();
void EMSCRIPTEN_KEEPALIVE eraseMemory();
void EMSCRIPTEN_KEEPALIVE killUnisonsChange();
}
std::string strip(std::string in){
std::string output;
for(int i=0;i<in.length();i++){
if(!isspace(in[i])){
output+=in[i];
}
}
return(output);
}
std::vector<uint8_t> getChordn(unsigned int chordNumber){
std::vector<uint8_t> output;
for(unsigned int i=1;i<=notesPerChord;i++){
auto cnl = Element::getByClassName("c" + std::to_string(chordNumber) + "n" + std::to_string(i) + "l");
auto cnn = Element::getByClassName("c" + std::to_string(chordNumber) + "n" + std::to_string(i) + "n");
std::string letterActually = (std::string)cnl[0]->dom_value + (std::string)cnn[0]->dom_value;
//std::cout<<"Letteractually: "<<letterActually<<std::endl;
if(!(strip(cnl[0]->dom_value) == "" || strip(cnn[0]->dom_value) == "")){
auto cnNote = noteFromLetter(letterActually);
if(!(!cnNote || strip(letterActually) == "")){
output.push_back(cnNote.value());
//std::cout<<" cnNote: "<<(unsigned int)cnNote.value()<<std::endl;
}
}
}
return(output);
}
std::vector<uint8_t> getMemoryn(unsigned int chordNumber){
std::vector<uint8_t> output;
for(unsigned int i=0;i<notesPerChord;i++){
auto cn = Element::getByClassName("ChordMemory" + std::to_string(chordNumber) + "c" + std::to_string(notesPerChord-i));
std::string letterActually = (std::string)cn[0]->dom_innerHTML;
//std::cout<<"Letteractually: "<<letterActually<<std::endl;
if(!(strip(letterActually) == "")){
auto cnNote = noteFromLetter(letterActually);
if(!(!cnNote || strip(letterActually) == "")){
output.push_back(cnNote.value());
//std::cout<<" cnNote: "<<(unsigned int)cnNote.value()<<std::endl;
}
}
}
return(output);
}
std::vector<std::string> getChordnL(unsigned int chordNumber){
std::vector<std::string> output;
for(unsigned int i=1;i<=notesPerChord;i++){
auto cnl = Element::getByClassName("c" + std::to_string(chordNumber) + "n" + std::to_string(i) + "l");
auto cnn = Element::getByClassName("c" + std::to_string(chordNumber) + "n" + std::to_string(i) + "n");
std::string letterActually = (std::string)cnl[0]->dom_value + (std::string)cnn[0]->dom_value;
if(!((strip(letterActually) == "") || strip(cnl[0]->dom_value)== "" || strip(cnn[0]->dom_value) == "")){
output.push_back(strip(letterActually));
}
}
return(output);
}
bool digOrMinus(char c){
if(std::isdigit(c)){
return(true);
}else if(c == '-'){
return(true);
}
return(false);
}
std::string removeDigOrMinus(std::string& str, bool inv){
std::string output;
for(int i=0;i<str.length();i++){
if(inv){
if(!digOrMinus(str[i])){
output += str[i];
}
}else{
if(digOrMinus(str[i])){
output += str[i];
}
}
}
return(output);
}
void setChordnL(unsigned int chordNumber, std::vector<std::string>& notes){
for(unsigned int i=1;i<=notesPerChord;i++){
auto cnl = Element::getByClassName("c" + std::to_string(chordNumber) + "n" + std::to_string(i) + "l");
auto cnn = Element::getByClassName("c" + std::to_string(chordNumber) + "n" + std::to_string(i) + "n");
cnn[0]->dom_value = (std::string)" ";
cnl[0]->dom_value = (std::string)" ";
}
//std::cout<<"Cleared notes"<<std::endl;
for(unsigned int i=1;i<=notesPerChord;i++){
auto cnl = Element::getByClassName("c" + std::to_string(chordNumber) + "n" + std::to_string(i) + "l");
auto cnn = Element::getByClassName("c" + std::to_string(chordNumber) + "n" + std::to_string(i) + "n");
std::string cnnVal = removeDigOrMinus(notes[i-1], false);
std::string cnlVal = removeDigOrMinus(notes[i-1], true);
cnn[0]->dom_value = (std::string)cnnVal;
cnl[0]->dom_value = (std::string)cnlVal;
}
}
void setChordMemory(unsigned int chordNumber, std::vector<std::string>& notes){
for(unsigned int i=0;i<notesPerChord;i++){
auto cn = Element::getByClassName("ChordMemory" + std::to_string(chordNumber) + "c" + std::to_string(notesPerChord-i));
cn[0]->dom_innerHTML = std::string("");
}
//std::cout<<"Cleared notes"<<std::endl;
for(unsigned int i=0;i<notesPerChord&&i<notes.size();i++){
auto cn = Element::getByClassName("ChordMemory" + std::to_string(chordNumber) + "c" + std::to_string(notesPerChord-i));
cn[0]->dom_innerHTML = (std::string)notes[i];
}
}
std::vector<std::vector<uint8_t>> lastOptimizedChord;
template<uint8_t dif> std::vector<std::pair<uint8_t, uint8_t>> getNumStepsRecur(std::vector<uint8_t>& chord){//O(n!)
if(chord.size() == 0){
return(std::vector<std::pair<uint8_t, uint8_t>>());
}
std::vector<std::pair<uint8_t, uint8_t>> found;
for(int ln = 0;ln<chord.size();ln++){
uint8_t lastNote = chord[ln];
for(int i=ln;i<chord.size();i++){
unsigned int noteDiference = (unsigned int)(std::abs((int)chord[i]-(int)lastNote));
if(noteDiference == dif){
uint8_t n1 = chord[i];
uint8_t n2 = lastNote;
if(n1<=n2){
found.push_back(std::make_pair(n1, n2));
}else{
found.push_back(std::make_pair(n2, n1));
}
}
}
}
return(found);
}
std::string getStringFromInterval(const std::vector<uint8_t>& intervals){
std::string output = "<";
for(auto& n : intervals){
output += std::to_string((int)n);
}
output += ">";
return(output);
}
std::string getAnalysisText(std::vector<uint8_t>& chord1, std::vector<uint8_t>& chord2, std::optional<std::pair<unsigned int, unsigned int>> chordsWithScore = std::nullopt){
std::string scoreAnalysis = "Overall Score: ";
unsigned int scoreVal = Score::scoreAll(chord1, chord2);
scoreAnalysis += std::to_string(scoreVal);
if(chordsWithScore){
scoreAnalysis += " (" + std::to_string(chordsWithScore.value().first) + "/" + std::to_string(chordsWithScore.value().second) + ") ";
}
if(chord1.size() > 0 && chord2.size() > 0){
auto chord1intervals = chordTable::getIntervals(chord1);
auto chord2intervals = chordTable::getIntervals(chord2);
auto chord1T = chordTable::lookup(chord1intervals);
auto chord2T = chordTable::lookup(chord2intervals);
scoreAnalysis += "<br>";
if(chord1T){
if(chord1T.value().second != "" && strip(chord1T.value().second) != ""){
scoreAnalysis += getStringFromInterval(chord1intervals) + " " + chord1T.value().second + " [" + chord1T.value().first + "]";
}else{
scoreAnalysis += getStringFromInterval(chord1intervals) + " Unknown [" + chord1T.value().first + "]";
}
}
scoreAnalysis += " - ";
if(chord2T){
if(chord2T.value().second != "" && strip(chord2T.value().second) != ""){
scoreAnalysis += getStringFromInterval(chord2intervals) + " " + chord2T.value().second + " [" + chord2T.value().first + "]";
}else{
scoreAnalysis += getStringFromInterval(chord2intervals) + " Unknown [" + chord2T.value().first + "]";
}
}
}
scoreAnalysis += "<br><br>";
scoreAnalysis += "Note Difference Score: " + std::to_string(Score::noteDifferenceScore) + "<br>";
scoreAnalysis += "Parallel Fifths Score: " + std::to_string(Score::parallelFithsScore);
if(chord1.size() > 0 && chord2.size() > 0){
auto chord1Fifths = getNumStepsRecur<7>(chord1);
auto chord2Fifths = getNumStepsRecur<7>(chord2);
if(chord1Fifths.size() > 0 && chord2Fifths.size() > 0){
scoreAnalysis += " (";
for(auto& o : chord1Fifths){
scoreAnalysis += letterFromNote(o.first) + ":" + letterFromNote(o.second);
}
scoreAnalysis += " - ";
for(auto& o : chord2Fifths){
scoreAnalysis += letterFromNote(o.first) + ":" + letterFromNote(o.second);
}
scoreAnalysis += ")";
}
}
scoreAnalysis += "<br>";
scoreAnalysis += "Parallel Octives Score: " + std::to_string(Score::parallelOctivesScore);
if(chord1.size() > 0 && chord2.size() > 0){
auto chord1Octives = getNumStepsRecur<12>(chord1);
auto chord2Octives = getNumStepsRecur<12>(chord2);
if(chord1Octives.size() > 0 && chord2Octives.size() > 0){
scoreAnalysis += " (";
for(auto& o : chord1Octives){
scoreAnalysis += letterFromNote(o.first) + ":" + letterFromNote(o.second);
}
scoreAnalysis += " - ";
for(auto& o : chord2Octives){
scoreAnalysis += letterFromNote(o.first) + ":" + letterFromNote(o.second);
}
scoreAnalysis += ")";
}
}
return(scoreAnalysis);
}
int showOptimizedChord(unsigned int index){
if(lastOptimizedChord.size() <= index){
std::cerr<<"invalid index of optimized chord"<<std::endl;
return -1;
}
std::vector<uint8_t> score = lastOptimizedChord[index];
auto chord1 = getChordn(1);
unsigned int scoreVal = Score::scoreAll(chord1, score);
if(scoreVal == std::numeric_limits<unsigned int>::max()){
std::cerr<<"invalid score value"<<std::endl;
return -1;
}
std::string resultText = "Result: ";
for(unsigned int i=1;i<=notesPerChord;i++){
auto noteScoreText = Element::getByClassName("optimizeButtonResult" + std::to_string(i))[0];
noteScoreText->dom_style["display"] = "none";
}
for(unsigned int i=0;i<score.size();i++){
auto noteScoreText = Element::getByClassName("optimizeButtonResult" + std::to_string(notesPerChord-i))[0];
noteScoreText->dom_innerHTML = letterFromNote(score[i]);
noteScoreText->dom_style["display"] = "";
}
int chordsWithScore = -1;
int cws_index = 0;
for(unsigned int i=index;i>0;i--){
if(scoreVal == Score::scoreAll(chord1, lastOptimizedChord[i])){
chordsWithScore++;
cws_index++;
}else{
break;
}
}
for(unsigned int i=index;i<lastOptimizedChord.size();i++){
if(scoreVal == Score::scoreAll(chord1, lastOptimizedChord[i])){
chordsWithScore++;
}else{
break;
}
}
if(cws_index == 0)
cws_index++;
if(chordsWithScore == 0)
chordsWithScore++;
auto scoreText = Element::getByClassName("optimizeButtonResult")[0];
scoreText->dom_innerHTML = (std::string)resultText;
auto analysisText = Element::getByClassName("optimizeButtonResultAnalysis")[0];
std::string analysisTextIH = getAnalysisText(chord1, lastOptimizedChord[index], std::make_pair(cws_index, chordsWithScore));
analysisText->dom_innerHTML = (std::string)analysisTextIH;
Element::getByClassName("playOptimizedChord")[0]->dom_style["display"] = "";
Element::getByClassName("memorizeOptimizedChord")[0]->dom_style["display"] = "";
Element::getByClassName("memorizeOptimizedChordStart")[0]->dom_style["display"] = "";
Element::getByClassName("nextChordButton")[0]->dom_style["display"] = "";
Element::getByClassName("previousChordButton")[0]->dom_style["display"] = "";
Element::getByClassName("optimizeButtonResultDisplay")[0]->dom_style["display"] = "";
Element::getByClassName("optimizeButtonResultAnalysis")[0]->dom_style["display"] = "";
Element::getByClassName("previousChordButton")[0]->dom_disabled = true;
return(0);
}
unsigned int currentChordIndex = 0;
void optimizeChordButtonClick(){
auto chord1 = getChordn(1);
auto chord2 = getChordn(2);
lastOptimizedChord = Score::optimizeScore(chord1, chord2);
std::sort(lastOptimizedChord.begin(), lastOptimizedChord.end(), [&](auto& obj1, auto& obj2){
return(Score::scoreAll(chord1, obj1) < Score::scoreAll(chord1, obj2));
});
currentChordIndex = 0;
showOptimizedChord(currentChordIndex);
}
int hasOptimizedChord(unsigned int index){
if(lastOptimizedChord.size() <= index){
std::cerr<<"invalid index of optimized chord"<<std::endl;
return -1;
}
std::vector<uint8_t> score = lastOptimizedChord[index];
auto chord1 = getChordn(1);
unsigned int scoreVal = Score::scoreAll(chord1, score);
if(scoreVal == std::numeric_limits<unsigned int>::max()){
std::cerr<<"invalid score value"<<std::endl;
return -1;
}
return(0);
}
void nextChordButtonClick(){
if(currentChordIndex < lastOptimizedChord.size()){
currentChordIndex++;
showOptimizedChord(currentChordIndex);
if(currentChordIndex+1>=lastOptimizedChord.size()){
Element::getByClassName("nextChordButton")[0]->dom_disabled = true;
}else{
Element::getByClassName("nextChordButton")[0]->dom_disabled = false;
}
Element::getByClassName("previousChordButton")[0]->dom_disabled = false;
playOptimizedChord();
if(currentChordIndex+1 < lastOptimizedChord.size() && hasOptimizedChord(currentChordIndex+1) != 0){
Element::getByClassName("nextChordButton")[0]->dom_disabled = true;
return;
}
}
}
void previousChordButtonClick(){
if(currentChordIndex > 0){
currentChordIndex--;
showOptimizedChord(currentChordIndex);
//std::cout<<currentChordIndex<<std::endl;
if(currentChordIndex < 1){
Element::getByClassName("previousChordButton")[0]->dom_disabled = true;
}else{
Element::getByClassName("previousChordButton")[0]->dom_disabled = false;
}
Element::getByClassName("nextChordButton")[0]->dom_disabled = false;
playOptimizedChord();
}
}
void onNoteChange(){
auto scoreText = Element::getByClassName("scoreButtonResult")[0];
auto chord1 = getChordn(1);
auto chord2 = getChordn(2);
unsigned int score = Score::scoreAll(chord1, chord2);
if(score == std::numeric_limits<unsigned int>::max()){
scoreText->dom_innerHTML = (std::string)"Score: N/A";
}else{
//scoreText->dom_innerHTML = "Score: " + std::to_string(score);
scoreText->dom_innerHTML = getAnalysisText(chord1, chord2);
}
}
void parallelOctiveWeightChange(){
Score::parallelOctivesWeight = std::stoi(Element::getByClassName("parallelOctiveWeightInput")[0]->dom_value);
onNoteChange();
}
void parallelFithWeightChange(){
Score::parallelFithsWeight = std::stoi(Element::getByClassName("parallelFithWeightInput")[0]->dom_value);
onNoteChange();
}
void memorizeOptimizedChord(){
auto chordi = lastOptimizedChord[currentChordIndex];
std::vector<std::string> chord;
for(int i=0;i<chordi.size();i++){
std::string note = letterFromNote(chordi[i]);
chord.push_back(note);
}
setChordMemory(memorizedChords+1, chord);
memorizedChords++;
if(memorizedChords+1>chordMemories){
Element::getByClassName("memorizeOptimizedChord")[0]->dom_disabled = true;
Element::getByClassName("memorizeChord1")[0]->dom_disabled = true;
Element::getByClassName("memorizeChord2")[0]->dom_disabled = true;
}
}
void memorizeChord1(){
auto chord = getChordnL(1);
setChordMemory(memorizedChords+1, chord);
memorizedChords++;
if(memorizedChords+1>chordMemories){
Element::getByClassName("memorizeOptimizedChord")[0]->dom_disabled = true;
Element::getByClassName("memorizeChord1")[0]->dom_disabled = true;
Element::getByClassName("memorizeChord2")[0]->dom_disabled = true;
}
}
void memorizeChord2(){
auto chord = getChordnL(2);
setChordMemory(memorizedChords+1, chord);
memorizedChords++;
if(memorizedChords+1>chordMemories){
Element::getByClassName("memorizeOptimizedChord")[0]->dom_disabled = true;
Element::getByClassName("memorizeChord1")[0]->dom_disabled = true;
Element::getByClassName("memorizeChord2")[0]->dom_disabled = true;
}
}
void memorizeOptimizedChordStart(){
auto chordi = lastOptimizedChord[currentChordIndex];
std::vector<std::string> chord;
for(int i=0;i<chordi.size();i++){
std::string note = letterFromNote(chordi[i]);
chord.push_back(note);
}
setChordnL(1, chord);
onNoteChange();
}
void memorizeChord1Start(){
auto chord = getChordnL(1);
setChordnL(2, chord);
onNoteChange();
}
void memorizeChord2Start(){
auto chord = getChordnL(2);
setChordnL(1, chord);
onNoteChange();
}
void clearMemory(){
std::vector<std::string> chord = {};
for(int i=1;i<=chordMemories;i++){
setChordMemory(i, chord);
}
memorizedChords = 0;
Element::getByClassName("memorizeOptimizedChord")[0]->dom_disabled = false;
Element::getByClassName("memorizeChord1")[0]->dom_disabled = false;
Element::getByClassName("memorizeChord2")[0]->dom_disabled = false;
}
void eraseMemory(){
std::vector<std::string> chord = {};
setChordMemory(memorizedChords, chord);
memorizedChords--;
Element::getByClassName("memorizeOptimizedChord")[0]->dom_disabled = false;
Element::getByClassName("memorizeChord1")[0]->dom_disabled = false;
Element::getByClassName("memorizeChord2")[0]->dom_disabled = false;
}
void setup(){
Element::getByClassName("playOptimizedChord")[0]->dom_style["display"] = "none";
Element::getByClassName("memorizeOptimizedChord")[0]->dom_style["display"] = "none";
Element::getByClassName("memorizeOptimizedChordStart")[0]->dom_style["display"] = "none";
Element::getByClassName("previousChordButton")[0]->dom_style["display"] = "none";
Element::getByClassName("nextChordButton")[0]->dom_style["display"] = "none";
Element::getByClassName("optimizeButtonResultDisplay")[0]->dom_style["display"] = "none";
Element::getByClassName("optimizeButtonResultAnalysis")[0]->dom_style["display"] = "none";
}
int main(){
std::cout<<"Voice Leading Calculator: v0x06"<<std::endl;
setup();
std::string initPlayer = "PlayerInit();";
GLOBAL_ACCESS->evalJS(initPlayer);
chordTable::load();
onNoteChange();
return(0);
}
void playOptimizedChord(){
if(!arpegiate){
playNotes(lastOptimizedChord[currentChordIndex], 127, 1);
}else{
playNotes(lastOptimizedChord[currentChordIndex], 127, 0.25, 0.25);
}
}
void arpegiateChordChange(){
arpegiate = Element::getByClassName("arpegiateChord")[0]->dom_checked;
}
void killUnisonsChange(){
Score::killUnisons = Element::getByClassName("removeUnison")[0]->dom_checked;
}
std::vector<std::string> sortChord(std::vector<std::string>& chord){
std::vector<std::string> output = chord;
std::sort(output.begin(), output.end(), [](const std::string& obj1, const std::string& obj2){
return(noteFromLetter(obj1) < noteFromLetter(obj2));
});
return(output);
}
void sortChord1(){
auto chord = getChordnL(1);
auto sorted = sortChord(chord);
setChordnL(1, sorted);
}
void sortChord2(){
auto chord = getChordnL(2);
auto sorted = sortChord(chord);
setChordnL(2, sorted);
}