-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMidi2Play_2.cpp
More file actions
614 lines (531 loc) · 14.4 KB
/
Copy pathMidi2Play_2.cpp
File metadata and controls
614 lines (531 loc) · 14.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
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
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
//
// Midi2PLAY v.1.1 - Por Rodrigo Gonzales (Rodrivas78) email: rrivas2009@gmail.com
// Esta aplicação converte sintaxe "mml" para a sintaxe aceita pelo
// comando PLAY do MSX-BASIC. Trabalha com arquivos contendo um máximo de 3 canais simultaneos.
// É necessário a utilização do programa "3ML Editor" para a conversão do arquivo MIDI (.mid) para MML.
// Também é necessária a formatação do arquivo de entrada ("mml.txt").
// Fazendo-se imprescindível a leitura das instruções.
// Compatível com todas as versões de MSX, não sendo necessário a utilização de
// hardwares musicais especializado como
// MSX-AUDIO, MSX-MUSIC, FM-PAC e etc.)
//
#include <iostream>
#include <fstream>
#include <cstdio>
#include <string>
#include <sstream>
#include "MultiArrayTester.h"
using namespace std;
int main()
{
int oitava;
const char token = '>'; //aumenta uma oitava
const char token2 = '<'; //diminui uma oitava
const char token3 = '&';
const char token4 = '[';
const char token5 = '/';
const char token6 = '"';
const char token7 = ']';
const char asteris = '*';
string channel = "Track ";
char strStore[1000]={};
char error ;
bool activateAsterCount = false;
bool activateCharCount = false;
int charCounter = 0;
int asterCounter = 0;
int braCount = 0;
int braCount2 = 0;
int braCount3 = 0;
int slashCount = 0;
int slashVar;
char decision;
int decision2;
int copyCount = 0;
int tokenCounter = 0;
const char a = 'A';
const char b = 'B';
const char c = 'C';
char newChar = ' ';
char charHolder;
int measures;
int measures2;
int mControl=0;
int m = 0;
int line = 10;
int matrixNum = 1;
int test=0;
char strNew= ' ';
char strNew2 = 'o'; //String 'O'
string strNew3= "";
//Novas variáveis
bool foundLetterO = false;
int charCounter2=0;
bool activNewCharCount = false;
int charNewCounter=0;
int lessOrGreaterThan=0;
int foundO=0;
ifstream filein("mml.txt"); //File to read from
ofstream fileout("t1.txt"); //Temporary file
if(!filein || !fileout) //if both files are not available
{
cout << " File (mml.txt) NOT FOUND!" << endl;
cout << " Press any key and ENTER" << endl;
cin >> error;
return 1;
}
else
{
cout << endl;
cout << "Found: mml.txt" << endl << endl;
}
cout << endl;
cout << " Do you wish concatenate measures?" << endl;
cout << " Y or N " << "(and press ENTER)" << endl;
cin >> decision;
cout << endl;
if(decision == 'N' || decision=='n')
{
slashVar = 3;
}
else
{
cout << " 2, 3 or 4 measures per line? (type the number and press ENTER)" << endl;
cin >> decision2;
switch (decision2)
{
case 2 : slashVar=5;
cout << endl;
break;
case 3 : slashVar=7;
cout << endl;
break;
case 4 : slashVar=9;
cout << endl;
break;
default: cout << " Not a valid value, measures will not be concatenated! " << endl << endl;
slashVar=3;
}
}
string strTemp0;
string strTemp2;
string strTemp3;
char charTemp2; // Nova variável: rotina de pagar as /
char strTemp; //char strTemp;
//----------------------Parser_support2----------------------------------------//
//-----------------------------------------------------------------------------
//Eliminação das barras '/' token5 //tipo:char
while(filein >> charTemp2)
{
if(charTemp2==token5)
{
charTemp2=strNew;
}
fileout << charTemp2;
}
fileout.close(); //Default
//Rotina para a contagem de compassos
ifstream filein9("t1.txt"); //File to read from
ofstream fileout9("t0.txt"); //Temporary file
while(filein9 >> strTemp0)//it will check line from test to strTemp string
{
if(strTemp0 == strReplace[0])
{
braCount++;
}
//itera sobre os a matriz ignorando as indicações de compasso duplicadas
if(strTemp0 == strReplace[m] && braCount==1)
{
m++; //vai para a próxima posição da matriz
}
measures = m;
fileout9 << measures;
}
filein9.close();
fileout9.close();
measures2 = measures;
m = 0;
braCount = 0;
ifstream filein2("t1.txt"); //File to read from
ofstream fileout2("t2.txt"); //Temporary file
//Elimina duplicaçoes dos *M1* etc. Tipo: string
while(filein2 >> strTemp2)//it will check line from test to strTemp string
{
//Procura e apaga as marcações fazendo sobrar as marcações duplicadas. Os valores da matriz estão no header
if(strTemp2 == strReplace[m])//if your word found then replace
{
strTemp2 = strNew;
m++; //vai para a próxima posição da matriz
mControl++; //mControl é a quantidade de compassos por voz
if(mControl==measures)
{
strTemp2 = strNew;
m=0;
mControl=0;
}
}
fileout2 << strTemp2;//output everything to fileout(temp.txt)
}
fileout2.close();
filein2.close();
ifstream filein4("t2.txt"); //File to read from
ofstream fileout4("t3.txt"); //Temporary file
ifstream filein5("void.txt"); //File to read from
ofstream fileout5("temp.txt"); //Temporary file
//Procura (em "temp.txt") por coinscidências, ao encontrar armazena as strings em outra matriz
// ou as envia ao arquivo void
// Detecta o caracater '*', se encontra 2 * para a contagem.
while(filein4 >> charTemp2)
{
if(activateCharCount==true)
{
charCounter++;
}
if(charTemp2==asteris)
{
asterCounter++;
if(asterCounter==2)
{
fileout5 << charTemp2;
activateCharCount=false;
fileout5 << ',';
asterCounter=0;
charCounter=0;
}
else
{
activateCharCount=true;
fileout5 << charTemp2;
}
}
if(charCounter==1 && asterCounter!=2)
{
fileout5 << charTemp2;
}
else if(charCounter==2 && asterCounter!=2)
{
fileout5 << charTemp2;
}
else if(charCounter==3 && asterCounter!=2)
{
fileout5 << charTemp2;
}
else if(charCounter==4 && asterCounter!=2)
{
fileout5 << charTemp2;
}
//Mostra no documento (temp) em que canal está a duplicação
if(charTemp2=='[')
{
braCount++;
if(braCount==1)
{
fileout5 << charTemp2;
fileout5 << "C1]";
braCount2=braCount;
}
else if(braCount==2)
{
fileout5 << charTemp2;
fileout5 << "C2]";
braCount2=braCount;
}
else if(braCount==3)
{
fileout5 << charTemp2;
fileout5 << "C3]";
braCount2=braCount;
braCount=0;
}
}
fileout4 << charTemp2;
}
filein4.close();
fileout4.close();
//----------------------------Parser_test2---------------------------------------------//
//-------------------------------------------------------------------------------------//
ifstream filein6("mml.txt"); //File to read from
ofstream fileout6("t4.txt"); //Temporary file
while(filein6 >> strTemp)//it will check line from test to strTemp char
{
//Procura pelo O para atualizar o valor da oitava
if(strTemp=='o')
{
foundLetterO = true;
foundO++;
//charCounter2++;
}
if(foundLetterO==true)
{
charCounter2++;
}
if(charCounter2==2)
{
oitava=(int)strTemp;
oitava=oitava-48;
charCounter2 = 0;
//fileout << oitava;
foundLetterO = false;
}
//ativa a contagem de caracteres
if(activateCharCount = true)
{
//conta os caracteres para encontrar a alteração de oitava logo após o /*M0*/
charCounter++;
}
if(activNewCharCount = true)
{
//conta os caracteres para que não haja alteração de oitava logo após as barras na primeira vez (ver caso "canon")
charNewCounter++;
}
// conta as 2 chaves ([ e ]) e incrementa braCount3 para controle da última aspas do canal
if(strTemp==token4 || strTemp==token7)
{
braCount3++;
}
if(charCounter==16) //Acrescenta o valor da oitava corrente 'o(n)' no inicio do fluxo do canal
{
fileout6 << strNew2;
fileout6 << oitava;
}
if(strTemp==token || strTemp==token2)
{
//greaterThan++;
lessOrGreaterThan++;
}
//inicia a contagem de caracteres ao encontrar '/' /trocar tudo por token asterisco
if(strTemp == token5) //token5 = '/'
{
activateCharCount=true;
slashCount++;
//apaga o '/' ao encontra-lo
strTemp = strNew;
//adição de aspas mais mudança de linha de acordo com o número de barras (slashs) encontradas
if(slashCount==1)
{
fileout6 << token6; //token6 = " " " aspas
}
//É possível permitir ao usuário a escolha de agrupamento de compassos
//se pretende agrupar os compassos de 2 em 2 -> slashCount==5, se de 3 em 3 -> slashCount==7
//Default: slashCount==3 (sem agrupamento).
else if(slashCount == 2)
{
//fileout6 << '#';
activNewCharCount = true;
charNewCounter=0; //Devido à essa variável é que o # esta sendo impresso duas vezes
}
else if(slashCount==slashVar) //else if(slashCount==7||braCount2%3)
{
slashCount=1;
fileout6 << token6; //token6 = " " " aspas
fileout6 << "\n";
fileout6 << token6;
}
}
//Insere últimas aspas de cada canal e zera o contador de barras
if(braCount3==3)
{
slashCount=0;
fileout6 << token6;
braCount3=1;
}
//rotina para eliminar > e inserir o O(n)
if(strTemp == token)//if your word > found then replace
{
//este condicional evita duplicação na alteração de oitavas
if(charCounter==16)
{
strTemp = strNew;
}
else
{
oitava++;
strTemp = strNew;
fileout6 << strNew2;
fileout6 << oitava;
}
}
//rotina para eliminar < e inserir o O(n)
if(strTemp == token2)//if your word is < found then replace
{
//este condicional evita duplicação na alteração de oitavas
if(charCounter==16)
{
strTemp = strNew;
}
else
{
oitava--;
strTemp = strNew;
fileout6 << strNew2;
fileout6 << oitava;
}
}
//rotina para eliminar o &
if(strTemp == token3)
{
strTemp = strNew;
}
if(strTemp==token4) //token 4 = [
{
braCount++; //Conta as chaves [ para atribuir a oitava selecionada para cada voz
cout << "In which octave ";
cout << token6 << channel << braCount << token6 << " begins: " <<endl;
cin >> oitava;
fileout6 << "\n\n";
charCounter = 0; // Zera a contagem para eliminação de > ou < logo após /*M0*/
activateCharCount = false; //desativa a contagem de caracateres
activNewCharCount = false;
lessOrGreaterThan = 0;
foundO = 0;
}
fileout6 << strTemp; //output everything to fileout(play.txt)
}
fileout6.close();
//-----------Measure_mark_eliminator-----Parser_test3------------------------------------//
//---------------------------------------------------------------------------------------//
activateCharCount=false;
charCounter=0;
braCount=0;
braCount3=0;
m=0;
ifstream filein7("t4.txt"); //File to read from
ofstream fileout7("t5.txt"); //Temporary file
//Iteração após a retirada de /* e */
while(filein7 >> strTemp3)//it will check line from test to strTemp string
{
//Modelo padrão, não funciona com *M* duplicados
//Elimina as indicações de compasso. Os valores da matriz estão no header
if(strTemp3 == strReplace[m])//if your word found then replace
{
strTemp3 = strNew3;
m++; //quantidade de compassos/measures
mControl++; //mControl é a quantidade de compassos por voz
if(mControl==measures2)
{
strTemp3 = strNew3;
m=0;
mControl=0;
}
}
fileout7 << strTemp3;//output everything to fileout(temp.txt)
}
filein7.close();
fileout7.close();
ifstream filein8("t5.txt"); //2nd file to read from
ofstream fileout8("play.txt"); //2nd temporary file
while(filein8 >> charTemp2)//it will check line from test to strTemp string
{
if(charTemp2 == token4) //token 4 = '['
{
fileout8 << '\n';
braCount++;
matrixNum = 0;
}
// Apaga o último ']'
if(charTemp2 == token7) //token 7 = ']'
{
m++;
if(m==2 && braCount2==1)
{
charTemp2 = newChar;
}
else if(m==3 && braCount2==2)
{
charTemp2 = newChar;
}
else if(m==4 && braCount2==3)
{
charTemp2 = newChar;
}
}
if(braCount==1)
{
charHolder=(int)65;
}
else if(braCount==2)
{
charHolder=(int)66;
}
else if(braCount==3)
{
charHolder=(int)67;
}
else if(braCount==4)
{
charHolder=(int)68;
}
else if(braCount==5)
{
charHolder=(int)69;
}
else if(braCount==6)
{
charHolder=(int)70;
}
else if(braCount==7)
{
charHolder=(int)71;
}
else if(braCount==8)
{
charHolder=(int)72;
}
else if(braCount==9)
{
charHolder=(int)73;
}
else if(braCount==10)
{
charHolder=(int)74;
}
else if(braCount==11)
{
charHolder=(int)75;
}
else if(braCount==12)
{
charHolder=(int)76;
}
if(charTemp2 == '"')//ao encontrar as aspas há uma quebra de linha
{
tokenCounter++;
// Controla o momento em que há troca das letras das matrizes
if(tokenCounter==1)
{
matrixNum = 1;
fileout8 << '\n';
fileout8 << line;
line+=10;
fileout8 << " ";
fileout8 << charHolder;
fileout8 << "$(";
fileout8 << matrixNum;
fileout8 << ")=";
}
if(tokenCounter==3)
{
matrixNum++;
fileout8 << '\n';
fileout8 << line;
line+=10;
fileout8 << " ";
fileout8 << charHolder;
fileout8 << "$(";
fileout8 << matrixNum;
fileout8 << ")=";
tokenCounter=1;
}
}
fileout8 << charTemp2;
}
//Remove os arquivos temporários
filein8.close();
remove("t0.txt");
remove("t1.txt");
remove("t2.txt");
remove("t3.txt");
remove("t4.txt");
remove("t5.txt");
return 0;
}