forked from musescore/MuseScore
/
importmxmlnoteduration.cpp
283 lines (251 loc) · 9.85 KB
/
importmxmlnoteduration.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
//=============================================================================
// MuseScore
// Music Composition & Notation
//
// Copyright (C) 2018 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
// as published by the Free Software Foundation and appearing in
// the file LICENCE.GPL
//=============================================================================
#include "libmscore/fraction.h"
#include "importmxmllogger.h"
#include "importmxmlnoteduration.h"
namespace Ms {
//---------------------------------------------------------
// noteTypeToFraction
//---------------------------------------------------------
/**
Convert MusicXML note type to fraction.
*/
static Fraction noteTypeToFraction(const QString& type)
{
if (type == "1024th")
return Fraction(1, 1024);
else if (type == "512th")
return Fraction(1, 512);
else if (type == "256th")
return Fraction(1, 256);
else if (type == "128th")
return Fraction(1, 128);
else if (type == "64th")
return Fraction(1, 64);
else if (type == "32nd")
return Fraction(1, 32);
else if (type == "16th")
return Fraction(1, 16);
else if (type == "eighth")
return Fraction(1, 8);
else if (type == "quarter")
return Fraction(1, 4);
else if (type == "half")
return Fraction(1, 2);
else if (type == "whole")
return Fraction(1, 1);
else if (type == "breve")
return Fraction(2, 1);
else if (type == "long")
return Fraction(4, 1);
else if (type == "maxima")
return Fraction(8, 1);
else
return Fraction(0, 0);
}
//---------------------------------------------------------
// calculateFraction
//---------------------------------------------------------
/**
Convert note type, number of dots and actual and normal notes into a duration
*/
static Fraction calculateFraction(const QString& type, const int dots, const Fraction timeMod)
{
// type
Fraction f = noteTypeToFraction(type);
if (f.isValid()) {
// dot(s)
Fraction ff = f;
for (int i = 0; i < dots; ++i) {
ff = ff * Fraction(1,2);
f += ff;
}
// tuplet
if (timeMod.isValid())
f *= timeMod;
// clean up (just in case)
f.reduce();
}
return f;
}
//---------------------------------------------------------
// checkTiming
//---------------------------------------------------------
/**
Do timing error checks.
Return empty string if OK, message in case of error.
*/
QString mxmlNoteDuration::checkTiming(const QString& type, const bool rest, const bool grace)
{
//qDebug("type %s rest %d grace %d", qPrintable(type), rest, grace);
QString errorStr;
// normalize duration
if (_specDura.isValid())
_specDura.reduce();
// default: use calculated duration
_calcDura = calculateFraction(type, _dots, _timeMod);
if (_calcDura.isValid()) {
_dura = _calcDura;
}
if (_specDura.isValid() && _calcDura.isValid()) {
if (_specDura != _calcDura) {
errorStr = QString("calculated duration (%1) not equal to specified duration (%2)")
.arg(_calcDura.print(), _specDura.print());
//qDebug("rest %d type '%s' timemod %s", rest, qPrintable(type), qPrintable(_timeMod.print()));
if (rest && type == "whole" && _specDura.isValid()) {
// Sibelius whole measure rest (not an error)
errorStr = "";
_dura = _specDura;
}
else if (grace && _specDura == Fraction(0, 1)) {
// grace note (not an error)
errorStr = "";
_dura = _specDura;
}
else {
// note:
// rounding error detection may conflict with changed duration detection (Overture).
// exporters that intentionally change note duration typically produce a large difference,
// most likely in practice this will not be an issue
const int maxDiff = 2; // maximum difference considered a rounding error
if (qAbs(_calcDura.ticks() - _specDura.ticks()) <= maxDiff) {
errorStr += " -> assuming rounding error";
_dura = _calcDura;
_specDura = _calcDura; // prevent changing off time
}
else
errorStr += " -> using calculated duration";
}
// Special case:
// Encore generates rests in tuplets w/o <tuplet> or <time-modification>.
// Detect this by comparing the actual duration with the expected duration
// based on note type. If actual is 2/3 of expected, the rest is part
// of a tuplet.
if (rest) {
if (2 * _calcDura.ticks() == 3 * _specDura.ticks()) {
_timeMod = Fraction(2, 3);
errorStr += errorStr.isEmpty() ? " ->" : ",";
errorStr += " assuming triplet";
_dura = _specDura;
}
}
}
}
else if (_specDura.isValid() && !_calcDura.isValid()) {
// do not report an error for typeless (whole measure) rests
if (!(rest && type == "")) {
errorStr = "calculated duration invalid, using specified duration";
}
_dura = _specDura;
}
else if (!_specDura.isValid() && _calcDura.isValid()) {
if (!grace) {
errorStr = "specified duration invalid, using calculated duration";
}
}
else {
errorStr = "calculated and specified duration invalid, using 4/4";
_dura = Fraction(4, 4);
}
return errorStr;
}
//---------------------------------------------------------
// duration
//---------------------------------------------------------
/**
Parse the /score-partwise/part/measure/note/duration node.
*/
void mxmlNoteDuration::duration(QXmlStreamReader& e)
{
_logger->logDebugTrace("MusicXMLParserPass1::duration", &e);
_specDura.set(0, 0); // invalid unless set correctly
int intDura = e.readElementText().toInt();
if (intDura > 0) {
if (_divs > 0) {
_specDura.set(intDura, 4 * _divs);
_specDura.reduce(); // prevent overflow in later Fraction operations
}
else
_logger->logError("illegal or uninitialized divisions", &e);
}
else
_logger->logError("illegal duration", &e);
//qDebug("specified duration %s valid %d", qPrintable(_specDura.print()), _specDura.isValid());
}
//---------------------------------------------------------
// readProperties
//---------------------------------------------------------
/**
Handle selected child elements of the /score-partwise/part/measure/note/duration node.
Return true if handled.
*/
bool mxmlNoteDuration::readProperties(QXmlStreamReader& e)
{
const QStringRef& tag(e.name());
//qDebug("tag %s", qPrintable(tag.toString()));
if (tag == "dot") {
_dots++;
e.skipCurrentElement(); // skip but don't log
return true;
}
else if (tag == "duration") {
duration(e);
return true;
}
else if (tag == "time-modification") {
timeModification(e);
return true;
}
return false;
}
//---------------------------------------------------------
// timeModification
//---------------------------------------------------------
/**
Parse the /score-partwise/part/measure/note/time-modification node.
*/
void mxmlNoteDuration::timeModification(QXmlStreamReader& e)
{
_logger->logDebugTrace("MusicXMLParserPass1::timeModification", &e);
int intActual = 0;
int intNormal = 0;
QString strActual;
QString strNormal;
while (e.readNextStartElement()) {
const QStringRef& tag(e.name());
if (tag == "actual-notes")
strActual = e.readElementText();
else if (tag == "normal-notes")
strNormal = e.readElementText();
else if (tag == "normal-type") {
// "measure" is not a valid normal-type,
// but would be accepted by setType()
QString strNormalType = e.readElementText();
if (strNormalType != "measure")
_normalType.setType(strNormalType);
}
else {
_logger->logDebugInfo(QString("skipping '%1'").arg(e.name().toString()), &e);
e.skipCurrentElement();
}
}
intActual = strActual.toInt();
intNormal = strNormal.toInt();
if (intActual > 0 && intNormal > 0)
_timeMod.set(intNormal, intActual);
else {
_timeMod.set(1, 1);
_logger->logError(QString("illegal time-modification: actual-notes %1 normal-notes %2")
.arg(strActual, strNormal), &e);
}
}
}