File: mxmlwriter.cpp

package info (click to toggle)
musescore-snapshot 3.2.s20190704+dfsg1-1
  • links: PTS, VCS
  • area: main
  • in suites: experimental
  • size: 218,116 kB
  • sloc: cpp: 290,563; xml: 200,238; sh: 3,706; ansic: 1,447; python: 393; makefile: 222; perl: 82; pascal: 79
file content (356 lines) | stat: -rw-r--r-- 11,687 bytes parent folder | download | duplicates (5)
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
//=============================================================================
//  BWW to MusicXML converter
//  Part of MusE Score
//  Linux Music Score Editor
//
//  Copyright (C) 2010 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.
//=============================================================================

/**
\file
 Output writer for BWW to MusicXML converter.
 */

#include <QtCore/QtDebug>

#include "mxmlwriter.h"

namespace Bww {

  /**
   MxmlWriter constructor.
   */

  MxmlWriter::MxmlWriter()
    : beats(4),
    beat(4),
    regularMeasureNumber(0),
    irregularMeasureNumber(0),
    tempo(0),
    ending(0)
  {
    qDebug() << "MxmlWriter::MxmlWriter()";

    stepAlterOctMap["LG"] = StepAlterOct('G', 0, 4);
    stepAlterOctMap["LA"] = StepAlterOct('A', 0, 4);
    stepAlterOctMap["B"] = StepAlterOct('B', 0, 4);
    stepAlterOctMap["C"] = StepAlterOct('C', 1, 5);
    stepAlterOctMap["D"] = StepAlterOct('D', 0, 5);
    stepAlterOctMap["E"] = StepAlterOct('E', 0, 5);
    stepAlterOctMap["F"] = StepAlterOct('F', 1, 5);
    stepAlterOctMap["HG"] = StepAlterOct('G', 0, 5);
    stepAlterOctMap["HA"] = StepAlterOct('A', 0, 5);

    typeMap["1"] = "whole";
    typeMap["2"] = "half";
    typeMap["4"] = "quarter";
    typeMap["8"] = "eighth";
    typeMap["16"] = "16th";
    typeMap["32"] = "32nd";
  }

  /**
   Begin a new measure.
   */

  void MxmlWriter::beginMeasure(const Bww::MeasureBeginFlags mbf)
  {
    // qDebug() << "MxmlWriter::beginMeasure()";
    QString implicit;
    QString strMeasureNumber;
    if (mbf.irregular)
    {
      implicit = " implicit=\"yes\"";
      if (irregularMeasureNumber == 0)
        strMeasureNumber = "0";
      else
        strMeasureNumber = QString("X%1").arg(irregularMeasureNumber);
      ++irregularMeasureNumber;
    }
    else
    {
      ++regularMeasureNumber;
      strMeasureNumber.setNum(regularMeasureNumber);
    }
    out << "    <measure number=\"" << strMeasureNumber << "\"" << implicit << ">" << endl;
    if ((regularMeasureNumber + irregularMeasureNumber) != 1 && mbf.firstOfSystem)
    {
      out << "      <print new-system=\"yes\"/>" << endl;
    }
    if (mbf.repeatBegin || mbf.endingFirst || mbf.endingSecond)
    {
      out << "      <barline location=\"left\">" << endl;
      if (mbf.repeatBegin)
      {
        out << "        <bar-style>heavy-light</bar-style>" << endl;
        out << "        <repeat direction=\"forward\"/>" << endl;
      }
      if (mbf.endingFirst)
      {
        out << "        <ending number=\"1\" type=\"start\"/>" << endl;
        ending = 1;
      }
      if (mbf.endingSecond)
      {
        out << "        <ending number=\"2\" type=\"start\"/>" << endl;
        ending = 2;
      }
      out << "      </barline>" << endl;
    }
    if ((regularMeasureNumber + irregularMeasureNumber) == 1)
    {
      out << "      <attributes>" << endl;
      out << "        <divisions>" << wholeDur() / 4 << "</divisions>" << endl;
//      out << "        <key print-object=\"no\">" << endl;
      out << "        <key>" << endl;
      out << "          <fifths>2</fifths>" << endl;
      out << "          <mode>major</mode>" << endl;
      out << "        </key>" << endl;
      out << "        <time>" << endl;
      out << "          <beats>" << beats << "</beats>" << endl;
      out << "          <beat-type>" << beat << "</beat-type>" << endl;
      out << "        </time>" << endl;
      out << "        <clef>" << endl;
      out << "          <sign>G</sign>" << endl;
      out << "          <line>2</line>" << endl;
      out << "        </clef>" << endl;
      out << "      </attributes>" << endl;
      if (tempo)
      {
        out << "      <direction placement=\"above\">" << endl;
        out << "        <direction-type>" << endl;
        out << "          <metronome parentheses=\"no\">" << endl;
        out << "            <beat-unit>quarter</beat-unit>" << endl;
        out << "            <per-minute>" << tempo << "</per-minute>" << endl;
        out << "          </metronome>" << endl;
        out << "        </direction-type>" << endl;
        out << "        <sound tempo=\"" << tempo << "\"/>" << endl;
        out << "      </direction>" << endl;
      }
    }
  }

  /**
   End the current measure.
   */

  void MxmlWriter::endMeasure(const Bww::MeasureEndFlags mef)
  {
    // qDebug() << "MxmlWriter::endMeasure()";
    if (mef.repeatEnd || mef.endingEnd || mef.lastOfPart || mef.doubleBarLine)
    {
      out << "      <barline location=\"right\">" << endl;
      if (mef.repeatEnd || mef.lastOfPart)
      {
        out << "        <bar-style>light-heavy</bar-style>" << endl;
      }
      else if (mef.doubleBarLine)
      {
        out << "        <bar-style>light-light</bar-style>" << endl;
      }
      if (mef.endingEnd)
      {
        if (ending == 1)
          out << "        <ending number=\"1\" type=\"stop\"/>" << endl;
        else if (ending == 2)
          out << "        <ending number=\"2\" type=\"discontinue\"/>" << endl;
        else
        {
          // TODO: error message
        }
      }
      if (mef.repeatEnd)
      {
        out << "        <repeat direction=\"backward\"/>" << endl;
      }
      out << "      </barline>" << endl;
    }
    out << "    </measure>" << endl;
  }

  /**
   Write a single note.
   */

  void MxmlWriter::note(const QString pitch, QVector<BeamType> beamList,
                        const QString type, const int dots,
                        bool tieStart, bool tieStop,
                        StartStop triplet,
                        bool grace)
  {
    // qDebug() << "MxmlWriter::note()";

    if (!stepAlterOctMap.contains(pitch)
      || !typeMap.contains(type))
      {
      // TODO: error message
      return;
    }
    StepAlterOct sao = stepAlterOctMap.value(pitch);

    int dur = wholeDur() / type.toInt();
    if (dots == 1) dur = 3 * dur / 2;
    if (triplet != ST_NONE) dur = 2 * dur / 3;
    out << "      <note>" << endl;
    if (grace) out << "        <grace/>" << endl;
    out << "        <pitch>" << endl;
    out << "          <step>" << sao.s << "</step>" << endl;
    if (sao.a) out << "          <alter>" << sao.a << "</alter>" << endl;
    out << "          <octave>" << sao.o << "</octave>" << endl;
    out << "        </pitch>" << endl;
    if (!grace)
      out << "        <duration>" << dur << "</duration>" << endl;
    if (tieStart)
      out << "        <tie type=\"start\"/>" << endl;
    if (tieStop)
      out << "        <tie type=\"stop\"/>" << endl;
    out << "        <type>" << typeMap.value(type) << "</type>" << endl;
    if (dots == 1) out << "        <dot/>" << endl;
    if (triplet != ST_NONE)
    {
      out << "        <time-modification>" << endl;
      out << "          <actual-notes>3</actual-notes>" << endl;
      out << "          <normal-notes>2</normal-notes>" << endl;
      out << "        </time-modification>" << endl;
    }
    if (grace)
      out << "        <stem>up</stem>" << endl;
    else
    {
      if (type != "1")
        out << "        <stem>down</stem>" << endl;
    }
    for (int i = 0; i < maxBeamLevel; ++i)
    {
      QString s;
      switch (beamList.at(i))
      {
      case BM_BEGIN:         s = "begin"; break;
      case BM_CONTINUE:      s = "continue"; break;
      case BM_END:           s = "end"; break;
      case BM_FORWARD_HOOK:  s = "forward hook"; break;
      case BM_BACKWARD_HOOK: s = "backward hook"; break;
      default:               s = ""; break;
      }
      if (s != "")
        out << "        <beam number=\"" << i + 1 << "\">"
            << s << "</beam>" << endl;
      else
        break;
    }
    if (tieStart || tieStop || triplet == ST_START || triplet == ST_STOP)
    {
      out << "        <notations>" << endl;
      if (tieStart)
        out << "          <tied type=\"start\"/>" << endl;
      if (tieStop)
        out << "          <tied type=\"stop\"/>" << endl;
      if (triplet == ST_START)
      {
        if (type == "1" || type == "2" || type == "4")
          out << "          <tuplet type=\"start\" bracket=\"yes\"/>" << endl;
        else
          out << "          <tuplet type=\"start\" bracket=\"no\"/>" << endl;
      }
      if (triplet == ST_STOP)
        out << "          <tuplet type=\"stop\"/>" << endl;
      out << "        </notations>" << endl;
    }
    out << "      </note>" << endl;
  }

  /**
   Write the header.
   */

  void MxmlWriter::header(const QString title, const QString /* type */,
                          const QString composer, const QString footer,
                          const unsigned int temp)
  {
    // qDebug() << "MxmlWriter::header()"
    //    << "title:" << title
    //    << "type:" << type
    //    << "composer:" << composer
    //    << "footer:" << footer
    //    << "temp:" << temp
    //    ;

    // save tempo for later use
    tempo = temp;

    // write the header
    out << "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" << endl;
    out << "<!DOCTYPE score-partwise PUBLIC "
        << "\"-//Recordare//DTD MusicXML 2.0 Partwise//EN\" "
        << "\"http://www.musicxml.org/dtds/partwise.dtd\">" << endl;
    out << "<score-partwise>" << endl;
    out << "  <work>" << endl;
    out << "    <work-title>" << title << "</work-title>" << endl;
    // TODO work-number is not allowed, replace
    // out << "    <work-number>" << type << "</work-number>" << endl;
    out << "  </work>" << endl;
    out << "  <identification>" << endl;
    out << "    <creator type=\"composer\">" << composer << "</creator>" << endl;
    out << "    <rights>" << footer << "</rights>" << endl;
    out << "    <encoding>" << endl;
    out << "      <software>bww2mxml</software>" << endl;
    // TODO fill in real date
    // out << "      <encoding-date>TBD</encoding-date>" << endl;
    out << "    </encoding>" << endl;
    out << "  </identification>" << endl;
    out << "  <part-list>" << endl;
    out << "    <score-part id=\"P1\">" << endl;
    out << "      <part-name>" << instrumentName() << "</part-name>" << endl;
    out << "      <score-instrument id=\"P1-I1\">" << endl;
    out << "        <instrument-name>" << instrumentName() << "</instrument-name>" << endl;
    out << "      </score-instrument>" << endl;
    out << "      <midi-instrument id=\"P1-I1\">" << endl;
    out << "        <midi-channel>1</midi-channel>" << endl;
    out << "        <midi-program>" << midiProgram() << "</midi-program>" << endl;
    out << "      </midi-instrument>" << endl;
    out << "    </score-part>" << endl;
    out << "  </part-list>" << endl;
    out << "  <part id=\"P1\">" << endl;
  }

  /**
   Store beats and beat type for later use.
   */

  void MxmlWriter::tsig(const int bts, const int bt)
  {
    // qDebug() << "MxmlWriter::tsig()"
    //    << "beats:" << bts
    //    << "beat:" << bt
    //   ;

    beats = bts;
    beat  = bt;
  }

  /**
   Write the trailer.
   */

  void MxmlWriter::trailer()
  {
    // qDebug() << "MxmlWriter::trailer()"
    //    ;

    out << "  </part>" << endl;
    out << "</score-partwise>" << endl;
  }

} // namespace Bww