2 Copyright (C) 1998 Paul Barton-Davis
4 This file was inspired by the MIDI parser for KeyKit by
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
31 #include "midi++/types.h"
32 #include "midi++/parser.h"
33 #include "midi++/port.h"
34 #include "midi++/mmc.h"
35 #include "pbd/transmitter.h"
41 Parser::midi_event_type_name (eventType t)
46 return "no midi messages";
49 return "raw midi data";
52 return "any midi message";
63 case MIDI::controller:
67 return "program change";
70 return "channel pressure";
76 return "system exclusive";
79 return "song position";
85 return "end of sysex";
100 return "active sense";
103 return "unknow MIDI event type";
107 Parser::Parser (Port &p)
113 memset (message_counter, 0, sizeof (message_counter[0]) * 256);
117 msgbuf = (unsigned char *) malloc (msglen);
118 msgbuf[msgindex++] = 0x90;
119 _mmc_forward = false;
123 /* this hack deals with the possibility of our first MIDI
124 bytes being running status messages.
130 pre_variable_state = NEEDSTATUS;
131 pre_variable_msgtype = none;
141 Parser::trace_event (Parser &, byte *msg, size_t len)
146 if ((o = trace_stream) == NULL) { /* can be asynchronously removed */
150 type = (eventType) (msg[0]&0xF0);
157 << " NoteOff NoteNum "
168 << " NoteOn NoteNum "
184 case MIDI::controller:
199 << " Program PropertyChange ProgNum "
208 << " Channel Pressure "
213 case MIDI::pitchbend:
218 << ((msg[2]<<7)|msg[1])
257 << "System Exclusive (1 byte : " << hex << (int) *msg << dec << ')'
263 << "System Exclusive (" << len << ") = [ " << hex;
264 for (unsigned int i = 0; i < len; ++i) {
265 *o << (int) msgbuf[i] << ' ';
267 *o << dec << ']' << endmsg;
273 *o << trace_prefix << "Song" << endmsg;
277 *o << trace_prefix << "Tune" << endmsg;
281 *o << trace_prefix << "End-of-System Exclusive" << endmsg;
285 *o << trace_prefix << "Timing" << endmsg;
289 *o << trace_prefix << "Start" << endmsg;
293 *o << trace_prefix << "Stop" << endmsg;
297 *o << trace_prefix << "Continue" << endmsg;
301 *o << trace_prefix << "Active Sense" << endmsg;
305 *o << trace_prefix << "Unrecognized MIDI message" << endmsg;
311 Parser::trace (bool onoff, ostream *o, const string &prefix)
313 trace_connection.disconnect ();
317 trace_prefix = prefix;
318 any.connect_same_thread (trace_connection, boost::bind (&Parser::trace_event, this, _1, _2, _3));
326 Parser::scanner (unsigned char inbyte)
330 // cerr << "parse: " << hex << (int) inbyte << dec << " state = " << state << " msgindex = " << msgindex << " runnable = " << runnable << endl;
332 /* Check active sensing early, so it doesn't interrupt sysex.
334 NOTE: active sense messages are not considered to fit under
335 "any" for the purposes of callbacks. If a caller wants
336 active sense messages handled, which is unlikely, then
337 they can just ask for it specifically. They are so unlike
338 every other MIDI message in terms of semantics that its
339 counter-productive to treat them similarly.
342 if (inbyte == 0xfe) {
343 message_counter[inbyte]++;
345 active_sense (*this);
350 /* If necessary, allocate larger message buffer. */
352 if (msgindex >= msglen) {
354 msgbuf = (unsigned char *) realloc (msgbuf, msglen);
358 Real time messages can occur ANYPLACE,
359 but do not interrupt running status.
389 boost::optional<int> res = edit (&inbyte, 1);
391 if (res.get_value_or (1) >= 0 && !_offline) {
392 realtime_msg (inbyte);
398 statusbit = (inbyte & 0x80);
401 * Variable length messages (ie. the 'system exclusive')
402 * can be terminated by the next status byte, not necessarily
403 * an EOX. Actually, since EOX is a status byte, this
404 * code ALWAYS handles the end of a VARIABLELENGTH message.
407 if (state == VARIABLELENGTH && statusbit) {
409 /* The message has ended, so process it */
411 /* add EOX to any sysex message */
413 if (inbyte == MIDI::eox) {
414 msgbuf[msgindex++] = inbyte;
418 cerr << "SYSEX: " << hex;
419 for (unsigned int i = 0; i < msgindex; ++i) {
420 cerr << (int) msgbuf[i] << ' ';
426 boost::optional<int> res = edit (msgbuf, msgindex);
428 if (res.get_value_or (1) >= 0) {
429 if (!possible_mmc (msgbuf, msgindex) || _mmc_forward) {
430 if (!possible_mtc (msgbuf, msgindex) || _mtc_forward) {
432 sysex (*this, msgbuf, msgindex);
437 any (*this, msgbuf, msgindex);
444 * Status bytes always start a new message, except EOX
451 if (inbyte == MIDI::eox) {
452 /* return to the state we had pre-sysex */
454 state = pre_variable_state;
455 runnable = was_runnable;
456 msgtype = pre_variable_msgtype;
458 if (state != NEEDSTATUS && runnable) {
459 msgbuf[msgindex++] = last_status_byte;
462 msgbuf[msgindex++] = inbyte;
463 if ((inbyte & 0xf0) == 0xf0) {
467 channel_msg (inbyte);
475 * We've got a Data byte.
478 msgbuf[msgindex++] = inbyte;
483 * We shouldn't get here, since in NEEDSTATUS mode
484 * we're expecting a new status byte, NOT any
485 * data bytes. On the other hand, some equipment
486 * with leaky modwheels and the like might be
487 * sending data bytes as part of running controller
488 * messages, so just handle it silently.
493 /* wait for the second byte */
499 /* We've completed a 1 or 2 byte message. */
500 if (edit.empty() || !(*edit (msgbuf, msgindex) == 0)) {
502 /* message not cancelled by an editor */
504 message_counter[msgbuf[0] & 0xF0]++;
507 signal (msgbuf, msgindex);
512 /* In Runnable mode, we reset the message
513 index, but keep the callbacks_pending and state the
514 same. This provides the "running status
519 /* If not Runnable, reset to NEEDSTATUS mode */
531 /** Call the real-time function for the specified byte, immediately.
532 * These can occur anywhere, so they don't change the state.
535 Parser::realtime_msg(unsigned char inbyte)
538 message_counter[inbyte]++;
546 timing (*this, _timestamp);
549 start (*this, _timestamp);
552 contineu (*this, _timestamp);
555 stop (*this, _timestamp);
558 /* !!! active sense message in realtime_msg: should not reach here
566 any (*this, &inbyte, 1);
570 /** Interpret a Channel (voice or mode) Message status byte.
573 Parser::channel_msg(unsigned char inbyte)
575 last_status_byte = inbyte;
576 runnable = true; /* Channel messages can use running status */
578 /* The high 4 bits, which determine the type of channel message. */
580 switch (inbyte&0xF0) {
583 state = NEEDTWOBYTES;
587 state = NEEDTWOBYTES;
591 state = NEEDTWOBYTES;
594 msgtype = MIDI::controller;
595 state = NEEDTWOBYTES;
606 msgtype = MIDI::pitchbend;
607 state = NEEDTWOBYTES;
612 /** Initialize (and possibly emit) the signals for the
613 * specified byte. Set the state that the state-machine
614 * should go into. If the signal is not emitted
615 * immediately, it will be when the state machine gets to
616 * the end of the MIDI message.
619 Parser::system_msg (unsigned char inbyte)
621 message_counter[inbyte]++;
625 pre_variable_msgtype = msgtype;
626 pre_variable_state = state;
627 was_runnable = runnable;
628 msgtype = MIDI::sysex;
629 state = VARIABLELENGTH;
632 msgtype = MIDI::mtc_quarter;
636 msgtype = MIDI::position;
637 state = NEEDTWOBYTES;
640 msgtype = MIDI::song;
653 // all these messages will be sent via any()
654 // when they are complete.
655 // any (*this, &inbyte, 1);
659 Parser::signal (byte *msg, size_t len)
661 channel_t chan = msg[0]&0xF;
669 channel_active_preparse[chan_i] (*this);
670 note_off (*this, (EventTwoBytes *) &msg[1]);
671 channel_note_off[chan_i]
672 (*this, (EventTwoBytes *) &msg[1]);
673 channel_active_postparse[chan_i] (*this);
677 channel_active_preparse[chan_i] (*this);
679 /* Hack to deal with MIDI sources that use velocity=0
684 note_off (*this, (EventTwoBytes *) &msg[1]);
685 channel_note_off[chan_i]
686 (*this, (EventTwoBytes *) &msg[1]);
688 note_on (*this, (EventTwoBytes *) &msg[1]);
689 channel_note_on[chan_i]
690 (*this, (EventTwoBytes *) &msg[1]);
693 channel_active_postparse[chan_i] (*this);
696 case MIDI::controller:
697 channel_active_preparse[chan_i] (*this);
698 controller (*this, (EventTwoBytes *) &msg[1]);
699 channel_controller[chan_i]
700 (*this, (EventTwoBytes *) &msg[1]);
701 channel_active_postparse[chan_i] (*this);
705 channel_active_preparse[chan_i] (*this);
706 program_change (*this, msg[1]);
707 channel_program_change[chan_i] (*this, msg[1]);
708 channel_active_postparse[chan_i] (*this);
712 channel_active_preparse[chan_i] (*this);
713 pressure (*this, msg[1]);
714 channel_pressure[chan_i] (*this, msg[1]);
715 channel_active_postparse[chan_i] (*this);
719 channel_active_preparse[chan_i] (*this);
720 poly_pressure (*this, (EventTwoBytes *) &msg[1]);
721 channel_poly_pressure[chan_i]
722 (*this, (EventTwoBytes *) &msg[1]);
723 channel_active_postparse[chan_i] (*this);
726 case MIDI::pitchbend:
727 channel_active_preparse[chan_i] (*this);
728 pitchbend (*this, (msg[2]<<7)|msg[1]);
729 channel_pitchbend[chan_i] (*this, (msg[2]<<7)|msg[1]);
730 channel_active_postparse[chan_i] (*this);
734 sysex (*this, msg, len);
737 case MIDI::mtc_quarter:
738 process_mtc_quarter_frame (msg);
739 mtc_quarter_frame (*this, *msg);
743 position (*this, msg, len);
747 song (*this, msg, len);
754 /* XXX some kind of warning ? */
758 any (*this, msg, len);
762 Parser::possible_mmc (byte *msg, size_t msglen)
764 if (!MachineControl::is_mmc (msg, msglen)) {
768 /* hand over the just the interior MMC part of
769 the sysex msg without the leading 0xF0
773 mmc (*this, &msg[1], msglen - 1);
780 Parser::set_offline (bool yn)
782 if (_offline != yn) {
784 OfflineStatusChanged ();
786 /* this hack deals with the possibility of our first MIDI
787 bytes being running status messages.