-
Notifications
You must be signed in to change notification settings - Fork 2.5k
/
musedata.cpp
756 lines (706 loc) · 25 KB
/
musedata.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
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
//=============================================================================
// MuseScore
// Linux Music Score Editor
//
// Copyright (C) 2007 Werner Schweer and others
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License version 2.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
//=============================================================================
#include "musedata.h"
#include "libmscore/score.h"
#include "libmscore/part.h"
#include "libmscore/staff.h"
#include "libmscore/barline.h"
#include "libmscore/clef.h"
#include "libmscore/key.h"
#include "libmscore/note.h"
#include "libmscore/chord.h"
#include "libmscore/rest.h"
#include "libmscore/text.h"
#include "libmscore/bracket.h"
#include "libmscore/tuplet.h"
#include "libmscore/slur.h"
#include "libmscore/dynamic.h"
#include "libmscore/lyrics.h"
#include "libmscore/articulation.h"
#include "libmscore/sig.h"
#include "libmscore/measure.h"
#include "libmscore/timesig.h"
#include "libmscore/segment.h"
#include "libmscore/sym.h"
namespace Ms {
//---------------------------------------------------------
// musicalAttribute
//---------------------------------------------------------
void MuseData::musicalAttribute(QString s, Part* part)
{
#if (QT_VERSION >= QT_VERSION_CHECK(5, 14, 0))
QStringList al = s.mid(3).split(" ", Qt::SkipEmptyParts);
#else
QStringList al = s.mid(3).split(" ", QString::SkipEmptyParts);
#endif
foreach(QString item, al) {
if (item.startsWith("K:")) {
int key = item.mid(2).toInt();
KeySigEvent ke;
ke.setKey(Key(key));
for (Staff* staff : *(part->staves()))
staff->setKey(curTick, ke);
}
else if (item.startsWith("Q:")) {
_division = item.mid(2).toInt();
}
else if (item.startsWith("T:")) {
QStringList tl = item.mid(2).split("/");
if (tl.size() != 2) {
qDebug("bad time sig <%s>", qPrintable(item));
continue;
}
int z = tl[0].toInt();
int n = tl[1].toInt();
if ((z > 0) && (n > 0)) {
//TODO score->sigmap()->add(curTick, Fraction(z, n));
TimeSig* ts = new TimeSig(score);
Staff* staff = part->staff(0);
ts->setTrack(staff->idx() * VOICES);
Measure* mes = score->tick2measure(curTick);
Segment* seg = mes->getSegment(SegmentType::TimeSig, curTick);
seg->add(ts);
}
}
else if (item.startsWith("X:"))
;
else if (item[0] == 'C') {
int staffIdx = 1;
// int col = 2;
if (item[1].isDigit()) {
staffIdx = item.mid(1,1).toInt();
// col = 3;
}
staffIdx -= 1;
/* int clef = item.mid(col).toInt();
ClefType mscoreClef = ClefType::G;
switch(clef) {
case 4: mscoreClef = ClefType::G; break;
case 22: mscoreClef = ClefType::F; break;
case 13: mscoreClef = ClefType::C3; break;
case 14: mscoreClef = ClefType::C2; break;
case 15: mscoreClef = ClefType::C1; break;
default:
qDebug("unknown clef %d", clef);
break;
}
*/
// Staff* staff = part->staff(staffIdx);
// staff->setClef(curTick, mscoreClef);
}
else
qDebug("unknown $key <%s>", qPrintable(item));
}
}
//---------------------------------------------------------
// readChord
//---------------------------------------------------------
void MuseData::readChord(Part*, const QString& s)
{
// a b c d e f g
static int table[7] = { 9, 11, 0, 2, 4, 5, 7 };
int step = s[1].toLatin1() - 'A';
int alter = 0;
int octave = 0;
for (int i = 2; i < 4; ++i) {
if (s[i] == '#')
alter += 1;
else if (s[i] == 'f')
alter -= 1;
else if (s[i].isDigit()) {
octave = s.mid(i,1).toInt();
break;
}
}
int staffIdx = 0;
if (s.size() >= 24) {
if (s[23].isDigit())
staffIdx = s.mid(23,1).toInt() - 1;
}
int pitch = table[step] + alter + (octave + 1) * 12;
if (pitch < 0)
pitch = 0;
if (pitch > 127)
pitch = 127;
Chord* chord = (Chord*)chordRest;
Note* note = new Note(score);
note->setPitch(pitch);
note->setTpcFromPitch();
note->setTrack(staffIdx * VOICES + voice);
chord->add(note);
}
//---------------------------------------------------------
// openSlur
//---------------------------------------------------------
void MuseData::openSlur(int idx, const Fraction& tick, Staff* staff, int voc)
{
int staffIdx = staff->idx();
if (slur[idx]) {
qDebug("%06d: slur %d already open", tick.ticks(), idx+1);
return;
}
slur[idx] = new Slur(score);
slur[idx]->setTick(tick);
slur[idx]->setTrack(staffIdx * VOICES + voc);
score->addElement(slur[idx]);
}
//---------------------------------------------------------
// closeSlur
//---------------------------------------------------------
void MuseData::closeSlur(int idx, const Fraction& tick, Staff* staff, int voc)
{
int staffIdx = staff->idx();
if (slur[idx]) {
slur[idx]->setTick2(tick);
slur[idx]->setTrack2(staffIdx * VOICES + voc);
slur[idx] = 0;
}
else
qDebug("%06d: slur %d not open", tick.ticks(), idx+1);
}
//---------------------------------------------------------
// readNote
//---------------------------------------------------------
void MuseData::readNote(Part* part, const QString& s)
{
// a b c d e f g
static int table[7] = { 9, 11, 0, 2, 4, 5, 7 };
int step = s[0].toLatin1() - 'A';
int alter = 0;
int octave = 0;
for (int i = 1; i < 3; ++i) {
if (s[i] == '#')
alter += 1;
else if (s[i] == 'f')
alter -= 1;
else if (s[i].isDigit()) {
octave = s.mid(i,1).toInt();
break;
}
}
Direction dir = Direction::AUTO;
if (s.size() >= 23) {
if (s[22] == 'u')
dir = Direction::UP;
else if (s[22] == 'd')
dir = Direction::DOWN;
}
int staffIdx = 0;
if (s.size() >= 24) {
if (s[23].isDigit())
staffIdx = s.mid(23,1).toInt() - 1;
}
Staff* staff = part->staff(staffIdx);
int gstaff = staff->idx();
int pitch = table[step] + alter + (octave + 1) * 12;
if (pitch < 0)
pitch = 0;
if (pitch > 127)
pitch = 127;
Fraction ticks = Fraction::fromTicks((s.mid(5, 3).toInt() * MScore::division + _division/2) / _division);
Fraction tick = curTick;
curTick += ticks;
Tuplet* tuplet = 0;
if (s.size() >= 22) {
int a = 1;
int b = 1;
if (s[19] != ' ') {
a = s[19].toLatin1() - '0';
if (a == 3 && s[20] != ':')
b = 2;
else {
b = s[21].toLatin1() - '0';
}
}
if (a == 3 && b == 2) { // triplet
if (chordRest && chordRest->tuplet() && ntuplet) {
tuplet = chordRest->tuplet();
}
else {
tuplet = new Tuplet(score);
tuplet->setTrack(gstaff * VOICES);
tuplet->setTick(tick);
ntuplet = a;
tuplet->setRatio(Fraction(a, b));
measure->add(tuplet);
}
}
else if (a == 1 && b == 1)
;
else
qDebug("unsupported tuple %d/%d", a, b);
}
Chord* chord = new Chord(score);
chordRest = chord;
chord->setTrack(gstaff * VOICES);
chord->setStemDirection(dir);
if (tuplet) {
chord->setTuplet(tuplet);
tuplet->add(chord);
--ntuplet;
}
TDuration d;
d.setVal(ticks.ticks());
chord->setDurationType(d);
Segment* segment = measure->getSegment(SegmentType::ChordRest, tick);
voice = 0;
for (; voice < VOICES; ++voice) {
Element* e = segment->element(gstaff * VOICES + voice);
if (e == 0) {
chord->setTrack(gstaff * VOICES + voice);
segment->add(chord);
break;
}
}
if (voice == VOICES) {
qDebug("cannot allocate voice");
delete chord;
return;
}
Note* note = new Note(score);
note->setPitch(pitch);
note->setTpcFromPitch();
note->setTrack(gstaff * VOICES + voice);
chord->add(note);
QString dynamics;
QString an = s.mid(31, 11);
for (int i = 0; i < an.size(); ++i) {
if (an[i] == '(')
openSlur(0, tick, staff, voice);
else if (an[i] == ')')
closeSlur(0, tick, staff, voice);
else if (an[i] == '[')
openSlur(1, tick, staff, voice);
else if (an[i] == ']')
closeSlur(1, tick, staff, voice);
else if (an[i] == '{')
openSlur(2, tick, staff, voice);
else if (an[i] == '}')
closeSlur(2, tick, staff, voice);
else if (an[i] == 'z')
openSlur(3, tick, staff, voice);
else if (an[i] == 'x')
closeSlur(3, tick, staff, voice);
else if (an[i] == '.') {
Articulation* atr = new Articulation(score);
atr->setSymId(SymId::articStaccatoAbove);
chord->add(atr);
}
else if (an[i] == '_') {
Articulation* atr = new Articulation(score);
atr->setSymId(SymId::articTenutoAbove);
chord->add(atr);
}
else if (an[i] == 'v') {
Articulation* atr = new Articulation(score);
atr->setSymId(SymId::stringsUpBow);
chord->add(atr);
}
else if (an[i] == 'n') {
Articulation* atr = new Articulation(score);
atr->setSymId(SymId::stringsDownBow);
chord->add(atr);
}
else if (an[i] == 't') {
Articulation* atr = new Articulation(score);
atr->setSymId(SymId::ornamentTrill);
chord->add(atr);
}
else if (an[i] == 'F') {
Articulation* atr = new Articulation(score);
atr->setUp(true);
atr->setSymId(SymId::fermataAbove);
chord->add(atr);
}
else if (an[i] == 'E') {
Articulation* atr = new Articulation(score);
atr->setUp(false);
atr->setSymId(SymId::fermataBelow);
chord->add(atr);
}
else if (an[i] == 'O') {
// Articulation* atr = new Articulation(score);
// atr->setArticulationType(ArticulationType::Downbow);
// chord->add(atr);
qDebug("%06d: open string '%c' not implemented", tick.ticks(), an[i].toLatin1());
}
else if (an[i] == '&') {
// skip editorial level
if (i <= an.size() && an[i+1].isDigit())
++i;
}
else if (an[i] == 'p')
dynamics += "p";
else if (an[i] == 'm')
dynamics += "m";
else if (an[i] == 'f')
dynamics += "f";
else if (an[i] == '-') // tie
;
else if (an[i] == '*') // start tuplet
;
else if (an[i] == '!') // stop tuplet
;
else if (an[i] == '+') // cautionary accidental
;
else if (an[i] == 'X') // ???
;
else if (an[i] == ' ')
;
else {
qDebug("%06d: notation '%c' not implemented", tick.ticks(), an[i].toLatin1());
}
}
if (!dynamics.isEmpty()) {
Dynamic* dyn = new Dynamic(score);
dyn->setDynamicType(dynamics);
dyn->setTrack(gstaff * VOICES);
Segment* seg = measure->getSegment(SegmentType::ChordRest, tick);
seg->add(dyn);
}
QString txt = s.mid(43, 36);
if (!txt.isEmpty()) {
QStringList sl = txt.split("|");
int no = 0;
foreach(QString w, sl) {
w = diacritical(w);
Lyrics* l = new Lyrics(score);
l->setPlainText(w);
l->setNo(no++);
l->setTrack(gstaff * VOICES);
Segment* seg = measure->tick2segment(tick);
seg->add(l);
}
}
}
//---------------------------------------------------------
// diacritical
// TODO: not complete
//---------------------------------------------------------
QString MuseData::diacritical(QString s)
{
struct TAB {
const char* a;
const char* b;
} tab[] = {
{ "\\\\", "\\" },
{ "\\2s", "ß" },
{ "\\3a", "ä" },
{ "\\3o", "ö" },
{ "\\3u", "ü" },
{ "\\s2", "ß" },
{ "\\a3", "ä" },
{ "\\o3", "ö" },
{ "\\u3", "ü" },
};
for (unsigned int i = 0; i < sizeof(tab)/sizeof(*tab); ++i) {
s = s.replace(tab[i].a, QString::fromUtf8(tab[i].b));
}
return s;
}
//---------------------------------------------------------
// readRest
//---------------------------------------------------------
void MuseData::readRest(Part* part, const QString& s)
{
Fraction ticks = Fraction::fromTicks((s.mid(5, 3).toInt() * MScore::division + _division/2) / _division);
Fraction tick = curTick;
curTick += ticks;
int staffIdx = 0;
if (s.size() >= 24) {
if (s[23].isDigit())
staffIdx = s.mid(23,1).toInt() - 1;
}
Staff* staff = part->staff(staffIdx);
int gstaff = staff->idx();
TDuration d;
d.setVal(ticks.ticks());
Rest* rest = new Rest(score, d);
rest->setTicks(d.fraction());
chordRest = rest;
rest->setTrack(gstaff * VOICES);
Segment* segment = measure->getSegment(SegmentType::ChordRest, tick);
voice = 0;
for (; voice < VOICES; ++voice) {
Element* e = segment->element(gstaff * VOICES + voice);
if (e == 0) {
rest->setTrack(gstaff * VOICES + voice);
segment->add(rest);
break;
}
}
if (voice == VOICES) {
qDebug("cannot allocate voice");
delete rest;
return;
}
}
//---------------------------------------------------------
// readBackup
//---------------------------------------------------------
void MuseData::readBackup(const QString& s)
{
Fraction ticks = Fraction::fromTicks((s.mid(5, 3).toInt() * MScore::division + _division/2) / _division);
if (s[0] == 'b')
curTick -= ticks;
else
curTick += ticks;
}
//---------------------------------------------------------
// createMeasure
//---------------------------------------------------------
Measure* MuseData::createMeasure()
{
for (MeasureBase* mb = score->first(); mb; mb = mb->next()) {
if (mb->type() != ElementType::MEASURE)
continue;
Measure* m = (Measure*)mb;
Fraction st = m->tick();
Fraction l = m->ticks();
if (curTick == st)
return m;
if (curTick > st && curTick < (st+l)) {
// irregular measure
#if 0 // TODO
Fraction f = score->sigmap()->timesig(st).fraction();
score->sigmap()->add(st, curTick - st, f);
score->sigmap()->add(curTick, f);
#endif
break;
}
if (curTick < st + l) {
qDebug("cannot create measure at %d", curTick.ticks());
return 0;
}
}
Measure* mes = new Measure(score);
mes->setTick(curTick);
#if 0
foreach(Staff* s, score->staves()) {
if (s->isTop()) {
BarLine* barLine = new BarLine(score);
barLine->setStaff(s);
mes->setEndBarLine(barLine);
}
}
#endif
score->measures()->add(mes);
return mes;
}
//---------------------------------------------------------
// readPart
//---------------------------------------------------------
void MuseData::readPart(QStringList sl, Part* part)
{
int line = 10;
QString s;
for (; line < sl.size(); ++line) {
s = sl[line];
if (!s.isEmpty() && s[0] == '$')
break;
}
if (line >= sl.size()) {
qDebug(" $ not found in part");
return;
}
curTick = Fraction(0,1);
slur[0] = 0;
slur[1] = 0;
slur[2] = 0;
slur[3] = 0;
measure = 0;
measure = createMeasure();
for (; line < sl.size(); ++line) {
s = sl[line];
// qDebug("%6d: <%s>", curTick.ticks(), qPrintable(s));
char c = s[0].toLatin1();
switch(c) {
case 'A':
case 'B':
case 'C':
case 'D':
case 'E':
case 'F':
case 'G':
readNote(part, s);
break;
case ' ': // chord
readChord(part, s);
break;
case 'r':
readRest(part, s);
break;
case 'g': // grace note
case 'c': // cue note
case 'f': // basso continuo
break;
case 'b': // backspace
case 'i': // forward space
readBackup(s);
break;
case 'm': // measure line / bar line
measure = createMeasure();
break;
case '*': // musical direction
break;
case 'P': // print suggestion
break;
case 'S': // sound record
break;
case '$':
musicalAttribute(s, part);
break;
default:
qDebug("unknown record <%s>", qPrintable(s));
break;
}
}
}
//---------------------------------------------------------
// countStaves
//---------------------------------------------------------
int MuseData::countStaves(const QStringList& sl)
{
int staves = 1;
for (int i = 10; i < sl.size(); ++i) {
QString s = sl[i];
char c = s[0].toLatin1();
switch(c) {
case 'A':
case 'B':
case 'C':
case 'D':
case 'E':
case 'F':
case 'G':
case 'r':
{
int staffIdx = 1;
if (s.size() >= 24) {
if (s[23].isDigit())
staffIdx = s.mid(23,1).toInt();
}
if (staffIdx > staves)
staves = staffIdx;
}
break;
}
}
return staves;
}
//---------------------------------------------------------
// read
// return false on error
//---------------------------------------------------------
bool MuseData::read(const QString& name)
{
QFile fp(name);
if (!fp.open(QIODevice::ReadOnly)) {
qDebug("Cannot open file <%s>", qPrintable(name));
return false;
}
QTextStream ts(&fp);
QStringList part;
bool commentMode = false;
for (;;) {
QString s(ts.readLine());
if (s.isNull())
break;
if (s.isEmpty()) {
if (!commentMode)
part.append(QString(""));
continue;
}
if (s[0] == '&') {
commentMode = !commentMode;
continue;
}
if (commentMode)
continue;
if (s[0] == '@')
continue;
if (s[0] == '/') {
parts.append(part);
Part* mpart = new Part(score);
int staves = countStaves(part);
for (int i = 0; i < staves; ++i) {
Staff* staff = new Staff(score);
staff->setPart(mpart);
mpart->insertStaff(staff, i);
score->staves().push_back(staff);
if ((staves == 2) && (i == 0)) {
staff->setBracketType(0, BracketType::BRACE);
staff->setBracketSpan(0, 2);
}
}
score->appendPart(mpart);
if(part.size() > 8)
mpart->setPlainLongName(part[8]);
part.clear();
continue;
}
if (s[0] == 'a') {
part.back().append(s.mid(1));
continue;
}
part.append(s);
}
fp.close();
return true;
}
//---------------------------------------------------------
// convert
//---------------------------------------------------------
void MuseData::convert()
{
for (int pn = 0; pn < parts.size(); ++pn) {
Part* part = (score->parts())[pn];
readPart(parts[pn], part);
}
#if 0
// crash if system does not fit on page (too many instruments)
Measure* measure = score->tick2measure(0);
if (measure) {
Text* text = new Text(score);
text->setSubtype(TEXT_TITLE);
text->setText(parts[0][6]);
text->setText("mops");
measure->add(text);
text = new Text(score);
text->setSubtype(TEXT_SUBTITLE);
text->setText(parts[0][6]);
measure->add(text);
}
#endif
}
//---------------------------------------------------------
// importMuseData
// return true on success
//---------------------------------------------------------
Score::FileError importMuseData(MasterScore* score, const QString& name)
{
if(!QFileInfo(name).exists())
return Score::FileError::FILE_NOT_FOUND;
MuseData md(score);
if (!md.read(name))
return Score::FileError::FILE_ERROR;
md.convert();
return Score::FileError::FILE_NO_ERROR;
}
}