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.
29 #include <midi++/types.h>
30 #include <midi++/parser.h>
31 #include <midi++/port.h>
32 #include <midi++/mmc.h>
33 #include <pbd/transmitter.h>
40 Parser::midi_event_type_name (eventType t)
45 return "no midi messages";
48 return "raw midi data";
51 return "any midi message";
62 case MIDI::controller:
66 return "program change";
69 return "channel pressure";
75 return "system exclusive";
78 return "song position";
84 return "end of sysex";
99 return "active sense";
102 return "unknow MIDI event type";
106 Parser::Parser (Port &p)
112 memset (message_counter, 0, sizeof (message_counter[0]) * 256);
116 msgbuf = (unsigned char *) malloc (msglen);
117 msgbuf[msgindex++] = 0x90;
118 _mmc_forward = false;
122 /* this hack deals with the possibility of our first MIDI
123 bytes being running status messages.
129 pre_variable_state = NEEDSTATUS;
130 pre_variable_msgtype = none;
140 Parser::trace_event (Parser &p, 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 Change ProgNum "
208 << " Channel Pressure "
213 case MIDI::pitchbend:
218 << ((msg[1]<<7)|msg[2])
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)
314 trace_connection.disconnect ();
318 trace_prefix = prefix;
319 trace_connection = any.connect
320 (mem_fun (*this, &Parser::trace_event));
328 Parser::scanner (unsigned char inbyte)
332 // cerr << "parse: " << hex << (int) inbyte << dec << " state = " << state << " msgindex = " << msgindex << " runnable = " << runnable << endl;
334 /* Check active sensing early, so it doesn't interrupt sysex.
336 NOTE: active sense messages are not considered to fit under
337 "any" for the purposes of callbacks. If a caller wants
338 active sense messages handled, which is unlikely, then
339 they can just ask for it specifically. They are so unlike
340 every other MIDI message in terms of semantics that its
341 counter-productive to treat them similarly.
344 if (inbyte == 0xfe) {
345 message_counter[inbyte]++;
347 active_sense (*this);
352 /* If necessary, allocate larger message buffer. */
354 if (msgindex >= msglen) {
356 msgbuf = (unsigned char *) realloc (msgbuf, msglen);
360 Real time messages can occur ANYPLACE,
361 but do not interrupt running status.
391 if (edit (&inbyte, 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) {
408 /* The message has ended, so process it */
410 /* add EOX to any sysex message */
412 if (inbyte == MIDI::eox) {
413 msgbuf[msgindex++] = inbyte;
417 cerr << "SYSEX: " << hex;
418 for (unsigned int i = 0; i < msgindex; ++i) {
419 cerr << (int) msgbuf[i] << ' ';
423 if (msgindex > 0 && edit (msgbuf, msgindex) >= 0) {
424 if (!possible_mmc (msgbuf, msgindex) || _mmc_forward) {
425 if (!possible_mtc (msgbuf, msgindex) || _mtc_forward) {
427 sysex (*this, msgbuf, msgindex);
432 any (*this, msgbuf, msgindex);
438 * Status bytes always start a new message, except EOX
445 if (inbyte == MIDI::eox) {
446 /* return to the state we had pre-sysex */
448 state = pre_variable_state;
449 runnable = was_runnable;
450 msgtype = pre_variable_msgtype;
452 if (state != NEEDSTATUS && runnable) {
453 msgbuf[msgindex++] = last_status_byte;
456 msgbuf[msgindex++] = inbyte;
457 if ((inbyte & 0xf0) == 0xf0) {
461 channel_msg (inbyte);
469 * We've got a Data byte.
472 msgbuf[msgindex++] = inbyte;
477 * We shouldn't get here, since in NEEDSTATUS mode
478 * we're expecting a new status byte, NOT any
479 * data bytes. On the other hand, some equipment
480 * with leaky modwheels and the like might be
481 * sending data bytes as part of running controller
482 * messages, so just handle it silently.
487 /* wait for the second byte */
493 /* We've completed a 1 or 2 byte message. */
495 if (edit (msgbuf, msgindex) == 0) {
497 /* message not cancelled by an editor */
499 message_counter[msgbuf[0] & 0xF0]++;
502 signal (msgbuf, msgindex);
507 /* In Runnable mode, we reset the message
508 index, but keep the callbacks_pending and state the
509 same. This provides the "running status
514 /* If not Runnable, reset to NEEDSTATUS mode */
527 * realtime_msg(inbyte)
529 * Call the real-time function for the specified byte, immediately.
530 * These can occur anywhere, so they don't change the state.
534 Parser::realtime_msg(unsigned char inbyte)
537 message_counter[inbyte]++;
557 /* !!! active sense message in realtime_msg: should not reach here
565 any (*this, &inbyte, 1);
569 * channel_msg(inbyte)
571 * Interpret a Channel (voice or mode) Message status byte.
575 Parser::channel_msg(unsigned char inbyte)
577 last_status_byte = inbyte;
578 runnable = true; /* Channel messages can use running status */
580 /* The high 4 bits, which determine the type of channel message. */
582 switch (inbyte&0xF0) {
585 state = NEEDTWOBYTES;
589 state = NEEDTWOBYTES;
593 state = NEEDTWOBYTES;
596 msgtype = MIDI::controller;
597 state = NEEDTWOBYTES;
608 msgtype = MIDI::pitchbend;
609 state = NEEDTWOBYTES;
617 * Initialize (and possibly emit) the signals for the
618 * specified byte. Set the state that the state-machine
619 * should go into. If the signal is not emitted
620 * immediately, it will be when the state machine gets to
621 * the end of the MIDI message.
625 Parser::system_msg (unsigned char inbyte)
627 message_counter[inbyte]++;
631 pre_variable_msgtype = msgtype;
632 pre_variable_state = state;
633 was_runnable = runnable;
634 msgtype = MIDI::sysex;
635 state = VARIABLELENGTH;
638 msgtype = MIDI::mtc_quarter;
642 msgtype = MIDI::position;
643 state = NEEDTWOBYTES;
646 msgtype = MIDI::song;
659 // all these messages will be sent via any()
660 // when they are complete.
661 // any (*this, &inbyte, 1);
665 Parser::signal (byte *msg, size_t len)
667 channel_t chan = msg[0]&0xF;
675 channel_active_preparse[chan_i] (*this);
676 note_off (*this, (EventTwoBytes *) &msg[1]);
677 channel_note_off[chan_i]
678 (*this, (EventTwoBytes *) &msg[1]);
679 channel_active_postparse[chan_i] (*this);
683 channel_active_preparse[chan_i] (*this);
685 /* Hack to deal with MIDI sources that use velocity=0
690 note_off (*this, (EventTwoBytes *) &msg[1]);
691 channel_note_off[chan_i]
692 (*this, (EventTwoBytes *) &msg[1]);
694 note_on (*this, (EventTwoBytes *) &msg[1]);
695 channel_note_on[chan_i]
696 (*this, (EventTwoBytes *) &msg[1]);
699 channel_active_postparse[chan_i] (*this);
702 case MIDI::controller:
703 channel_active_preparse[chan_i] (*this);
704 controller (*this, (EventTwoBytes *) &msg[1]);
705 channel_controller[chan_i]
706 (*this, (EventTwoBytes *) &msg[1]);
707 channel_active_postparse[chan_i] (*this);
711 channel_active_preparse[chan_i] (*this);
712 program_change (*this, msg[1]);
713 channel_program_change[chan_i] (*this, msg[1]);
714 channel_active_postparse[chan_i] (*this);
718 channel_active_preparse[chan_i] (*this);
719 pressure (*this, msg[1]);
720 channel_pressure[chan_i] (*this, msg[1]);
721 channel_active_postparse[chan_i] (*this);
725 channel_active_preparse[chan_i] (*this);
726 poly_pressure (*this, (EventTwoBytes *) &msg[1]);
727 channel_poly_pressure[chan_i]
728 (*this, (EventTwoBytes *) &msg[1]);
729 channel_active_postparse[chan_i] (*this);
732 case MIDI::pitchbend:
733 channel_active_preparse[chan_i] (*this);
734 pitchbend (*this, (msg[1]<<7)|msg[2]);
735 channel_pitchbend[chan_i] (*this, (msg[1]<<7)|msg[2]);
736 channel_active_postparse[chan_i] (*this);
740 sysex (*this, msg, len);
743 case MIDI::mtc_quarter:
744 process_mtc_quarter_frame (msg);
745 mtc_quarter_frame (*this, *msg);
749 position (*this, msg, len);
753 song (*this, msg, len);
760 /* XXX some kind of warning ? */
764 any (*this, msg, len);
768 Parser::possible_mmc (byte *msg, size_t msglen)
770 if (!MachineControl::is_mmc (msg, msglen)) {
774 /* hand over the just the interior MMC part of
775 the sysex msg without the leading 0xF0
779 mmc (*this, &msg[1], msglen - 1);
786 Parser::set_offline (bool yn)
788 if (_offline != yn) {
790 OfflineStatusChanged ();
792 /* this hack deals with the possibility of our first MIDI
793 bytes being running status messages.