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
|
/*
Rosegarden
A MIDI and audio sequencer and musical notation editor.
This program is Copyright 2000-2008
Guillaume Laurent <glaurent@telegraph-road.org>,
Chris Cannam <cannam@all-day-breakfast.com>,
Richard Bown <richard.bown@ferventsoftware.com>
The moral rights of Guillaume Laurent, Chris Cannam, and Richard
Bown to claim authorship of this work have been asserted.
Other copyrights also apply to some parts of this work. Please
see the AUTHORS file and individual file headers for details.
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License as
published by the Free Software Foundation; either version 2 of the
License, or (at your option) any later version. See the file
COPYING included with this distribution for more information.
*/
#include "MupExporter.h"
#include "misc/Debug.h"
#include "base/BaseProperties.h"
#include "base/Composition.h"
#include "base/Event.h"
#include "base/Exception.h"
#include "base/NotationQuantizer.h"
#include "base/NotationTypes.h"
#include "base/Segment.h"
#include "base/SegmentNotationHelper.h"
#include "base/Sets.h"
#include "base/Track.h"
#include "gui/general/ProgressReporter.h"
#include <tqobject.h>
using std::string;
namespace Rosegarden
{
using namespace BaseProperties;
MupExporter::MupExporter(TQObject *parent,
Composition *composition,
string fileName) :
ProgressReporter(parent, "mupExporter"),
m_composition(composition),
m_fileName(fileName)
{
// nothing else
}
MupExporter::~MupExporter()
{
// nothing
}
bool
MupExporter::write()
{
Composition *c = m_composition;
std::ofstream str(m_fileName.c_str(), std::ios::out);
if (!str) {
std::cerr << "MupExporter::write() - can't write file " << m_fileName
<< std::endl;
return false;
}
str << "score\n";
str << "\tstaffs=" << c->getNbTracks() << "\n";
int ts = c->getTimeSignatureCount();
std::pair<timeT, TimeSignature> tspair;
if (ts > 0)
tspair = c->getTimeSignatureChange(0);
str << "\ttime="
<< tspair.second.getNumerator() << "/"
<< tspair.second.getDenominator() << "\n";
for (int barNo = -1; barNo < c->getNbBars(); ++barNo) {
for (TrackId trackNo = c->getMinTrackId();
trackNo <= c->getMaxTrackId(); ++trackNo) {
if (barNo < 0) {
writeClefAndKey(str, trackNo);
continue;
}
if (barNo == 0 && trackNo == 0) {
str << "\nmusic\n";
}
str << "\t" << trackNo + 1 << ":";
Segment *s = 0;
timeT barStart = c->getBarStart(barNo);
timeT barEnd = c->getBarEnd(barNo);
for (Composition::iterator ci = c->begin(); ci != c->end(); ++ci) {
if ((*ci)->getTrack() == trackNo &&
(*ci)->getStartTime() < barEnd &&
(*ci)->getEndMarkerTime() > barStart) {
s = *ci;
break;
}
}
TimeSignature timeSig(c->getTimeSignatureAt(barStart));
if (!s) {
// write empty bar
writeInventedRests(str, timeSig, 0, barEnd - barStart);
continue;
}
if (s->getStartTime() > barStart) {
writeInventedRests(str, timeSig,
0, s->getStartTime() - barStart);
}
// Mup insists that every bar has the correct duration, and won't
// recover if one goes wrong. Keep careful tabs on this: it means
// that for example we have to round chord durations down where
// the next chord starts too soon
//!!! we _really_ can't cope with time sig changes yet!
timeT writtenDuration = writeBar(str, c, s, barStart, barEnd,
timeSig, trackNo);
if (writtenDuration < timeSig.getBarDuration()) {
RG_DEBUG << "writtenDuration: " << writtenDuration
<< ", bar duration " << timeSig.getBarDuration()
<< endl;
writeInventedRests(str, timeSig, writtenDuration,
timeSig.getBarDuration() - writtenDuration);
} else if (writtenDuration > timeSig.getBarDuration()) {
std::cerr << "WARNING: overfull bar in Mup export: duration " << writtenDuration
<< " into bar of duration " << timeSig.getBarDuration()
<< std::endl;
//!!! warn user
}
str << "\n";
}
if (barNo >= 0)
str << "bar" << std::endl;
}
str << "\n" << std::endl;
str.close();
return true;
}
timeT
MupExporter::writeBar(std::ofstream &str,
Composition *c,
Segment *s,
timeT barStart, timeT barEnd,
TimeSignature &timeSig,
TrackId trackNo)
{
timeT writtenDuration = 0;
SegmentNotationHelper helper(*s);
helper.setNotationProperties();
long currentGroupId = -1;
string currentGroupType = "";
long currentTupletCount = 3;
bool first = true;
bool openBeamWaiting = false;
for (Segment::iterator si = s->findTime(barStart);
s->isBeforeEndMarker(si) &&
(*si)->getNotationAbsoluteTime() < barEnd; ++si) {
if ((*si)->isa(Note::EventType)) {
Chord chord(*s, si, c->getNotationQuantizer());
Event *e = *chord.getInitialNote();
timeT absTime = e->getNotationAbsoluteTime();
timeT duration = e->getNotationDuration();
try {
// tuplet compensation, etc
Note::Type type = e->get<Int>(NOTE_TYPE);
int dots = e->get
<Int>(NOTE_DOTS);
duration = Note(type, dots).getDuration();
} catch (Exception e) { // no properties
std::cerr << "WARNING: MupExporter::writeBar: incomplete note properties: " << e.getMessage() << std::endl;
}
timeT toNext = duration;
Segment::iterator nextElt = chord.getFinalElement();
if (s->isBeforeEndMarker(++nextElt)) {
toNext = (*nextElt)->getNotationAbsoluteTime() - absTime;
if (toNext < duration)
duration = toNext;
}
bool enteringGroup = false;
if (e->has(BEAMED_GROUP_ID) && e->has(BEAMED_GROUP_TYPE)) {
long id = e->get
<Int>(BEAMED_GROUP_ID);
string type = e->get
<String>(BEAMED_GROUP_TYPE);
if (id != currentGroupId) {
// leave previous group first
if (currentGroupId >= 0) {
if (!openBeamWaiting)
str << " ebm";
openBeamWaiting = false;
if (currentGroupType == GROUP_TYPE_TUPLED) {
str << "; }" << currentTupletCount;
}
}
currentGroupId = id;
currentGroupType = type;
enteringGroup = true;
}
} else {
if (currentGroupId >= 0) {
if (!openBeamWaiting)
str << " ebm";
openBeamWaiting = false;
if (currentGroupType == GROUP_TYPE_TUPLED) {
str << "; }" << currentTupletCount;
}
currentGroupId = -1;
currentGroupType = "";
}
}
if (openBeamWaiting)
str << " bm";
if (!first)
str << ";";
str << " ";
if (currentGroupType == GROUP_TYPE_TUPLED) {
e->get
<Int>(BEAMED_GROUP_UNTUPLED_COUNT, currentTupletCount);
if (enteringGroup)
str << "{ ";
//!!! duration = helper.getCompensatedNotationDuration(e);
}
writeDuration(str, duration);
if (toNext > duration && currentGroupType != GROUP_TYPE_TUPLED) {
writeInventedRests
(str, timeSig,
absTime + duration - barStart, toNext - duration);
}
writtenDuration += toNext;
for (Chord::iterator chi = chord.begin();
chi != chord.end(); ++chi) {
writePitch(str, trackNo, **chi);
}
openBeamWaiting = false;
if (currentGroupType == GROUP_TYPE_BEAMED ||
currentGroupType == GROUP_TYPE_TUPLED) {
if (enteringGroup)
openBeamWaiting = true;
}
si = chord.getFinalElement();
first = false;
} else if ((*si)->isa(Note::EventRestType)) {
if (currentGroupId >= 0) {
if (!openBeamWaiting)
str << " ebm";
openBeamWaiting = false;
if (currentGroupType == GROUP_TYPE_TUPLED) {
str << "; }" << currentTupletCount;
}
currentGroupId = -1;
currentGroupType = "";
}
if (openBeamWaiting)
str << " bm";
if (!first)
str << ";";
str << " ";
writeDuration(str, (*si)->getNotationDuration());
writtenDuration += (*si)->getNotationDuration();
str << "r";
first = false;
openBeamWaiting = false;
} // ignore all other sorts of events for now
}
if (currentGroupId >= 0) {
if (!openBeamWaiting)
str << " ebm";
openBeamWaiting = false;
if (currentGroupType == GROUP_TYPE_TUPLED) {
str << "; }" << currentTupletCount;
}
}
if (openBeamWaiting)
str << " bm";
if (!first)
str << ";";
return writtenDuration;
}
void
MupExporter::writeClefAndKey(std::ofstream &str, TrackId trackNo)
{
Composition *c = m_composition;
for (Composition::iterator i = c->begin(); i != c->end(); ++i) {
if ((*i)->getTrack() == trackNo) {
Clef clef((*i)->getClefAtTime((*i)->getStartTime()));
Rosegarden::Key key((*i)->getKeyAtTime((*i)->getStartTime()));
str << "staff " << trackNo + 1 << "\n";
if (clef.getClefType() == Clef::Treble) {
str << "\tclef=treble\n";
} else if (clef.getClefType() == Clef::Alto) {
str << "\tclef=alto\n";
} else if (clef.getClefType() == Clef::Tenor) {
str << "\tclef=tenor\n";
} else if (clef.getClefType() == Clef::Bass) {
str << "\tclef=bass\n";
}
str << "\tkey=" << key.getAccidentalCount()
<< (key.isSharp() ? "#" : "&")
<< (key.isMinor() ? "minor" : "major") << std::endl;
m_clefKeyMap[trackNo] = ClefKeyPair(clef, key);
return ;
}
}
}
void
MupExporter::writeInventedRests(std::ofstream &str,
TimeSignature &timeSig,
timeT offset,
timeT duration)
{
str << " ";
DurationList dlist;
timeSig.getDurationListForInterval(dlist, duration, offset);
for (DurationList::iterator i = dlist.begin();
i != dlist.end(); ++i) {
writeDuration(str, *i);
str << "r;";
}
}
void
MupExporter::writePitch(std::ofstream &str, TrackId trackNo,
Event *event)
{
long pitch = 0;
if (!event->get
<Int>(PITCH, pitch)) {
str << "c"; // have to write something, or it won't parse
return ;
}
Accidental accidental = Accidentals::NoAccidental;
(void)event->get
<String>(ACCIDENTAL, accidental);
// mup octave: treble clef is in octave 4?
ClefKeyPair ck;
ClefKeyMap::iterator ckmi = m_clefKeyMap.find(trackNo);
if (ckmi != m_clefKeyMap.end())
ck = ckmi->second;
Pitch p(pitch, accidental);
Accidental acc(p.getDisplayAccidental(ck.second));
char note(p.getNoteName(ck.second));
int octave(p.getOctave());
// just to avoid assuming that the note names returned by Pitch are in
// the same set as those expected by Mup -- in practice they are the same
// letters but this changes the case
str << "cdefgab"[Pitch::getIndexForNote(note)];
if (acc == Accidentals::DoubleFlat)
str << "&&";
else if (acc == Accidentals::Flat)
str << "&";
else if (acc == Accidentals::Sharp)
str << "#";
else if (acc == Accidentals::DoubleSharp)
str << "##";
else if (acc == Accidentals::Natural)
str << "n";
str << octave + 1;
}
void
MupExporter::writeDuration(std::ofstream &str, timeT duration)
{
Note note(Note::getNearestNote(duration, 2));
int n = Note::Semibreve - note.getNoteType();
if (n < 0)
str << "1/" << (1 << ( -n));
else
str << (1 << n);
for (int d = 0; d < note.getDots(); ++d)
str << ".";
}
}
|