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.
30 #include <midi++/types.h>
31 #include <midi++/parser.h>
32 #include <midi++/port.h>
33 #include <midi++/mmc.h>
34 #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 &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)
313 trace_connection.disconnect ();
317 trace_prefix = prefix;
318 trace_connection = any.connect (mem_fun (*this, &Parser::trace_event));
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 if (edit (&inbyte, 1) >= 0 && !_offline) {
390 realtime_msg (inbyte);
396 statusbit = (inbyte & 0x80);
399 * Variable length messages (ie. the 'system exclusive')
400 * can be terminated by the next status byte, not necessarily
401 * an EOX. Actually, since EOX is a status byte, this
402 * code ALWAYS handles the end of a VARIABLELENGTH message.
405 if (state == VARIABLELENGTH && statusbit) {
406 /* The message has ended, so process it */
408 /* add EOX to any sysex message */
410 if (inbyte == MIDI::eox) {
411 msgbuf[msgindex++] = inbyte;
415 cerr << "SYSEX: " << hex;
416 for (unsigned int i = 0; i < msgindex; ++i) {
417 cerr << (int) msgbuf[i] << ' ';
421 if (msgindex > 0 && edit (msgbuf, msgindex) >= 0) {
422 if (!possible_mmc (msgbuf, msgindex) || _mmc_forward) {
423 if (!possible_mtc (msgbuf, msgindex) || _mtc_forward) {
425 sysex (*this, msgbuf, msgindex);
430 any (*this, msgbuf, msgindex);
436 * Status bytes always start a new message, except EOX
443 if (inbyte == MIDI::eox) {
444 /* return to the state we had pre-sysex */
446 state = pre_variable_state;
447 runnable = was_runnable;
448 msgtype = pre_variable_msgtype;
450 if (state != NEEDSTATUS && runnable) {
451 msgbuf[msgindex++] = last_status_byte;
454 msgbuf[msgindex++] = inbyte;
455 if ((inbyte & 0xf0) == 0xf0) {
459 channel_msg (inbyte);
467 * We've got a Data byte.
470 msgbuf[msgindex++] = inbyte;
475 * We shouldn't get here, since in NEEDSTATUS mode
476 * we're expecting a new status byte, NOT any
477 * data bytes. On the other hand, some equipment
478 * with leaky modwheels and the like might be
479 * sending data bytes as part of running controller
480 * messages, so just handle it silently.
485 /* wait for the second byte */
491 /* We've completed a 1 or 2 byte message. */
493 if (edit (msgbuf, msgindex) == 0) {
495 /* message not cancelled by an editor */
497 message_counter[msgbuf[0] & 0xF0]++;
500 signal (msgbuf, msgindex);
505 /* In Runnable mode, we reset the message
506 index, but keep the callbacks_pending and state the
507 same. This provides the "running status
512 /* If not Runnable, reset to NEEDSTATUS mode */
524 /** Call the real-time function for the specified byte, immediately.
525 * These can occur anywhere, so they don't change the state.
528 Parser::realtime_msg(unsigned char inbyte)
531 message_counter[inbyte]++;
551 /* !!! active sense message in realtime_msg: should not reach here
559 any (*this, &inbyte, 1);
563 /** Interpret a Channel (voice or mode) Message status byte.
566 Parser::channel_msg(unsigned char inbyte)
568 last_status_byte = inbyte;
569 runnable = true; /* Channel messages can use running status */
571 /* The high 4 bits, which determine the type of channel message. */
573 switch (inbyte&0xF0) {
576 state = NEEDTWOBYTES;
580 state = NEEDTWOBYTES;
584 state = NEEDTWOBYTES;
587 msgtype = MIDI::controller;
588 state = NEEDTWOBYTES;
599 msgtype = MIDI::pitchbend;
600 state = NEEDTWOBYTES;
605 /** Initialize (and possibly emit) the signals for the
606 * specified byte. Set the state that the state-machine
607 * should go into. If the signal is not emitted
608 * immediately, it will be when the state machine gets to
609 * the end of the MIDI message.
612 Parser::system_msg (unsigned char inbyte)
614 message_counter[inbyte]++;
618 pre_variable_msgtype = msgtype;
619 pre_variable_state = state;
620 was_runnable = runnable;
621 msgtype = MIDI::sysex;
622 state = VARIABLELENGTH;
625 msgtype = MIDI::mtc_quarter;
629 msgtype = MIDI::position;
630 state = NEEDTWOBYTES;
633 msgtype = MIDI::song;
646 // all these messages will be sent via any()
647 // when they are complete.
648 // any (*this, &inbyte, 1);
652 Parser::signal (byte *msg, size_t len)
654 channel_t chan = msg[0]&0xF;
662 channel_active_preparse[chan_i] (*this);
663 note_off (*this, (EventTwoBytes *) &msg[1]);
664 channel_note_off[chan_i]
665 (*this, (EventTwoBytes *) &msg[1]);
666 channel_active_postparse[chan_i] (*this);
670 channel_active_preparse[chan_i] (*this);
672 /* Hack to deal with MIDI sources that use velocity=0
677 note_off (*this, (EventTwoBytes *) &msg[1]);
678 channel_note_off[chan_i]
679 (*this, (EventTwoBytes *) &msg[1]);
681 note_on (*this, (EventTwoBytes *) &msg[1]);
682 channel_note_on[chan_i]
683 (*this, (EventTwoBytes *) &msg[1]);
686 channel_active_postparse[chan_i] (*this);
689 case MIDI::controller:
690 channel_active_preparse[chan_i] (*this);
691 controller (*this, (EventTwoBytes *) &msg[1]);
692 channel_controller[chan_i]
693 (*this, (EventTwoBytes *) &msg[1]);
694 channel_active_postparse[chan_i] (*this);
698 channel_active_preparse[chan_i] (*this);
699 program_change (*this, msg[1]);
700 channel_program_change[chan_i] (*this, msg[1]);
701 channel_active_postparse[chan_i] (*this);
705 channel_active_preparse[chan_i] (*this);
706 pressure (*this, msg[1]);
707 channel_pressure[chan_i] (*this, msg[1]);
708 channel_active_postparse[chan_i] (*this);
712 channel_active_preparse[chan_i] (*this);
713 poly_pressure (*this, (EventTwoBytes *) &msg[1]);
714 channel_poly_pressure[chan_i]
715 (*this, (EventTwoBytes *) &msg[1]);
716 channel_active_postparse[chan_i] (*this);
719 case MIDI::pitchbend:
720 channel_active_preparse[chan_i] (*this);
721 pitchbend (*this, (msg[1]<<7)|msg[2]);
722 channel_pitchbend[chan_i] (*this, (msg[1]<<7)|msg[2]);
723 channel_active_postparse[chan_i] (*this);
727 sysex (*this, msg, len);
730 case MIDI::mtc_quarter:
731 process_mtc_quarter_frame (msg);
732 mtc_quarter_frame (*this, *msg);
736 position (*this, msg, len);
740 song (*this, msg, len);
747 /* XXX some kind of warning ? */
751 any (*this, msg, len);
755 Parser::possible_mmc (byte *msg, size_t msglen)
757 if (!MachineControl::is_mmc (msg, msglen)) {
761 /* hand over the just the interior MMC part of
762 the sysex msg without the leading 0xF0
766 mmc (*this, &msg[1], msglen - 1);
773 Parser::set_offline (bool yn)
775 if (_offline != yn) {
777 OfflineStatusChanged ();
779 /* this hack deals with the possibility of our first MIDI
780 bytes being running status messages.