Removed unused midicontrollable.cc
[ardour.git] / libs / midi++2 / midiparser.cc
1 /*
2     Copyright (C) 1998 Paul Barton-Davis
3
4     This file was inspired by the MIDI parser for KeyKit by 
5     Tim Thompson. 
6     
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.
11
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.
16
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.
20
21     $Id$
22 */
23
24 #include <cstdlib>
25 #include <unistd.h>
26 #include <string>
27 #include <iostream>
28
29 #include <midi++/types.h>
30 #include <midi++/parser.h>
31 #include <midi++/port.h>
32 #include <midi++/mmc.h>
33 #include <pbd/transmitter.h>
34
35 using namespace std;
36 using namespace sigc;
37 using namespace MIDI;
38
39 const char *
40 Parser::midi_event_type_name (eventType t)
41
42 {
43         switch (t) {
44         case none:
45                 return "no midi messages";
46
47         case raw:
48                 return "raw midi data";
49
50         case MIDI::any:
51                 return "any midi message";
52           
53         case off:
54                 return "note off";
55           
56         case on:
57                 return "note on";
58           
59         case polypress:
60                 return "aftertouch";
61           
62         case MIDI::controller:
63                 return "controller";
64           
65         case program:
66                 return "program change";
67           
68         case chanpress:
69                 return "channel pressure";
70           
71         case MIDI::pitchbend:
72                 return "pitch bend";
73           
74         case MIDI::sysex:
75                 return "system exclusive";
76           
77         case MIDI::song:
78                 return "song position";
79           
80         case MIDI::tune:
81                 return "tune";
82           
83         case MIDI::eox:
84                 return "end of sysex";
85           
86         case MIDI::timing:
87                 return "timing";
88           
89         case MIDI::start:
90                 return "start";
91           
92         case MIDI::stop:
93                 return "continue";
94           
95         case MIDI::contineu:
96                 return "stop";
97           
98         case active:
99                 return "active sense";
100           
101         default:
102                 return "unknow MIDI event type";
103         }
104 }
105
106 Parser::Parser (Port &p) 
107         : _port (p)
108
109 {
110         trace_stream = 0;
111         trace_prefix = "";
112         memset (message_counter, 0, sizeof (message_counter[0]) * 256);
113         msgindex = 0;
114         msgtype = none;
115         msglen = 256;
116         msgbuf = (unsigned char *) malloc (msglen);
117         msgbuf[msgindex++] = 0x90;
118         _mmc_forward = false;
119         reset_mtc_state ();
120         _offline = false;
121
122         /* this hack deals with the possibility of our first MIDI
123            bytes being running status messages.
124         */
125
126         channel_msg (0x90);
127         state = NEEDSTATUS;
128
129         pre_variable_state = NEEDSTATUS;
130         pre_variable_msgtype = none;
131 }
132
133 Parser::~Parser ()
134
135 {
136         delete msgbuf;
137 }
138
139 void
140 Parser::trace_event (Parser &p, byte *msg, size_t len)
141
142 {
143         eventType type;
144         ostream *o;
145
146         if ((o = trace_stream) == NULL) { /* can be asynchronously removed */
147                 return;
148         }
149         
150         type = (eventType) (msg[0]&0xF0);
151
152         switch (type) {
153         case off:
154                 *o << trace_prefix
155                    << "Channel "
156                    << (msg[0]&0xF)+1
157                    << " NoteOff NoteNum "
158                    << (int) msg[1]
159                    << " Vel "
160                    << (int) msg[2]
161                    << endmsg;
162                 break;
163                 
164         case on:
165                 *o << trace_prefix
166                    << "Channel "
167                    << (msg[0]&0xF)+1
168                    << " NoteOn NoteNum "
169                    << (int) msg[1]
170                    << " Vel "
171                    << (int) msg[2]
172                    << endmsg;
173                 break;
174             
175         case polypress:
176                 *o << trace_prefix
177                    << "Channel "
178                    << (msg[0]&0xF)+1
179                    << " PolyPressure"
180                    << (int) msg[1]
181                    << endmsg;
182                 break;
183             
184         case MIDI::controller:
185                 *o << trace_prefix
186                    << "Channel "
187                    << (msg[0]&0xF)+1
188                    << " Controller "
189                    << (int) msg[1]
190                    << " Value "
191                    << (int) msg[2]
192                    << endmsg;
193                 break;
194                 
195         case program:
196                 *o << trace_prefix 
197                    << "Channel "
198                    << (msg[0]&0xF)+1
199                    <<  " Program Change ProgNum "
200                    << (int) msg[1]
201                    << endmsg;
202                 break;
203                 
204         case chanpress:
205                 *o << trace_prefix 
206                    << "Channel "
207                    << (msg[0]&0xF)+1
208                    << " Channel Pressure "
209                    << (int) msg[1]
210                    << endmsg;
211                 break;
212             
213         case MIDI::pitchbend:
214                 *o << trace_prefix
215                    << "Channel "
216                    << (msg[0]&0xF)+1
217                    << " Pitch Bend "
218                    << ((msg[1]<<7)|msg[2])
219                    << endmsg;
220                 break;
221             
222         case MIDI::sysex:
223                 if (len == 1) {
224                         switch (msg[0]) {
225                         case 0xf8:
226                                 *o << trace_prefix
227                                    << "Clock"
228                                    << endmsg;
229                                 break;
230                         case 0xfa:
231                                 *o << trace_prefix
232                                    << "Start"
233                                    << endmsg;
234                                 break;
235                         case 0xfb:
236                                 *o << trace_prefix
237                                    << "Continue"
238                                    << endmsg;
239                                 break;
240                         case 0xfc:
241                                 *o << trace_prefix
242                                    << "Stop"
243                                    << endmsg;
244                                 break;
245                         case 0xfe:
246                                 *o << trace_prefix
247                                    << "Active Sense"
248                                    << endmsg;
249                                 break;
250                         case 0xff:
251                                 *o << trace_prefix
252                                    << "System Reset"
253                                    << endmsg;
254                                 break;
255                         default:
256                                 *o << trace_prefix
257                                    << "System Exclusive (1 byte : " << hex << (int) *msg << dec << ')'
258                                    << endmsg;           
259                                 break;
260                         } 
261                 } else {
262                         *o << trace_prefix
263                            << "System Exclusive (" << len << ") = [ " << hex;
264                         for (unsigned int i = 0; i < len; ++i) {
265                                 *o << (int) msgbuf[i] << ' ';
266                         }
267                         *o << dec << ']' << endmsg;
268                         
269                 }
270                 break;
271             
272         case MIDI::song:
273                 *o << trace_prefix << "Song" << endmsg;
274                 break;
275             
276         case MIDI::tune:
277                 *o << trace_prefix << "Tune" << endmsg;
278                 break;
279             
280         case MIDI::eox:
281                 *o << trace_prefix << "End-of-System Exclusive" << endmsg;
282                 break;
283             
284         case MIDI::timing:
285                 *o << trace_prefix << "Timing" << endmsg;
286                 break;
287             
288         case MIDI::start:
289                 *o << trace_prefix << "Start" << endmsg;
290                 break;
291             
292         case MIDI::stop:
293                 *o << trace_prefix << "Stop" << endmsg;
294                 break;
295             
296         case MIDI::contineu:
297                 *o << trace_prefix << "Continue" << endmsg;
298                 break;
299             
300         case active:
301                 *o << trace_prefix << "Active Sense" << endmsg;
302                 break;
303             
304         default:
305                 *o << trace_prefix << "Unrecognized MIDI message" << endmsg;
306                 break;
307         }
308 }
309
310 void
311 Parser::trace (bool onoff, ostream *o, const string &prefix)
312
313 {
314         trace_connection.disconnect ();
315
316         if (onoff) {
317                 trace_stream = o;
318                 trace_prefix = prefix;
319                 trace_connection = any.connect 
320                         (mem_fun (*this, &Parser::trace_event));
321         } else {
322                 trace_prefix = "";
323                 trace_stream = 0;
324         }
325 }
326
327 void
328 Parser::scanner (unsigned char inbyte)
329 {
330         bool statusbit;
331
332         // cerr << "parse: " << hex << (int) inbyte << dec << " state = " << state << " msgindex = " << msgindex << " runnable = " << runnable << endl;
333
334         /* Check active sensing early, so it doesn't interrupt sysex. 
335            
336            NOTE: active sense messages are not considered to fit under
337            "any" for the purposes of callbacks. If a caller wants
338            active sense messages handled, which is unlikely, then
339            they can just ask for it specifically. They are so unlike
340            every other MIDI message in terms of semantics that its
341            counter-productive to treat them similarly.
342         */
343         
344         if (inbyte == 0xfe) {
345                 message_counter[inbyte]++;
346                 if (!_offline) {
347                         active_sense (*this);
348                 }
349                 return;
350         }
351         
352         /* If necessary, allocate larger message buffer. */
353         
354         if (msgindex >= msglen) {
355                 msglen *= 2;
356                 msgbuf = (unsigned char *) realloc (msgbuf, msglen);
357         }
358         
359         /*
360           Real time messages can occur ANYPLACE,
361           but do not interrupt running status. 
362         */
363
364         bool rtmsg = false;
365         
366         switch (inbyte) {
367         case 0xf8:
368                 rtmsg = true;
369                 break;
370         case 0xfa:
371                 rtmsg = true;
372                 break;
373         case 0xfb:
374                 rtmsg = true;
375                 break;
376         case 0xfc:
377                 rtmsg = true;
378                 break;
379         case 0xfd:
380                 rtmsg = true;
381                 break;
382         case 0xfe:
383                 rtmsg = true;
384                 break;
385         case 0xff:
386                 rtmsg = true;
387                 break;
388         }
389
390         if (rtmsg) {
391                 if (edit (&inbyte, 1) >= 0 && !_offline) {
392                         realtime_msg (inbyte);
393                 }
394                 
395                 return;
396         } 
397
398         statusbit = (inbyte & 0x80);
399
400         /*
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.
405          */
406         
407         if (state == VARIABLELENGTH && statusbit)  {
408                 /* The message has ended, so process it */
409
410                 /* add EOX to any sysex message */
411
412                 if (inbyte == MIDI::eox) {
413                         msgbuf[msgindex++] = inbyte;
414                 }
415
416 #if 0
417                 cerr << "SYSEX: " << hex;
418                 for (unsigned int i = 0; i < msgindex; ++i) {
419                         cerr << (int) msgbuf[i] << ' ';
420                 }
421                 cerr << dec << endl;
422 #endif
423                 if (msgindex > 0 && edit (msgbuf, msgindex) >= 0) {
424                         if (!possible_mmc (msgbuf, msgindex) || _mmc_forward) {
425                                 if (!possible_mtc (msgbuf, msgindex) || _mtc_forward) {
426                                         if (!_offline) {
427                                                 sysex (*this, msgbuf, msgindex);
428                                         }
429                                 }
430                         }
431                         if (!_offline) {
432                                 any (*this, msgbuf, msgindex);
433                         }
434                 }
435         }
436         
437         /*
438          * Status bytes always start a new message, except EOX
439          */
440         
441         if (statusbit) {
442
443                 msgindex = 0;
444
445                 if (inbyte == MIDI::eox) {
446                         /* return to the state we had pre-sysex */
447
448                         state = pre_variable_state;
449                         runnable = was_runnable;
450                         msgtype = pre_variable_msgtype;
451
452                         if (state != NEEDSTATUS && runnable) {
453                                 msgbuf[msgindex++] = last_status_byte;
454                         }
455                 } else {
456                         msgbuf[msgindex++] = inbyte;
457                         if ((inbyte & 0xf0) == 0xf0) {
458                                 system_msg (inbyte);
459                                 runnable = false;
460                         } else {
461                                 channel_msg (inbyte);
462                         }
463                 }
464
465                 return;
466         }
467         
468         /*
469          * We've got a Data byte.
470          */
471         
472         msgbuf[msgindex++] = inbyte;
473
474         switch (state) {
475         case NEEDSTATUS:
476                 /*
477                  * We shouldn't get here, since in NEEDSTATUS mode
478                  * we're expecting a new status byte, NOT any
479                  * data bytes. On the other hand, some equipment
480                  * with leaky modwheels and the like might be
481                  * sending data bytes as part of running controller
482                  * messages, so just handle it silently.
483                  */
484                 break;
485                 
486         case NEEDTWOBYTES:
487                 /* wait for the second byte */
488                 if (msgindex < 3)
489                         return;
490                 /*FALLTHRU*/
491                 
492         case NEEDONEBYTE:
493                 /* We've completed a 1 or 2 byte message. */
494                 
495                 if (edit (msgbuf, msgindex) == 0) {
496                         
497                         /* message not cancelled by an editor */
498                         
499                         message_counter[msgbuf[0] & 0xF0]++;
500
501                         if (!_offline) {
502                                 signal (msgbuf, msgindex);
503                         }
504                 }
505                 
506                 if (runnable) {
507                         /* In Runnable mode, we reset the message 
508                            index, but keep the callbacks_pending and state the 
509                            same.  This provides the "running status 
510                            byte" feature.
511                         */
512                         msgindex = 1;
513                 } else {
514                         /* If not Runnable, reset to NEEDSTATUS mode */
515                         state = NEEDSTATUS;
516                 }
517                 break;
518                 
519         case VARIABLELENGTH:
520                 /* nothing to do */
521                 break;
522         }
523         return;
524 }
525
526 /*
527  * realtime_msg(inbyte)
528  *
529  * Call the real-time function for the specified byte, immediately.
530  * These can occur anywhere, so they don't change the state.
531  */
532
533 void
534 Parser::realtime_msg(unsigned char inbyte)
535
536 {
537         message_counter[inbyte]++;
538
539         if (_offline) {
540                 return;
541         }
542
543         switch (inbyte) {
544         case 0xf8:
545                 timing (*this);
546                 break;
547         case 0xfa:
548                 start (*this);
549                 break;
550         case 0xfb:
551                 contineu (*this);
552                 break;
553         case 0xfc:
554                 stop (*this);
555                 break;
556         case 0xfe:
557                 /* !!! active sense message in realtime_msg: should not reach here
558                  */  
559                 break;
560         case 0xff:
561                 reset (*this);
562                 break;
563         }
564
565         any (*this, &inbyte, 1);
566 }
567
568 /*
569  * channel_msg(inbyte)
570  *
571  * Interpret a Channel (voice or mode) Message status byte.
572  */
573
574 void
575 Parser::channel_msg(unsigned char inbyte)
576 {
577         last_status_byte = inbyte;
578         runnable = true;                /* Channel messages can use running status */
579     
580         /* The high 4 bits, which determine the type of channel message. */
581     
582         switch (inbyte&0xF0) {
583         case 0x80:
584                 msgtype = off;
585                 state = NEEDTWOBYTES;
586                 break;
587         case 0x90:
588                 msgtype = on;
589                 state = NEEDTWOBYTES;
590                 break;
591         case 0xa0:
592                 msgtype = polypress;
593                 state = NEEDTWOBYTES;
594                 break;
595         case 0xb0:
596                 msgtype = MIDI::controller;
597                 state = NEEDTWOBYTES;
598                 break;
599         case 0xc0:
600                 msgtype = program;
601                 state = NEEDONEBYTE;
602                 break;
603         case 0xd0:
604                 msgtype = chanpress;
605                 state = NEEDONEBYTE;
606                 break;
607         case 0xe0:
608                 msgtype = MIDI::pitchbend;
609                 state = NEEDTWOBYTES;
610                 break;
611         }
612 }
613
614 /*
615  * system_msg(inbyte)
616  *
617  * Initialize (and possibly emit) the signals for the
618  * specified byte.  Set the state that the state-machine
619  * should go into.  If the signal is not emitted
620  * immediately, it will be when the state machine gets to
621  * the end of the MIDI message.
622  */
623
624 void
625 Parser::system_msg (unsigned char inbyte)
626 {
627         message_counter[inbyte]++;
628
629         switch (inbyte) {
630         case 0xf0:
631                 pre_variable_msgtype = msgtype;
632                 pre_variable_state = state;
633                 was_runnable = runnable;
634                 msgtype = MIDI::sysex;
635                 state = VARIABLELENGTH;
636                 break;
637         case 0xf1:
638                 msgtype = MIDI::mtc_quarter;
639                 state = NEEDONEBYTE;
640                 break;
641         case 0xf2:
642                 msgtype = MIDI::position;
643                 state = NEEDTWOBYTES;
644                 break;
645         case 0xf3:
646                 msgtype = MIDI::song;
647                 state = NEEDONEBYTE;
648                 break;
649         case 0xf6:
650                 if (!_offline) {
651                         tune (*this);
652                 }
653                 state = NEEDSTATUS;
654                 break;
655         case 0xf7:
656                 break;
657         }
658
659         // all these messages will be sent via any() 
660         // when they are complete.
661         // any (*this, &inbyte, 1);
662 }
663
664 void 
665 Parser::signal (byte *msg, size_t len)
666 {
667         channel_t chan = msg[0]&0xF;
668         int chan_i = chan;
669
670         switch (msgtype) {
671         case none:
672                 break;
673                 
674         case off:
675                 channel_active_preparse[chan_i] (*this);
676                 note_off (*this, (EventTwoBytes *) &msg[1]);
677                 channel_note_off[chan_i] 
678                         (*this, (EventTwoBytes *) &msg[1]);
679                 channel_active_postparse[chan_i] (*this);
680                 break;
681                 
682         case on:
683                 channel_active_preparse[chan_i] (*this);
684
685                 /* Hack to deal with MIDI sources that use velocity=0
686                    instead of noteOff.
687                 */
688
689                 if (msg[2] == 0) {
690                         note_off (*this, (EventTwoBytes *) &msg[1]);
691                         channel_note_off[chan_i] 
692                                 (*this, (EventTwoBytes *) &msg[1]);
693                 } else {
694                         note_on (*this, (EventTwoBytes *) &msg[1]);
695                         channel_note_on[chan_i] 
696                                 (*this, (EventTwoBytes *) &msg[1]);
697                 }
698
699                 channel_active_postparse[chan_i] (*this);
700                 break;
701                 
702         case MIDI::controller:
703                 channel_active_preparse[chan_i] (*this);
704                 controller (*this, (EventTwoBytes *) &msg[1]);
705                 channel_controller[chan_i] 
706                         (*this, (EventTwoBytes *) &msg[1]);
707                 channel_active_postparse[chan_i] (*this);
708                 break;
709                 
710         case program:
711                 channel_active_preparse[chan_i] (*this);
712                 program_change (*this, msg[1]);
713                 channel_program_change[chan_i] (*this, msg[1]);
714                 channel_active_postparse[chan_i] (*this);
715                 break;
716                 
717         case chanpress:
718                 channel_active_preparse[chan_i] (*this);
719                 pressure (*this, msg[1]);
720                 channel_pressure[chan_i] (*this, msg[1]);
721                 channel_active_postparse[chan_i] (*this);
722                 break;
723                 
724         case polypress:
725                 channel_active_preparse[chan_i] (*this);
726                 poly_pressure (*this, (EventTwoBytes *) &msg[1]);
727                 channel_poly_pressure[chan_i] 
728                         (*this, (EventTwoBytes *) &msg[1]);
729                 channel_active_postparse[chan_i] (*this);
730                 break;
731                 
732         case MIDI::pitchbend:
733                 channel_active_preparse[chan_i] (*this);
734                 pitchbend (*this, (msg[1]<<7)|msg[2]);
735                 channel_pitchbend[chan_i] (*this, (msg[1]<<7)|msg[2]);
736                 channel_active_postparse[chan_i] (*this);
737                 break;
738                 
739         case MIDI::sysex:
740                 sysex (*this, msg, len);
741                 break;
742
743         case MIDI::mtc_quarter:
744                 process_mtc_quarter_frame (msg);
745                 mtc_quarter_frame (*this, *msg);
746                 break;
747                 
748         case MIDI::position:
749                 position (*this, msg, len);
750                 break;
751                 
752         case MIDI::song:
753                 song (*this, msg, len);
754                 break;
755         
756         case MIDI::tune:
757                 tune (*this);
758         
759         default:
760                 /* XXX some kind of warning ? */
761                 break;
762         }
763         
764         any (*this, msg, len);
765 }
766
767 bool
768 Parser::possible_mmc (byte *msg, size_t msglen)
769 {
770         if (!MachineControl::is_mmc (msg, msglen)) {
771                 return false;
772         }
773
774         /* hand over the just the interior MMC part of
775            the sysex msg without the leading 0xF0
776         */
777
778         if (!_offline) {
779                 mmc (*this, &msg[1], msglen - 1);
780         }
781
782         return true;
783 }
784
785 void
786 Parser::set_offline (bool yn)
787 {
788         if (_offline != yn) {
789                 _offline = yn;
790                 OfflineStatusChanged ();
791
792                 /* this hack deals with the possibility of our first MIDI
793                    bytes being running status messages.
794                 */
795                 
796                 channel_msg (0x90);
797                 state = NEEDSTATUS;
798         }
799 }
800