File: importxmlfirstpass.cpp

package info (click to toggle)
musescore 2.3.2%2Bdfsg2-7~deb10u1
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 187,932 kB
  • sloc: cpp: 264,610; xml: 176,707; sh: 3,311; ansic: 1,384; python: 356; makefile: 188; perl: 82; pascal: 78
file content (237 lines) | stat: -rw-r--r-- 7,295 bytes parent folder | download | duplicates (10)
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
//=============================================================================
//  MuseScore
//  Music Composition & Notation
//
//  Copyright (C) 2013 - 2015 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 "importxmlfirstpass.h"

namespace Ms {

// TODO: move somewhere else

MusicXmlPart::MusicXmlPart(QString id, QString name)
      : id(id), name(name)
      {
      octaveShifts.resize(MAX_STAVES);
      }


void MusicXmlPart::addMeasureNumberAndDuration(QString measureNumber, Fraction measureDuration)
      {
      measureNumbers.append(measureNumber);
      measureDurations.append(measureDuration);
      }

void MusicXmlPart::setMaxStaff(const int staff)
      {
      if (staff > _maxStaff)
            _maxStaff = staff;
      }

Fraction MusicXmlPart::measureDuration(int i) const
      {
      if (i >= 0 && i < measureDurations.size())
            return measureDurations.at(i);
      return Fraction(0, 0); // return invalid fraction
      }

QString MusicXmlPart::toString() const
      {
      auto res = QString("part id '%1' name '%2' print %3 abbr '%4' print %5 maxStaff %6\n")
            .arg(id).arg(name).arg(printName).arg(abbr).arg(printAbbr).arg(_maxStaff);

      for (VoiceList::const_iterator i = voicelist.constBegin(); i != voicelist.constEnd(); ++i) {
            res += QString("voice %1 map staff data %2\n")
                  .arg(i.key() + 1)
                  .arg(i.value().toString());
            }

      for (int i = 0; i < measureNumbers.size(); ++i) {
            if (i > 0)
                  res += "\n";
            res += QString("measure %1 duration %2 (%3)")
                  .arg(measureNumbers.at(i))
                  .arg(measureDurations.at(i).print())
                  .arg(measureDurations.at(i).ticks());
            }

      return res;
      }

int MusicXmlPart::octaveShift(const int staff, const Fraction f) const
      {
      if (staff < 0 || MAX_STAVES <= staff)
            return 0;
      if (f < Fraction(0, 1))
            return 0;
      return octaveShifts[staff].octaveShift(f);
      }

void MusicXmlPart::addOctaveShift(const int staff, const int shift, const Fraction f)
      {
      if (staff < 0 || MAX_STAVES <= staff)
            return;
      if (f < Fraction(0, 1))
            return;
      octaveShifts[staff].addOctaveShift(shift, f);
      }

void MusicXmlPart::calcOctaveShifts()
      {
      for (int i = 0; i < MAX_STAVES; ++i) {
            octaveShifts[i].calcOctaveShiftShifts();
            }
      }

//---------------------------------------------------------
//   instrument
//---------------------------------------------------------

const QString MusicXmlInstrList::instrument(const Fraction f) const
      {
      if (empty())
            return "";
      auto i = upper_bound(f);
      if (i == begin())
            return "";
      --i;
      return i->second;
      }

//---------------------------------------------------------
//   setInstrument
//---------------------------------------------------------

void MusicXmlInstrList::setInstrument(const QString instr, const Fraction f)
      {
      // TODO determine how to handle multiple instrument changes at the same time
      // current implementation keeps the first one
      if (!insert({ f, instr }).second)
            qDebug("instr '%s', tick %s (%d): element already exists",
                   qPrintable(instr), qPrintable(f.print()), f.ticks());
      //(*this)[f] = instr;
      }

int MusicXmlOctaveShiftList::octaveShift(const Fraction f) const
      {
      if (empty())
            return 0;
      auto i = upper_bound(f);
      if (i == begin())
            return 0;
      --i;
      return i->second;
      }

void MusicXmlOctaveShiftList::addOctaveShift(const int shift, const Fraction f)
      {
      Q_ASSERT(Fraction(0, 1) <= f);

      //qDebug("addOctaveShift(shift %d f %s)", shift, qPrintable(f.print()));
      auto i = find(f);
      if (i == end()) {
            //qDebug("addOctaveShift: not found, inserting");
            insert({ f, shift });
            }
      else {
            //qDebug("addOctaveShift: found %d, adding", (*this)[f]);
            (*this)[f] += shift;
            //qDebug("addOctaveShift: res %d", (*this)[f]);
            }
      }

void MusicXmlOctaveShiftList::calcOctaveShiftShifts()
      {
      /*
      for (auto i = cbegin(); i != cend(); ++i)
            qDebug(" [%s : %d]", qPrintable((*i).first.print()), (*i).second);
       */

      // to each MusicXmlOctaveShiftList entry, add the sum of all previous ones
      int currentShift = 0;
      for (auto i = begin(); i != end(); ++i) {
            currentShift += i->second;
            i->second = currentShift;
            }

      /*
      for (auto i = cbegin(); i != cend(); ++i)
            qDebug(" [%s : %d]", qPrintable((*i).first.print()), (*i).second);
       */

      }


//---------------------------------------------------------
//   LyricNumberHandler
//   collect lyric numbering information and determine order
//
//   MusicXML lyrics may contain name and number attributes,
//   plus position information (typically default-y).
//   Name and number are simply tokens with no specified usage.
//   Default-y cannot easily be used to determine the lyrics
//   line, as it tends to differ per system depending on the
//   actual notes present.
//
//   Simply collecting all possible lyric number attributes
//   within a MusicXML part and assigning lyrics position
//   based on alphabetically sorting works well for all
//   common MusicXML files.
//---------------------------------------------------------

//---------------------------------------------------------
//   addNumber
//---------------------------------------------------------

void LyricNumberHandler::addNumber(const QString number)
      {
      if (_numberToNo.find(number) == _numberToNo.end())
            _numberToNo[number] = -1;       // unassiged
      }

//---------------------------------------------------------
//   toString
//---------------------------------------------------------

QString LyricNumberHandler::toString() const
      {
      QString res;
      for (const auto& p : _numberToNo) {
            if (!res.isEmpty())
                  res += " ";
            res += QString("%1:%2").arg(p.first).arg(p.second);
            }
      return res;
      }

//---------------------------------------------------------
//   getLyricNo
//---------------------------------------------------------

int LyricNumberHandler::getLyricNo(const QString& number) const
      {
      const auto it = _numberToNo.find(number);
      return it == _numberToNo.end() ? 0 : it->second;
      }

//---------------------------------------------------------
//   determineLyricNos
//---------------------------------------------------------

void LyricNumberHandler::determineLyricNos()
      {
      int i = 0;
      for (auto& p : _numberToNo) {
            p.second = i;
            ++i;
            }
      }

}