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 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483
|
//=============================================================================
// MuseScore
// Music Composition & Notation
//
// Copyright (C) 2008-2011 Werner Schweer
//
// 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 "libmscore/xml.h"
#include "libmscore/note.h"
#include "libmscore/sig.h"
#include "libmscore/tie.h"
#include "event.h"
#include "libmscore/staff.h"
#include "libmscore/instrument.h"
#include "libmscore/part.h"
namespace Ms {
//---------------------------------------------------------
// MidiCoreEvent::write
//---------------------------------------------------------
void MidiCoreEvent::write(XmlWriter& xml) const
{
switch(_type) {
case ME_NOTEON:
xml.tagE(QString("note-on channel=\"%1\" pitch=\"%2\" velo=\"%3\"")
.arg(_channel).arg(_a).arg(_b));
break;
case ME_NOTEOFF:
xml.tagE(QString("note-off channel=\"%1\" pitch=\"%2\" velo=\"%3\"")
.arg(_channel).arg(_a).arg(_b));
break;
case ME_CONTROLLER:
if (_a == CTRL_PROGRAM) {
if (_channel == 0) {
xml.tagE(QString("program value=\"%1\"").arg(_b));
}
else {
xml.tagE(QString("program channel=\"%1\" value=\"%2\"")
.arg(channel()).arg(_b));
}
}
else {
if (channel() == 0) {
xml.tagE(QString("controller ctrl=\"%1\" value=\"%2\"")
.arg(_a).arg(_b));
}
else {
xml.tagE(QString("controller channel=\"%1\" ctrl=\"%2\" value=\"%3\"")
.arg(channel()).arg(_a).arg(_b));
}
}
break;
default:
qDebug("MidiCoreEvent::write: unknown type");
break;
}
}
//---------------------------------------------------------
// Event::Event
//---------------------------------------------------------
Event::Event()
{
_type = 0;
_ontime = -1;
_noquantOntime = 0;
_noquantDuration = 0;
_channel = 0;
_a = 0;
_b = 0;
_duration = 0;
_tpc = 0;
_voice = 0;
_edata = 0;
_len = 0;
_metaType = 0;
_note = 0;
_tuning = 0.0;
}
Event::Event(int t)
{
_type = t;
_ontime = -1;
_noquantOntime = 0;
_noquantDuration = 0;
_channel = 0;
_a = 0;
_b = 0;
_duration = 0;
_tpc = 0;
_voice = 0;
_edata = 0;
_len = 0;
_metaType = 0;
_note = 0;
_tuning = 0.0;
}
Event::Event(const Event& e)
: PlayEvent(e)
{
_type = e._type;
_ontime = e._ontime;
_noquantOntime = e._noquantOntime;
_noquantDuration = e._noquantDuration;
_channel = e._channel;
_a = e._a;
_b = e._b;
_duration = e._duration;
_tpc = e._tpc;
_voice = e._voice;
_notes = e._notes;
if (e._edata) {
_edata = new unsigned char[e._len + 1]; // don’t forget trailing zero
memcpy(_edata, e._edata, e._len+1);
}
else
_edata = 0;
_len = e._len;
_metaType = e._metaType;
_note = e._note;
_tuning = e._tuning;
}
Event::~Event()
{
delete[] _edata;
}
//---------------------------------------------------------
// NPlayEvent::NPlayEvent (beatType2metronomeEvent)
//---------------------------------------------------------
NPlayEvent::NPlayEvent(BeatType beatType)
{
setType(ME_TICK2);
setVelo(127);
switch (beatType) {
case BeatType::DOWNBEAT:
setType(ME_TICK1);
break;
case BeatType::SIMPLE_STRESSED:
case BeatType::COMPOUND_STRESSED:
// use defaults
break;
case BeatType::SIMPLE_UNSTRESSED:
case BeatType::COMPOUND_UNSTRESSED:
setVelo(80);
break;
case BeatType::COMPOUND_SUBBEAT:
setVelo(25);
break;
case BeatType::SUBBEAT:
setVelo(15);
break;
}
}
//---------------------------------------------------------
// isMuted
//---------------------------------------------------------
bool NPlayEvent::isMuted() const
{
if (!notes.empty()) {
const Note* n = notes[0];
MasterScore* cs = n->masterScore();
Staff* staff = n->staff();
Instrument* instr = staff->part()->instrument(n->tick());
const Channel* a = instr->playbackChannel(n->subchannel(), cs);
return a->mute() || a->soloMute() || !staff->playbackVoice(n->voice());
}
return false;
}
//---------------------------------------------------------
// dump
//---------------------------------------------------------
void Event::dump() const
{
printf("event ");
switch (_type) {
case ME_NOTEON: printf("noteon "); break;
case ME_CONTROLLER: printf("controller"); break;
case ME_PROGRAM: printf("program "); break;
default: printf("0x%02x ", _type); break;
}
printf(" 0x%02x 0x%02x\n", _a, _b);
}
//---------------------------------------------------------
// isChannelEvent
//---------------------------------------------------------
bool MidiCoreEvent::isChannelEvent() const
{
switch(_type) {
case ME_NOTEOFF:
case ME_NOTEON:
case ME_POLYAFTER:
case ME_CONTROLLER:
case ME_PROGRAM:
case ME_AFTERTOUCH:
case ME_PITCHBEND:
case ME_NOTE:
case ME_CHORD:
return true;
default:
return false;
}
// Prevent "unreachable code" warning.
// return false;
}
//---------------------------------------------------------
// Event::write
//---------------------------------------------------------
void Event::write(XmlWriter& xml) const
{
switch(_type) {
case ME_NOTE:
xml.tagE(QString("note tick=\"%1\" channel=\"%2\" len=\"%3\" pitch=\"%4\" velo=\"%5\"")
.arg(_ontime).arg(_channel).arg(_duration).arg(_a).arg(_b));
break;
case ME_NOTEON:
xml.tagE(QString("note-on tick=\"%1\" channel=\"%2\" pitch=\"%3\" velo=\"%4\"")
.arg(_ontime).arg(_channel).arg(_a).arg(_b));
break;
case ME_NOTEOFF:
xml.tagE(QString("note-off tick=\"%1\" channel=\"%2\" pitch=\"%3\" velo=\"%4\"")
.arg(_ontime).arg(_channel).arg(_a).arg(_b));
break;
case ME_CONTROLLER:
if (_a == CTRL_PROGRAM) {
if ((_ontime == -1) && (_channel == 0)) {
xml.tagE(QString("program value=\"%1\"").arg(_b));
}
else {
xml.tagE(QString("program tick=\"%1\" channel=\"%2\" value=\"%3\"")
.arg(ontime()).arg(channel()).arg(_b));
}
}
else {
if ((ontime() == -1) && (channel() == 0)) {
xml.tagE(QString("controller ctrl=\"%1\" value=\"%2\"")
.arg(_a).arg(_b));
}
else {
xml.tagE(QString("controller tick=\"%1\" channel=\"%2\" ctrl=\"%3\" value=\"%4\"")
.arg(ontime()).arg(channel()).arg(_a).arg(_b));
}
}
break;
case ME_SYSEX:
xml.stag(QString("sysex tick=\"%1\" len=\"%2\"").arg(ontime()).arg(_len));
xml.dump(_len, _edata);
xml.etag();
break;
case ME_META:
switch(metaType()) {
case META_TRACK_NAME:
xml.tag(QString("TrackName tick=\"%1\"").arg(ontime()), QString((char*)(edata())));
break;
case META_LYRIC:
xml.tag(QString("Lyric tick=\"%1\"").arg(ontime()), QString((char*)(edata())));
break;
case META_KEY_SIGNATURE:
{
const char* keyTable[] = {
"Ces", "Ges", "Des", "As", "Es", "Bes", "F",
"C",
"G", "D", "A", "E", "B", "Fis", "Cis"
};
int key = (char)(_edata[0]) + 7;
if (key < 0 || key > 14) {
qDebug("bad key signature %d", key);
key = 0;
}
QString sex(_edata[1] ? "Minor" : "Major");
QString keyName(keyTable[key]);
xml.tag(QString("Key tick=\"%1\" key=\"%2\" sex=\"%3\"").arg(ontime()).arg(_edata[0]).arg(_edata[1]),
QString("%1 %2").arg(keyName).arg(sex));
}
break;
case META_TIME_SIGNATURE:
xml.tagE(QString("TimeSig tick=\"%1\" num=\"%2\" denom=\"%3\" metro=\"%4\" quarter=\"%5\"")
.arg(ontime())
.arg(int(_edata[0]))
.arg(int(_edata[1]))
.arg(int(_edata[2]))
.arg(int(_edata[3])));
break;
case META_TEMPO:
{
unsigned tempo = _edata[2] + (_edata[1] << 8) + (_edata[0] << 16);
xml.tagE(QString("Tempo tick=\"%1\" value=\"%2\"").arg(ontime()).arg(tempo));
}
break;
default:
xml.stag(QString("Meta tick=\"%1\" type=\"%2\" len=\"%3\" name=\"%4\"")
.arg(ontime()).arg(metaType()).arg(_len).arg(midiMetaName(metaType())));
xml.dump(_len, _edata);
xml.etag();
break;
}
break;
}
}
bool Event::operator==(const Event&) const
{
return false; // TODO
}
//---------------------------------------------------------
// midi_meta_name
//---------------------------------------------------------
QString midiMetaName(int meta)
{
const char* s = "";
switch (meta) {
case 0: s = "Sequence Number"; break;
case 1: s = "Text Event"; break;
case 2: s = "Copyright"; break;
case 3: s = "Sequence/Track Name"; break;
case 4: s = "Instrument Name"; break;
case 5: s = "Lyric"; break;
case 6: s = "Marker"; break;
case 7: s = "Cue Point"; break;
case 8:
case 9:
case 0x0a:
case 0x0b:
case 0x0c:
case 0x0d:
case 0x0e:
case 0x0f: s = "Text"; break;
case 0x20: s = "Channel Prefix"; break;
case 0x21: s = "Port Change"; break;
case 0x2f: s = "End of Track"; break;
case META_TEMPO: s = "Tempo"; break;
case 0x54: s = "SMPTE Offset"; break;
case META_TIME_SIGNATURE: s = "Time Signature"; break;
case META_KEY_SIGNATURE: s = "Key Signature"; break;
case 0x74: s = "Sequencer-Specific1"; break;
case 0x7f: s = "Sequencer-Specific2"; break;
default:
break;
}
return QString(s);
}
//---------------------------------------------------------
// insert
//---------------------------------------------------------
void EventList::insert(const Event& e)
{
int ontime = e.ontime();
if (!isEmpty() && last().ontime() > ontime) {
for (auto i = begin(); i != end(); ++i) {
if (i->ontime() > ontime) {
QList<Event>::insert(i, e);
return;
}
}
}
append(e);
}
//---------------------------------------------------------
// class EventMap::fixupMIDI
//---------------------------------------------------------
static void logCollisionNote(const Note* note, unsigned int indent)
{
QString info = note->accessibleInfo() + note->accessibleBarbeat();
fprintf(stderr, " %*s%s\n", indent, "- ", qPrintable(info));
}
static void logCollisionNotes(const NPlayEvent* event)
{
for (auto it = event->notes.cbegin(); it != event->notes.cend(); ++it) {
const Note* note1 = *it;
unsigned int indent = /* initial */ 2;
while (note1) {
logCollisionNote(note1, indent);
indent += /* linked */ 4;
for (ScoreElement* se : note1->linkList()) {
if (se != note1 && se->isNote())
logCollisionNote(toNote(se), indent);
}
indent -= /* linked */ 4 - /* next level */ 2;
note1 = note1->tieFor() ? note1->tieFor()->endNote() : 0;
}
}
}
void EventMap::fixupMIDI()
{
/* track info for each of the 128 possible MIDI notes */
struct channelInfo {
/* which event the first ME_NOTEON came from */
NPlayEvent *event[128];
/* how often is the note on right now? */
unsigned short nowPlaying[128];
};
/* track info for each channel (on the heap, 0-initialised) */
struct channelInfo *info = (struct channelInfo *)calloc(_highestChannel + 1, sizeof(struct channelInfo));
auto it = begin();
while (it != end()) {
bool discard = false;
NPlayEvent& event = it->second;
/* ME_NOTEOFF is never emitted, no need to check for it */
if (event.type() == ME_NOTEON && !event.isMuted()) {
unsigned short np = info[event.channel()].nowPlaying[event.pitch()];
if (event.velo() == 0) {
/* already off (should not happen) or still playing? */
if (np == 0 || --np > 0)
discard = true;
else {
/* hoist NOTEOFF to same track as NOTEON */
event.setOriginatingStaff(info[event.channel()].event[event.pitch()]->getOriginatingStaff());
/* copy linked Notes */
event.notes = info[event.channel()].event[event.pitch()]->notes;
}
}
else if (++np > 1) {
/* already playing */
discard = true;
/* log the collision */
fprintf(stderr, "MIDI collision detected: newly played...\n");
logCollisionNotes(&event);
fprintf(stderr, " ... interferes with already playing...\n");
logCollisionNotes(info[event.channel()].event[event.pitch()]);
fprintf(stderr, " ... which will continue to sound.\n");
/* carry over the corresponding score notes */
info[event.channel()].event[event.pitch()]->notes.insert(info[event.channel()].event[event.pitch()]->notes.end(), event.notes.begin(), event.notes.end());
}
else {
info[event.channel()].event[event.pitch()] = &event;
}
info[event.channel()].nowPlaying[event.pitch()] = np;
}
if (discard)
it = erase(it);
else
++it;
}
free((void *)info);
}
}
|