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
|
//=========================================================
// MusE
// Linux Music Editor
// $Id: exportmidi.cpp,v 1.9.2.1 2009/04/01 01:37:10 terminator356 Exp $
//
// (C) Copyright 1999-2003 Werner Schweer (ws@seh.de)
//
// 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; version 2 of
// the License, or (at your option) any later version.
//
// 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
//
//=========================================================
#include <stdio.h>
#include <QString>
#include "al/sig.h" // Tim.
#include "app.h"
#include "midifile.h"
#include "midi.h"
#include "midictrl.h"
#include "globals.h"
#include "filedialog.h"
#include "track.h"
#include "song.h"
#include "mpevent.h"
#include "event.h"
#include "marker/marker.h"
#include "drummap.h"
#include "gconfig.h"
namespace MusECore {
//---------------------------------------------------------
// addController
//---------------------------------------------------------
static void addController(MPEventList* l, int tick, int port, int channel, int a, int b)
{
// p3.3.37
//if (a < 0x1000) { // 7 Bit Controller
if (a < CTRL_14_OFFSET) { // 7 Bit Controller
l->add(MidiPlayEvent(tick, port, channel, ME_CONTROLLER, a, b));
}
//else if (a < 0x20000) { // 14 Bit Controller
else if (a < CTRL_RPN_OFFSET) { // 14 Bit Controller
int ctrlH = (a >> 8) & 0x7f;
int ctrlL = a & 0x7f;
int dataH = (b >> 7) & 0x7f;
int dataL = b & 0x7f;
l->add(MidiPlayEvent(tick, port, channel, ME_CONTROLLER, ctrlH, dataH));
l->add(MidiPlayEvent(tick+1, port, channel, ME_CONTROLLER, ctrlL, dataL));
}
//else if (a < 0x30000) { // RPN 7-Bit Controller
else if (a < CTRL_NRPN_OFFSET) { // RPN 7-Bit Controller
int ctrlH = (a >> 8) & 0x7f;
int ctrlL = a & 0x7f;
l->add(MidiPlayEvent(tick, port, channel, ME_CONTROLLER, CTRL_HRPN, ctrlH));
l->add(MidiPlayEvent(tick+1, port, channel, ME_CONTROLLER, CTRL_LRPN, ctrlL));
l->add(MidiPlayEvent(tick+2, port, channel, ME_CONTROLLER, CTRL_HDATA, b));
}
//else if (a < 0x40000) { // NRPN 7-Bit Controller
else if (a < CTRL_INTERNAL_OFFSET) { // NRPN 7-Bit Controller
int ctrlH = (a >> 8) & 0x7f;
int ctrlL = a & 0x7f;
l->add(MidiPlayEvent(tick, port, channel, ME_CONTROLLER, CTRL_HNRPN, ctrlH));
l->add(MidiPlayEvent(tick+1, port, channel, ME_CONTROLLER, CTRL_LNRPN, ctrlL));
l->add(MidiPlayEvent(tick+2, port, channel, ME_CONTROLLER, CTRL_HDATA, b));
}
else if (a == CTRL_PITCH) {
int a = b + 8192;
int b = a >> 7;
l->add(MidiPlayEvent(tick, port, channel, ME_PITCHBEND, a & 0x7f, b & 0x7f));
}
else if (a == CTRL_PROGRAM) {
int hb = (b >> 16) & 0xff;
int lb = (b >> 8) & 0xff;
int pr = b & 0x7f;
int tickoffset = 0;
switch(MusEGlobal::song->mtype()) {
case MT_GM: // no HBANK/LBANK
break;
case MT_GS:
case MT_XG:
case MT_UNKNOWN:
if (hb != 0xff) {
l->add(MidiPlayEvent(tick, port, channel, ME_CONTROLLER, CTRL_HBANK, hb));
++tickoffset;
}
if (lb != 0xff) {
l->add(MidiPlayEvent(tick+tickoffset, port, channel, ME_CONTROLLER, CTRL_LBANK, lb));
++tickoffset;
}
break;
}
l->add(MidiPlayEvent(tick+tickoffset, port, channel, ME_PROGRAM, pr, 0));
}
//else if (a < 0x60000) { // RPN14 Controller
else if (a < CTRL_NRPN14_OFFSET) { // RPN14 Controller
int ctrlH = (a >> 8) & 0x7f;
int ctrlL = a & 0x7f;
int dataH = (b >> 7) & 0x7f;
int dataL = b & 0x7f;
l->add(MidiPlayEvent(tick, port, channel, ME_CONTROLLER, CTRL_HRPN, ctrlH));
l->add(MidiPlayEvent(tick+1, port, channel, ME_CONTROLLER, CTRL_LRPN, ctrlL));
l->add(MidiPlayEvent(tick+2, port, channel, ME_CONTROLLER, CTRL_HDATA, dataH));
l->add(MidiPlayEvent(tick+3, port, channel, ME_CONTROLLER, CTRL_LDATA, dataL));
}
//else if (a < 0x70000) { // NRPN14 Controller
else if (a < CTRL_NONE_OFFSET) { // NRPN14 Controller
int ctrlH = (a >> 8) & 0x7f;
int ctrlL = a & 0x7f;
int dataH = (b >> 7) & 0x7f;
int dataL = b & 0x7f;
l->add(MidiPlayEvent(tick, port, channel, ME_CONTROLLER, CTRL_HNRPN, ctrlH));
l->add(MidiPlayEvent(tick+1, port, channel, ME_CONTROLLER, CTRL_LNRPN, ctrlL));
l->add(MidiPlayEvent(tick+2, port, channel, ME_CONTROLLER, CTRL_HDATA, dataH));
l->add(MidiPlayEvent(tick+3, port, channel, ME_CONTROLLER, CTRL_LDATA, dataL));
}
}
} // namespace MusECore
namespace MusEGui {
//---------------------------------------------------------
// exportMidi
//---------------------------------------------------------
void MusE::exportMidi()
{
MusEGui::MFile file(QString("midis"), QString(".mid"));
//FILE* fp = file.open("w", midi_file_pattern, this, false, true,
FILE* fp = file.open("w", MusEGlobal::midi_file_save_pattern, this, false, true,
tr("MusE: Export Midi"));
if (fp == 0)
return;
MusECore::MidiFile mf(fp);
//MusECore::MidiTrackList* tl = MusEGlobal::song->midis();
MusECore::TrackList* tl = MusEGlobal::song->tracks(); // Changed to full track list so user can rearrange tracks.
//int ntracks = tl->size();
MusECore::MidiFileTrackList* mtl = new MusECore::MidiFileTrackList;
int i = 0;
MusECore::MidiFileTrack* mft = 0;
//for (MusECore::iMidiTrack im = tl->begin(); im != tl->end(); ++im, ++i) {
for (MusECore::ciTrack im = tl->begin(); im != tl->end(); ++im) {
if(!(*im)->isMidiTrack())
continue;
MusECore::MidiTrack* track = (MusECore::MidiTrack*)(*im);
//MusECore::MidiFileTrack* mft = new MusECore::MidiFileTrack;
if (i == 0 || (i != 0 && MusEGlobal::config.smfFormat != 0)) // Changed to single track. Tim
{
mft = new MusECore::MidiFileTrack;
mtl->push_back(mft);
}
MusECore::MPEventList* l = &(mft->events);
int port = track->outPort();
int channel = track->outChannel();
//---------------------------------------------------
// only first midi track contains
// - Track Marker
// - copyright
// - time signature
// - tempo map
// - GM/GS/XG Initialization
//---------------------------------------------------
if (i == 0) {
//---------------------------------------------------
// Write Track Marker
//
MusECore::MarkerList* ml = MusEGlobal::song->marker();
for (MusECore::ciMarker m = ml->begin(); m != ml->end(); ++m) {
QByteArray ba = m->second.name().toLatin1();
const char* name = ba.constData();
int len = strlen(name);
MusECore::MidiPlayEvent ev(m->first, port, MusECore::ME_META, (unsigned char*)name, len);
ev.setA(0x6);
l->add(ev);
}
//---------------------------------------------------
// Write Copyright
//
QByteArray ba = MusEGlobal::config.copyright.toLatin1();
const char* copyright = ba.constData();
if (copyright && *copyright) {
int len = strlen(copyright);
MusECore::MidiPlayEvent ev(0, port, MusECore::ME_META, (unsigned char*)copyright, len);
ev.setA(0x2);
l->add(ev);
}
//---------------------------------------------------
// Write Coment
//
//if (MusEGlobal::config.smfFormat == 0) // Only for smf 0 added by Tim. FIXME: Is this correct? See below.
{
QString comment = track->comment();
if (!comment.isEmpty()) {
int len = comment.length();
MusECore::MidiPlayEvent ev(0, port, MusECore::ME_META, (const unsigned char*)(comment.toLatin1().constData()), len);
ev.setA(0x1);
l->add(ev);
}
}
//---------------------------------------------------
// Write Songtype SYSEX: GM/GS/XG
//
switch(MusEGlobal::song->mtype()) {
case MT_GM:
l->add(MusECore::MidiPlayEvent(0, port, MusECore::ME_SYSEX, MusECore::gmOnMsg, MusECore::gmOnMsgLen));
break;
case MT_GS:
l->add(MusECore::MidiPlayEvent(0, port, MusECore::ME_SYSEX, MusECore::gmOnMsg, MusECore::gmOnMsgLen));
l->add(MusECore::MidiPlayEvent(250, port, MusECore::ME_SYSEX, MusECore::gsOnMsg, MusECore::gsOnMsgLen));
break;
case MT_XG:
l->add(MusECore::MidiPlayEvent(0, port, MusECore::ME_SYSEX, MusECore::gmOnMsg, MusECore::gmOnMsgLen));
l->add(MusECore::MidiPlayEvent(250, port, MusECore::ME_SYSEX, MusECore::xgOnMsg, MusECore::xgOnMsgLen));
break;
case MT_UNKNOWN:
break;
}
//---------------------------------------------------
// Write Tempomap
//
MusECore::TempoList* tl = &MusEGlobal::tempomap;
for (MusECore::ciTEvent e = tl->begin(); e != tl->end(); ++e) {
MusECore::TEvent* event = e->second;
unsigned char data[3];
int tempo = event->tempo;
data[2] = tempo & 0xff;
data[1] = (tempo >> 8) & 0xff;
data[0] = (tempo >> 16) & 0xff;
MusECore::MidiPlayEvent ev(event->tick, port, MusECore::ME_META, data, 3);
ev.setA(0x51);
l->add(ev);
}
//---------------------------------------------------
// Write Signatures
//
///const SigList* sl = &sigmap;
const AL::SigList* sl = &AL::sigmap;
///for (ciSigEvent e = sl->begin(); e != sl->end(); ++e) {
for (AL::ciSigEvent e = sl->begin(); e != sl->end(); ++e) {
///SigEvent* event = e->second;
AL::SigEvent* event = e->second;
int sz = (MusEGlobal::config.exp2ByteTimeSigs ? 2 : 4); // export 2 byte timesigs instead of 4 ?
unsigned char data[sz];
data[0] = event->sig.z;
switch(event->sig.n) {
case 1: data[1] = 0; break;
case 2: data[1] = 1; break;
case 4: data[1] = 2; break;
case 8: data[1] = 3; break;
case 16: data[1] = 4; break;
case 32: data[1] = 5; break;
case 64: data[1] = 6; break;
default:
fprintf(stderr, "falsche Signatur; nenner %d\n", event->sig.n);
break;
}
// By T356. In muse the metronome pulse is fixed at 24 (once per quarter-note).
// The number of 32nd notes per 24 MIDI clock signals (per quarter-note) is 8.
if(!MusEGlobal::config.exp2ByteTimeSigs)
{
data[2] = 24;
data[3] = 8;
}
MusECore::MidiPlayEvent ev(event->tick, port, MusECore::ME_META, data, sz);
ev.setA(0x58);
l->add(ev);
}
}
//-----------------------------------
// track name
//-----------------------------------
if (i == 0 || (i != 0 && MusEGlobal::config.smfFormat != 0))
{
if (!track->name().isEmpty()) {
QByteArray ba = track->name().toLatin1();
const char* name = ba.constData();
int len = strlen(name);
MusECore::MidiPlayEvent ev(0, port, MusECore::ME_META, (unsigned char*)name, len+1);
ev.setA(0x3); // Meta Sequence/Track Name
l->add(ev);
}
}
//-----------------------------------
// track comment
//-----------------------------------
// FIXME: What are these 0x0F? All I found was that they are unspecified part of the sixteen text meta events.
// So why not use 0x01? And do we include it for all tracks, or only above 1? Tim.
//if (i == 0 || (i != 0 && MusEGlobal::config.smfFormat != 0))
//if (i != 0 && MusEGlobal::config.smfFormat != 0)
if (MusEGlobal::config.smfFormat != 0)
{
if (!track->comment().isEmpty()) {
QByteArray ba = track->comment().toLatin1();
const char* comment = ba.constData();
int len = strlen(comment);
MusECore::MidiPlayEvent ev(0, port, MusECore::ME_META, (unsigned char*)comment, len+1);
ev.setA(0xf); // Meta Text
l->add(ev);
}
}
MusECore::PartList* parts = track->parts();
for (MusECore::iPart p = parts->begin(); p != parts->end(); ++p) {
MusECore::MidiPart* part = (MusECore::MidiPart*) (p->second);
MusECore::EventList* evlist = part->events();
for (MusECore::iEvent i = evlist->begin(); i != evlist->end(); ++i) {
MusECore::Event ev = i->second;
int tick = ev.tick() + part->tick();
switch (ev.type()) {
case MusECore::Note:
{
if (ev.velo() == 0) {
printf("Warning: midi note has velocity 0, (ignored)\n");
continue;
}
int pitch;
if (track->type() == MusECore::Track::DRUM) {
//
// Map drum-notes to the drum-map values
//
int instr = ev.pitch();
pitch = MusEGlobal::drumMap[instr].anote;
// port = MusEGlobal::drumMap[instr].port;
// channel = MusEGlobal::drumMap[instr].channel;
}
else
pitch = ev.pitch();
int velo = ev.velo();
int len = ev.lenTick();
//---------------------------------------
// apply trackinfo values
//---------------------------------------
if (track->transposition
|| track->velocity
|| track->compression != 100
|| track->len != 100) {
pitch += track->transposition;
if (pitch > 127)
pitch = 127;
if (pitch < 0)
pitch = 0;
velo += track->velocity;
velo = (velo * track->compression) / 100;
if (velo > 127)
velo = 127;
if (velo < 1) // no off event
velo = 1;
len = (len * track->len) / 100;
}
if (len <= 0)
len = 1;
l->add(MusECore::MidiPlayEvent(tick, port, channel, MusECore::ME_NOTEON, pitch, velo));
if(MusEGlobal::config.expOptimNoteOffs) // Save space by replacing note offs with note on velocity 0
l->add(MusECore::MidiPlayEvent(tick+len, port, channel, MusECore::ME_NOTEON, pitch, 0));
else
l->add(MusECore::MidiPlayEvent(tick+len, port, channel, MusECore::ME_NOTEOFF, pitch, velo));
}
break;
case MusECore::Controller:
addController(l, tick, port, channel, ev.dataA(), ev.dataB());
break;
case MusECore::Sysex:
l->add(MusECore::MidiPlayEvent(tick, port, MusECore::ME_SYSEX, ev.eventData()));
break;
case MusECore::PAfter:
l->add(MusECore::MidiPlayEvent(tick, port, channel, MusECore::ME_AFTERTOUCH, ev.dataA(), ev.dataB()));
break;
case MusECore::CAfter:
l->add(MusECore::MidiPlayEvent(tick, port, channel, MusECore::ME_POLYAFTER, ev.dataA(), ev.dataB()));
break;
case MusECore::Meta:
{
MusECore::MidiPlayEvent mpev(tick, port, MusECore::ME_META, ev.eventData());
mpev.setA(ev.dataA());
l->add(mpev);
}
break;
case MusECore::Wave:
break;
}
}
}
++i;
}
mf.setDivision(MusEGlobal::config.midiDivision);
mf.setMType(MusEGlobal::song->mtype());
//mf.setTrackList(mtl, ntracks);
mf.setTrackList(mtl, i);
mf.write();
// TESTING: Cleanup. I did not valgrind this feature in last memleak fixes, but I suspect it leaked.
//for(MusECore::iMidiFileTrack imft = mtl->begin(); imft != mtl->end(); ++imft)
// delete *imft;
//delete mtl;
}
} // namespace MusEGui
|