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"
42 Parser::midi_event_type_name (eventType t)
47 return "no midi messages";
50 return "raw midi data";
53 return "any midi message";
64 case MIDI::controller:
68 return "program change";
71 return "channel pressure";
77 return "system exclusive";
80 return "song position";
86 return "end of sysex";
101 return "active sense";
104 return "unknow MIDI event type";
108 Parser::Parser (Port &p)
114 memset (message_counter, 0, sizeof (message_counter[0]) * 256);
118 msgbuf = (unsigned char *) malloc (msglen);
119 msgbuf[msgindex++] = 0x90;
120 _mmc_forward = false;
124 /* this hack deals with the possibility of our first MIDI
125 bytes being running status messages.
131 pre_variable_state = NEEDSTATUS;
132 pre_variable_msgtype = none;
142 Parser::trace_event (Parser &, byte *msg, size_t len)
147 if ((o = trace_stream) == NULL) { /* can be asynchronously removed */
151 type = (eventType) (msg[0]&0xF0);
158 << " NoteOff NoteNum "
169 << " NoteOn NoteNum "
185 case MIDI::controller:
200 << " Program Change ProgNum "
209 << " Channel Pressure "
214 case MIDI::pitchbend:
219 << ((msg[2]<<7)|msg[1])
258 << "System Exclusive (1 byte : " << hex << (int) *msg << dec << ')'
264 << "System Exclusive (" << len << ") = [ " << hex;
265 for (unsigned int i = 0; i < len; ++i) {
266 *o << (int) msgbuf[i] << ' ';
268 *o << dec << ']' << endmsg;
274 *o << trace_prefix << "Song" << endmsg;
278 *o << trace_prefix << "Tune" << endmsg;
282 *o << trace_prefix << "End-of-System Exclusive" << endmsg;
286 *o << trace_prefix << "Timing" << endmsg;
290 *o << trace_prefix << "Start" << endmsg;
294 *o << trace_prefix << "Stop" << endmsg;
298 *o << trace_prefix << "Continue" << endmsg;
302 *o << trace_prefix << "Active Sense" << endmsg;
306 *o << trace_prefix << "Unrecognized MIDI message" << endmsg;
312 Parser::trace (bool onoff, ostream *o, const string &prefix)
314 trace_connection.disconnect ();
317 cerr << "enabling tracing for port " << _port.name() << endl;
319 trace_prefix = prefix;
320 trace_connection = any.connect (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 */
526 /** Call the real-time function for the specified byte, immediately.
527 * These can occur anywhere, so they don't change the state.
530 Parser::realtime_msg(unsigned char inbyte)
533 message_counter[inbyte]++;
541 timing (*this, _midi_clock_timestamp);
544 start (*this, _midi_clock_timestamp);
547 contineu (*this, _midi_clock_timestamp);
550 stop (*this, _midi_clock_timestamp);
553 /* !!! active sense message in realtime_msg: should not reach here
561 any (*this, &inbyte, 1);
565 /** Interpret a Channel (voice or mode) Message status byte.
568 Parser::channel_msg(unsigned char inbyte)
570 last_status_byte = inbyte;
571 runnable = true; /* Channel messages can use running status */
573 /* The high 4 bits, which determine the type of channel message. */
575 switch (inbyte&0xF0) {
578 state = NEEDTWOBYTES;
582 state = NEEDTWOBYTES;
586 state = NEEDTWOBYTES;
589 msgtype = MIDI::controller;
590 state = NEEDTWOBYTES;
601 msgtype = MIDI::pitchbend;
602 state = NEEDTWOBYTES;
607 /** Initialize (and possibly emit) the signals for the
608 * specified byte. Set the state that the state-machine
609 * should go into. If the signal is not emitted
610 * immediately, it will be when the state machine gets to
611 * the end of the MIDI message.
614 Parser::system_msg (unsigned char inbyte)
616 message_counter[inbyte]++;
620 pre_variable_msgtype = msgtype;
621 pre_variable_state = state;
622 was_runnable = runnable;
623 msgtype = MIDI::sysex;
624 state = VARIABLELENGTH;
627 msgtype = MIDI::mtc_quarter;
631 msgtype = MIDI::position;
632 state = NEEDTWOBYTES;
635 msgtype = MIDI::song;
648 // all these messages will be sent via any()
649 // when they are complete.
650 // any (*this, &inbyte, 1);
654 Parser::signal (byte *msg, size_t len)
656 channel_t chan = msg[0]&0xF;
664 channel_active_preparse[chan_i] (*this);
665 note_off (*this, (EventTwoBytes *) &msg[1]);
666 channel_note_off[chan_i]
667 (*this, (EventTwoBytes *) &msg[1]);
668 channel_active_postparse[chan_i] (*this);
672 channel_active_preparse[chan_i] (*this);
674 /* Hack to deal with MIDI sources that use velocity=0
679 note_off (*this, (EventTwoBytes *) &msg[1]);
680 channel_note_off[chan_i]
681 (*this, (EventTwoBytes *) &msg[1]);
683 note_on (*this, (EventTwoBytes *) &msg[1]);
684 channel_note_on[chan_i]
685 (*this, (EventTwoBytes *) &msg[1]);
688 channel_active_postparse[chan_i] (*this);
691 case MIDI::controller:
692 channel_active_preparse[chan_i] (*this);
693 controller (*this, (EventTwoBytes *) &msg[1]);
694 channel_controller[chan_i]
695 (*this, (EventTwoBytes *) &msg[1]);
696 channel_active_postparse[chan_i] (*this);
700 channel_active_preparse[chan_i] (*this);
701 program_change (*this, msg[1]);
702 channel_program_change[chan_i] (*this, msg[1]);
703 channel_active_postparse[chan_i] (*this);
707 channel_active_preparse[chan_i] (*this);
708 pressure (*this, msg[1]);
709 channel_pressure[chan_i] (*this, msg[1]);
710 channel_active_postparse[chan_i] (*this);
714 channel_active_preparse[chan_i] (*this);
715 poly_pressure (*this, (EventTwoBytes *) &msg[1]);
716 channel_poly_pressure[chan_i]
717 (*this, (EventTwoBytes *) &msg[1]);
718 channel_active_postparse[chan_i] (*this);
721 case MIDI::pitchbend:
722 channel_active_preparse[chan_i] (*this);
723 pitchbend (*this, (msg[2]<<7)|msg[1]);
724 channel_pitchbend[chan_i] (*this, (msg[2]<<7)|msg[1]);
725 channel_active_postparse[chan_i] (*this);
729 sysex (*this, msg, len);
732 case MIDI::mtc_quarter:
733 process_mtc_quarter_frame (msg);
734 mtc_quarter_frame (*this, *msg);
738 position (*this, msg, len);
742 song (*this, msg, len);
749 /* XXX some kind of warning ? */
753 any (*this, msg, len);
757 Parser::possible_mmc (byte *msg, size_t msglen)
759 if (!MachineControl::is_mmc (msg, msglen)) {
763 /* hand over the just the interior MMC part of
764 the sysex msg without the leading 0xF0
768 mmc (*this, &msg[1], msglen - 1);
775 Parser::set_offline (bool yn)
777 if (_offline != yn) {
779 OfflineStatusChanged ();
781 /* this hack deals with the possibility of our first MIDI
782 bytes being running status messages.