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
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
|
//=========================================================
// MusE
// Linux Music Editor
// $Id: songfile.cpp,v 1.25.2.12 2009/11/04 15:06:07 spamatica Exp $
//
// (C) Copyright 1999/2000 Werner Schweer (ws@seh.de)
//=========================================================
#include <assert.h>
#include <qmessagebox.h>
#include <uuid/uuid.h>
#include "app.h"
#include "song.h"
#include "arranger.h"
//#include "arranger/arranger.h" // p4.0.2
#include "cobject.h"
#include "drumedit.h"
//#include "midiedit/drumedit.h" // p4.0.2
#include "pianoroll.h"
//#include "midiedit/pianoroll.h" // p4.0.2
#include "globals.h"
#include "xml.h"
#include "drummap.h"
//#include "midiedit/drummap.h" // p4.0.2
#include "event.h"
#include "marker/marker.h"
#include "midiport.h"
#include "audio.h"
#include "mitplugin.h"
//#include "mplugins/mitplugin.h" // p4.0.2
#include "wave.h"
#include "midictrl.h"
#include "amixer.h"
//#include "mixer/amixer.h" // p4.0.2
#include "conf.h"
#include "driver/jackmidi.h"
//struct ClonePart {
//const EventList* el;
// const Part* cp;
// int id;
//ClonePart(const EventList* e, int i) : el(e), id(i) {}
// ClonePart(const Part* p, int i) : cp(p), id(i) {}
// };
//typedef std::list<ClonePart> CloneList;
//typedef CloneList::iterator iClone;
//---------------------------------------------------------
// ClonePart
//---------------------------------------------------------
ClonePart::ClonePart(const Part* p, int i)
{
cp = p;
id = i;
uuid_generate(uuid);
}
//static CloneList cloneList;
//static CloneList copyCloneList;
CloneList cloneList;
//CloneList copyCloneList;
/*
//---------------------------------------------------------
// updateCloneList
//---------------------------------------------------------
void updateCloneList(Part* oPart, Part* nPart)
{
for(iClone i = cloneList.begin(); i != cloneList.end(); ++i)
{
if(i->cp == oPart)
{
i->cp = nPart;
break;
}
}
}
void updateCloneList(PartList* oParts, PartList* nParts)
{
for(iPart ip = oParts->begin(); ip != oParts->end(); ++ip)
{
for(iClone i = cloneList.begin(); i != cloneList.end(); ++i)
{
if(i->cp == oPart)
{
i->cp = nPart;
break;
}
}
}
}
//---------------------------------------------------------
// clearClipboardAndCloneList
//---------------------------------------------------------
void clearClipboardAndCloneList()
{
//QApplication::clipboard()->clear(QClipboard::Clipboard);
cloneList.clear();
}
*/
//---------------------------------------------------------
// NKey::write
//---------------------------------------------------------
void NKey::write(int level, Xml& xml) const
{
xml.intTag(level, "key", val);
}
//---------------------------------------------------------
// NKey::read
//---------------------------------------------------------
void NKey::read(Xml& xml)
{
for (;;) {
Xml::Token token = xml.parse();
switch (token) {
case Xml::Error:
case Xml::End:
return;
case Xml::Text:
val = xml.s1().toInt();
break;
case Xml::TagEnd:
if (xml.s1() == "key")
return;
default:
break;
}
}
}
//---------------------------------------------------------
// Scale::write
//---------------------------------------------------------
void Scale::write(int level, Xml& xml) const
{
xml.intTag(level, "scale", val);
}
//---------------------------------------------------------
// Scale::read
//---------------------------------------------------------
void Scale::read(Xml& xml)
{
for (;;) {
Xml::Token token = xml.parse();
switch (token) {
case Xml::Error:
case Xml::End:
return;
case Xml::Text:
val = xml.s1().toInt();
break;
case Xml::TagEnd:
if (xml.s1() == "scale")
return;
default:
break;
}
}
}
//---------------------------------------------------------
// readXmlPart
//---------------------------------------------------------
Part* readXmlPart(Xml& xml, Track* track, bool doClone, bool toTrack)
{
int id = -1;
Part* npart = 0;
uuid_t uuid;
uuid_clear(uuid);
bool uuidvalid = false;
bool clone = true;
bool wave = false;
bool isclone = false;
for (;;) {
Xml::Token token = xml.parse();
const QString& tag = xml.s1();
switch (token) {
case Xml::Error:
case Xml::End:
return npart;
case Xml::TagStart:
// If the part has not been created yet...
if(!npart)
{
// If an id was found...
if(id != -1)
{
for(iClone i = cloneList.begin(); i != cloneList.end(); ++i)
{
// Is a matching part found in the clone list?
if(i->id == id)
{
// If it's a regular paste (not paste clone), and the original part is
// not a clone, defer so that a new copy is created in TagStart above.
//if(!doClone && i->cp->cevents()->arefCount() <= 1)
//if(!doClone && !isclone)
// break;
// This makes a clone, chains the part, and increases ref counts.
npart = track->newPart((Part*)i->cp, true);
break;
}
}
}
else
// If a uuid was found...
if(uuidvalid)
{
for(iClone i = cloneList.begin(); i != cloneList.end(); ++i)
{
// Is a matching part found in the clone list?
if(uuid_compare(uuid, i->uuid) == 0)
{
Track* cpt = i->cp->track();
// If we want to paste to the given track...
if(toTrack)
{
// If the given track type is not the same as the part's
// original track type, we can't continue. Just return.
if(!track || cpt->type() != track->type())
{
xml.skip("part");
return 0;
}
}
else
// ...else we want to paste to the part's original track.
{
// Make sure the track exists (has not been deleted).
if((cpt->isMidiTrack() && song->midis()->find(cpt) != song->midis()->end()) ||
(cpt->type() == Track::WAVE && song->waves()->find(cpt) != song->waves()->end()))
track = cpt;
else
// Track was not found. Try pasting to the given track, as above...
{
if(!track || cpt->type() != track->type())
{
// No luck. Just return.
xml.skip("part");
return 0;
}
}
}
// If it's a regular paste (not paste clone), and the original part is
// not a clone, defer so that a new copy is created in TagStart above.
//if(!doClone && i->cp->cevents()->arefCount() <= 1)
if(!doClone && !isclone)
break;
// This makes a clone, chains the part, and increases ref counts.
npart = track->newPart((Part*)i->cp, true);
break;
}
}
}
// If the part still has not been created yet...
if(!npart)
{
// A clone was not created from any matching part. Create a non-clone part now.
if(!track)
{
xml.skip("part");
return 0;
}
// If we're pasting to selected track and the 'wave'
// variable is valid, check for mismatch...
if(toTrack && uuidvalid)
{
// If both the part and track are not midi or wave...
if((wave && track->isMidiTrack()) ||
(!wave && track->type() == Track::WAVE))
{
xml.skip("part");
return 0;
}
}
if (track->isMidiTrack())
npart = new MidiPart((MidiTrack*)track);
else if (track->type() == Track::WAVE)
npart = new WavePart((WaveTrack*)track);
else
{
xml.skip("part");
return 0;
}
// Signify a new non-clone part was created.
// Even if the original part was itself a clone, clear this because the
// attribute section did not create a clone from any matching part.
clone = false;
// If an id or uuid was found, add the part to the clone list
// so that subsequent parts can look it up and clone from it...
if(id != -1)
{
ClonePart ncp(npart, id);
cloneList.push_back(ncp);
}
else
if(uuidvalid)
{
ClonePart ncp(npart);
// New ClonePart creates its own uuid, but we need to replace it.
uuid_copy(ncp.uuid, uuid);
cloneList.push_back(ncp);
}
}
}
if (tag == "name")
npart->setName(xml.parse1());
else if (tag == "poslen") {
((PosLen*)npart)->read(xml, "poslen");
}
else if (tag == "pos") {
Pos pos;
pos.read(xml, "pos"); // obsolete
npart->setTick(pos.tick());
}
else if (tag == "len") {
Pos len;
len.read(xml, "len"); // obsolete
npart->setLenTick(len.tick());
}
else if (tag == "selected")
npart->setSelected(xml.parseInt());
else if (tag == "color")
npart->setColorIndex(xml.parseInt());
else if (tag == "mute")
npart->setMute(xml.parseInt());
else if (tag == "event")
{
// If a new non-clone part was created, accept the events...
if(!clone)
{
EventType type = Wave;
if(track->isMidiTrack())
type = Note;
Event e(type);
e.read(xml);
// stored tickpos for event has absolute value. However internally
// tickpos is relative to start of part, we substract tick().
// TODO: better handling for wave event
e.move( -npart->tick() );
int tick = e.tick();
// Do not discard events belonging to clone parts,
// at least not yet. A later clone might have a longer,
// fully accommodating part length!
//if ((tick < 0) || (tick >= (int) lenTick())) {
//if ((tick < 0) || ( id == -1 && !clone && (tick >= (int)lenTick()) ))
// No way to tell at the moment whether there will be clones referencing this...
// No choice but to accept all events past 0.
if(tick < 0)
{
//printf("readClone: warning: event not in part: %d - %d -%d, discarded\n",
printf("readClone: warning: event at tick:%d not in part:%s, discarded\n",
tick, npart->name().latin1());
}
else
{
npart->events()->add(e);
}
}
else
// ...Otherwise a clone was created, so we don't need the events.
xml.skip(tag);
}
else
xml.unknown("readXmlPart");
break;
case Xml::Attribut:
if (tag == "type")
{
if(xml.s2() == "wave")
wave = true;
}
else if (tag == "cloneId")
{
id = xml.s2().toInt();
//if(id != -1)
//{
// for(iClone i = cloneList.begin(); i != cloneList.end(); ++i)
// {
// Is a matching part found in the clone list?
// if(i->id == id)
// {
// If it's a regular paste (not paste clone), and the original part is
// not a clone, defer so that a new copy is created in TagStart above.
//if(!doClone && i->cp->cevents()->arefCount() <= 1)
//if(!doClone && !isclone)
// break;
// This makes a clone, chains the part, and increases ref counts.
// npart = track->newPart((Part*)i->cp, true);
// break;
// }
// }
//}
}
else if (tag == "uuid")
{
uuid_parse(xml.s2().latin1(), uuid);
if(!uuid_is_null(uuid))
{
uuidvalid = true;
/*
for(iClone i = cloneList.begin(); i != cloneList.end(); ++i)
{
// Is a matching part found in the clone list?
if(uuid_compare(uuid, i->uuid) == 0)
{
Track* cpt = i->cp->track();
// If we want to paste to the given track...
if(toTrack)
{
// If the given track type is not the same as the part's
// original track type, we can't continue. Just return.
if(!track || cpt->type() != track->type())
{
xml.skip("part");
return 0;
}
}
else
// ...else we want to paste to the part's original track.
{
// Make sure the track exists (has not been deleted).
if((cpt->isMidiTrack() && song->midis()->find(cpt) != song->midis()->end()) ||
(cpt->type() == Track::WAVE && song->waves()->find(cpt) != song->waves()->end()))
track = cpt;
else
// Track was not found. Try pasting to the given track, as above...
{
if(!track || cpt->type() != track->type())
{
// No luck. Just return.
xml.skip("part");
return 0;
}
}
}
// If it's a regular paste (not paste clone), and the original part is
// not a clone, defer so that a new copy is created in TagStart above.
//if(!doClone && i->cp->cevents()->arefCount() <= 1)
if(!doClone && !isclone)
break;
// This makes a clone, chains the part, and increases ref counts.
npart = track->newPart((Part*)i->cp, true);
break;
}
}
*/
}
}
else if(tag == "isclone")
isclone = xml.s2().toInt();
break;
case Xml::TagEnd:
if (tag == "part")
return npart;
default:
break;
}
}
return npart;
}
//---------------------------------------------------------
// Part::write
// If isCopy is true, write the xml differently so that
// we can have 'Paste Clone' feature.
//---------------------------------------------------------
//void Part::write(int level, Xml& xml) const
void Part::write(int level, Xml& xml, bool isCopy, bool forceWavePaths) const
{
const EventList* el = cevents();
int id = -1;
uuid_t uuid;
uuid_clear(uuid);
bool dumpEvents = true;
bool wave = _track->type() == Track::WAVE;
if(isCopy)
{
//for(iClone i = copyCloneList.begin(); i != copyCloneList.end(); ++i)
for(iClone i = cloneList.begin(); i != cloneList.end(); ++i)
{
//if(i->el == el) {
if(i->cp->cevents() == el)
{
//id = i->id;
uuid_copy(uuid, i->uuid);
dumpEvents = false;
break;
}
}
//if(id == -1)
if(uuid_is_null(uuid))
{
//id = copyCloneList.size();
//id = cloneList.size();
//ClonePart cp(el, id);
//ClonePart cp(this, id);
ClonePart cp(this);
uuid_copy(uuid, cp.uuid);
//copyCloneList.push_back(cp);
cloneList.push_back(cp);
}
}
else
{
if (el->arefCount() > 1)
{
for (iClone i = cloneList.begin(); i != cloneList.end(); ++i)
{
//if (i->el == el) {
if (i->cp->cevents() == el)
{
id = i->id;
//uuid_copy(id, i->uid);
dumpEvents = false;
break;
}
}
if (id == -1)
//if(uuid_is_null(id))
{
id = cloneList.size();
//ClonePart cp(el, id);
ClonePart cp(this, id);
//ClonePart cp(this);
cloneList.push_back(cp);
}
}
}
// Special markers if this is a copy operation and the
// part is a clone.
if(isCopy)
{
char sid[40]; // uuid string is 36 chars. Try 40 for good luck.
sid[0] = 0;
uuid_unparse_lower(uuid, sid);
//if(midi)
// xml.nput(level, "<midipart uuid=\"%s\"", sid);
//else
if(wave)
xml.nput(level, "<part type=\"wave\" uuid=\"%s\"", sid);
else
xml.nput(level, "<part uuid=\"%s\"", sid);
if(el->arefCount() > 1)
xml.nput(" isclone=\"1\"");
xml.put(">");
level++;
}
else
if (id != -1)
//if(!uuid_is_null(id))
{
xml.tag(level++, "part cloneId=\"%d\"", id);
//char sid[40]; // uuid string is 36 chars. Try 40 for good luck.
//sid[0] = 0;
//uuid_unparse_lower(id, sid);
//xml.tag(level++, "part cloneId=\"%s\"", sid);
}
else
xml.tag(level++, "part");
xml.strTag(level, "name", _name);
// PosLen poslen(*this);
// int tickpos = tick();
// poslen.setTick(tickpos);
PosLen::write(level, xml, "poslen");
xml.intTag(level, "selected", _selected);
xml.intTag(level, "color", _colorIndex);
if (_mute)
xml.intTag(level, "mute", _mute);
if (dumpEvents) {
for (ciEvent e = el->begin(); e != el->end(); ++e)
//e->second.write(level, xml, *this);
e->second.write(level, xml, *this, forceWavePaths);
}
xml.etag(level, "part");
}
/*
//---------------------------------------------------------
// Part::read
//---------------------------------------------------------
void Part::read(Xml& xml, int, bool toTrack) // int newPartOffset
{
int id = -1;
bool containsEvents = false;
uuid_t uuid;
uuid_clear(uuid);
bool uuidvalid = false;
bool clone = false;
for (;;) {
Xml::Token token = xml.parse();
const QString& tag = xml.s1();
switch (token) {
case Xml::Error:
case Xml::End:
return;
case Xml::TagStart:
if (tag == "name")
_name = xml.parse1();
else if (tag == "poslen") {
PosLen::read(xml, "poslen");
}
else if (tag == "pos") {
Pos pos;
pos.read(xml, "pos"); // obsolete
setTick(pos.tick());
}
else if (tag == "len") {
Pos len;
len.read(xml, "len"); // obsolete
setLenTick(len.tick());
}
else if (tag == "selected")
_selected = xml.parseInt();
else if (tag == "color")
_colorIndex = xml.parseInt();
else if (tag == "mute")
_mute = xml.parseInt();
else if (tag == "event") {
containsEvents = true;
EventType type = Wave;
if (_track->isMidiTrack())
type = Note;
Event e(type);
e.read(xml);
// stored tickpos for event has absolute value. However internally
// tickpos is relative to start of part, we substract tick().
// TODO: better handling for wave event
e.move(-tick());
int tick = e.tick();
// Changed by T356. Do not discard events belonging to clone parts,
// at least not yet. A later clone might have a longer,
// fully accommodating part length!
//if ((tick < 0) || (tick >= (int) lenTick())) {
if ((tick < 0) || ( id == -1 && !clone && (tick >= (int)lenTick()) ))
{
//printf("Part::read: warning: event not in part: %d - %d -%d, discarded\n",
printf("Part::read: warning: event at tick:%d not in part:%s, discarded\n",
tick, name().latin1());
}
else {
_events->add(e);
*/
/*
// TODO: This should NOT be done here since the event list
// might be deleted below. Since after reading a part it
// likely (always?) that (msg)AddPart() or (msg)ChangePart()
// will be called (must check if they're ever called BEFORE
// Part::read), then those routines will take care of it,
// they are already coded to do so.
// Note the redundancy of doing it here AND (msg)Add/ChangePart !
// Try to eliminate this section altogether by verifying that
// (msg)Add/ChangePart (or one of the other routines which add port
// controller values) is always called after Part::read...
if (e.type() == Controller) {
MidiTrack* mt = (MidiTrack*)_track;
int channel = mt->outChannel();
MidiPort* mp = &midiPorts[mt->outPort()];
// tick is relative to part, controller needs an absolute value hence
// part offset is added. If newPartOffset was given we use that instead of
// the recorded offset!
if (!newPartOffset)
newPartOffset=this->tick();
int ctl = e.dataA();
if(mt->type() == Track::DRUM)
{
// Is it a drum controller event, according to the track port's instrument?
MidiController* mc = mp->drumController(ctl);
if(mc)
{
int note = ctl & 0x7f;
ctl &= ~0xff;
channel = drumMap[note].channel;
mp = &midiPorts[drumMap[note].port];
ctl |= drumMap[note].anote;
}
}
// Removed by T356
// check if controller exists
//if (mp->hwCtrlState(channel, e.dataA()) == CTRL_VAL_UNKNOWN) {
// mp->addManagedController(channel, e.dataA());
// }
// Changed by T356
// add controller value
//mp->setCtrl(channel, tick+newPartOffset, e.dataA(), e.dataB());
mp->setControllerVal(channel, tick+newPartOffset, ctl, e.dataB(), this);
}
*/
/*
}
}
else
xml.unknown("Part::read");
break;
case Xml::Attribut:
if (tag == "cloneId")
id = xml.s2().toInt();
else if (tag == "uuid")
{
uuid_parse(xml.s2().latin1(), uuid);
if(!uuid_is_null(uuid))
uuidvalid = true;
}
else if (tag == "isclone")
clone = xml.s2().toInt();
break;
case Xml::TagEnd:
if (tag == "part")
{
*/
/*
if (id != -1)
{
// clone part
if (containsEvents) {
// add to cloneList:
//ClonePart cp(_events, id);
ClonePart cp(this, id);
cloneList.push_back(cp);
}
else {
// replace event list with clone event
// list
for (iClone i = cloneList.begin();
i != cloneList.end(); ++i) {
if (i->id == id) {
delete _events;
//_events = (EventList*)(i->el);
_events = (EventList*)(i->cp->cevents());
_events->incRef(1);
_events->incARef(1);
//i->cp->chainClone(this);
chainClone((Part*)i->cp, this);
break;
}
}
}
*/
/*
if(id != -1)
{
// See if the part exists in the clone list.
// The clone list is also the copy/paste clone list.
// Care must be taken to ensure the list is ALWAYS EMPTY
// before loading or dropping parts INTO muse, because the
// current song parts are NOT the same as when the imported parts
// were created, (even if they were created from the current session,
// we should NOT look them up). Always back up the list, clear it,
// read part(s), then restore the list so that paste works after.
Part* cp = 0;
for(iClone i = cloneList.begin(); i != cloneList.end(); ++i)
{
if(i->id == id)
{
cp = (Part*)i->cp;
break;
}
}
// Was a matching part found in the clone list?
if(cp)
{
// Make this part a clone of that part. Use its event list...
delete _events;
_events = (EventList*)(cp->cevents());
_events->incRef(1);
_events->incARef(1);
chainClone(cp, this);
}
else
{
// No matching part to clone was found in the clone list.
// Does the part contain some events?
//if(containsEvents)
{
// Add the part to the clone list so that subsequent parts
// can look it up and clone from it...
ClonePart ncp(this, id);
cloneList.push_back(ncp);
}
// Otherwise this part has no matching part in the clone list
// and no events of its own. Nothing left to do, we now have
// a blank part with the original offset, colour etc.
}
}
else
// If a uuid was found, do the same as above. Using uuids
// allows foolproof rejection of copied parts not found
// in the clone list, particularly when copying parts from
// another instance of muse.
if(uuidvalid)
{
Part* cp = 0;
for(iClone i = cloneList.begin(); i != cloneList.end(); ++i)
{
if(uuid_compare(uuid, i->uuid) == 0)
{
cp = (Part*)i->cp;
break;
}
}
// If a matching part was found, and we want to paste to the original track...
if(cp && !toTrack)
{
// Make sure the track exists (has not been deleted).
if((cp->track()->isMidiTrack() && song->midis()->find(cp->track()) != song->midis()->end()) ||
(cp->track()->type() == Track::WAVE && song->waves()->find(cp->track()) != song->waves()->end()))
setTrack(cp->track());
}
// Was a matching part found in the clone list, and was it
// originally a clone part?
if(cp && clone)
{
// Make this part a clone of that part. Use its event list...
delete _events;
_events = (EventList*)(cp->cevents());
_events->incRef(1);
_events->incARef(1);
// Chain the clone.
// Use the slower function which makes sure it chains to a part
// within a valid (non-deleted) track.
//chainClone(cp, this);
chainClone(this);
}
else
{
// No matching part to clone was found in the clone list.
// Does the part contain some events?
//if(containsEvents)
{
// Add the part to the clone list so that subsequent parts
// can look it up and clone from it...
ClonePart ncp(this);
// New ClonePart creates its own uuid, but we need to replace it.
uuid_copy(ncp.uuid, uuid);
cloneList.push_back(ncp);
}
}
}
return;
}
default:
break;
}
}
}
*/
//---------------------------------------------------------
// writeFont
//---------------------------------------------------------
void Song::writeFont(int level, Xml& xml, const char* name,
const QFont& font) const
{
xml.nput(level, "<%s family=\"%s\" size=\"%d\"",
//name, font.family().latin1(), font.pointSize());
name, Xml::xmlString(font.family()).latin1(), font.pointSize());
if (font.weight() != QFont::Normal)
xml.nput(" weight=\"%d\"", font.weight());
if (font.italic())
xml.nput(" italic=\"1\"");
xml.nput(" />\n");
}
//---------------------------------------------------------
// readFont
//---------------------------------------------------------
QFont Song::readFont(Xml& xml, const char* name)
{
QFont f;
for (;;) {
Xml::Token token = xml.parse();
switch (token) {
case Xml::Error:
case Xml::End:
return f;
case Xml::TagStart:
xml.unknown("readFont");
break;
case Xml::Attribut:
if (xml.s1() == "family")
f.setFamily(xml.s2());
else if (xml.s1() == "size")
f.setPointSize(xml.s2().toInt());
else if (xml.s1() == "weight")
f.setWeight(xml.s2().toInt());
else if (xml.s1() == "italic")
f.setItalic(xml.s2().toInt());
break;
case Xml::TagEnd:
if (xml.s1() == name)
return f;
default:
break;
}
}
return f;
}
//---------------------------------------------------------
// readPart
//---------------------------------------------------------
Part* MusE::readPart(Xml& xml)
{
Part* part = 0;
for (;;) {
Xml::Token token = xml.parse();
const QString& tag = xml.s1();
switch (token) {
case Xml::Error:
case Xml::End:
return part;
case Xml::Text:
{
int trackIdx, partIdx;
sscanf(tag.latin1(), "%d:%d", &trackIdx, &partIdx);
Track* track = song->tracks()->index(trackIdx);
if (track)
part = track->parts()->find(partIdx);
}
break;
case Xml::TagStart:
xml.unknown("readPart");
break;
case Xml::TagEnd:
if (tag == "part")
return part;
default:
break;
}
}
}
//---------------------------------------------------------
// readToplevels
//---------------------------------------------------------
void MusE::readToplevels(Xml& xml)
{
PartList* pl = new PartList;
for (;;) {
Xml::Token token = xml.parse();
const QString& tag = xml.s1();
switch (token) {
case Xml::Error:
case Xml::End:
return;
case Xml::TagStart:
if (tag == "part") {
Part* part = readPart(xml);
if (part)
pl->add(part);
}
else if (tag == "pianoroll") {
// p3.3.34
// Do not open if there are no parts.
// Had bogus '-1' part index for list edit in med file,
// causing list edit to segfault on song load.
// Somehow that -1 was put there on write, because the
// current part didn't exist anymore, so no index number
// could be found for it on write. Watching... may be fixed.
// But for now be safe for all the top levels...
if(!pl->empty())
{
startPianoroll(pl);
toplevels.back().cobject()->readStatus(xml);
pl = new PartList;
}
}
else if (tag == "drumedit") {
if(!pl->empty())
{
startDrumEditor(pl);
toplevels.back().cobject()->readStatus(xml);
pl = new PartList;
}
}
else if (tag == "listeditor") {
if(!pl->empty())
{
startListEditor(pl);
toplevels.back().cobject()->readStatus(xml);
pl = new PartList;
}
}
else if (tag == "master") {
startMasterEditor();
toplevels.back().cobject()->readStatus(xml);
}
else if (tag == "lmaster") {
startLMasterEditor();
toplevels.back().cobject()->readStatus(xml);
}
else if (tag == "marker") {
showMarker(true);
toplevels.back().cobject()->readStatus(xml);
}
else if (tag == "waveedit") {
if(!pl->empty())
{
startWaveEditor(pl);
toplevels.back().cobject()->readStatus(xml);
pl = new PartList;
}
}
else if (tag == "cliplist") {
startClipList();
toplevels.back().cobject()->readStatus(xml);
}
else
xml.unknown("MusE");
break;
case Xml::Attribut:
break;
case Xml::TagEnd:
if (tag == "toplevels") {
delete pl;
return;
}
default:
break;
}
}
}
//---------------------------------------------------------
// readCtrl
//---------------------------------------------------------
void MusE::readCtrl(Xml&, int /*prt*/, int /*channel*/)
{
#if 0
ChannelState* iState = midiPorts[prt].iState(channel);
int idx = 0;
int val = -1;
for (;;) {
Xml::Token token = xml.parse();
switch (token) {
case Xml::Error:
case Xml::End:
return;
case Xml::TagStart:
xml.unknown("readCtrl");
break;
case Xml::Attribut:
if (xml.s1() == "idx")
idx = xml.s2().toInt();
else if (xml.s1() == "val")
val = xml.s2().toInt();
break;
case Xml::TagEnd:
if (xml.s1() == "ctrl") {
iState->controller[idx] = val;
// printf("%d %d ctrl %d val %d\n", prt, channel, idx, val);
return;
}
default:
break;
}
}
#endif
}
//---------------------------------------------------------
// readMidichannel
//---------------------------------------------------------
void MusE::readMidichannel(Xml& xml, int prt)
{
int channel = 0;
// MidiPort* port = &midiPorts[prt];
for (;;) {
Xml::Token token = xml.parse();
const QString& tag = xml.s1();
switch (token) {
case Xml::Error:
case Xml::End:
return;
case Xml::TagStart:
if (tag == "pitch") {
//TODO port->setCtrl(channel, 0, CTRL_PITCH, xml.parseInt());
}
else if (tag == "program") {
//TODO port->setCtrl(channel, 0, CTRL_PROGRAM, xml.parseInt());
}
else if (tag == "ctrl")
readCtrl(xml, prt, channel);
else {
xml.unknown("readMidichannel");
}
break;
case Xml::Attribut:
if (tag == "ch") {
channel = xml.s2().toInt();
}
break;
case Xml::TagEnd:
if (tag == "midichannel")
return;
default:
break;
}
}
}
//---------------------------------------------------------
// readMidiport
//---------------------------------------------------------
void MusE::readMidiport(Xml& xml)
{
int port = 0;
for (;;) {
Xml::Token token = xml.parse();
const QString& tag = xml.s1();
switch (token) {
case Xml::Error:
case Xml::End:
return;
case Xml::TagStart:
if (tag == "midichannel")
readMidichannel(xml, port);
else {
xml.unknown("readMidiport");
}
break;
case Xml::Attribut:
if (tag == "port") {
port = xml.s2().toInt();
}
break;
case Xml::TagEnd:
if (tag == "midiport") {
return;
}
default:
break;
}
}
}
//---------------------------------------------------------
// readMarker
//---------------------------------------------------------
void Song::readMarker(Xml& xml)
{
Marker m;
m.read(xml);
_markerList->add(m);
}
//---------------------------------------------------------
// read
//---------------------------------------------------------
void Song::read(Xml& xml)
{
cloneList.clear();
for (;;) {
Xml::Token token;
token = xml.parse();
const QString& tag = xml.s1();
switch (token) {
case Xml::Error:
case Xml::End:
return;
case Xml::TagStart:
if (tag == "master")
setMasterFlag(xml.parseInt());
else if (tag == "info")
songInfoStr = xml.parse1();
else if (tag == "loop")
setLoop(xml.parseInt());
else if (tag == "punchin")
setPunchin(xml.parseInt());
else if (tag == "punchout")
setPunchout(xml.parseInt());
else if (tag == "record")
setRecord(xml.parseInt());
else if (tag == "solo")
soloFlag = xml.parseInt();
else if (tag == "type")
_mtype = MType(xml.parseInt());
else if (tag == "recmode")
_recMode = xml.parseInt();
else if (tag == "cycle")
_cycleMode = xml.parseInt();
else if (tag == "click")
setClick(xml.parseInt());
else if (tag == "quantize")
_quantize = xml.parseInt();
else if (tag == "len")
_len = xml.parseInt();
else if (tag == "follow")
_follow = FollowMode(xml.parseInt());
else if (tag == "tempolist") {
tempomap.read(xml);
}
else if (tag == "siglist")
sigmap.read(xml);
else if (tag == "miditrack") {
MidiTrack* track = new MidiTrack();
track->read(xml);
insertTrack0(track, -1);
}
else if (tag == "drumtrack") {
MidiTrack* track = new MidiTrack();
track->setType(Track::DRUM);
track->read(xml);
insertTrack0(track, -1);
}
else if (tag == "wavetrack") {
WaveTrack* track = new WaveTrack();
track->read(xml);
insertTrack0(track,-1);
// Now that the track has been added to the lists in insertTrack2(),
// OSC can find the track and its plugins, and start their native guis if required...
track->showPendingPluginNativeGuis();
}
else if (tag == "AudioInput") {
AudioInput* track = new AudioInput();
track->read(xml);
insertTrack0(track,-1);
track->showPendingPluginNativeGuis();
}
else if (tag == "AudioOutput") {
AudioOutput* track = new AudioOutput();
track->read(xml);
insertTrack0(track,-1);
track->showPendingPluginNativeGuis();
}
else if (tag == "AudioGroup") {
AudioGroup* track = new AudioGroup();
track->read(xml);
insertTrack0(track,-1);
track->showPendingPluginNativeGuis();
}
else if (tag == "AudioAux") {
AudioAux* track = new AudioAux();
track->read(xml);
insertTrack0(track,-1);
track->showPendingPluginNativeGuis();
}
else if (tag == "SynthI") {
SynthI* track = new SynthI();
track->read(xml);
// Done in SynthI::read()
// insertTrack(track,-1);
//track->showPendingPluginNativeGuis();
}
else if (tag == "Route") {
readRoute(xml);
}
else if (tag == "marker")
readMarker(xml);
else if (tag == "globalPitchShift")
_globalPitchShift = xml.parseInt();
else if (tag == "automation")
automation = xml.parseInt();
else if (tag == "cpos") {
int pos = xml.parseInt();
Pos p(pos, true);
setPos(Song::CPOS, p, false, false, false);
}
else if (tag == "lpos") {
int pos = xml.parseInt();
Pos p(pos, true);
setPos(Song::LPOS, p, false, false, false);
}
else if (tag == "rpos") {
int pos = xml.parseInt();
Pos p(pos, true);
setPos(Song::RPOS, p, false, false, false);
}
else if (tag == "drummap")
readDrumMap(xml, false);
else
xml.unknown("Song");
break;
case Xml::Attribut:
break;
case Xml::TagEnd:
if (tag == "song") {
return;
}
default:
break;
}
}
dirty = false;
// Since cloneList is also used for copy/paste operations,
// clear the copy clone list again.
cloneList.clear();
}
//---------------------------------------------------------
// read
// read song
//---------------------------------------------------------
void MusE::read(Xml& xml, bool skipConfig)
{
bool skipmode = true;
for (;;) {
Xml::Token token = xml.parse();
const QString& tag = xml.s1();
switch (token) {
case Xml::Error:
case Xml::End:
return;
case Xml::TagStart:
if (skipmode && tag == "muse")
skipmode = false;
else if (skipmode)
break;
else if (tag == "configuration")
if (skipConfig)
//xml.skip(tag);
readConfiguration(xml,true /* only read sequencer settings */);
else
readConfiguration(xml, false);
else if (tag == "song")
{
song->read(xml);
audio->msgUpdateSoloStates();
}
else if (tag == "midiport")
readMidiport(xml);
else if (tag == "Controller") { // obsolete
MidiController* ctrl = new MidiController;
ctrl->read(xml);
delete ctrl;
}
else if (tag == "mplugin")
readStatusMidiInputTransformPlugin(xml);
else if (tag == "toplevels")
readToplevels(xml);
else
xml.unknown("muse");
break;
case Xml::Attribut:
if (tag == "version") {
int major = xml.s2().section('.', 0, 0).toInt();
int minor = xml.s2().section('.', 1, 1).toInt();
xml.setVersion(major, minor);
}
break;
case Xml::TagEnd:
if (!skipmode && tag == "muse")
return;
default:
break;
}
}
}
//---------------------------------------------------------
// write
//---------------------------------------------------------
void Song::write(int level, Xml& xml) const
{
xml.tag(level++, "song");
xml.strTag(level, "info", songInfoStr);
xml.intTag(level, "automation", automation);
xml.intTag(level, "cpos", song->cpos());
xml.intTag(level, "rpos", song->rpos());
xml.intTag(level, "lpos", song->lpos());
xml.intTag(level, "master", _masterFlag);
xml.intTag(level, "loop", loopFlag);
xml.intTag(level, "punchin", punchinFlag);
xml.intTag(level, "punchout", punchoutFlag);
xml.intTag(level, "record", recordFlag);
xml.intTag(level, "solo", soloFlag);
xml.intTag(level, "type", _mtype);
xml.intTag(level, "recmode", _recMode);
xml.intTag(level, "cycle", _cycleMode);
xml.intTag(level, "click", _click);
xml.intTag(level, "quantize", _quantize);
xml.intTag(level, "len", _len);
xml.intTag(level, "follow", _follow);
if (_globalPitchShift)
xml.intTag(level, "globalPitchShift", _globalPitchShift);
// Make a backup of the current clone list, to retain any 'copy' items,
// so that pasting works properly after.
CloneList copyCloneList = cloneList;
cloneList.clear();
// write tracks
for (ciTrack i = _tracks.begin(); i != _tracks.end(); ++i)
(*i)->write(level, xml);
// write routing
for (ciTrack i = _tracks.begin(); i != _tracks.end(); ++i) {
// p3.3.38 Changed
//if ((*i)->isMidiTrack())
// continue;
//WaveTrack* track = (WaveTrack*)(*i);
//track->writeRouting(level, xml);
(*i)->writeRouting(level, xml);
}
// Write midi device routing.
for (iMidiDevice i = midiDevices.begin(); i != midiDevices.end(); ++i) {
//MidiJackDevice* mjd = dynamic_cast<MidiJackDevice*>(*i);
//if (!mjd)
// continue;
//mjd->writeRouting(level, xml);
(*i)->writeRouting(level, xml);
}
// p3.3.49 Write midi port routing.
for (int i = 0; i < MIDI_PORTS; ++i) {
midiPorts[i].writeRouting(level, xml);
}
tempomap.write(level, xml);
sigmap.write(level, xml);
_markerList->write(level, xml);
writeDrumMap(level, xml, false);
xml.tag(level, "/song");
// Restore backup of the clone list, to retain any 'copy' items,
// so that pasting works properly after.
cloneList.clear();
cloneList = copyCloneList;
}
//---------------------------------------------------------
// write
// write song
//---------------------------------------------------------
void MusE::write(Xml& xml) const
{
xml.header();
int level = 0;
xml.tag(level++, "muse version=\"2.0\"");
writeConfiguration(level, xml);
writeStatusMidiInputTransformPlugins(level, xml);
song->write(level, xml);
if (!toplevels.empty()) {
xml.tag(level++, "toplevels");
for (ciToplevel i = toplevels.begin(); i != toplevels.end(); ++i) {
if (i->cobject()->isVisible())
i->cobject()->writeStatus(level, xml);
}
xml.tag(level--, "/toplevels");
}
xml.tag(level, "/muse");
}
|