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 &p, 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[1]<<7)|msg[2])
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 ();
318 trace_prefix = prefix;
319 trace_connection = any.connect (mem_fun (*this, &Parser::trace_event));
327 Parser::scanner (unsigned char inbyte)
331 // cerr << "parse: " << hex << (int) inbyte << dec << " state = " << state << " msgindex = " << msgindex << " runnable = " << runnable << endl;
333 /* Check active sensing early, so it doesn't interrupt sysex.
335 NOTE: active sense messages are not considered to fit under
336 "any" for the purposes of callbacks. If a caller wants
337 active sense messages handled, which is unlikely, then
338 they can just ask for it specifically. They are so unlike
339 every other MIDI message in terms of semantics that its
340 counter-productive to treat them similarly.
343 if (inbyte == 0xfe) {
344 message_counter[inbyte]++;
346 active_sense (*this);
351 /* If necessary, allocate larger message buffer. */
353 if (msgindex >= msglen) {
355 msgbuf = (unsigned char *) realloc (msgbuf, msglen);
359 Real time messages can occur ANYPLACE,
360 but do not interrupt running status.
390 if (edit (&inbyte, 1) >= 0 && !_offline) {
391 realtime_msg (inbyte);
397 statusbit = (inbyte & 0x80);
400 * Variable length messages (ie. the 'system exclusive')
401 * can be terminated by the next status byte, not necessarily
402 * an EOX. Actually, since EOX is a status byte, this
403 * code ALWAYS handles the end of a VARIABLELENGTH message.
406 if (state == VARIABLELENGTH && statusbit) {
407 /* The message has ended, so process it */
409 /* add EOX to any sysex message */
411 if (inbyte == MIDI::eox) {
412 msgbuf[msgindex++] = inbyte;
416 cerr << "SYSEX: " << hex;
417 for (unsigned int i = 0; i < msgindex; ++i) {
418 cerr << (int) msgbuf[i] << ' ';
422 if (msgindex > 0 && edit (msgbuf, msgindex) >= 0) {
423 if (!possible_mmc (msgbuf, msgindex) || _mmc_forward) {
424 if (!possible_mtc (msgbuf, msgindex) || _mtc_forward) {
426 sysex (*this, msgbuf, msgindex);
431 any (*this, msgbuf, msgindex);
437 * Status bytes always start a new message, except EOX
444 if (inbyte == MIDI::eox) {
445 /* return to the state we had pre-sysex */
447 state = pre_variable_state;
448 runnable = was_runnable;
449 msgtype = pre_variable_msgtype;
451 if (state != NEEDSTATUS && runnable) {
452 msgbuf[msgindex++] = last_status_byte;
455 msgbuf[msgindex++] = inbyte;
456 if ((inbyte & 0xf0) == 0xf0) {
460 channel_msg (inbyte);
468 * We've got a Data byte.
471 msgbuf[msgindex++] = inbyte;
476 * We shouldn't get here, since in NEEDSTATUS mode
477 * we're expecting a new status byte, NOT any
478 * data bytes. On the other hand, some equipment
479 * with leaky modwheels and the like might be
480 * sending data bytes as part of running controller
481 * messages, so just handle it silently.
486 /* wait for the second byte */
492 /* We've completed a 1 or 2 byte message. */
494 if (edit (msgbuf, msgindex) == 0) {
496 /* message not cancelled by an editor */
498 message_counter[msgbuf[0] & 0xF0]++;
501 signal (msgbuf, msgindex);
506 /* In Runnable mode, we reset the message
507 index, but keep the callbacks_pending and state the
508 same. This provides the "running status
513 /* If not Runnable, reset to NEEDSTATUS mode */
525 /** Call the real-time function for the specified byte, immediately.
526 * These can occur anywhere, so they don't change the state.
529 Parser::realtime_msg(unsigned char inbyte)
532 message_counter[inbyte]++;
552 /* !!! active sense message in realtime_msg: should not reach here
560 any (*this, &inbyte, 1);
564 /** Interpret a Channel (voice or mode) Message status byte.
567 Parser::channel_msg(unsigned char inbyte)
569 last_status_byte = inbyte;
570 runnable = true; /* Channel messages can use running status */
572 /* The high 4 bits, which determine the type of channel message. */
574 switch (inbyte&0xF0) {
577 state = NEEDTWOBYTES;
581 state = NEEDTWOBYTES;
585 state = NEEDTWOBYTES;
588 msgtype = MIDI::controller;
589 state = NEEDTWOBYTES;
600 msgtype = MIDI::pitchbend;
601 state = NEEDTWOBYTES;
606 /** Initialize (and possibly emit) the signals for the
607 * specified byte. Set the state that the state-machine
608 * should go into. If the signal is not emitted
609 * immediately, it will be when the state machine gets to
610 * the end of the MIDI message.
613 Parser::system_msg (unsigned char inbyte)
615 message_counter[inbyte]++;
619 pre_variable_msgtype = msgtype;
620 pre_variable_state = state;
621 was_runnable = runnable;
622 msgtype = MIDI::sysex;
623 state = VARIABLELENGTH;
626 msgtype = MIDI::mtc_quarter;
630 msgtype = MIDI::position;
631 state = NEEDTWOBYTES;
634 msgtype = MIDI::song;
647 // all these messages will be sent via any()
648 // when they are complete.
649 // any (*this, &inbyte, 1);
653 Parser::signal (byte *msg, size_t len)
655 channel_t chan = msg[0]&0xF;
663 channel_active_preparse[chan_i] (*this);
664 note_off (*this, (EventTwoBytes *) &msg[1]);
665 channel_note_off[chan_i]
666 (*this, (EventTwoBytes *) &msg[1]);
667 channel_active_postparse[chan_i] (*this);
671 channel_active_preparse[chan_i] (*this);
673 /* Hack to deal with MIDI sources that use velocity=0
678 note_off (*this, (EventTwoBytes *) &msg[1]);
679 channel_note_off[chan_i]
680 (*this, (EventTwoBytes *) &msg[1]);
682 note_on (*this, (EventTwoBytes *) &msg[1]);
683 channel_note_on[chan_i]
684 (*this, (EventTwoBytes *) &msg[1]);
687 channel_active_postparse[chan_i] (*this);
690 case MIDI::controller:
691 channel_active_preparse[chan_i] (*this);
692 controller (*this, (EventTwoBytes *) &msg[1]);
693 channel_controller[chan_i]
694 (*this, (EventTwoBytes *) &msg[1]);
695 channel_active_postparse[chan_i] (*this);
699 channel_active_preparse[chan_i] (*this);
700 program_change (*this, msg[1]);
701 channel_program_change[chan_i] (*this, msg[1]);
702 channel_active_postparse[chan_i] (*this);
706 channel_active_preparse[chan_i] (*this);
707 pressure (*this, msg[1]);
708 channel_pressure[chan_i] (*this, msg[1]);
709 channel_active_postparse[chan_i] (*this);
713 channel_active_preparse[chan_i] (*this);
714 poly_pressure (*this, (EventTwoBytes *) &msg[1]);
715 channel_poly_pressure[chan_i]
716 (*this, (EventTwoBytes *) &msg[1]);
717 channel_active_postparse[chan_i] (*this);
720 case MIDI::pitchbend:
721 channel_active_preparse[chan_i] (*this);
722 pitchbend (*this, (msg[1]<<7)|msg[2]);
723 channel_pitchbend[chan_i] (*this, (msg[1]<<7)|msg[2]);
724 channel_active_postparse[chan_i] (*this);
728 sysex (*this, msg, len);
731 case MIDI::mtc_quarter:
732 process_mtc_quarter_frame (msg);
733 mtc_quarter_frame (*this, *msg);
737 position (*this, msg, len);
741 song (*this, msg, len);
748 /* XXX some kind of warning ? */
752 any (*this, msg, len);
756 Parser::possible_mmc (byte *msg, size_t msglen)
758 if (!MachineControl::is_mmc (msg, msglen)) {
762 /* hand over the just the interior MMC part of
763 the sysex msg without the leading 0xF0
767 mmc (*this, &msg[1], msglen - 1);
774 Parser::set_offline (bool yn)
776 if (_offline != yn) {
778 OfflineStatusChanged ();
780 /* this hack deals with the possibility of our first MIDI
781 bytes being running status messages.