2008-06-02 17:41:35 -04:00
|
|
|
/*
|
2019-08-02 23:10:55 -04:00
|
|
|
* Copyright (C) 1998-2018 Paul Davis <paul@linuxaudiosystems.com>
|
|
|
|
* Copyright (C) 2009-2010 Carl Hetherington <carl@carlh.net>
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License along
|
|
|
|
* with this program; if not, write to the Free Software Foundation, Inc.,
|
|
|
|
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
|
|
|
*/
|
2008-06-02 17:41:35 -04:00
|
|
|
|
|
|
|
#include <cstring>
|
|
|
|
#include <cstdlib>
|
|
|
|
#include <unistd.h>
|
2008-12-12 09:43:24 -05:00
|
|
|
#include <cstring>
|
2008-06-02 17:41:35 -04:00
|
|
|
#include <iostream>
|
|
|
|
#include <iterator>
|
|
|
|
|
2009-02-25 13:26:51 -05:00
|
|
|
#include "midi++/types.h"
|
|
|
|
#include "midi++/parser.h"
|
2012-04-23 22:28:51 -04:00
|
|
|
#include "midi++/port.h"
|
2009-02-25 13:26:51 -05:00
|
|
|
#include "midi++/mmc.h"
|
|
|
|
#include "pbd/transmitter.h"
|
2008-06-02 17:41:35 -04:00
|
|
|
|
|
|
|
using namespace std;
|
|
|
|
using namespace MIDI;
|
|
|
|
|
|
|
|
const char *
|
|
|
|
Parser::midi_event_type_name (eventType t)
|
|
|
|
|
|
|
|
{
|
|
|
|
switch (t) {
|
|
|
|
case none:
|
|
|
|
return "no midi messages";
|
|
|
|
|
|
|
|
case raw:
|
|
|
|
return "raw midi data";
|
|
|
|
|
|
|
|
case MIDI::any:
|
|
|
|
return "any midi message";
|
2015-10-05 10:17:49 -04:00
|
|
|
|
2008-06-02 17:41:35 -04:00
|
|
|
case off:
|
|
|
|
return "note off";
|
2015-10-05 10:17:49 -04:00
|
|
|
|
2008-06-02 17:41:35 -04:00
|
|
|
case on:
|
|
|
|
return "note on";
|
2015-10-05 10:17:49 -04:00
|
|
|
|
2008-06-02 17:41:35 -04:00
|
|
|
case polypress:
|
|
|
|
return "aftertouch";
|
2015-10-05 10:17:49 -04:00
|
|
|
|
2008-06-02 17:41:35 -04:00
|
|
|
case MIDI::controller:
|
|
|
|
return "controller";
|
2015-10-05 10:17:49 -04:00
|
|
|
|
2008-06-02 17:41:35 -04:00
|
|
|
case program:
|
|
|
|
return "program change";
|
2015-10-05 10:17:49 -04:00
|
|
|
|
2008-06-02 17:41:35 -04:00
|
|
|
case chanpress:
|
|
|
|
return "channel pressure";
|
2015-10-05 10:17:49 -04:00
|
|
|
|
2008-06-02 17:41:35 -04:00
|
|
|
case MIDI::pitchbend:
|
|
|
|
return "pitch bend";
|
2015-10-05 10:17:49 -04:00
|
|
|
|
2008-06-02 17:41:35 -04:00
|
|
|
case MIDI::sysex:
|
|
|
|
return "system exclusive";
|
2015-10-05 10:17:49 -04:00
|
|
|
|
2008-06-02 17:41:35 -04:00
|
|
|
case MIDI::song:
|
|
|
|
return "song position";
|
2015-10-05 10:17:49 -04:00
|
|
|
|
2008-06-02 17:41:35 -04:00
|
|
|
case MIDI::tune:
|
|
|
|
return "tune";
|
2015-10-05 10:17:49 -04:00
|
|
|
|
2008-06-02 17:41:35 -04:00
|
|
|
case MIDI::eox:
|
|
|
|
return "end of sysex";
|
2015-10-05 10:17:49 -04:00
|
|
|
|
2008-06-02 17:41:35 -04:00
|
|
|
case MIDI::timing:
|
|
|
|
return "timing";
|
2015-10-05 10:17:49 -04:00
|
|
|
|
2008-06-02 17:41:35 -04:00
|
|
|
case MIDI::start:
|
|
|
|
return "start";
|
2015-10-05 10:17:49 -04:00
|
|
|
|
2008-06-02 17:41:35 -04:00
|
|
|
case MIDI::stop:
|
|
|
|
return "continue";
|
2015-10-05 10:17:49 -04:00
|
|
|
|
2008-06-02 17:41:35 -04:00
|
|
|
case MIDI::contineu:
|
|
|
|
return "stop";
|
2015-10-05 10:17:49 -04:00
|
|
|
|
2008-06-02 17:41:35 -04:00
|
|
|
case active:
|
|
|
|
return "active sense";
|
2015-10-05 10:17:49 -04:00
|
|
|
|
2008-06-02 17:41:35 -04:00
|
|
|
default:
|
2016-02-22 15:01:23 -05:00
|
|
|
return "unknown MIDI event type";
|
2008-06-02 17:41:35 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-10-04 14:51:05 -04:00
|
|
|
Parser::Parser ()
|
2008-06-02 17:41:35 -04:00
|
|
|
{
|
|
|
|
trace_stream = 0;
|
|
|
|
trace_prefix = "";
|
|
|
|
memset (message_counter, 0, sizeof (message_counter[0]) * 256);
|
|
|
|
msgindex = 0;
|
|
|
|
msgtype = none;
|
|
|
|
msglen = 256;
|
|
|
|
msgbuf = (unsigned char *) malloc (msglen);
|
|
|
|
msgbuf[msgindex++] = 0x90;
|
|
|
|
_mmc_forward = false;
|
|
|
|
reset_mtc_state ();
|
|
|
|
_offline = false;
|
|
|
|
|
|
|
|
/* this hack deals with the possibility of our first MIDI
|
|
|
|
bytes being running status messages.
|
|
|
|
*/
|
|
|
|
|
|
|
|
channel_msg (0x90);
|
|
|
|
state = NEEDSTATUS;
|
|
|
|
|
|
|
|
pre_variable_state = NEEDSTATUS;
|
|
|
|
pre_variable_msgtype = none;
|
|
|
|
}
|
|
|
|
|
|
|
|
Parser::~Parser ()
|
|
|
|
|
|
|
|
{
|
2014-02-12 15:14:40 -05:00
|
|
|
free (msgbuf);
|
2008-06-02 17:41:35 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2018-09-18 18:51:59 -04:00
|
|
|
Parser::trace_event (Parser &, MIDI::byte *msg, size_t len, samplecnt_t /*when*/)
|
2008-06-02 17:41:35 -04:00
|
|
|
{
|
|
|
|
eventType type;
|
|
|
|
ostream *o;
|
|
|
|
|
|
|
|
if ((o = trace_stream) == NULL) { /* can be asynchronously removed */
|
|
|
|
return;
|
|
|
|
}
|
2015-10-05 10:17:49 -04:00
|
|
|
|
2008-06-02 17:41:35 -04:00
|
|
|
type = (eventType) (msg[0]&0xF0);
|
|
|
|
|
|
|
|
switch (type) {
|
|
|
|
case off:
|
|
|
|
*o << trace_prefix
|
|
|
|
<< "Channel "
|
|
|
|
<< (msg[0]&0xF)+1
|
|
|
|
<< " NoteOff NoteNum "
|
|
|
|
<< (int) msg[1]
|
|
|
|
<< " Vel "
|
|
|
|
<< (int) msg[2]
|
|
|
|
<< endmsg;
|
|
|
|
break;
|
2015-10-05 10:17:49 -04:00
|
|
|
|
2008-06-02 17:41:35 -04:00
|
|
|
case on:
|
|
|
|
*o << trace_prefix
|
|
|
|
<< "Channel "
|
|
|
|
<< (msg[0]&0xF)+1
|
|
|
|
<< " NoteOn NoteNum "
|
|
|
|
<< (int) msg[1]
|
|
|
|
<< " Vel "
|
|
|
|
<< (int) msg[2]
|
|
|
|
<< endmsg;
|
|
|
|
break;
|
2015-10-05 10:17:49 -04:00
|
|
|
|
2008-06-02 17:41:35 -04:00
|
|
|
case polypress:
|
|
|
|
*o << trace_prefix
|
|
|
|
<< "Channel "
|
|
|
|
<< (msg[0]&0xF)+1
|
2015-11-24 18:00:11 -05:00
|
|
|
<< " PolyPressure "
|
2008-06-02 17:41:35 -04:00
|
|
|
<< (int) msg[1]
|
|
|
|
<< endmsg;
|
|
|
|
break;
|
2015-10-05 10:17:49 -04:00
|
|
|
|
2008-06-02 17:41:35 -04:00
|
|
|
case MIDI::controller:
|
|
|
|
*o << trace_prefix
|
|
|
|
<< "Channel "
|
|
|
|
<< (msg[0]&0xF)+1
|
|
|
|
<< " Controller "
|
|
|
|
<< (int) msg[1]
|
|
|
|
<< " Value "
|
|
|
|
<< (int) msg[2]
|
|
|
|
<< endmsg;
|
|
|
|
break;
|
2015-10-05 10:17:49 -04:00
|
|
|
|
2008-06-02 17:41:35 -04:00
|
|
|
case program:
|
2015-10-04 14:51:05 -04:00
|
|
|
*o << trace_prefix
|
2008-06-02 17:41:35 -04:00
|
|
|
<< "Channel "
|
|
|
|
<< (msg[0]&0xF)+1
|
2010-05-15 19:57:41 -04:00
|
|
|
<< " Program Change ProgNum "
|
2008-06-02 17:41:35 -04:00
|
|
|
<< (int) msg[1]
|
|
|
|
<< endmsg;
|
|
|
|
break;
|
2015-10-05 10:17:49 -04:00
|
|
|
|
2008-06-02 17:41:35 -04:00
|
|
|
case chanpress:
|
2015-10-04 14:51:05 -04:00
|
|
|
*o << trace_prefix
|
2008-06-02 17:41:35 -04:00
|
|
|
<< "Channel "
|
|
|
|
<< (msg[0]&0xF)+1
|
|
|
|
<< " Channel Pressure "
|
|
|
|
<< (int) msg[1]
|
|
|
|
<< endmsg;
|
|
|
|
break;
|
2015-10-05 10:17:49 -04:00
|
|
|
|
2008-06-02 17:41:35 -04:00
|
|
|
case MIDI::pitchbend:
|
|
|
|
*o << trace_prefix
|
|
|
|
<< "Channel "
|
|
|
|
<< (msg[0]&0xF)+1
|
|
|
|
<< " Pitch Bend "
|
2008-07-15 00:08:49 -04:00
|
|
|
<< ((msg[2]<<7)|msg[1])
|
2008-06-02 17:41:35 -04:00
|
|
|
<< endmsg;
|
|
|
|
break;
|
2015-10-05 10:17:49 -04:00
|
|
|
|
2008-06-02 17:41:35 -04:00
|
|
|
case MIDI::sysex:
|
|
|
|
if (len == 1) {
|
|
|
|
switch (msg[0]) {
|
|
|
|
case 0xf8:
|
|
|
|
*o << trace_prefix
|
|
|
|
<< "Clock"
|
|
|
|
<< endmsg;
|
|
|
|
break;
|
|
|
|
case 0xfa:
|
|
|
|
*o << trace_prefix
|
|
|
|
<< "Start"
|
|
|
|
<< endmsg;
|
|
|
|
break;
|
|
|
|
case 0xfb:
|
|
|
|
*o << trace_prefix
|
|
|
|
<< "Continue"
|
|
|
|
<< endmsg;
|
|
|
|
break;
|
|
|
|
case 0xfc:
|
|
|
|
*o << trace_prefix
|
|
|
|
<< "Stop"
|
|
|
|
<< endmsg;
|
|
|
|
break;
|
|
|
|
case 0xfe:
|
|
|
|
*o << trace_prefix
|
|
|
|
<< "Active Sense"
|
|
|
|
<< endmsg;
|
|
|
|
break;
|
|
|
|
case 0xff:
|
|
|
|
*o << trace_prefix
|
|
|
|
<< "System Reset"
|
|
|
|
<< endmsg;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
*o << trace_prefix
|
|
|
|
<< "System Exclusive (1 byte : " << hex << (int) *msg << dec << ')'
|
2015-10-05 10:17:49 -04:00
|
|
|
<< endmsg;
|
2008-06-02 17:41:35 -04:00
|
|
|
break;
|
2015-10-04 14:51:05 -04:00
|
|
|
}
|
2008-06-02 17:41:35 -04:00
|
|
|
} else {
|
|
|
|
*o << trace_prefix
|
|
|
|
<< "System Exclusive (" << len << ") = [ " << hex;
|
|
|
|
for (unsigned int i = 0; i < len; ++i) {
|
|
|
|
*o << (int) msgbuf[i] << ' ';
|
|
|
|
}
|
|
|
|
*o << dec << ']' << endmsg;
|
2015-10-05 10:17:49 -04:00
|
|
|
|
2008-06-02 17:41:35 -04:00
|
|
|
}
|
|
|
|
break;
|
2015-10-05 10:17:49 -04:00
|
|
|
|
2008-06-02 17:41:35 -04:00
|
|
|
case MIDI::song:
|
|
|
|
*o << trace_prefix << "Song" << endmsg;
|
|
|
|
break;
|
2015-10-05 10:17:49 -04:00
|
|
|
|
2008-06-02 17:41:35 -04:00
|
|
|
case MIDI::tune:
|
|
|
|
*o << trace_prefix << "Tune" << endmsg;
|
|
|
|
break;
|
2015-10-05 10:17:49 -04:00
|
|
|
|
2008-06-02 17:41:35 -04:00
|
|
|
case MIDI::eox:
|
|
|
|
*o << trace_prefix << "End-of-System Exclusive" << endmsg;
|
|
|
|
break;
|
2015-10-05 10:17:49 -04:00
|
|
|
|
2008-06-02 17:41:35 -04:00
|
|
|
case MIDI::timing:
|
|
|
|
*o << trace_prefix << "Timing" << endmsg;
|
|
|
|
break;
|
2015-10-05 10:17:49 -04:00
|
|
|
|
2008-06-02 17:41:35 -04:00
|
|
|
case MIDI::start:
|
|
|
|
*o << trace_prefix << "Start" << endmsg;
|
|
|
|
break;
|
2015-10-05 10:17:49 -04:00
|
|
|
|
2008-06-02 17:41:35 -04:00
|
|
|
case MIDI::stop:
|
|
|
|
*o << trace_prefix << "Stop" << endmsg;
|
|
|
|
break;
|
2015-10-05 10:17:49 -04:00
|
|
|
|
2008-06-02 17:41:35 -04:00
|
|
|
case MIDI::contineu:
|
|
|
|
*o << trace_prefix << "Continue" << endmsg;
|
|
|
|
break;
|
2015-10-05 10:17:49 -04:00
|
|
|
|
2008-06-02 17:41:35 -04:00
|
|
|
case active:
|
|
|
|
*o << trace_prefix << "Active Sense" << endmsg;
|
|
|
|
break;
|
2015-10-05 10:17:49 -04:00
|
|
|
|
2008-06-02 17:41:35 -04:00
|
|
|
default:
|
|
|
|
*o << trace_prefix << "Unrecognized MIDI message" << endmsg;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
Parser::trace (bool onoff, ostream *o, const string &prefix)
|
|
|
|
{
|
|
|
|
trace_connection.disconnect ();
|
|
|
|
|
|
|
|
if (onoff) {
|
|
|
|
trace_stream = o;
|
|
|
|
trace_prefix = prefix;
|
2018-09-18 18:51:59 -04:00
|
|
|
any.connect_same_thread (trace_connection, boost::bind (&Parser::trace_event, this, _1, _2, _3, _4));
|
2008-06-02 17:41:35 -04:00
|
|
|
} else {
|
|
|
|
trace_prefix = "";
|
|
|
|
trace_stream = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
Parser::scanner (unsigned char inbyte)
|
|
|
|
{
|
|
|
|
bool statusbit;
|
2010-07-01 11:02:54 -04:00
|
|
|
boost::optional<int> edit_result;
|
2008-06-02 17:41:35 -04:00
|
|
|
|
|
|
|
// cerr << "parse: " << hex << (int) inbyte << dec << " state = " << state << " msgindex = " << msgindex << " runnable = " << runnable << endl;
|
2015-10-05 10:17:49 -04:00
|
|
|
|
2015-10-04 14:51:05 -04:00
|
|
|
/* Check active sensing early, so it doesn't interrupt sysex.
|
2015-10-05 10:17:49 -04:00
|
|
|
|
2008-06-02 17:41:35 -04:00
|
|
|
NOTE: active sense messages are not considered to fit under
|
|
|
|
"any" for the purposes of callbacks. If a caller wants
|
|
|
|
active sense messages handled, which is unlikely, then
|
|
|
|
they can just ask for it specifically. They are so unlike
|
|
|
|
every other MIDI message in terms of semantics that its
|
|
|
|
counter-productive to treat them similarly.
|
|
|
|
*/
|
2015-10-05 10:17:49 -04:00
|
|
|
|
2008-06-02 17:41:35 -04:00
|
|
|
if (inbyte == 0xfe) {
|
|
|
|
message_counter[inbyte]++;
|
|
|
|
if (!_offline) {
|
|
|
|
active_sense (*this);
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
2015-10-05 10:17:49 -04:00
|
|
|
|
2018-07-04 15:40:47 -04:00
|
|
|
/* ditto for system reset, except do even less */
|
|
|
|
|
|
|
|
if (inbyte == 0xff) {
|
|
|
|
message_counter[inbyte]++;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2008-06-02 17:41:35 -04:00
|
|
|
/* If necessary, allocate larger message buffer. */
|
2015-10-05 10:17:49 -04:00
|
|
|
|
2008-06-02 17:41:35 -04:00
|
|
|
if (msgindex >= msglen) {
|
|
|
|
msglen *= 2;
|
|
|
|
msgbuf = (unsigned char *) realloc (msgbuf, msglen);
|
|
|
|
}
|
2015-10-05 10:17:49 -04:00
|
|
|
|
2008-06-02 17:41:35 -04:00
|
|
|
/*
|
|
|
|
Real time messages can occur ANYPLACE,
|
2015-10-04 14:51:05 -04:00
|
|
|
but do not interrupt running status.
|
2008-06-02 17:41:35 -04:00
|
|
|
*/
|
|
|
|
|
|
|
|
bool rtmsg = false;
|
2015-10-05 10:17:49 -04:00
|
|
|
|
2008-06-02 17:41:35 -04:00
|
|
|
switch (inbyte) {
|
|
|
|
case 0xf8:
|
|
|
|
rtmsg = true;
|
|
|
|
break;
|
|
|
|
case 0xfa:
|
|
|
|
rtmsg = true;
|
|
|
|
break;
|
|
|
|
case 0xfb:
|
|
|
|
rtmsg = true;
|
|
|
|
break;
|
|
|
|
case 0xfc:
|
|
|
|
rtmsg = true;
|
|
|
|
break;
|
|
|
|
case 0xfd:
|
|
|
|
rtmsg = true;
|
|
|
|
break;
|
|
|
|
case 0xfe:
|
|
|
|
rtmsg = true;
|
|
|
|
break;
|
|
|
|
case 0xff:
|
|
|
|
rtmsg = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (rtmsg) {
|
2010-01-31 23:08:49 -05:00
|
|
|
boost::optional<int> res = edit (&inbyte, 1);
|
2015-10-05 10:17:49 -04:00
|
|
|
|
2010-01-31 23:08:49 -05:00
|
|
|
if (res.get_value_or (1) >= 0 && !_offline) {
|
2008-06-02 17:41:35 -04:00
|
|
|
realtime_msg (inbyte);
|
2015-10-04 14:51:05 -04:00
|
|
|
}
|
2015-10-05 10:17:49 -04:00
|
|
|
|
2008-06-02 17:41:35 -04:00
|
|
|
return;
|
2015-10-04 14:51:05 -04:00
|
|
|
}
|
2008-06-02 17:41:35 -04:00
|
|
|
|
|
|
|
statusbit = (inbyte & 0x80);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Variable length messages (ie. the 'system exclusive')
|
|
|
|
* can be terminated by the next status byte, not necessarily
|
|
|
|
* an EOX. Actually, since EOX is a status byte, this
|
|
|
|
* code ALWAYS handles the end of a VARIABLELENGTH message.
|
|
|
|
*/
|
2009-12-22 15:21:43 -05:00
|
|
|
|
2008-06-02 17:41:35 -04:00
|
|
|
if (state == VARIABLELENGTH && statusbit) {
|
2009-12-22 15:21:43 -05:00
|
|
|
|
2008-06-02 17:41:35 -04:00
|
|
|
/* The message has ended, so process it */
|
|
|
|
|
|
|
|
/* add EOX to any sysex message */
|
|
|
|
|
|
|
|
if (inbyte == MIDI::eox) {
|
|
|
|
msgbuf[msgindex++] = inbyte;
|
|
|
|
}
|
|
|
|
|
|
|
|
#if 0
|
|
|
|
cerr << "SYSEX: " << hex;
|
|
|
|
for (unsigned int i = 0; i < msgindex; ++i) {
|
|
|
|
cerr << (int) msgbuf[i] << ' ';
|
|
|
|
}
|
|
|
|
cerr << dec << endl;
|
|
|
|
#endif
|
2010-01-31 23:08:49 -05:00
|
|
|
if (msgindex > 0) {
|
|
|
|
|
|
|
|
boost::optional<int> res = edit (msgbuf, msgindex);
|
|
|
|
|
|
|
|
if (res.get_value_or (1) >= 0) {
|
|
|
|
if (!possible_mmc (msgbuf, msgindex) || _mmc_forward) {
|
|
|
|
if (!possible_mtc (msgbuf, msgindex) || _mtc_forward) {
|
|
|
|
if (!_offline) {
|
|
|
|
sysex (*this, msgbuf, msgindex);
|
|
|
|
}
|
2008-06-02 17:41:35 -04:00
|
|
|
}
|
|
|
|
}
|
2010-01-31 23:08:49 -05:00
|
|
|
if (!_offline) {
|
2018-09-18 18:51:59 -04:00
|
|
|
any (*this, msgbuf, msgindex, _timestamp);
|
2015-10-04 14:51:05 -04:00
|
|
|
}
|
2008-06-02 17:41:35 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2015-10-05 10:17:49 -04:00
|
|
|
|
2008-06-02 17:41:35 -04:00
|
|
|
/*
|
|
|
|
* Status bytes always start a new message, except EOX
|
|
|
|
*/
|
2015-10-05 10:17:49 -04:00
|
|
|
|
2008-06-02 17:41:35 -04:00
|
|
|
if (statusbit) {
|
|
|
|
|
|
|
|
msgindex = 0;
|
|
|
|
|
|
|
|
if (inbyte == MIDI::eox) {
|
|
|
|
/* return to the state we had pre-sysex */
|
|
|
|
|
|
|
|
state = pre_variable_state;
|
|
|
|
runnable = was_runnable;
|
|
|
|
msgtype = pre_variable_msgtype;
|
|
|
|
|
|
|
|
if (state != NEEDSTATUS && runnable) {
|
|
|
|
msgbuf[msgindex++] = last_status_byte;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
msgbuf[msgindex++] = inbyte;
|
|
|
|
if ((inbyte & 0xf0) == 0xf0) {
|
|
|
|
system_msg (inbyte);
|
|
|
|
runnable = false;
|
|
|
|
} else {
|
|
|
|
channel_msg (inbyte);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
2015-10-05 10:17:49 -04:00
|
|
|
|
2008-06-02 17:41:35 -04:00
|
|
|
/*
|
|
|
|
* We've got a Data byte.
|
|
|
|
*/
|
2015-10-05 10:17:49 -04:00
|
|
|
|
2008-06-02 17:41:35 -04:00
|
|
|
msgbuf[msgindex++] = inbyte;
|
|
|
|
|
|
|
|
switch (state) {
|
|
|
|
case NEEDSTATUS:
|
|
|
|
/*
|
|
|
|
* We shouldn't get here, since in NEEDSTATUS mode
|
|
|
|
* we're expecting a new status byte, NOT any
|
|
|
|
* data bytes. On the other hand, some equipment
|
|
|
|
* with leaky modwheels and the like might be
|
|
|
|
* sending data bytes as part of running controller
|
|
|
|
* messages, so just handle it silently.
|
|
|
|
*/
|
|
|
|
break;
|
2015-10-05 10:17:49 -04:00
|
|
|
|
2008-06-02 17:41:35 -04:00
|
|
|
case NEEDTWOBYTES:
|
|
|
|
/* wait for the second byte */
|
2019-09-18 11:27:09 -04:00
|
|
|
if (msgindex < 3) {
|
2008-06-02 17:41:35 -04:00
|
|
|
return;
|
2019-09-18 11:27:09 -04:00
|
|
|
}
|
|
|
|
/* fallthrough */
|
2015-10-05 10:17:49 -04:00
|
|
|
|
2008-06-02 17:41:35 -04:00
|
|
|
case NEEDONEBYTE:
|
|
|
|
/* We've completed a 1 or 2 byte message. */
|
2010-07-01 11:02:54 -04:00
|
|
|
|
|
|
|
edit_result = edit (msgbuf, msgindex);
|
2015-10-04 14:51:05 -04:00
|
|
|
|
2010-07-01 11:02:54 -04:00
|
|
|
if (edit_result.get_value_or (1)) {
|
2015-10-05 10:17:49 -04:00
|
|
|
|
2008-06-02 17:41:35 -04:00
|
|
|
/* message not cancelled by an editor */
|
2015-10-05 10:17:49 -04:00
|
|
|
|
2008-06-02 17:41:35 -04:00
|
|
|
message_counter[msgbuf[0] & 0xF0]++;
|
|
|
|
|
|
|
|
if (!_offline) {
|
|
|
|
signal (msgbuf, msgindex);
|
|
|
|
}
|
|
|
|
}
|
2015-10-05 10:17:49 -04:00
|
|
|
|
2008-06-02 17:41:35 -04:00
|
|
|
if (runnable) {
|
2015-10-04 14:51:05 -04:00
|
|
|
/* In Runnable mode, we reset the message
|
|
|
|
index, but keep the callbacks_pending and state the
|
|
|
|
same. This provides the "running status
|
2008-06-02 17:41:35 -04:00
|
|
|
byte" feature.
|
|
|
|
*/
|
|
|
|
msgindex = 1;
|
|
|
|
} else {
|
|
|
|
/* If not Runnable, reset to NEEDSTATUS mode */
|
|
|
|
state = NEEDSTATUS;
|
|
|
|
}
|
|
|
|
break;
|
2015-10-05 10:17:49 -04:00
|
|
|
|
2008-06-02 17:41:35 -04:00
|
|
|
case VARIABLELENGTH:
|
|
|
|
/* nothing to do */
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Call the real-time function for the specified byte, immediately.
|
|
|
|
* These can occur anywhere, so they don't change the state.
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
Parser::realtime_msg(unsigned char inbyte)
|
|
|
|
|
|
|
|
{
|
|
|
|
message_counter[inbyte]++;
|
|
|
|
|
|
|
|
if (_offline) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
switch (inbyte) {
|
|
|
|
case 0xf8:
|
2009-12-01 13:31:00 -05:00
|
|
|
timing (*this, _timestamp);
|
2008-06-02 17:41:35 -04:00
|
|
|
break;
|
|
|
|
case 0xfa:
|
2009-12-01 13:31:00 -05:00
|
|
|
start (*this, _timestamp);
|
2008-06-02 17:41:35 -04:00
|
|
|
break;
|
|
|
|
case 0xfb:
|
2009-12-01 13:31:00 -05:00
|
|
|
contineu (*this, _timestamp);
|
2008-06-02 17:41:35 -04:00
|
|
|
break;
|
|
|
|
case 0xfc:
|
2009-12-01 13:31:00 -05:00
|
|
|
stop (*this, _timestamp);
|
2008-06-02 17:41:35 -04:00
|
|
|
break;
|
|
|
|
case 0xfe:
|
|
|
|
/* !!! active sense message in realtime_msg: should not reach here
|
2015-10-04 14:51:05 -04:00
|
|
|
*/
|
2008-06-02 17:41:35 -04:00
|
|
|
break;
|
|
|
|
case 0xff:
|
|
|
|
reset (*this);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2018-09-18 18:51:59 -04:00
|
|
|
any (*this, &inbyte, 1, _timestamp);
|
2008-06-02 17:41:35 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/** Interpret a Channel (voice or mode) Message status byte.
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
Parser::channel_msg(unsigned char inbyte)
|
|
|
|
{
|
|
|
|
last_status_byte = inbyte;
|
|
|
|
runnable = true; /* Channel messages can use running status */
|
2015-10-04 14:51:05 -04:00
|
|
|
|
2008-06-02 17:41:35 -04:00
|
|
|
/* The high 4 bits, which determine the type of channel message. */
|
2015-10-04 14:51:05 -04:00
|
|
|
|
2008-06-02 17:41:35 -04:00
|
|
|
switch (inbyte&0xF0) {
|
|
|
|
case 0x80:
|
|
|
|
msgtype = off;
|
|
|
|
state = NEEDTWOBYTES;
|
|
|
|
break;
|
|
|
|
case 0x90:
|
|
|
|
msgtype = on;
|
|
|
|
state = NEEDTWOBYTES;
|
|
|
|
break;
|
|
|
|
case 0xa0:
|
|
|
|
msgtype = polypress;
|
|
|
|
state = NEEDTWOBYTES;
|
|
|
|
break;
|
|
|
|
case 0xb0:
|
|
|
|
msgtype = MIDI::controller;
|
|
|
|
state = NEEDTWOBYTES;
|
|
|
|
break;
|
|
|
|
case 0xc0:
|
|
|
|
msgtype = program;
|
|
|
|
state = NEEDONEBYTE;
|
|
|
|
break;
|
|
|
|
case 0xd0:
|
|
|
|
msgtype = chanpress;
|
|
|
|
state = NEEDONEBYTE;
|
|
|
|
break;
|
|
|
|
case 0xe0:
|
|
|
|
msgtype = MIDI::pitchbend;
|
|
|
|
state = NEEDTWOBYTES;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Initialize (and possibly emit) the signals for the
|
|
|
|
* specified byte. Set the state that the state-machine
|
|
|
|
* should go into. If the signal is not emitted
|
|
|
|
* immediately, it will be when the state machine gets to
|
|
|
|
* the end of the MIDI message.
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
Parser::system_msg (unsigned char inbyte)
|
|
|
|
{
|
|
|
|
message_counter[inbyte]++;
|
|
|
|
|
|
|
|
switch (inbyte) {
|
|
|
|
case 0xf0:
|
|
|
|
pre_variable_msgtype = msgtype;
|
|
|
|
pre_variable_state = state;
|
|
|
|
was_runnable = runnable;
|
|
|
|
msgtype = MIDI::sysex;
|
|
|
|
state = VARIABLELENGTH;
|
|
|
|
break;
|
|
|
|
case 0xf1:
|
|
|
|
msgtype = MIDI::mtc_quarter;
|
|
|
|
state = NEEDONEBYTE;
|
|
|
|
break;
|
|
|
|
case 0xf2:
|
|
|
|
msgtype = MIDI::position;
|
|
|
|
state = NEEDTWOBYTES;
|
|
|
|
break;
|
|
|
|
case 0xf3:
|
|
|
|
msgtype = MIDI::song;
|
|
|
|
state = NEEDONEBYTE;
|
|
|
|
break;
|
|
|
|
case 0xf6:
|
|
|
|
if (!_offline) {
|
|
|
|
tune (*this);
|
|
|
|
}
|
|
|
|
state = NEEDSTATUS;
|
|
|
|
break;
|
|
|
|
case 0xf7:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2015-10-04 14:51:05 -04:00
|
|
|
// all these messages will be sent via any()
|
2008-06-02 17:41:35 -04:00
|
|
|
// when they are complete.
|
2018-09-18 18:51:59 -04:00
|
|
|
// any (*this, &inbyte, 1, _timestamp);
|
2008-06-02 17:41:35 -04:00
|
|
|
}
|
|
|
|
|
2015-10-04 14:51:05 -04:00
|
|
|
void
|
2013-07-25 02:19:51 -04:00
|
|
|
Parser::signal (MIDI::byte *msg, size_t len)
|
2008-06-02 17:41:35 -04:00
|
|
|
{
|
|
|
|
channel_t chan = msg[0]&0xF;
|
|
|
|
int chan_i = chan;
|
|
|
|
|
|
|
|
switch (msgtype) {
|
|
|
|
case none:
|
|
|
|
break;
|
2015-10-05 10:17:49 -04:00
|
|
|
|
2008-06-02 17:41:35 -04:00
|
|
|
case off:
|
|
|
|
channel_active_preparse[chan_i] (*this);
|
|
|
|
note_off (*this, (EventTwoBytes *) &msg[1]);
|
2015-10-04 14:51:05 -04:00
|
|
|
channel_note_off[chan_i]
|
2008-06-02 17:41:35 -04:00
|
|
|
(*this, (EventTwoBytes *) &msg[1]);
|
|
|
|
channel_active_postparse[chan_i] (*this);
|
|
|
|
break;
|
2015-10-05 10:17:49 -04:00
|
|
|
|
2008-06-02 17:41:35 -04:00
|
|
|
case on:
|
|
|
|
channel_active_preparse[chan_i] (*this);
|
|
|
|
|
|
|
|
/* Hack to deal with MIDI sources that use velocity=0
|
|
|
|
instead of noteOff.
|
|
|
|
*/
|
|
|
|
|
|
|
|
if (msg[2] == 0) {
|
|
|
|
note_off (*this, (EventTwoBytes *) &msg[1]);
|
2015-10-04 14:51:05 -04:00
|
|
|
channel_note_off[chan_i]
|
2008-06-02 17:41:35 -04:00
|
|
|
(*this, (EventTwoBytes *) &msg[1]);
|
|
|
|
} else {
|
|
|
|
note_on (*this, (EventTwoBytes *) &msg[1]);
|
2015-10-04 14:51:05 -04:00
|
|
|
channel_note_on[chan_i]
|
2008-06-02 17:41:35 -04:00
|
|
|
(*this, (EventTwoBytes *) &msg[1]);
|
|
|
|
}
|
|
|
|
|
|
|
|
channel_active_postparse[chan_i] (*this);
|
|
|
|
break;
|
2015-10-05 10:17:49 -04:00
|
|
|
|
2008-06-02 17:41:35 -04:00
|
|
|
case MIDI::controller:
|
|
|
|
channel_active_preparse[chan_i] (*this);
|
|
|
|
controller (*this, (EventTwoBytes *) &msg[1]);
|
2015-10-04 14:51:05 -04:00
|
|
|
channel_controller[chan_i]
|
2008-06-02 17:41:35 -04:00
|
|
|
(*this, (EventTwoBytes *) &msg[1]);
|
|
|
|
channel_active_postparse[chan_i] (*this);
|
|
|
|
break;
|
2015-10-05 10:17:49 -04:00
|
|
|
|
2008-06-02 17:41:35 -04:00
|
|
|
case program:
|
|
|
|
channel_active_preparse[chan_i] (*this);
|
|
|
|
program_change (*this, msg[1]);
|
|
|
|
channel_program_change[chan_i] (*this, msg[1]);
|
|
|
|
channel_active_postparse[chan_i] (*this);
|
|
|
|
break;
|
2015-10-05 10:17:49 -04:00
|
|
|
|
2008-06-02 17:41:35 -04:00
|
|
|
case chanpress:
|
|
|
|
channel_active_preparse[chan_i] (*this);
|
|
|
|
pressure (*this, msg[1]);
|
|
|
|
channel_pressure[chan_i] (*this, msg[1]);
|
|
|
|
channel_active_postparse[chan_i] (*this);
|
|
|
|
break;
|
2015-10-05 10:17:49 -04:00
|
|
|
|
2008-06-02 17:41:35 -04:00
|
|
|
case polypress:
|
|
|
|
channel_active_preparse[chan_i] (*this);
|
|
|
|
poly_pressure (*this, (EventTwoBytes *) &msg[1]);
|
2015-10-04 14:51:05 -04:00
|
|
|
channel_poly_pressure[chan_i]
|
2008-06-02 17:41:35 -04:00
|
|
|
(*this, (EventTwoBytes *) &msg[1]);
|
|
|
|
channel_active_postparse[chan_i] (*this);
|
|
|
|
break;
|
2015-10-05 10:17:49 -04:00
|
|
|
|
2008-06-02 17:41:35 -04:00
|
|
|
case MIDI::pitchbend:
|
|
|
|
channel_active_preparse[chan_i] (*this);
|
2008-07-15 00:08:49 -04:00
|
|
|
pitchbend (*this, (msg[2]<<7)|msg[1]);
|
|
|
|
channel_pitchbend[chan_i] (*this, (msg[2]<<7)|msg[1]);
|
2008-06-02 17:41:35 -04:00
|
|
|
channel_active_postparse[chan_i] (*this);
|
|
|
|
break;
|
2015-10-05 10:17:49 -04:00
|
|
|
|
2008-06-02 17:41:35 -04:00
|
|
|
case MIDI::sysex:
|
|
|
|
sysex (*this, msg, len);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case MIDI::mtc_quarter:
|
|
|
|
process_mtc_quarter_frame (msg);
|
|
|
|
mtc_quarter_frame (*this, *msg);
|
|
|
|
break;
|
2015-10-05 10:17:49 -04:00
|
|
|
|
2008-06-02 17:41:35 -04:00
|
|
|
case MIDI::position:
|
2018-09-25 17:46:49 -04:00
|
|
|
position (*this, msg, len, _timestamp);
|
2008-06-02 17:41:35 -04:00
|
|
|
break;
|
2015-10-05 10:17:49 -04:00
|
|
|
|
2008-06-02 17:41:35 -04:00
|
|
|
case MIDI::song:
|
|
|
|
song (*this, msg, len);
|
|
|
|
break;
|
2015-10-05 10:17:49 -04:00
|
|
|
|
2008-06-02 17:41:35 -04:00
|
|
|
case MIDI::tune:
|
|
|
|
tune (*this);
|
2015-10-05 10:17:49 -04:00
|
|
|
|
2008-06-02 17:41:35 -04:00
|
|
|
default:
|
|
|
|
/* XXX some kind of warning ? */
|
|
|
|
break;
|
|
|
|
}
|
2015-10-05 10:17:49 -04:00
|
|
|
|
2018-09-18 18:51:59 -04:00
|
|
|
any (*this, msg, len, _timestamp);
|
2008-06-02 17:41:35 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
2013-07-25 02:19:51 -04:00
|
|
|
Parser::possible_mmc (MIDI::byte *msg, size_t msglen)
|
2008-06-02 17:41:35 -04:00
|
|
|
{
|
|
|
|
if (!MachineControl::is_mmc (msg, msglen)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* hand over the just the interior MMC part of
|
|
|
|
the sysex msg without the leading 0xF0
|
|
|
|
*/
|
|
|
|
|
|
|
|
if (!_offline) {
|
|
|
|
mmc (*this, &msg[1], msglen - 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
Parser::set_offline (bool yn)
|
|
|
|
{
|
|
|
|
if (_offline != yn) {
|
|
|
|
_offline = yn;
|
|
|
|
OfflineStatusChanged ();
|
|
|
|
|
|
|
|
/* this hack deals with the possibility of our first MIDI
|
|
|
|
bytes being running status messages.
|
|
|
|
*/
|
2015-10-05 10:17:49 -04:00
|
|
|
|
2008-06-02 17:41:35 -04:00
|
|
|
channel_msg (0x90);
|
|
|
|
state = NEEDSTATUS;
|
|
|
|
}
|
|
|
|
}
|