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 "unknown 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, samplecnt_t /*when*/)
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, _4));
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 /* ditto for system reset, except do even less */
351 if (inbyte == 0xff) {
352 message_counter[inbyte]++;
356 /* If necessary, allocate larger message buffer. */
358 if (msgindex >= msglen) {
360 msgbuf = (unsigned char *) realloc (msgbuf, msglen);
364 Real time messages can occur ANYPLACE,
365 but do not interrupt running status.
395 boost::optional<int> res = edit (&inbyte, 1);
397 if (res.get_value_or (1) >= 0 && !_offline) {
398 realtime_msg (inbyte);
404 statusbit = (inbyte & 0x80);
407 * Variable length messages (ie. the 'system exclusive')
408 * can be terminated by the next status byte, not necessarily
409 * an EOX. Actually, since EOX is a status byte, this
410 * code ALWAYS handles the end of a VARIABLELENGTH message.
413 if (state == VARIABLELENGTH && statusbit) {
415 /* The message has ended, so process it */
417 /* add EOX to any sysex message */
419 if (inbyte == MIDI::eox) {
420 msgbuf[msgindex++] = inbyte;
424 cerr << "SYSEX: " << hex;
425 for (unsigned int i = 0; i < msgindex; ++i) {
426 cerr << (int) msgbuf[i] << ' ';
432 boost::optional<int> res = edit (msgbuf, msgindex);
434 if (res.get_value_or (1) >= 0) {
435 if (!possible_mmc (msgbuf, msgindex) || _mmc_forward) {
436 if (!possible_mtc (msgbuf, msgindex) || _mtc_forward) {
438 sysex (*this, msgbuf, msgindex);
443 any (*this, msgbuf, msgindex, _timestamp);
450 * Status bytes always start a new message, except EOX
457 if (inbyte == MIDI::eox) {
458 /* return to the state we had pre-sysex */
460 state = pre_variable_state;
461 runnable = was_runnable;
462 msgtype = pre_variable_msgtype;
464 if (state != NEEDSTATUS && runnable) {
465 msgbuf[msgindex++] = last_status_byte;
468 msgbuf[msgindex++] = inbyte;
469 if ((inbyte & 0xf0) == 0xf0) {
473 channel_msg (inbyte);
481 * We've got a Data byte.
484 msgbuf[msgindex++] = inbyte;
489 * We shouldn't get here, since in NEEDSTATUS mode
490 * we're expecting a new status byte, NOT any
491 * data bytes. On the other hand, some equipment
492 * with leaky modwheels and the like might be
493 * sending data bytes as part of running controller
494 * messages, so just handle it silently.
499 /* wait for the second byte */
505 /* We've completed a 1 or 2 byte message. */
507 edit_result = edit (msgbuf, msgindex);
509 if (edit_result.get_value_or (1)) {
511 /* message not cancelled by an editor */
513 message_counter[msgbuf[0] & 0xF0]++;
516 signal (msgbuf, msgindex);
521 /* In Runnable mode, we reset the message
522 index, but keep the callbacks_pending and state the
523 same. This provides the "running status
528 /* If not Runnable, reset to NEEDSTATUS mode */
540 /** Call the real-time function for the specified byte, immediately.
541 * These can occur anywhere, so they don't change the state.
544 Parser::realtime_msg(unsigned char inbyte)
547 message_counter[inbyte]++;
555 timing (*this, _timestamp);
558 start (*this, _timestamp);
561 contineu (*this, _timestamp);
564 stop (*this, _timestamp);
567 /* !!! active sense message in realtime_msg: should not reach here
575 any (*this, &inbyte, 1, _timestamp);
579 /** Interpret a Channel (voice or mode) Message status byte.
582 Parser::channel_msg(unsigned char inbyte)
584 last_status_byte = inbyte;
585 runnable = true; /* Channel messages can use running status */
587 /* The high 4 bits, which determine the type of channel message. */
589 switch (inbyte&0xF0) {
592 state = NEEDTWOBYTES;
596 state = NEEDTWOBYTES;
600 state = NEEDTWOBYTES;
603 msgtype = MIDI::controller;
604 state = NEEDTWOBYTES;
615 msgtype = MIDI::pitchbend;
616 state = NEEDTWOBYTES;
621 /** Initialize (and possibly emit) the signals for the
622 * specified byte. Set the state that the state-machine
623 * should go into. If the signal is not emitted
624 * immediately, it will be when the state machine gets to
625 * the end of the MIDI message.
628 Parser::system_msg (unsigned char inbyte)
630 message_counter[inbyte]++;
634 pre_variable_msgtype = msgtype;
635 pre_variable_state = state;
636 was_runnable = runnable;
637 msgtype = MIDI::sysex;
638 state = VARIABLELENGTH;
641 msgtype = MIDI::mtc_quarter;
645 msgtype = MIDI::position;
646 state = NEEDTWOBYTES;
649 msgtype = MIDI::song;
662 // all these messages will be sent via any()
663 // when they are complete.
664 // any (*this, &inbyte, 1, _timestamp);
668 Parser::signal (MIDI::byte *msg, size_t len)
670 channel_t chan = msg[0]&0xF;
678 channel_active_preparse[chan_i] (*this);
679 note_off (*this, (EventTwoBytes *) &msg[1]);
680 channel_note_off[chan_i]
681 (*this, (EventTwoBytes *) &msg[1]);
682 channel_active_postparse[chan_i] (*this);
686 channel_active_preparse[chan_i] (*this);
688 /* Hack to deal with MIDI sources that use velocity=0
693 note_off (*this, (EventTwoBytes *) &msg[1]);
694 channel_note_off[chan_i]
695 (*this, (EventTwoBytes *) &msg[1]);
697 note_on (*this, (EventTwoBytes *) &msg[1]);
698 channel_note_on[chan_i]
699 (*this, (EventTwoBytes *) &msg[1]);
702 channel_active_postparse[chan_i] (*this);
705 case MIDI::controller:
706 channel_active_preparse[chan_i] (*this);
707 controller (*this, (EventTwoBytes *) &msg[1]);
708 channel_controller[chan_i]
709 (*this, (EventTwoBytes *) &msg[1]);
710 channel_active_postparse[chan_i] (*this);
714 channel_active_preparse[chan_i] (*this);
715 program_change (*this, msg[1]);
716 channel_program_change[chan_i] (*this, msg[1]);
717 channel_active_postparse[chan_i] (*this);
721 channel_active_preparse[chan_i] (*this);
722 pressure (*this, msg[1]);
723 channel_pressure[chan_i] (*this, msg[1]);
724 channel_active_postparse[chan_i] (*this);
728 channel_active_preparse[chan_i] (*this);
729 poly_pressure (*this, (EventTwoBytes *) &msg[1]);
730 channel_poly_pressure[chan_i]
731 (*this, (EventTwoBytes *) &msg[1]);
732 channel_active_postparse[chan_i] (*this);
735 case MIDI::pitchbend:
736 channel_active_preparse[chan_i] (*this);
737 pitchbend (*this, (msg[2]<<7)|msg[1]);
738 channel_pitchbend[chan_i] (*this, (msg[2]<<7)|msg[1]);
739 channel_active_postparse[chan_i] (*this);
743 sysex (*this, msg, len);
746 case MIDI::mtc_quarter:
747 process_mtc_quarter_frame (msg);
748 mtc_quarter_frame (*this, *msg);
752 position (*this, msg, len);
756 song (*this, msg, len);
763 /* XXX some kind of warning ? */
767 any (*this, msg, len, _timestamp);
771 Parser::possible_mmc (MIDI::byte *msg, size_t msglen)
773 if (!MachineControl::is_mmc (msg, msglen)) {
777 /* hand over the just the interior MMC part of
778 the sysex msg without the leading 0xF0
782 mmc (*this, &msg[1], msglen - 1);
789 Parser::set_offline (bool yn)
791 if (_offline != yn) {
793 OfflineStatusChanged ();
795 /* this hack deals with the possibility of our first MIDI
796 bytes being running status messages.