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
)
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
&, 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
[2]<<7)|msg
[1])
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 any
.connect_same_thread (trace_connection
, boost::bind (&Parser::trace_event
, this, _1
, _2
, _3
));
326 Parser::scanner (unsigned char inbyte
)
329 boost::optional
<int> edit_result
;
331 // cerr << "parse: " << hex << (int) inbyte << dec << " state = " << state << " msgindex = " << msgindex << " runnable = " << runnable << endl;
333 /* Check active sensing early, so it doesn't interrupt sysex.
335 NOTE: active sense messages are not considered to fit under
336 "any" for the purposes of callbacks. If a caller wants
337 active sense messages handled, which is unlikely, then
338 they can just ask for it specifically. They are so unlike
339 every other MIDI message in terms of semantics that its
340 counter-productive to treat them similarly.
343 if (inbyte
== 0xfe) {
344 message_counter
[inbyte
]++;
346 active_sense (*this);
351 /* If necessary, allocate larger message buffer. */
353 if (msgindex
>= msglen
) {
355 msgbuf
= (unsigned char *) realloc (msgbuf
, msglen
);
359 Real time messages can occur ANYPLACE,
360 but do not interrupt running status.
390 boost::optional
<int> res
= edit (&inbyte
, 1);
392 if (res
.get_value_or (1) >= 0 && !_offline
) {
393 realtime_msg (inbyte
);
399 statusbit
= (inbyte
& 0x80);
402 * Variable length messages (ie. the 'system exclusive')
403 * can be terminated by the next status byte, not necessarily
404 * an EOX. Actually, since EOX is a status byte, this
405 * code ALWAYS handles the end of a VARIABLELENGTH message.
408 if (state
== VARIABLELENGTH
&& statusbit
) {
410 /* The message has ended, so process it */
412 /* add EOX to any sysex message */
414 if (inbyte
== MIDI::eox
) {
415 msgbuf
[msgindex
++] = inbyte
;
419 cerr
<< "SYSEX: " << hex
;
420 for (unsigned int i
= 0; i
< msgindex
; ++i
) {
421 cerr
<< (int) msgbuf
[i
] << ' ';
427 boost::optional
<int> res
= edit (msgbuf
, msgindex
);
429 if (res
.get_value_or (1) >= 0) {
430 if (!possible_mmc (msgbuf
, msgindex
) || _mmc_forward
) {
431 if (!possible_mtc (msgbuf
, msgindex
) || _mtc_forward
) {
433 sysex (*this, msgbuf
, msgindex
);
438 any (*this, msgbuf
, msgindex
);
445 * Status bytes always start a new message, except EOX
452 if (inbyte
== MIDI::eox
) {
453 /* return to the state we had pre-sysex */
455 state
= pre_variable_state
;
456 runnable
= was_runnable
;
457 msgtype
= pre_variable_msgtype
;
459 if (state
!= NEEDSTATUS
&& runnable
) {
460 msgbuf
[msgindex
++] = last_status_byte
;
463 msgbuf
[msgindex
++] = inbyte
;
464 if ((inbyte
& 0xf0) == 0xf0) {
468 channel_msg (inbyte
);
476 * We've got a Data byte.
479 msgbuf
[msgindex
++] = inbyte
;
484 * We shouldn't get here, since in NEEDSTATUS mode
485 * we're expecting a new status byte, NOT any
486 * data bytes. On the other hand, some equipment
487 * with leaky modwheels and the like might be
488 * sending data bytes as part of running controller
489 * messages, so just handle it silently.
494 /* wait for the second byte */
500 /* We've completed a 1 or 2 byte message. */
502 edit_result
= edit (msgbuf
, msgindex
);
504 if (edit_result
.get_value_or (1)) {
506 /* message not cancelled by an editor */
508 message_counter
[msgbuf
[0] & 0xF0]++;
511 signal (msgbuf
, msgindex
);
516 /* In Runnable mode, we reset the message
517 index, but keep the callbacks_pending and state the
518 same. This provides the "running status
523 /* If not Runnable, reset to NEEDSTATUS mode */
535 /** Call the real-time function for the specified byte, immediately.
536 * These can occur anywhere, so they don't change the state.
539 Parser::realtime_msg(unsigned char inbyte
)
542 message_counter
[inbyte
]++;
550 timing (*this, _timestamp
);
553 start (*this, _timestamp
);
556 contineu (*this, _timestamp
);
559 stop (*this, _timestamp
);
562 /* !!! active sense message in realtime_msg: should not reach here
570 any (*this, &inbyte
, 1);
574 /** Interpret a Channel (voice or mode) Message status byte.
577 Parser::channel_msg(unsigned char inbyte
)
579 last_status_byte
= inbyte
;
580 runnable
= true; /* Channel messages can use running status */
582 /* The high 4 bits, which determine the type of channel message. */
584 switch (inbyte
&0xF0) {
587 state
= NEEDTWOBYTES
;
591 state
= NEEDTWOBYTES
;
595 state
= NEEDTWOBYTES
;
598 msgtype
= MIDI::controller
;
599 state
= NEEDTWOBYTES
;
610 msgtype
= MIDI::pitchbend
;
611 state
= NEEDTWOBYTES
;
616 /** Initialize (and possibly emit) the signals for the
617 * specified byte. Set the state that the state-machine
618 * should go into. If the signal is not emitted
619 * immediately, it will be when the state machine gets to
620 * the end of the MIDI message.
623 Parser::system_msg (unsigned char inbyte
)
625 message_counter
[inbyte
]++;
629 pre_variable_msgtype
= msgtype
;
630 pre_variable_state
= state
;
631 was_runnable
= runnable
;
632 msgtype
= MIDI::sysex
;
633 state
= VARIABLELENGTH
;
636 msgtype
= MIDI::mtc_quarter
;
640 msgtype
= MIDI::position
;
641 state
= NEEDTWOBYTES
;
644 msgtype
= MIDI::song
;
657 // all these messages will be sent via any()
658 // when they are complete.
659 // any (*this, &inbyte, 1);
663 Parser::signal (byte
*msg
, size_t len
)
665 channel_t chan
= msg
[0]&0xF;
673 channel_active_preparse
[chan_i
] (*this);
674 note_off (*this, (EventTwoBytes
*) &msg
[1]);
675 channel_note_off
[chan_i
]
676 (*this, (EventTwoBytes
*) &msg
[1]);
677 channel_active_postparse
[chan_i
] (*this);
681 channel_active_preparse
[chan_i
] (*this);
683 /* Hack to deal with MIDI sources that use velocity=0
688 note_off (*this, (EventTwoBytes
*) &msg
[1]);
689 channel_note_off
[chan_i
]
690 (*this, (EventTwoBytes
*) &msg
[1]);
692 note_on (*this, (EventTwoBytes
*) &msg
[1]);
693 channel_note_on
[chan_i
]
694 (*this, (EventTwoBytes
*) &msg
[1]);
697 channel_active_postparse
[chan_i
] (*this);
700 case MIDI::controller
:
701 channel_active_preparse
[chan_i
] (*this);
702 controller (*this, (EventTwoBytes
*) &msg
[1]);
703 channel_controller
[chan_i
]
704 (*this, (EventTwoBytes
*) &msg
[1]);
705 channel_active_postparse
[chan_i
] (*this);
709 channel_active_preparse
[chan_i
] (*this);
710 program_change (*this, msg
[1]);
711 channel_program_change
[chan_i
] (*this, msg
[1]);
712 channel_active_postparse
[chan_i
] (*this);
716 channel_active_preparse
[chan_i
] (*this);
717 pressure (*this, msg
[1]);
718 channel_pressure
[chan_i
] (*this, msg
[1]);
719 channel_active_postparse
[chan_i
] (*this);
723 channel_active_preparse
[chan_i
] (*this);
724 poly_pressure (*this, (EventTwoBytes
*) &msg
[1]);
725 channel_poly_pressure
[chan_i
]
726 (*this, (EventTwoBytes
*) &msg
[1]);
727 channel_active_postparse
[chan_i
] (*this);
730 case MIDI::pitchbend
:
731 channel_active_preparse
[chan_i
] (*this);
732 pitchbend (*this, (msg
[2]<<7)|msg
[1]);
733 channel_pitchbend
[chan_i
] (*this, (msg
[2]<<7)|msg
[1]);
734 channel_active_postparse
[chan_i
] (*this);
738 sysex (*this, msg
, len
);
741 case MIDI::mtc_quarter
:
742 process_mtc_quarter_frame (msg
);
743 mtc_quarter_frame (*this, *msg
);
747 position (*this, msg
, len
);
751 song (*this, msg
, len
);
758 /* XXX some kind of warning ? */
762 any (*this, msg
, len
);
766 Parser::possible_mmc (byte
*msg
, size_t msglen
)
768 if (!MachineControl::is_mmc (msg
, msglen
)) {
772 /* hand over the just the interior MMC part of
773 the sysex msg without the leading 0xF0
777 mmc (*this, &msg
[1], msglen
- 1);
784 Parser::set_offline (bool yn
)
786 if (_offline
!= yn
) {
788 OfflineStatusChanged ();
790 /* this hack deals with the possibility of our first MIDI
791 bytes being running status messages.