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[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 ();
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 */
526 * realtime_msg(inbyte)
528 * Call the real-time function for the specified byte, immediately.
529 * These can occur anywhere, so they don't change the state.
533 Parser::realtime_msg(unsigned char inbyte)
536 message_counter[inbyte]++;
556 /* !!! active sense message in realtime_msg: should not reach here
564 any (*this, &inbyte, 1);
568 * channel_msg(inbyte)
570 * Interpret a Channel (voice or mode) Message status byte.
574 Parser::channel_msg(unsigned char inbyte)
576 last_status_byte = inbyte;
577 runnable = true; /* Channel messages can use running status */
579 /* The high 4 bits, which determine the type of channel message. */
581 switch (inbyte&0xF0) {
584 state = NEEDTWOBYTES;
588 state = NEEDTWOBYTES;
592 state = NEEDTWOBYTES;
595 msgtype = MIDI::controller;
596 state = NEEDTWOBYTES;
607 msgtype = MIDI::pitchbend;
608 state = NEEDTWOBYTES;
616 * Initialize (and possibly emit) the signals for the
617 * specified byte. Set the state that the state-machine
618 * should go into. If the signal is not emitted
619 * immediately, it will be when the state machine gets to
620 * the end of the MIDI message.
624 Parser::system_msg (unsigned char inbyte)
626 message_counter[inbyte]++;
630 pre_variable_msgtype = msgtype;
631 pre_variable_state = state;
632 was_runnable = runnable;
633 msgtype = MIDI::sysex;
634 state = VARIABLELENGTH;
637 msgtype = MIDI::mtc_quarter;
641 msgtype = MIDI::position;
642 state = NEEDTWOBYTES;
645 msgtype = MIDI::song;
658 // all these messages will be sent via any()
659 // when they are complete.
660 // any (*this, &inbyte, 1);
664 Parser::signal (byte *msg, size_t len)
666 channel_t chan = msg[0]&0xF;
674 channel_active_preparse[chan_i] (*this);
675 note_off (*this, (EventTwoBytes *) &msg[1]);
676 channel_note_off[chan_i]
677 (*this, (EventTwoBytes *) &msg[1]);
678 channel_active_postparse[chan_i] (*this);
682 channel_active_preparse[chan_i] (*this);
684 /* Hack to deal with MIDI sources that use velocity=0
689 note_off (*this, (EventTwoBytes *) &msg[1]);
690 channel_note_off[chan_i]
691 (*this, (EventTwoBytes *) &msg[1]);
693 note_on (*this, (EventTwoBytes *) &msg[1]);
694 channel_note_on[chan_i]
695 (*this, (EventTwoBytes *) &msg[1]);
698 channel_active_postparse[chan_i] (*this);
701 case MIDI::controller:
702 channel_active_preparse[chan_i] (*this);
703 controller (*this, (EventTwoBytes *) &msg[1]);
704 channel_controller[chan_i]
705 (*this, (EventTwoBytes *) &msg[1]);
706 channel_active_postparse[chan_i] (*this);
710 channel_active_preparse[chan_i] (*this);
711 program_change (*this, msg[1]);
712 channel_program_change[chan_i] (*this, msg[1]);
713 channel_active_postparse[chan_i] (*this);
717 channel_active_preparse[chan_i] (*this);
718 pressure (*this, msg[1]);
719 channel_pressure[chan_i] (*this, msg[1]);
720 channel_active_postparse[chan_i] (*this);
724 channel_active_preparse[chan_i] (*this);
725 poly_pressure (*this, (EventTwoBytes *) &msg[1]);
726 channel_poly_pressure[chan_i]
727 (*this, (EventTwoBytes *) &msg[1]);
728 channel_active_postparse[chan_i] (*this);
731 case MIDI::pitchbend:
732 channel_active_preparse[chan_i] (*this);
733 pitchbend (*this, (msg[2]<<7)|msg[1]);
734 channel_pitchbend[chan_i] (*this, (msg[2]<<7)|msg[1]);
735 channel_active_postparse[chan_i] (*this);
739 sysex (*this, msg, len);
742 case MIDI::mtc_quarter:
743 process_mtc_quarter_frame (msg);
744 mtc_quarter_frame (*this, *msg);
748 position (*this, msg, len);
752 song (*this, msg, len);
759 /* XXX some kind of warning ? */
763 any (*this, msg, len);
767 Parser::possible_mmc (byte *msg, size_t msglen)
769 if (!MachineControl::is_mmc (msg, msglen)) {
773 /* hand over the just the interior MMC part of
774 the sysex msg without the leading 0xF0
778 mmc (*this, &msg[1], msglen - 1);
785 Parser::set_offline (bool yn)
787 if (_offline != yn) {
789 OfflineStatusChanged ();
791 /* this hack deals with the possibility of our first MIDI
792 bytes being running status messages.