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"
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)
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 &, byte *msg, size_t len)
145 if ((o = trace_stream) == NULL) { /* can be asynchronously removed */
149 type = (eventType) (msg[0]&0xF0);
156 << " NoteOff NoteNum "
167 << " NoteOn NoteNum "
183 case MIDI::controller:
198 << " Program Change ProgNum "
207 << " Channel Pressure "
212 case MIDI::pitchbend:
217 << ((msg[2]<<7)|msg[1])
256 << "System Exclusive (1 byte : " << hex << (int) *msg << dec << ')'
262 << "System Exclusive (" << len << ") = [ " << hex;
263 for (unsigned int i = 0; i < len; ++i) {
264 *o << (int) msgbuf[i] << ' ';
266 *o << dec << ']' << endmsg;
272 *o << trace_prefix << "Song" << endmsg;
276 *o << trace_prefix << "Tune" << endmsg;
280 *o << trace_prefix << "End-of-System Exclusive" << endmsg;
284 *o << trace_prefix << "Timing" << endmsg;
288 *o << trace_prefix << "Start" << endmsg;
292 *o << trace_prefix << "Stop" << endmsg;
296 *o << trace_prefix << "Continue" << endmsg;
300 *o << trace_prefix << "Active Sense" << endmsg;
304 *o << trace_prefix << "Unrecognized MIDI message" << endmsg;
310 Parser::trace (bool onoff, ostream *o, const string &prefix)
312 trace_connection.disconnect ();
316 trace_prefix = prefix;
317 any.connect_same_thread (trace_connection, boost::bind (&Parser::trace_event, this, _1, _2, _3));
325 Parser::scanner (unsigned char inbyte)
328 boost::optional<int> edit_result;
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 boost::optional<int> res = edit (&inbyte, 1);
391 if (res.get_value_or (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) {
409 /* The message has ended, so process it */
411 /* add EOX to any sysex message */
413 if (inbyte == MIDI::eox) {
414 msgbuf[msgindex++] = inbyte;
418 cerr << "SYSEX: " << hex;
419 for (unsigned int i = 0; i < msgindex; ++i) {
420 cerr << (int) msgbuf[i] << ' ';
426 boost::optional<int> res = edit (msgbuf, msgindex);
428 if (res.get_value_or (1) >= 0) {
429 if (!possible_mmc (msgbuf, msgindex) || _mmc_forward) {
430 if (!possible_mtc (msgbuf, msgindex) || _mtc_forward) {
432 sysex (*this, msgbuf, msgindex);
437 any (*this, msgbuf, msgindex);
444 * Status bytes always start a new message, except EOX
451 if (inbyte == MIDI::eox) {
452 /* return to the state we had pre-sysex */
454 state = pre_variable_state;
455 runnable = was_runnable;
456 msgtype = pre_variable_msgtype;
458 if (state != NEEDSTATUS && runnable) {
459 msgbuf[msgindex++] = last_status_byte;
462 msgbuf[msgindex++] = inbyte;
463 if ((inbyte & 0xf0) == 0xf0) {
467 channel_msg (inbyte);
475 * We've got a Data byte.
478 msgbuf[msgindex++] = inbyte;
483 * We shouldn't get here, since in NEEDSTATUS mode
484 * we're expecting a new status byte, NOT any
485 * data bytes. On the other hand, some equipment
486 * with leaky modwheels and the like might be
487 * sending data bytes as part of running controller
488 * messages, so just handle it silently.
493 /* wait for the second byte */
499 /* We've completed a 1 or 2 byte message. */
501 edit_result = edit (msgbuf, msgindex);
503 if (edit_result.get_value_or (1)) {
505 /* message not cancelled by an editor */
507 message_counter[msgbuf[0] & 0xF0]++;
510 signal (msgbuf, msgindex);
515 /* In Runnable mode, we reset the message
516 index, but keep the callbacks_pending and state the
517 same. This provides the "running status
522 /* If not Runnable, reset to NEEDSTATUS mode */
534 /** Call the real-time function for the specified byte, immediately.
535 * These can occur anywhere, so they don't change the state.
538 Parser::realtime_msg(unsigned char inbyte)
541 message_counter[inbyte]++;
549 timing (*this, _timestamp);
552 start (*this, _timestamp);
555 contineu (*this, _timestamp);
558 stop (*this, _timestamp);
561 /* !!! active sense message in realtime_msg: should not reach here
569 any (*this, &inbyte, 1);
573 /** Interpret a Channel (voice or mode) Message status byte.
576 Parser::channel_msg(unsigned char inbyte)
578 last_status_byte = inbyte;
579 runnable = true; /* Channel messages can use running status */
581 /* The high 4 bits, which determine the type of channel message. */
583 switch (inbyte&0xF0) {
586 state = NEEDTWOBYTES;
590 state = NEEDTWOBYTES;
594 state = NEEDTWOBYTES;
597 msgtype = MIDI::controller;
598 state = NEEDTWOBYTES;
609 msgtype = MIDI::pitchbend;
610 state = NEEDTWOBYTES;
615 /** Initialize (and possibly emit) the signals for the
616 * specified byte. Set the state that the state-machine
617 * should go into. If the signal is not emitted
618 * immediately, it will be when the state machine gets to
619 * the end of the MIDI message.
622 Parser::system_msg (unsigned char inbyte)
624 message_counter[inbyte]++;
628 pre_variable_msgtype = msgtype;
629 pre_variable_state = state;
630 was_runnable = runnable;
631 msgtype = MIDI::sysex;
632 state = VARIABLELENGTH;
635 msgtype = MIDI::mtc_quarter;
639 msgtype = MIDI::position;
640 state = NEEDTWOBYTES;
643 msgtype = MIDI::song;
656 // all these messages will be sent via any()
657 // when they are complete.
658 // any (*this, &inbyte, 1);
662 Parser::signal (byte *msg, size_t len)
664 channel_t chan = msg[0]&0xF;
672 channel_active_preparse[chan_i] (*this);
673 note_off (*this, (EventTwoBytes *) &msg[1]);
674 channel_note_off[chan_i]
675 (*this, (EventTwoBytes *) &msg[1]);
676 channel_active_postparse[chan_i] (*this);
680 channel_active_preparse[chan_i] (*this);
682 /* Hack to deal with MIDI sources that use velocity=0
687 note_off (*this, (EventTwoBytes *) &msg[1]);
688 channel_note_off[chan_i]
689 (*this, (EventTwoBytes *) &msg[1]);
691 note_on (*this, (EventTwoBytes *) &msg[1]);
692 channel_note_on[chan_i]
693 (*this, (EventTwoBytes *) &msg[1]);
696 channel_active_postparse[chan_i] (*this);
699 case MIDI::controller:
700 channel_active_preparse[chan_i] (*this);
701 controller (*this, (EventTwoBytes *) &msg[1]);
702 channel_controller[chan_i]
703 (*this, (EventTwoBytes *) &msg[1]);
704 channel_active_postparse[chan_i] (*this);
708 channel_active_preparse[chan_i] (*this);
709 program_change (*this, msg[1]);
710 channel_program_change[chan_i] (*this, msg[1]);
711 channel_active_postparse[chan_i] (*this);
715 channel_active_preparse[chan_i] (*this);
716 pressure (*this, msg[1]);
717 channel_pressure[chan_i] (*this, msg[1]);
718 channel_active_postparse[chan_i] (*this);
722 channel_active_preparse[chan_i] (*this);
723 poly_pressure (*this, (EventTwoBytes *) &msg[1]);
724 channel_poly_pressure[chan_i]
725 (*this, (EventTwoBytes *) &msg[1]);
726 channel_active_postparse[chan_i] (*this);
729 case MIDI::pitchbend:
730 channel_active_preparse[chan_i] (*this);
731 pitchbend (*this, (msg[2]<<7)|msg[1]);
732 channel_pitchbend[chan_i] (*this, (msg[2]<<7)|msg[1]);
733 channel_active_postparse[chan_i] (*this);
737 sysex (*this, msg, len);
740 case MIDI::mtc_quarter:
741 process_mtc_quarter_frame (msg);
742 mtc_quarter_frame (*this, *msg);
746 position (*this, msg, len);
750 song (*this, msg, len);
757 /* XXX some kind of warning ? */
761 any (*this, msg, len);
765 Parser::possible_mmc (byte *msg, size_t msglen)
767 if (!MachineControl::is_mmc (msg, msglen)) {
771 /* hand over the just the interior MMC part of
772 the sysex msg without the leading 0xF0
776 mmc (*this, &msg[1], msglen - 1);
783 Parser::set_offline (bool yn)
785 if (_offline != yn) {
787 OfflineStatusChanged ();
789 /* this hack deals with the possibility of our first MIDI
790 bytes being running status messages.