Skip to content

Commit b09003d

Browse files
OmarEmaraDevJojo-Schmitz
authored andcommitted
Fix #207346: Restrict first track to timing events
Currently, exported MIDI files will have data events in the first MIDI track. While this is not forbidden by the standard, the industry convention is to only put timing related events in the first track, in which case it is called a tempo track. MuseScore should follow that convention because a lot of the available software assumes this convention. This has been discussed and agreed upon in: https://musescore.org/en/node/207346 Duplicate of musescore#7523
1 parent d5087a2 commit b09003d

17 files changed

+13
-11
lines changed

audio/exports/exportmidi.cpp

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -33,11 +33,10 @@ namespace Ms {
3333
// writeHeader
3434
//---------------------------------------------------------
3535

36-
void ExportMidi::writeHeader()
36+
void ExportMidi::writeHeader(MidiTrack& tempoTrack)
3737
{
3838
if (mf.tracks().isEmpty())
3939
return;
40-
MidiTrack &track = mf.tracks().front();
4140
#if 0 // TODO
4241
MeasureBase* measure = cs->first();
4342

@@ -86,7 +85,7 @@ void ExportMidi::writeHeader()
8685
//--------------------------------------------
8786

8887
int staffIdx = 0;
89-
for (auto& track1: mf.tracks()) {
88+
for (auto& track: mf.tracks()) {
9089
Staff* staff = cs->staff(staffIdx);
9190

9291
QByteArray partName = staff->partName().toUtf8();
@@ -101,7 +100,7 @@ void ExportMidi::writeHeader()
101100
ev.setEData(data);
102101
ev.setLen(len);
103102

104-
track1.insert(0, ev);
103+
track.insert(0, ev);
105104

106105

107106
++staffIdx;
@@ -149,7 +148,7 @@ void ExportMidi::writeHeader()
149148
ev.setMetaType(META_TIME_SIGNATURE);
150149
ev.setEData(data);
151150
ev.setLen(4);
152-
track.insert(pauseMap.addPauseTicks(is->first + tickOffset), ev);
151+
tempoTrack.insert(pauseMap.addPauseTicks(is->first + tickOffset), ev);
153152
}
154153
}
155154

@@ -159,7 +158,7 @@ void ExportMidi::writeHeader()
159158
//---------------------------------------------------
160159

161160
staffIdx = 0;
162-
for (auto& track1: mf.tracks()) {
161+
for (auto& track: mf.tracks()) {
163162
Staff* staff = cs->staff(staffIdx);
164163
KeyList* keys = staff->keyList();
165164

@@ -183,7 +182,7 @@ void ExportMidi::writeHeader()
183182
data[1] = 0; // major
184183
ev.setEData(data);
185184
int tick = ik->first + tickOffset;
186-
track1.insert(pauseMap.addPauseTicks(tick), ev);
185+
track.insert(pauseMap.addPauseTicks(tick), ev);
187186
if (tick == 0)
188187
initialKeySigFound = true;
189188
}
@@ -200,7 +199,7 @@ void ExportMidi::writeHeader()
200199
data[0] = key;
201200
data[1] = 0; // major
202201
ev.setEData(data);
203-
track1.insert(0, ev);
202+
track.insert(0, ev);
204203
}
205204

206205
++staffIdx;
@@ -228,7 +227,7 @@ void ExportMidi::writeHeader()
228227
data[1] = tempo >> 8;
229228
data[2] = tempo;
230229
ev.setEData(data);
231-
track.insert(it->first, ev);
230+
tempoTrack.insert(it->first, ev);
232231
}
233232
}
234233

@@ -249,6 +248,8 @@ bool ExportMidi::write(QIODevice* device, bool midiExpandRepeats, bool exportRPN
249248
mf.setDivision(MScore::division);
250249
mf.setFormat(1);
251250
QList<MidiTrack>& tracks = mf.tracks();
251+
MidiTrack tempoTrack;
252+
tempoTrack.setOutChannel(0);
252253

253254
for (int i = 0; i < cs->nstaves(); ++i)
254255
tracks.append(MidiTrack());
@@ -257,7 +258,7 @@ bool ExportMidi::write(QIODevice* device, bool midiExpandRepeats, bool exportRPN
257258
cs->renderMidi(&events, false, midiExpandRepeats, synthState);
258259

259260
pauseMap.calculate(cs);
260-
writeHeader();
261+
writeHeader(tempoTrack);
261262

262263
int staffIdx = 0;
263264
for (auto &track: tracks) {
@@ -370,6 +371,7 @@ bool ExportMidi::write(QIODevice* device, bool midiExpandRepeats, bool exportRPN
370371
}
371372
++staffIdx;
372373
}
374+
tracks.prepend(tempoTrack);
373375
return !mf.write(device);
374376
}
375377

audio/exports/exportmidi.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ class ExportMidi {
4747

4848
PauseMap pauseMap;
4949

50-
void writeHeader();
50+
void writeHeader(MidiTrack& tempoTrack);
5151

5252
public:
5353
MidiFile mf;
12 Bytes
Binary file not shown.
12 Bytes
Binary file not shown.
12 Bytes
Binary file not shown.
12 Bytes
Binary file not shown.
12 Bytes
Binary file not shown.
12 Bytes
Binary file not shown.
12 Bytes
Binary file not shown.
12 Bytes
Binary file not shown.

0 commit comments

Comments
 (0)