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";
111 memset (message_counter, 0, sizeof (message_counter[0]) * 256);
115 msgbuf = (unsigned char *) malloc (msglen);
116 msgbuf[msgindex++] = 0x90;
117 _mmc_forward = false;
121 /* this hack deals with the possibility of our first MIDI
122 bytes being running status messages.
128 pre_variable_state = NEEDSTATUS;
129 pre_variable_msgtype = none;
139 Parser::trace_event (Parser &, MIDI::byte *msg, size_t len)
144 if ((o = trace_stream) == NULL) { /* can be asynchronously removed */
148 type = (eventType) (msg[0]&0xF0);
155 << " NoteOff NoteNum "
166 << " NoteOn NoteNum "
182 case MIDI::controller:
197 << " Program Change ProgNum "
206 << " Channel Pressure "
211 case MIDI::pitchbend:
216 << ((msg[2]<<7)|msg[1])
255 << "System Exclusive (1 byte : " << hex << (int) *msg << dec << ')'
261 << "System Exclusive (" << len << ") = [ " << hex;
262 for (unsigned int i = 0; i < len; ++i) {
263 *o << (int) msgbuf[i] << ' ';
265 *o << dec << ']' << endmsg;
271 *o << trace_prefix << "Song" << endmsg;
275 *o << trace_prefix << "Tune" << endmsg;
279 *o << trace_prefix << "End-of-System Exclusive" << endmsg;
283 *o << trace_prefix << "Timing" << endmsg;
287 *o << trace_prefix << "Start" << endmsg;
291 *o << trace_prefix << "Stop" << endmsg;
295 *o << trace_prefix << "Continue" << endmsg;
299 *o << trace_prefix << "Active Sense" << endmsg;
303 *o << trace_prefix << "Unrecognized MIDI message" << endmsg;
309 Parser::trace (bool onoff, ostream *o, const string &prefix)
311 trace_connection.disconnect ();
315 trace_prefix = prefix;
316 any.connect_same_thread (trace_connection, boost::bind (&Parser::trace_event, this, _1, _2, _3));
324 Parser::scanner (unsigned char inbyte)
327 boost::optional<int> edit_result;
329 // cerr << "parse: " << hex << (int) inbyte << dec << " state = " << state << " msgindex = " << msgindex << " runnable = " << runnable << endl;
331 /* Check active sensing early, so it doesn't interrupt sysex.
333 NOTE: active sense messages are not considered to fit under
334 "any" for the purposes of callbacks. If a caller wants
335 active sense messages handled, which is unlikely, then
336 they can just ask for it specifically. They are so unlike
337 every other MIDI message in terms of semantics that its
338 counter-productive to treat them similarly.
341 if (inbyte == 0xfe) {
342 message_counter[inbyte]++;
344 active_sense (*this);
349 /* If necessary, allocate larger message buffer. */
351 if (msgindex >= msglen) {
353 msgbuf = (unsigned char *) realloc (msgbuf, msglen);
357 Real time messages can occur ANYPLACE,
358 but do not interrupt running status.
388 boost::optional<int> res = edit (&inbyte, 1);
390 if (res.get_value_or (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) {
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] << ' ';
425 boost::optional<int> res = edit (msgbuf, msgindex);
427 if (res.get_value_or (1) >= 0) {
428 if (!possible_mmc (msgbuf, msgindex) || _mmc_forward) {
429 if (!possible_mtc (msgbuf, msgindex) || _mtc_forward) {
431 sysex (*this, msgbuf, msgindex);
436 any (*this, msgbuf, msgindex);
443 * Status bytes always start a new message, except EOX
450 if (inbyte == MIDI::eox) {
451 /* return to the state we had pre-sysex */
453 state = pre_variable_state;
454 runnable = was_runnable;
455 msgtype = pre_variable_msgtype;
457 if (state != NEEDSTATUS && runnable) {
458 msgbuf[msgindex++] = last_status_byte;
461 msgbuf[msgindex++] = inbyte;
462 if ((inbyte & 0xf0) == 0xf0) {
466 channel_msg (inbyte);
474 * We've got a Data byte.
477 msgbuf[msgindex++] = inbyte;
482 * We shouldn't get here, since in NEEDSTATUS mode
483 * we're expecting a new status byte, NOT any
484 * data bytes. On the other hand, some equipment
485 * with leaky modwheels and the like might be
486 * sending data bytes as part of running controller
487 * messages, so just handle it silently.
492 /* wait for the second byte */
498 /* We've completed a 1 or 2 byte message. */
500 edit_result = edit (msgbuf, msgindex);
502 if (edit_result.get_value_or (1)) {
504 /* message not cancelled by an editor */
506 message_counter[msgbuf[0] & 0xF0]++;
509 signal (msgbuf, msgindex);
514 /* In Runnable mode, we reset the message
515 index, but keep the callbacks_pending and state the
516 same. This provides the "running status
521 /* If not Runnable, reset to NEEDSTATUS mode */
533 /** Call the real-time function for the specified byte, immediately.
534 * These can occur anywhere, so they don't change the state.
537 Parser::realtime_msg(unsigned char inbyte)
540 message_counter[inbyte]++;
548 timing (*this, _timestamp);
551 start (*this, _timestamp);
554 contineu (*this, _timestamp);
557 stop (*this, _timestamp);
560 /* !!! active sense message in realtime_msg: should not reach here
568 any (*this, &inbyte, 1);
572 /** Interpret a Channel (voice or mode) Message status byte.
575 Parser::channel_msg(unsigned char inbyte)
577 last_status_byte = inbyte;
578 runnable = true; /* Channel messages can use running status */
580 /* The high 4 bits, which determine the type of channel message. */
582 switch (inbyte&0xF0) {
585 state = NEEDTWOBYTES;
589 state = NEEDTWOBYTES;
593 state = NEEDTWOBYTES;
596 msgtype = MIDI::controller;
597 state = NEEDTWOBYTES;
608 msgtype = MIDI::pitchbend;
609 state = NEEDTWOBYTES;
614 /** Initialize (and possibly emit) the signals for the
615 * specified byte. Set the state that the state-machine
616 * should go into. If the signal is not emitted
617 * immediately, it will be when the state machine gets to
618 * the end of the MIDI message.
621 Parser::system_msg (unsigned char inbyte)
623 message_counter[inbyte]++;
627 pre_variable_msgtype = msgtype;
628 pre_variable_state = state;
629 was_runnable = runnable;
630 msgtype = MIDI::sysex;
631 state = VARIABLELENGTH;
634 msgtype = MIDI::mtc_quarter;
638 msgtype = MIDI::position;
639 state = NEEDTWOBYTES;
642 msgtype = MIDI::song;
655 // all these messages will be sent via any()
656 // when they are complete.
657 // any (*this, &inbyte, 1);
661 Parser::signal (MIDI::byte *msg, size_t len)
663 channel_t chan = msg[0]&0xF;
671 channel_active_preparse[chan_i] (*this);
672 note_off (*this, (EventTwoBytes *) &msg[1]);
673 channel_note_off[chan_i]
674 (*this, (EventTwoBytes *) &msg[1]);
675 channel_active_postparse[chan_i] (*this);
679 channel_active_preparse[chan_i] (*this);
681 /* Hack to deal with MIDI sources that use velocity=0
686 note_off (*this, (EventTwoBytes *) &msg[1]);
687 channel_note_off[chan_i]
688 (*this, (EventTwoBytes *) &msg[1]);
690 note_on (*this, (EventTwoBytes *) &msg[1]);
691 channel_note_on[chan_i]
692 (*this, (EventTwoBytes *) &msg[1]);
695 channel_active_postparse[chan_i] (*this);
698 case MIDI::controller:
699 channel_active_preparse[chan_i] (*this);
700 controller (*this, (EventTwoBytes *) &msg[1]);
701 channel_controller[chan_i]
702 (*this, (EventTwoBytes *) &msg[1]);
703 channel_active_postparse[chan_i] (*this);
707 channel_active_preparse[chan_i] (*this);
708 program_change (*this, msg[1]);
709 channel_program_change[chan_i] (*this, msg[1]);
710 channel_active_postparse[chan_i] (*this);
714 channel_active_preparse[chan_i] (*this);
715 pressure (*this, msg[1]);
716 channel_pressure[chan_i] (*this, msg[1]);
717 channel_active_postparse[chan_i] (*this);
721 channel_active_preparse[chan_i] (*this);
722 poly_pressure (*this, (EventTwoBytes *) &msg[1]);
723 channel_poly_pressure[chan_i]
724 (*this, (EventTwoBytes *) &msg[1]);
725 channel_active_postparse[chan_i] (*this);
728 case MIDI::pitchbend:
729 channel_active_preparse[chan_i] (*this);
730 pitchbend (*this, (msg[2]<<7)|msg[1]);
731 channel_pitchbend[chan_i] (*this, (msg[2]<<7)|msg[1]);
732 channel_active_postparse[chan_i] (*this);
736 sysex (*this, msg, len);
739 case MIDI::mtc_quarter:
740 process_mtc_quarter_frame (msg);
741 mtc_quarter_frame (*this, *msg);
745 position (*this, msg, len);
749 song (*this, msg, len);
756 /* XXX some kind of warning ? */
760 any (*this, msg, len);
764 Parser::possible_mmc (MIDI::byte *msg, size_t msglen)
766 if (!MachineControl::is_mmc (msg, msglen)) {
770 /* hand over the just the interior MMC part of
771 the sysex msg without the leading 0xF0
775 mmc (*this, &msg[1], msglen - 1);
782 Parser::set_offline (bool yn)
784 if (_offline != yn) {
786 OfflineStatusChanged ();
788 /* this hack deals with the possibility of our first MIDI
789 bytes being running status messages.