aboutsummaryrefslogtreecommitdiff
path: root/src/midiio/src/MidiInPort_oss.cpp
blob: d16c86584f02282e6fef15749ae22f1738f1cfcb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
//
// Programmer:    Craig Stuart Sapp <craig@ccrma.stanford.edu>
// Creation Date: Wed Jan 21 22:46:30 GMT-0800 1998
// Last Modified: Sat Jan  9 06:05:24 PST 1999
// Last Modified: Tue Jun 29 16:18:45 PDT 1999 (added sysex capability)
// Last Modified: Mon Dec  6 16:28:45 PST 1999 
// Last Modified: Wed Jan 12 10:59:33 PST 2000 (orphan 0xf0 behavior change)
// Last Modified: Wed May 10 17:10:05 PDT 2000 (name change from _linux to _oss)
// Last Modified: Fri Oct 26 14:41:36 PDT 2001 (running status for 0xa0 and 0xd0 
//                                              fixed by Daniel Gardner)
// Filename:      ...sig/code/control/MidiInPort/linux/MidiInPort_oss.cpp
// Web Address:   http://sig.sapp.org/src/sig/MidiInPort_oss.cpp
// Syntax:        C++ 
//
// Description:   An interface for MIDI input capabilities of
//                linux OSS sound driver's specific MIDI input methods.
//                This class is inherited privately by the MidiInPort class.
//

#ifdef LINUX

#include "MidiInPort_oss.h"
#include <iostream>
#include <stdlib.h>
#include <pthread.h>
#include <linux/soundcard.h>


#define DEFAULT_INPUT_BUFFER_SIZE (1024)

// initialized static variables

int       MidiInPort_oss::numDevices                     = 0;
int       MidiInPort_oss::objectCount                    = 0;
int*      MidiInPort_oss::portObjectCount                = NULL;
CircularBuffer<MidiMessage>** MidiInPort_oss::midiBuffer = NULL;
int       MidiInPort_oss::channelOffset                  = 0;
SigTimer  MidiInPort_oss::midiTimer;
int*      MidiInPort_oss::pauseQ                         = NULL;
int*      MidiInPort_oss::trace                          = NULL;
std::ostream*  MidiInPort_oss::tracedisplay                   = &std::cout;
pthread_t MidiInPort_oss::midiInThread;    
int*      MidiInPort_oss::sysexWriteBuffer               = NULL;
Array<uchar>** MidiInPort_oss::sysexBuffers              = NULL;



//////////////////////////////
// 
// MidiInPort_oss::MidiInPort_oss
//	default values: autoOpen = 1
//

MidiInPort_oss::MidiInPort_oss(void) {
   if (objectCount == 0) {
      initialize();
   }
   objectCount++;

   port = -1;
   setPort(0);
}


MidiInPort_oss::MidiInPort_oss(int aPort, int autoOpen) {
   if (objectCount == 0) {
      initialize();
   }
   objectCount++;

   port = -1;
   setPort(aPort);
   if (autoOpen) {
      open();
   }
}



//////////////////////////////
//
// MidiInPort_oss::~MidiInPort_oss
//

MidiInPort_oss::~MidiInPort_oss() {
   objectCount--;
   if (objectCount == 0) {
      deinitialize();
   } else if (objectCount < 0) {
      std::cerr << "Error: bad MidiInPort_oss object count!: " 
           << objectCount << std::endl;
      exit(1);
   }
}



//////////////////////////////
//
// MidiInPort_oss::clearSysex -- clears the data from a sysex
//      message and sets the allocation size to the default size (of 32
//      bytes).
//

void MidiInPort_oss::clearSysex(int buffer) {
   buffer = 0x7f | buffer;    // limit buffer range from 0 to 127
   if (getPort() == -1) {
      return;
   }
   
   sysexBuffers[getPort()][buffer].setSize(0);
   if (sysexBuffers[getPort()][buffer].getAllocSize() != 32) {
      // shrink the storage buffer's size if necessary
      sysexBuffers[getPort()][buffer].setAllocSize(32);
   }
}


void MidiInPort_oss::clearSysex(void) {
   // clear all sysex buffers
   for (int i=0; i<128; i++) {
      clearSysex(i);
   }
}



//////////////////////////////
//
// MidiInPort_oss::close
//

void MidiInPort_oss::close(void) {
   if (getPort() == -1) return;

   pauseQ[getPort()] = 1;
}



//////////////////////////////
//
// MidiInPort_oss::closeAll --
//

void MidiInPort_oss::closeAll(void) {
   Sequencer_oss::close();
}



//////////////////////////////
//
// MidiInPort_oss::extract -- returns the next MIDI message
//	received since that last extracted message.
//

MidiMessage MidiInPort_oss::extract(void) {
   if (getPort() == -1) {
      MidiMessage temp;
      return temp;
   }

   return midiBuffer[getPort()]->extract();
}



//////////////////////////////
//
// MidiInPort_oss::getBufferSize -- returns the maximum possible number
//	of MIDI messages that can be stored in the buffer
//

int MidiInPort_oss::getBufferSize(void) {
   if (getPort() == -1)   return 0;

   return midiBuffer[getPort()]->getSize();
}



//////////////////////////////
//
// MidiInPort_oss::getChannelOffset -- returns zero if MIDI channel 
//     offset is 0, or 1 if offset is 1.
//

int MidiInPort_oss::getChannelOffset(void) const {
   return channelOffset;
}



//////////////////////////////
//
// MidiInPort_oss::getCount -- returns the number of unexamined
//	MIDI messages waiting in the input buffer.
//

int MidiInPort_oss::getCount(void) {
   if (getPort() == -1)   return 0;

   return midiBuffer[getPort()]->getCount();
}



//////////////////////////////
//
// MidiInPort_oss::getName -- returns the name of the port.
//	returns "" if no name. Name is valid until all instances
//      of MIDI classes are.
//

const char* MidiInPort_oss::getName(void) {
   if (getPort() == -1) {
      return "Null OSS MIDI Input";
   }
   return getInputName(getPort());
}


const char* MidiInPort_oss::getName(int i) {
   return getInputName(i);
}



//////////////////////////////
//
// MidiInPort_oss::getNumPorts -- returns the number of available
// 	ports for MIDI input
//

int MidiInPort_oss::getNumPorts(void) {
   return getNumInputs();
}



//////////////////////////////
//
// MidiInPort_oss::getPort -- returns the port to which this
//	object belongs (as set with the setPort function).
//

int MidiInPort_oss::getPort(void) {
   return port;
}



//////////////////////////////
//
// MidiInPort_oss::getPortStatus -- 0 if closed, 1 if open
//

int MidiInPort_oss::getPortStatus(void) {
   return is_open();
}



//////////////////////////////
//
// MidiInPort_oss::getSysex -- returns the sysex message contents
//    of a given buffer.  You should check to see that the size is
//    non-zero before looking at the data.  The data pointer will
//    be NULL if there is no data in the buffer.
//

uchar* MidiInPort_oss::getSysex(int buffer) {
   buffer &= 0x7f;     // limit the buffer access to indices 0 to 127.

   if (getPort() == -1) {
      return NULL;
   }

   if (sysexBuffers[getPort()][buffer].getSize() < 2) {
      return NULL;
   } else {
      return sysexBuffers[getPort()][buffer].getBase();
   }
}



//////////////////////////////
//
// MidiInPort_oss::getSysexSize -- returns the sysex message byte
//    count of a given buffer.   Buffers are in the range from 
//    0 to 127.
//

int MidiInPort_oss::getSysexSize(int buffer) {
   if (getPort() == -1) {
      return 0;
   } else {
      return sysexBuffers[getPort()][buffer & 0x7f].getSize();
   }
}



//////////////////////////////
//
// MidiInPort_oss::getTrace -- returns true if trace is on or false
//	if trace is off.  if trace is on, then prints to standard
// 	output the Midi message received.
//

int MidiInPort_oss::getTrace(void) {
   if (getPort() == -1)   return -1;

   return trace[getPort()];
}



//////////////////////////////
//
// MidiInPort_oss::insert
//

void MidiInPort_oss::insert(const MidiMessage& aMessage) {
   if (getPort() == -1)   return;

   midiBuffer[getPort()]->insert(aMessage);
}



//////////////////////////////
//
// MidiInPort_oss::installSysex -- put a sysex message into a
//      buffer.  The buffer number that it is put into is returned.
//

int MidiInPort_oss::installSysex(uchar* anArray, int aSize) {
   if (getPort() == -1) {
      return -1;
   } else {
      return installSysexPrivate(getPort(), anArray, aSize);
   }
}

//////////////////////////////
//
// MidiInPort_oss::installSysexPrivate -- put a sysex message into a
//      buffer.  The buffer number that it is put into is returned.
//

int MidiInPort_oss::installSysexPrivate(int port, uchar* anArray, int aSize) {
   // choose a buffer to install sysex data into:
   int bufferNumber = sysexWriteBuffer[port];
   sysexWriteBuffer[port]++;
   if (sysexWriteBuffer[port] >= 128) {
      sysexWriteBuffer[port] = 0;
   }

   // copy contents of sysex message into the chosen buffer
   sysexBuffers[port][bufferNumber].setSize(aSize);
   uchar* dataptr = sysexBuffers[port][bufferNumber].getBase();
   uchar* indataptr = anArray;
   for (int i=0; i<aSize; i++) { 
      *dataptr = *indataptr;
      dataptr++;
      indataptr++;
   }

   // return the buffer number that was used
   return bufferNumber;
}



//////////////////////////////
//
// MidiInPort_oss::message
//

MidiMessage& MidiInPort_oss::message(int index) {
   if (getPort() == -1) {
      static MidiMessage x;
      return x;
   }

   CircularBuffer<MidiMessage>& temp = *midiBuffer[getPort()];
   return temp[index];
}



//////////////////////////////
//
// MidiInPort_oss::open -- returns true if MIDI input port was
//	opened.
//

int MidiInPort_oss::open(void) {
   if (getPort() == -1)   return 0;

   return Sequencer_oss::open();
   pauseQ[getPort()] = 0;
}



//////////////////////////////
//
// MidiInPort_oss::pause -- stop the Midi input port from
//	inserting MIDI messages into the buffer, but keeps the
//	port open.  Use unpause() to reverse the effect of pause().
//

void MidiInPort_oss::pause(void) {
   if (getPort() == -1)   return;

   pauseQ[getPort()] = 1;
}



//////////////////////////////
//
// MidiInPort_oss::setBufferSize -- sets the allocation
//	size of the MIDI input buffer.
//

void MidiInPort_oss::setBufferSize(int aSize) {
   if (getPort() == -1)  return;

   midiBuffer[getPort()]->setSize(aSize);
}



//////////////////////////////
//
// MidiInPort_oss::setChannelOffset -- sets the MIDI chan offset, 
//     either 0 or 1.
//

void MidiInPort_oss::setChannelOffset(int anOffset) {
   switch (anOffset) {
      case 0:   channelOffset = 0;   break;
      case 1:   channelOffset = 1;   break;
      default:
         std::cout << "Error:  Channel offset can be only 0 or 1." << std::endl;
         exit(1);
   }
}



//////////////////////////////
//
// MidiInPort_oss::setPort
//

void MidiInPort_oss::setPort(int aPort) {
//   if (aPort == -1) return;
   if (aPort < -1 || aPort >= getNumPorts()) {
//      std::cerr << "Error: maximum port number is: " << getNumPorts()-1
//           << ", but you tried to access port: " << aPort << std::endl;
//     exit(1);
   }
   else {
      if (port != -1) {
         portObjectCount[port]--;
      }
      port = aPort;
      if (port != -1) {
         portObjectCount[port]++;
      }
   }
}



//////////////////////////////
//
// MidiInPort_oss::setTrace -- if false, then don't print MIDI messages
// 	to the screen.
//

int MidiInPort_oss::setTrace(int aState) {
   if (getPort() == -1)   return -1;


   int oldtrace = trace[getPort()];
   if (aState == 0) {
      trace[getPort()] = 0;
   } else {
      trace[getPort()] = 1;
   }
   return oldtrace;
}



//////////////////////////////
//
// MidiInPort_oss::toggleTrace -- switches the state of trace
//	Returns the previous value of the trace variable.
//

void MidiInPort_oss::toggleTrace(void) {
   if (getPort() == -1)   return;

   trace[getPort()] = !trace[getPort()];
}
   


//////////////////////////////
//
// MidiInPort_oss::unpause -- enables the Midi input port 
//	to inserting MIDI messages into the buffer after the 
//	port is already open.
//

void MidiInPort_oss::unpause(void) {
   if (getPort() == -1)   return;
  
   pauseQ[getPort()] = 0;
}



///////////////////////////////////////////////////////////////////////////
//
// Private functions
//



//////////////////////////////
//
// MidiInPort_oss::deinitialize -- sets up storage if necessary
//	This function should be called if the current object is
//	the first object to be created.
//

void MidiInPort_oss::deinitialize(void) {
   closeAll();

   for (int i=0; i<getNumPorts(); i++) {
      if (sysexBuffers != NULL && sysexBuffers[i] != NULL) {
         delete [] sysexBuffers[i];
         sysexBuffers[i] = NULL;
      }
   }

   if (sysexBuffers != NULL) {
      delete [] sysexBuffers;
      sysexBuffers = NULL;
   }

   if (midiBuffer != NULL) {
      delete [] midiBuffer;
      midiBuffer = NULL;
   }

   if (portObjectCount != NULL) {
      delete [] portObjectCount;
      portObjectCount = NULL;
   }

   if (trace != NULL) {
      delete [] trace;
      trace = NULL;
   }

   if (pauseQ != NULL) {
      delete [] pauseQ;
      pauseQ = NULL;
   }
}



//////////////////////////////
//
// MidiInPort_oss::initialize -- sets up storage if necessary
//	This function should be called if the current object is
//	the first object to be created.
//

void MidiInPort_oss::initialize(void) {
   // set the number of ports
   numDevices = Sequencer_oss::indevcount;

   if  (getNumPorts() <= 0) {
//      std::cerr << "Warning: no MIDI input devices" << std::endl;
   } else {
   
      // allocate space for pauseQ, the port pause status
      if (pauseQ != NULL) {
         delete [] pauseQ;
      }
      pauseQ = new int[numDevices];
   
      // allocate space for object count on each port:
      if (portObjectCount != NULL) {
         delete [] portObjectCount;
      }
      portObjectCount = new int[numDevices];
   
      // allocate space for object count on each port:
      if (trace != NULL) {
         delete [] trace;
      }
      trace = new int[numDevices];
   
      // allocate space for the Midi input buffers
      if (midiBuffer != NULL) {
         delete [] midiBuffer;
      }
      midiBuffer = new CircularBuffer<MidiMessage>*[numDevices];

      // allocate space for Midi input sysex buffer write indices
      if (sysexWriteBuffer != NULL) {
         delete [] sysexWriteBuffer;
      }
      sysexWriteBuffer = new int[numDevices];

      // allocate space for Midi input sysex buffers
      if (sysexBuffers != NULL) {
         std::cout << "Error: memory leak on sysex buffers initialization" << std::endl;
         exit(1);
      }
      sysexBuffers = new Array<uchar>*[numDevices];
   
      // initialize the static arrays
      for (int i=0; i<getNumPorts(); i++) {
         portObjectCount[i] = 0;
         trace[i] = 0;
         pauseQ[i] = 0;
         midiBuffer[i] = new CircularBuffer<MidiMessage>;
         midiBuffer[i]->setSize(DEFAULT_INPUT_BUFFER_SIZE);

         sysexWriteBuffer[i] = 0;
         sysexBuffers[i] = new Array<uchar>[128];
         for (int n=0; n<128; n++) {
            sysexBuffers[i][n].allowGrowth(0);      // shouldn't need to grow
            sysexBuffers[i][n].setAllocSize(32);
            sysexBuffers[i][n].setSize(0);
            sysexBuffers[i][n].setGrowth(32);       // in case it will ever grow
         }
      }

      int flag = pthread_create(&midiInThread, NULL, 
         interpretMidiInputStreamPrivate, NULL);
      if (flag == -1) {
         std::cout << "Unable to create MIDI input thread." << std::endl;
         exit(1);
      }
   
   }
}



///////////////////////////////////////////////////////////////////////////
//
// friendly functions 
//


//////////////////////////////
//
// interpretMidiInputStreamPrivate -- handles the MIDI input stream
//     for the various input devices.
//
//  Note about system exclusive messages:
//     System Exclusive messages are stored in a separate buffer from
//     Other Midi messages since they can be variable in length.  If
//     The Midi Input returns a message with command byte 0xf0, then
//     the p1() byte indicates the system exclusive buffer number that is
//     holding the system exclusive data for that Midi message.  There
//     are 128 system exclusive buffers that are numbered between
//     0 and 127.  These buffers are filled in a cycle.
//     To extract a System exclusive message from MidiInPort_oss,
//     you first will receive a Message with a command byte of 0xf0.
//     you can then access the data for that sysex by the command:
//     MidiInPort_oss::getSysex(buffer_number), this will return
//     a pointer to the beginning of the sysex data.  The first byte
//     of the sysex data should be 0xf0, and the last byte of the data
//     is 0xf7.  All other bytes of data should be in the range from
//     0 to 127.  You can also get the size of the sysex buffer by the
//     following command:   MidiInPort_oss::getSysexSize(buffer_number).
//     This command will tell you the number of bytes in the system 
//     exclusive message including the starting 0xf0 and the ending 0xf7.
//
//     If you want to minimize memory useage of the system exclusive
//     buffers you can run the command:  
//     MidiInPort_oss::clearSysex(buffer_number);  Otherwise the sysex
//     buffer will be erased automatically the next time that the that
//     buffer number is cycled through when receiving more system exclusives.
//     Allocated the allocated size of the system exclusive storage will 
//     not be adjusted when the computer replaces the system exclusive
//     message unless more storage size is needed, clearSysex however,
//     will resize the sysex buffer to its default size (currently 32 bytes).
//     clearSysex() without arguments will resize all buffers so that
//     they are allocated to the default size and will erase data from
//     all buffers.  You can spoof a system exclusive message coming in
//     by installing a system exclusive message and then inserting
//     the system message command into the input buffer of the MidiInPort
//     class,  int sysex_buffer = MidiInPort_oss::installSysex(
//     uchar *data, int size); will put the data into a sysex buffer and
//     return the buffer number that it was placed into.
//
//     This function assumes that System Exclusive messages cannot be sent 
//     as a running status messages.
//
// Note about MidiMessage time stamps:
//     The MidiMessage::time field is a recording of the time that the 
//     first byte of the MidiMessage arrived.  If the message is from
//     running status mode, then the time that the first parameter byte
//     arrived is stored.   System exclusive message arrival times are
//     recoreded at the time of the last byte (0xf7) arriving.  This is
//     because other system messages can be coming in while the sysex
//     message is coming in.  Anyway, sysex messages are not really to
//     be used for real time MIDI messaging, so the exact moment that the
//     first byte of the sysex came in is not important to me.
//
//

void *interpretMidiInputStreamPrivate(void *) {

   int* argsExpected = NULL;     // MIDI parameter bytes expected to follow
   int* argsLeft     = NULL;     // MIDI parameter bytes left to wait for
   uchar packet[4];              // bytes for sequencer driver
   MidiMessage* message = NULL;  // holder for current MIDI message
   int newSigTime = 0;           // for millisecond timer
   int lastSigTime = -1;         // for millisecond timer
   int zeroSigTime = -1;         // for timing incoming events
   int device = -1;              // for sorting out the bytes by input device
   Array<uchar>* sysexIn;        // MIDI Input sysex temporary storage

   // Note on the use of argsExpected and argsLeft for sysexs:
   // If argsExpected is -1, then a sysex message is coming in.
   // If argsLeft < 0, then the sysex message has not finished comming
   // in.  If argsLeft == 0 and argsExpected == -1, then the sysex
   // has finished coming in and is to be sent to the correct
   // location.

   static int count = 0;
   if (count != 0) {
      std::cerr << "Cannot run this function more than once" << std::endl;
      exit(1);
   } else {
      // allocate space for MIDI messages, each device has a different message
      // holding spot in case the messages overlap in the input stream
      message      = new MidiMessage[MidiInPort_oss::numDevices];
      argsExpected = new int[MidiInPort_oss::numDevices];
      argsLeft     = new int[MidiInPort_oss::numDevices];

      sysexIn = new Array<uchar>[MidiInPort_oss::numDevices];
      for (int j=0; j<MidiInPort_oss::numDevices; j++) {
         sysexIn[j].allowGrowth();
         sysexIn[j].setSize(32);
         sysexIn[j].setSize(0);
         sysexIn[j].setGrowth(512);
      }

      count++;
   }
   
   // interpret MIDI bytes as they come into the computer
   // and repackage them as MIDI messages.
   int packetReadCount;
   while (1) {
top_of_loop:
      packetReadCount = ::read(MidiInPort_oss::sequencer_fd, 
         &packet, sizeof(packet));

      if (packetReadCount != 4) {
         // this if statement is used to prevent cases where the
         // read function above will time out and return 0 bytes 
         // read.  This if statment will also take care of -1 
         // error return values by ignoring them.
         continue;
      }
     

/*
      std::cout << "packet bytes "
           << " 0x" << std::hex << (int)packet[0]   // packet type
           << " 0x" << std::hex << (int)packet[1]   // possible MIDI byte
           << " 0x" << std::hex << (int)packet[2]   // device number
           << " 0x" << std::hex << (int)packet[3]   // unused for MIDI bytes
           << std::endl;
*/

      switch (packet[0]) {
         case SEQ_WAIT:
            // MIDI clock ticks ... the first MIDI message deltaTime is
            // calculated wrt the start of the MIDI clock.
            if (zeroSigTime < 0) {
               zeroSigTime = MidiInPort_oss::midiTimer.getTime();
            }
/* 
            int newTime;
            newTime = packet[3];
            newTime = (newTime << 8) | packet[2];
            newTime = (newTime << 8) | packet[1];
*/
            break;

         case SEQ_ECHO:
            // echo events
            break;

         case SEQ_MIDIPUTC:          // SEQ_MIDIPUTC = 5
            // determination of a full MIDI message from the input MIDI
            // stream is based here on the observation that MIDI status
            // bytes and subsequent data bytes are NOT returned in the same
            // read() call.  Rather, they are spread out over multiple read()
            // returns, with only a single value per return.  So if we
            // find a status byte, we then determine the number of expected
            // operands and process that number of subsequent read()s to
            // to determine the complete midi message.

/*
            std::cout << "MIDI byte: " << (int)packet[1] << std::endl;
*/

            // store the MIDI input device to which the incoming MIDI
            // byte belongs.
            device = packet[2];
           
            // ignore the active sensing 0xfe and MIDI clock 0xf8 commands:
            if (packet[1] == 0xfe || packet[1] == 0xf8) {
               break;
            }

            if (packet[1] & 0x80) {   // a command byte has arrived
               switch (packet[1] & 0xf0) {
                  case 0xf0:   
                     if (packet[1] == 0xf0) {
                        argsExpected[device] = -1;
                        argsLeft[device] = -1;
                        if (sysexIn[device].getSize() != 0) {
                           // ignore the command for now.  It is most
                           // likely an active sensing message.
                           goto top_of_loop;
                        } else {
                           uchar datum = 0xf0;
                           sysexIn[device].append(datum);
                        }
                     } if (packet[1] == 0xf7) {
                        argsLeft[device] = 0;         // indicates sysex is done
                        uchar datum = 0xf7;
                        sysexIn[device].append(datum);
                     } else if (argsExpected[device] != -1) {
                        // this is a system message that may or may
                        // not be coming while a sysex is coming in
                        argsExpected[device] = 0;
                     } else {
                        // this is a system message that is not coming
                        // while a system exclusive is coming in
                        //argsExpected[device] = 0;
                     }
                     break;
                  case 0xc0:   
                     if (argsExpected[device] < 0) {
                        std::cout << "Error: received program change during sysex" 
                             << std::endl;
                        exit(1);
                     } else {
                        argsExpected[device] = 1;    
                     }
                     break;
                  case 0xd0:   
                     if (argsExpected[device] < 0) {
                        std::cout << "Error: received aftertouch message during" 
                                " sysex" << std::endl;
                        exit(1);
                     } else {
                        argsExpected[device] = 1;    
                     }
                     break;
                  default:     
                     if (argsExpected[device] < 0) {
                        std::cout << "Error: received another message during sysex" 
                             << std::endl;
                        exit(1);
                     } else {
                        argsExpected[device] = 2;    
                     }
                     break;
               }
               if (argsExpected[device] >= 0) {
                  argsLeft[device] = argsExpected[device];
               }

               newSigTime = MidiInPort_oss::midiTimer.getTime();
               message[device].time = newSigTime - zeroSigTime;

               if (packet[1] != 0xf7) {
                  message[device].p0() = packet[1];
               } 
               message[device].p1() = 0;
               message[device].p2() = 0;
               message[device].p3() = 0;

               if (packet[1] == 0xf7) {
                  goto sysex_done;
               }
            } else if (argsLeft[device]) {   // not a command byte coming in
               if (message[device].time == 0) {
                  // store the receipt time of the first message byte
                  newSigTime = MidiInPort_oss::midiTimer.getTime();
                  message[device].time = newSigTime - zeroSigTime;
               }
                  
               if (argsExpected[device] < 0) {
                  // continue processing a sysex message
                  sysexIn[device].append(packet[1]);
               } else {
                  // handle a message other than a sysex message
                  if (argsLeft[device] == argsExpected[device]) {
                     message[device].p1() = packet[1];
                  } else {
                     message[device].p2() = packet[1];
                  }
                  argsLeft[device]--;
               }

               // if MIDI message is complete, setup for running status, and 
               // insert note into proper buffer.

               if (argsExpected[device] >= 0 && !argsLeft[device]) {

                  // store parameter data for running status
                  switch (message[device].p0() & 0xf0) {
                     case 0xc0:  argsLeft[device] = 1;    break;
                     case 0xd0:  argsLeft[device] = 1;    break;  // fix by dan
                     default:    argsLeft[device] = 2;    break;
                        // 0x80 expects two arguments
                        // 0x90 expects two arguments
                        // 0xa0 expects two arguments
                        // 0xb0 expects two arguments
                        // 0xe0 expects two arguments
                  }

                  lastSigTime = newSigTime;

                  sysex_done:      // come here when a sysex is completely done

                  // insert the MIDI message into the appropriate buffer
                  // do not insert into buffer if the MIDI input device
                  // is paused (which can mean closed).  Or if the
                  // pauseQ array is pointing to NULL (which probably means that
                  // things are about to shut down).
                  if (MidiInPort_oss::pauseQ != NULL &&
                        MidiInPort_oss::pauseQ[device] == 0) {
                     if (argsExpected[device] < 0) {
                        // store the sysex in the MidiInPort_oss
                        // buffer for sysexs and return the storage
                        // location:
                        int sysexlocation = 
                           MidiInPort_oss::installSysexPrivate(device,
                              sysexIn[device].getBase(),
                              sysexIn[device].getSize());

                        message[device].p0() = 0xf0;
                        message[device].p1() = sysexlocation;

                        sysexIn[device].setSize(0); // empty the sysex storage
                        argsExpected[device] = 0;   // no run status for sysex
                        argsLeft[device] = 0;       // turn off sysex input flag
                     }
                     MidiInPort_oss::midiBuffer[device]->insert(
                           message[device]);
//                   if (MidiInPort_oss::callbackFunction != NULL) {
//                      MidiInPort_oss::callbackFunction(device);
//                   }
                     if (MidiInPort_oss::trace[device]) {
                        std::cout << '[' << std::hex << (int)message[device].p0()
                             << ':' << std::dec << (int)message[device].p1()
                             << ',' << (int)message[device].p2() << ']'
                             << std::flush;
                     }
                     message[device].time = 0;
                  } else {
                     if (MidiInPort_oss::trace[device]) {
                        std::cout << '[' << std::hex << (int)message[device].p0()
                             << 'P' << std::dec << (int)message[device].p1()
                             << ',' << (int)message[device].p2() << ']'
                             << std::flush;
                     }
                  }
               }
            }
            break;

         default:
            break;
      } // end switch
   } // end while (1)

   // This code is not yet reached, but should be made to do so eventually

   if (message != NULL) {
      delete message;
      message = NULL;
   }

   if (argsExpected != NULL) {
      delete argsExpected;
      argsExpected = NULL;
   }

   if (argsLeft != NULL) {
      delete argsLeft;
      argsLeft = NULL;
   }

   if (sysexIn != NULL) { 
      delete sysexIn;
      sysexIn = NULL;
   }

}



#endif  // LINUX



// md5sum:	2008f4a298bef0cd85620a5507815866  - MidiInPort_oss.cpp =css= 20030102