File: fweelin_midiio.cc

package info (click to toggle)
freewheeling 0.6.4-1
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 3,244 kB
  • sloc: cpp: 24,079; xml: 3,631; ansic: 1,818; sh: 1,232; makefile: 31
file content (1376 lines) | stat: -rw-r--r-- 44,833 bytes parent folder | download | duplicates (2)
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
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
/*
   Control is such a trick--

   We can guide the ship
   but are we ever really in control
   of where we land?
*/

/* Copyright 2004-2011 Jan Pekau
   
   This file is part of Freewheeling.
   
   Freewheeling 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.
   
   Freewheeling 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 Freewheeling.  If not, see <http://www.gnu.org/licenses/>. */

#include <sys/time.h>

#if HAVE_SYS_RESOURCE_H
#include <sys/resource.h>
#endif

#include <stdio.h>
#include <errno.h>
#include <unistd.h>
#include <stdlib.h>

#include <math.h>
#include <string.h>

#include <pthread.h>
#include <sched.h>
#include <sys/mman.h>

#include "fweelin_midiio.h"
#include "fweelin_core.h"

// ******** MIDI 

#define MIDI_CLIENT_NAME "FreeWheeling"

#ifdef __MACOSX__

// ******** MACOSX MIDI

void MidiIO::SetMIDIInput (int idx) {
  if (inputidx != -1) {
    // Deselect previous source
    MIDIEndpointRef src = MIDIGetSource(inputidx);
    if (src != 0) {
      //printf("DISCONNECT MIDI: %d\n",inputidx);
      MIDIPortDisconnectSource(in_ports[0], src);
    }
  }
  
  inputidx = idx;
  
  // Select new source
  MIDIEndpointRef src = MIDIGetSource(inputidx);
  if (src != 0) {
    //printf("CONNECT MIDI: %d\n",inputidx);
    MIDIPortConnectSource(in_ports[0], src, 0);
  }
};

// Open MIDI engine with num_in writeable ports and num_out readable ports.
// Nonzero is returned on error
char MidiIO::open_midi (int num_in, int num_out) {
  if (MIDIClientCreate(CFSTR(MIDI_CLIENT_NAME), 0, 0, &client) != noErr) {
    fprintf(stderr, "MIDI: Error opening MIDI client.\n");
    return -1;
  }
  
  int l1;
  char portname[64];
  
  numins = num_in;
  numouts = num_out;
  in_ports = new MIDIPortRef[num_in];
  out_ports = new MIDIPortRef[num_out];
  out_sources = new MIDIEndpointRef[num_out];
  
  for (l1 = 0; l1 < num_in; l1++) {
    sprintf(portname, MIDI_CLIENT_NAME " IN %d", l1+1);
    if (MIDIInputPortCreate(client, CFStringCreateWithCString(0,portname,kCFStringEncodingUTF8), MidiInputProc, this, &in_ports[l1]) != noErr) {
      fprintf(stderr, "MIDI: Error creating MIDI port.\n");
      return -1;
    }
  }
  for (l1 = 0; l1 < num_out; l1++) {
    sprintf(portname, MIDI_CLIENT_NAME " OUT %d", l1+1);
    if (MIDIOutputPortCreate(client, CFStringCreateWithCString(0,portname,kCFStringEncodingUTF8), &out_ports[l1]) != noErr) {   
      fprintf(stderr, "MIDI: Error creating MIDI port.\n");
      return -1;
    }
  }
    
  // Make list of input sources
  int n = MIDIGetNumberOfSources();
  printf("MIDI: Input possible from %d sources.\n", n);
  CFStringRef endname;
  char cendname[1024];
  
  FweelinMac::ClearMIDIInputList();
  for (int i = 0; i < n; ++i) {
    MIDIEndpointRef src = MIDIGetSource(i);
    MIDIObjectGetStringProperty(src, kMIDIPropertyName, &endname);
    CFStringGetCString(endname, cendname, 1024, kCFStringEncodingUTF8);
    printf("  MIDI: source name: %s\n",cendname);
    
    FweelinMac::AddMIDIInputSource(cendname);
  }
  
  // Connect first input
  SetMIDIInput(0);
  
#if 0
  // We no longer use MIDISend to send to destinations--
  // because many apps connect to sources, we instead create virtual sources for each output
  // and use MIDIReceived to send to them
  
  // Discover a place to send MIDI data
  n = MIDIGetNumberOfDestinations();
  printf("MIDI: Output possible to %d destinations.\n",n);
  if (n > 0)
    dest = MIDIGetDestination(n-1);
  
  if (dest == 0) {
    printf("MIDI: WARNING: No place to send MIDI.\n");
    return 0; // No place to send data
  }
  
  // Get name of destination we are sending to
  CFStringRef pName;
  char name[64];
  MIDIObjectGetStringProperty(dest, kMIDIPropertyName, &pName);
  CFStringGetCString(pName, name, sizeof(name), 0);
  CFRelease(pName);
  printf("MIDI: Sending MIDI to destination: %s\n", name);

  // MIDI transmit using destinations
#define MIDI_TRANSMIT(idx) MIDISend(out_ports[idx], dest, packetList)

#else
  dest = 0;

  // Create virtual sources
  char sourcename[64];
  for (int i = 0; i < num_out; i++) {
    sprintf(sourcename, MIDI_CLIENT_NAME " OUT %d", i+1);
    if (MIDISourceCreate(client,CFStringCreateWithCString(0,sourcename,kCFStringEncodingUTF8),
                         &out_sources[i]) != noErr) {
      fprintf(stderr, "MIDI: Error creating virtual MIDI source.\n");
      return -1;
    }
  }

// MIDI transmit using virtual sources
#define MIDI_TRANSMIT(idx) MIDIReceived(out_sources[idx], packetList)

#endif
  
  return 0;
}

int MidiIO::activate() {
  FloConfig *fs = app->getCFG();

  // Open up MIDI
  if (open_midi(1, fs->GetNumMIDIOuts())) {
    fprintf(stderr, "MIDI: Can't open MIDI client.\n");
    exit(1);
  }
  
  listen_events();

  // Request initial patch from patch browser
  PatchBrowser *br = (PatchBrowser *) app->getBROWSER(B_Patch);
  if (br != 0)
    br->SetMIDIForPatch();
    
  // Prepare auto-bypass
  bp = new BypassInfo[fs->GetNumMIDIOuts()*MAX_MIDI_CHANNELS];

  inst->checkfreq = inst->app->getAUDIO()->get_srate(); // How often to check auto-bypass conditions for MIDI

  return 0;
}

void MidiIO::close() {
  printf("MIDI: begin close...\n");
  
  unlisten_events();

  printf("MIDI: end\n");
}

void MidiIO::MidiInputProc (const MIDIPacketList *pktlist, void *refCon, void *connRefCon) {
  MidiIO *inst = static_cast<MidiIO *>(refCon);

  // Check whether to unbypass used channels
  inst->CheckUnbypass();

  MIDIPacket *packet = (MIDIPacket *) pktlist->packet;
  for (int j = 0; j < pktlist->numPackets; j++) {
    if ((packet->data[0] >= 0x80 && packet->data[0] <= 0x8F) ||
        (packet->data[0] >= 0x90 && packet->data[0] <= 0x9F &&
         packet->data[2] == 0)) {                       // Interpret NoteOn velocity 0 as note off
      // Note Off
      int base = (packet->data[0] >= 0x90 ? 0x90 : 0x80);
      inst->ReceiveNoteOffEvent(packet->data[0] - base,
                                packet->data[1],        
                                packet->data[2]);
    } else if (packet->data[0] >= 0x90 && packet->data[0] <= 0x9F) {
      // Note On
      inst->ReceiveNoteOnEvent(packet->data[0] - 0x90,
                               packet->data[1], 
                               packet->data[2]);
    } else if (packet->data[0] >= 0xB0 && packet->data[0] <= 0xBF) {
      // Control Change
      inst->ReceiveControlChangeEvent(packet->data[0] - 0xB0,
                                      packet->data[1],
                                      packet->data[2]);
    } else if (packet->data[0] >= 0xC0 && packet->data[0] <= 0xCF) {
      // Program Change
      inst->ReceiveProgramChangeEvent(packet->data[0] - 0xC0,
                                      packet->data[1]);
    } else if (packet->data[0] >= 0xD0 && packet->data[0] <= 0xDF) {
      // Channel Pressure
      inst->ReceiveChannelPressureEvent(packet->data[0] - 0xD0,
                                        packet->data[1]);
    } else if (packet->data[0] >= 0xE0 && packet->data[0] <= 0xEF) {
      // Pitch Bend
      int lsb = packet->data[1],
        msb = packet->data[2],
        benderval = msb << 8 + lsb;
      
      inst->ReceivePitchBendEvent(packet->data[0] - 0xE0,
                                  benderval);
    }   
    
    // Advance
    packet = MIDIPacketNext(packet);
  }  

  // Check whether to bypass unused channels
  inst->CheckBypass();
}

void MidiIO::OutputController (int port, int chan, int ctrl, int val) {
  unsigned char msg[3]; 
  msg[0] = 0xB0 + chan;
  msg[1] = (unsigned char) ctrl;
  if (val > 127)
    val = 127;
  else if (val < 0)
    val = 0;
  msg[2] = (unsigned char) val;
  
  curPacket = MIDIPacketListAdd(packetList,sizeof(obuf),
                                curPacket,0 /*Now*/,3,msg);
};

void MidiIO::OutputProgramChange (int port, int chan, int val) {
  unsigned char msg[2]; 
  msg[0] = 0xC0 + chan;
  if (val > 127)
    val = 127;
  else if (val < 0)
    val = 0;
  msg[1] = (unsigned char) val;
  
  curPacket = MIDIPacketListAdd(packetList,sizeof(obuf),
                                curPacket,0 /*Now*/,2,msg);
};

void MidiIO::OutputChannelPressure (int port, int chan, int val) {
  unsigned char msg[2]; 
  msg[0] = 0xD0 + chan;
  if (val > 127)
    val = 127;
  else if (val < 0)
    val = 0;
  msg[1] = (unsigned char) val;
  
  curPacket = MIDIPacketListAdd(packetList,sizeof(obuf),
                                curPacket,0 /*Now*/,2,msg);
};

void MidiIO::OutputPitchBend (int port, int chan, int val) {
  unsigned char msg[3]; 
  msg[0] = 0xE0 + chan;
  msg[1] = (unsigned char) (val & 0x00FF);      // LSB
  msg[2] = (unsigned char) (val >> 8);          // MSB
  
  curPacket = MIDIPacketListAdd(packetList,sizeof(obuf),
                                curPacket,0 /*Now*/,3,msg);
};

void MidiIO::OutputNote (int port, int chan, char down, int notenum, int vel) {
  unsigned char msg[3]; 
  if (down)
    msg[0] = 0x90 + chan; // NoteOn
  else
    msg[0] = 0x80 + chan; // NoteOff
  msg[1] = (unsigned char) notenum;
  msg[2] = (unsigned char) vel;
  
  curPacket = MIDIPacketListAdd(packetList,sizeof(obuf),
                                curPacket,0 /*Now*/,3,msg);
};

void MidiIO::OutputClock (int port) {
  unsigned char msg = 0xF8;       
  curPacket = MIDIPacketListAdd(packetList,sizeof(obuf),
                                curPacket,0 /*Now*/,1,&msg);
};


void MidiIO::OutputStart (int port) {
  unsigned char msg = 0xFA;       
  curPacket = MIDIPacketListAdd(packetList,sizeof(obuf),
                                curPacket,0 /*Now*/,1,&msg);
};

void MidiIO::OutputStop (int port) {
  unsigned char msg = 0xFC;       
  curPacket = MIDIPacketListAdd(packetList,sizeof(obuf),
                                curPacket,0 /*Now*/,1,&msg);
};

void MidiIO::OutputStartOnPort () {
  // Init output packet
  curPacket = MIDIPacketListInit(packetList);
};b

void MidiIO::OutputEndOnPort (int port) {
  // Send MIDI packet- all messages for this port
  MIDI_TRANSMIT(port);
};

#else

// ******** LINUX MIDI

// Open MIDI engine with num_in writeable ports and num_out readable ports.
// Nonzero is returned on error
char MidiIO::open_midi (int num_in, int num_out) {
  int l1;
  char portname[64];
  
  numins = num_in;
  numouts = num_out;
  in_ports = new int[num_in];
  out_ports = new int[num_out];
  
  if (snd_seq_open(&seq_handle, "default", SND_SEQ_OPEN_DUPLEX, 0) < 0) {
    fprintf(stderr, "MIDI: Error opening ALSA MIDI.\n");
    return -1;
  }
  snd_seq_set_client_name(seq_handle, MIDI_CLIENT_NAME);
  for (l1 = 0; l1 < num_in; l1++) {
    sprintf(portname, MIDI_CLIENT_NAME " IN %d", l1+1);
    if ((in_ports[l1] = snd_seq_create_simple_port(seq_handle, portname,
                                                   SND_SEQ_PORT_CAP_WRITE|SND_SEQ_PORT_CAP_SUBS_WRITE,
                                                   SND_SEQ_PORT_TYPE_APPLICATION)) < 0) {
      fprintf(stderr, "MIDI: Error creating MIDI port.\n");
      return -1;
    }
  }
  for (l1 = 0; l1 < num_out; l1++) {
    sprintf(portname, MIDI_CLIENT_NAME " OUT %d", l1+1);
    if ((out_ports[l1] = snd_seq_create_simple_port(seq_handle, portname,
                                                    SND_SEQ_PORT_CAP_READ|SND_SEQ_PORT_CAP_SUBS_READ,
                                                    SND_SEQ_PORT_TYPE_APPLICATION)) < 0) {
      fprintf(stderr, "MIDI: Error creating MIDI port.\n");
      return -1;
    }
  }
  
  return 0;
}

int MidiIO::activate() {
  // Linux MIDI handling using ALSA seq on its own thread
  printf("MIDI: Starting MIDI thread..\n");

  const static size_t STACKSIZE = 1024*64;
  pthread_attr_t attr;
  pthread_attr_init(&attr);
  pthread_attr_setstacksize(&attr,STACKSIZE);
  printf("MIDI: Stacksize: %zd.\n",STACKSIZE);

  midithreadgo = 1;
  int ret = pthread_create(&midi_thread,
                           &attr,
                           run_midi_thread,
                           this);
  if (ret != 0) {
    printf("(start) pthread_create failed, exiting");
    return 1;
  }
  RT_RWThreads::RegisterReaderOrWriter(midi_thread);

  // Setup high priority threads
  struct sched_param schp;
  memset(&schp, 0, sizeof(schp));

  // MIDI thread at SCHED_FIFO- yes! High priority!
  //schp.sched_priority = sched_get_priority_max(SCHED_OTHER);
  //schp.sched_priority = sched_get_priority_min(SCHED_FIFO);
  schp.sched_priority = sched_get_priority_max(SCHED_FIFO);

#ifdef RLIMIT_RTPRIO
  // Check the rtprio limit (/etc/security/limits.conf)
  struct rlimit user_limits;
  memset(&user_limits, 0, sizeof(user_limits));
  if (getrlimit(RLIMIT_RTPRIO, &user_limits) == 0 &&
      user_limits.rlim_max > 0 &&
      schp.sched_priority > 0 &&
      user_limits.rlim_max < static_cast<rlim_t>(schp.sched_priority)) {
    schp.sched_priority = user_limits.rlim_max;
  }
#endif

  printf("MIDI: HiPri Thread %d\n",schp.sched_priority);
  if (pthread_setschedparam(midi_thread, SCHED_FIFO /* OTHER */, &schp) != 0) {    
    printf("MIDI: Can't set realtime thread, will use nonRT!\n");
  }

  // Request initial patch from patch browser
  PatchBrowser *br = (PatchBrowser *) app->getBROWSER(B_Patch);
  if (br != 0)
    br->SetMIDIForPatch();

  // Prepare auto-bypass
  bp = new BypassInfo[app->getCFG()->GetNumMIDIOuts()*MAX_MIDI_CHANNELS];

  return 0;
}

void MidiIO::close() {
  printf("MIDI: begin close...\n");
  midithreadgo = 0;
  pthread_join(midi_thread,0);
  printf("MIDI: end\n");
}

void *MidiIO::run_midi_thread(void *ptr)
{
  MidiIO *inst = static_cast<MidiIO *>(ptr);
  int npfd;
  struct pollfd *pfd;
  
  FloConfig *fs = inst->app->getCFG();
  inst->checkfreq = inst->app->getAUDIO()->get_srate(); // How often to check auto-bypass conditions for MIDI
    
  printf("MIDIthread start..\n");
  // printf("*** MIDI THREAD: %li\n",pthread_self());
  
  // Open up ALSA MIDI client
  if (inst->open_midi(1, fs->GetNumMIDIOuts())) {
    fprintf(stderr, "MIDI: Can't open ALSA MIDI.\n");
    exit(1);
  }
  npfd = snd_seq_poll_descriptors_count(inst->seq_handle, POLLIN);
  pfd = (struct pollfd *)alloca(npfd * sizeof(struct pollfd));
  snd_seq_poll_descriptors(inst->seq_handle, pfd, npfd, POLLIN);
  
  inst->listen_events();
  
  // Main MIDI loop
  while (inst->midithreadgo) {
    // Every second, come out of poll-- so we don't get locked up
    // if no MIDI events come in
    if (poll(pfd, npfd, 1000) > 0) {
      // There's an event here! 
      
      // Check whether to unbypass used channels
      inst->CheckUnbypass();

      // Read event(s)      
      snd_seq_event_t *ev;
      // static int cnt = 0;
      do {
        snd_seq_event_input(inst->seq_handle, &ev);
        switch (ev->type) {
#if 0
          case SND_SEQ_EVENT_QFRAME:
            printf("MTC quarterframe\n");
            break;
            
          case SND_SEQ_EVENT_CLOCK:
            if (cnt % 24 == 0)
              printf("clock: %d\n",cnt);
            cnt++;
            //printf("clock: %d\n",ev->data.time.tick);
            //printf("clock: %d\n",ev->data.time.time.tv_sec);
            //printf("clock: %d\n",ev->data.queue.param.time.time.tv_sec);
            break;
            
          case SND_SEQ_EVENT_TICK:
            printf("MIDI: 'tick' not yet implemented\n");
            break;
            
          case SND_SEQ_EVENT_TEMPO:
            printf("MIDI: 'tempo' not yet implemented\n");
            break;
#endif
            
          case SND_SEQ_EVENT_CONTROLLER: 
            // Control Change
            inst->ReceiveControlChangeEvent(ev->data.control.channel,
                                            ev->data.control.param,
                                            ev->data.control.value);
            break;

          case SND_SEQ_EVENT_CHANPRESS:
            // Channel aftertouch 
            inst->ReceiveChannelPressureEvent(ev->data.control.channel,
                                              ev->data.control.value);
            break;

          case SND_SEQ_EVENT_PGMCHANGE:
            // Program Change
            inst->ReceiveProgramChangeEvent(ev->data.control.channel,
                                            ev->data.control.value);
            break;
            
          case SND_SEQ_EVENT_PITCHBEND:
            // Pitch Bend
            inst->ReceivePitchBendEvent(ev->data.control.channel,
                                        ev->data.control.value);
            break;
            
          case SND_SEQ_EVENT_NOTEON:
            // Note On
            inst->ReceiveNoteOnEvent(ev->data.control.channel,
                                     ev->data.note.note,
                                     ev->data.note.velocity);
            break;
            
          case SND_SEQ_EVENT_NOTEOFF: 
            // Note Off
            inst->ReceiveNoteOffEvent(ev->data.control.channel,
                                      ev->data.note.note,
                                      ev->data.note.velocity);
            break;
        }
        
        snd_seq_free_event(ev);
      } while (snd_seq_event_input_pending(inst->seq_handle, 0) > 0);
    }
    
    // Every MIDI msg or at max 1 s intervals, check auto-bypass conditions
    inst->CheckBypass();
  }
  
  inst->unlisten_events();
  
  printf("MIDI: thread done\n");
  return 0;
}

void MidiIO::OutputController (int port, int chan, int ctrl, int val) {
  if (val > 127)
    val = 127;
  else if (val < 0)
    val = 0;

  snd_seq_event_t outev;
  snd_seq_ev_set_subs(&outev);
  snd_seq_ev_set_direct(&outev);
  snd_seq_ev_set_source(&outev,out_ports[port]);
  snd_seq_ev_set_controller(&outev,chan,ctrl,val);
  snd_seq_event_output_direct(seq_handle, &outev);
};

void MidiIO::OutputProgramChange (int port, int chan, int val) {
  if (val > 127)
    val = 127;
  else if (val < 0)
    val = 0;

  snd_seq_event_t outev;
  snd_seq_ev_set_subs(&outev);
  snd_seq_ev_set_direct(&outev);
  snd_seq_ev_set_source(&outev,out_ports[port]);
  snd_seq_ev_set_pgmchange(&outev,chan,val);
  snd_seq_event_output_direct(seq_handle, &outev);
};

void MidiIO::OutputChannelPressure (int port, int chan, int val) {
  if (val > 127)
    val = 127;
  else if (val < 0)
    val = 0;

  snd_seq_event_t outev;
  snd_seq_ev_set_subs(&outev);
  snd_seq_ev_set_direct(&outev);
  snd_seq_ev_set_source(&outev,out_ports[port]);
  snd_seq_ev_set_chanpress(&outev,chan,val);
  snd_seq_event_output_direct(seq_handle, &outev);
};

void MidiIO::OutputPitchBend (int port, int chan, int val) {
  snd_seq_event_t outev;
  snd_seq_ev_set_subs(&outev);
  snd_seq_ev_set_direct(&outev);
  snd_seq_ev_set_source(&outev,out_ports[port]);
  snd_seq_ev_set_pitchbend(&outev,chan,val);
  snd_seq_event_output_direct(seq_handle, &outev);
};

void MidiIO::OutputNote (int port, int chan, char down, int notenum, int vel) {
  snd_seq_event_t outev;
  snd_seq_ev_set_subs(&outev);
  snd_seq_ev_set_direct(&outev);
  snd_seq_ev_set_source(&outev,out_ports[port]);
  if (down)
    snd_seq_ev_set_noteon(&outev,chan,notenum,vel);
  else
    snd_seq_ev_set_noteoff(&outev,chan,notenum,vel);
  snd_seq_event_output_direct(seq_handle, &outev);
};

void MidiIO::OutputClock (int port) {
  snd_seq_event_t outev;
  snd_seq_ev_set_subs(&outev);
  snd_seq_ev_set_direct(&outev);
  snd_seq_ev_set_source(&outev,out_ports[port]);
  outev.type = SND_SEQ_EVENT_CLOCK;
  snd_seq_ev_set_fixed(&outev);
  snd_seq_event_output_direct(seq_handle, &outev);  
};

void MidiIO::OutputStart (int port) {
  snd_seq_event_t outev;
  snd_seq_ev_set_subs(&outev);
  snd_seq_ev_set_direct(&outev);
  snd_seq_ev_set_source(&outev,out_ports[port]);
  outev.type = SND_SEQ_EVENT_START;
  snd_seq_ev_set_fixed(&outev);
  snd_seq_event_output_direct(seq_handle, &outev);  
};

void MidiIO::OutputSPP (int port) {
  snd_seq_event_t outev;
  snd_seq_ev_set_subs(&outev);
  snd_seq_ev_set_direct(&outev);
  snd_seq_ev_set_source(&outev,out_ports[port]);
  outev.type = SND_SEQ_EVENT_SONGPOS;
  outev.data.control.value = 0; // Always start at the beginning
  snd_seq_ev_set_fixed(&outev);
  snd_seq_event_output_direct(seq_handle, &outev);
};

void MidiIO::OutputStop (int port) {
  snd_seq_event_t outev;
  snd_seq_ev_set_subs(&outev);
  snd_seq_ev_set_direct(&outev);
  snd_seq_ev_set_source(&outev,out_ports[port]);
  outev.type = SND_SEQ_EVENT_STOP;
  snd_seq_ev_set_fixed(&outev);
  snd_seq_event_output_direct(seq_handle, &outev);  
};

// Events are sent directly- no buffer emptying necessary
void MidiIO::OutputStartOnPort () {};
void MidiIO::OutputEndOnPort (int port) {};

#endif

// ******** CROSS-PLATFORM MIDI CODE

MidiIO::MidiIO (Fweelin *app) : bendertune(0), curbender(0), 
                                echoport(1), echochan(-1), curpatch(0), 
                                numins(0), numouts(0), app(app), 
                                
                                checkfreq(40000), lastchecktime(0), 
                                
#ifdef __MACOSX__
                                client(0), in_ports(0), out_ports(0), out_sources(0), dest(0), 
                                inputidx(-1), curPacket(0),
                                packetList((MIDIPacketList *) obuf),
#else
                                seq_handle(0), in_ports(0), out_ports(0),
                                midithreadgo(0),
#endif
                                bp(0), midisyncxmit(0)
{
  note_def_port = new int[MAX_MIDI_NOTES];
  note_patch = new PatchItem *[MAX_MIDI_NOTES];
  memset(note_def_port,0,sizeof(int) * MAX_MIDI_NOTES);
  memset(note_patch,0,sizeof(PatchItem *) * MAX_MIDI_NOTES);  
};

void MidiIO::listen_events () {
  FloConfig *fs = app->getCFG();

  app->getEMG()->ListenEvent(this,0,T_EV_SetMidiTuning);
  app->getEMG()->ListenEvent(this,0,T_EV_SetMidiEchoPort);
  app->getEMG()->ListenEvent(this,0,T_EV_SetMidiEchoChannel);
  app->getEMG()->ListenEvent(this,fs->GetInputMatrix(),
                                  T_EV_Input_MIDIKey);
  app->getEMG()->ListenEvent(this,fs->GetInputMatrix(),
                                  T_EV_Input_MIDIController);
  app->getEMG()->ListenEvent(this,fs->GetInputMatrix(),
                                  T_EV_Input_MIDIProgramChange);
  app->getEMG()->ListenEvent(this,fs->GetInputMatrix(),
                                  T_EV_Input_MIDIPitchBend);
  app->getEMG()->ListenEvent(this,0,T_EV_Input_MIDIClock);
  app->getEMG()->ListenEvent(this,0,T_EV_Input_MIDIStartStop);
}

void MidiIO::unlisten_events () {
  FloConfig *fs = app->getCFG();

  app->getEMG()->UnlistenEvent(this,0,T_EV_SetMidiTuning);
  app->getEMG()->UnlistenEvent(this,0,T_EV_SetMidiEchoPort);
  app->getEMG()->UnlistenEvent(this,0,T_EV_SetMidiEchoChannel);
  app->getEMG()->UnlistenEvent(this,fs->GetInputMatrix(),
                                    T_EV_Input_MIDIKey);
  app->getEMG()->UnlistenEvent(this,fs->GetInputMatrix(),
                                    T_EV_Input_MIDIController);
  app->getEMG()->UnlistenEvent(this,fs->GetInputMatrix(),
                                    T_EV_Input_MIDIProgramChange);
  app->getEMG()->UnlistenEvent(this,fs->GetInputMatrix(),
                                    T_EV_Input_MIDIPitchBend);
  app->getEMG()->UnlistenEvent(this,0,T_EV_Input_MIDIClock);
  app->getEMG()->UnlistenEvent(this,0,T_EV_Input_MIDIStartStop);
}

void MidiIO::ReceiveNoteOffEvent (int channel, int notenum, int vel) {
  // Note Off
  MIDIKeyInputEvent *mevt = (MIDIKeyInputEvent *)
  Event::GetEventByType(T_EV_Input_MIDIKey);
  mevt->down = 0;
  mevt->channel = channel;
  mevt->notenum = notenum;
  if (mevt->notenum < 0 || mevt->notenum >= MAX_MIDI_NOTES) {
    printf("MIDI: Bad MIDI note #%d!\n",mevt->notenum);
    mevt->notenum = 0;
  }
  mevt->vel = vel;
  mevt->outport = note_def_port[mevt->notenum];
  if (app->getCFG()->IsDebugInfo())
    printf("MIDI: Note %d off, channel %d velocity %d\n",
           mevt->notenum, mevt->channel, mevt->vel);
  app->getEMG()->BroadcastEventNow(mevt, this);
};

void MidiIO::ReceiveNoteOnEvent (int channel, int notenum, int vel) {
  MIDIKeyInputEvent *mevt = (MIDIKeyInputEvent *)
  Event::GetEventByType(T_EV_Input_MIDIKey);
  mevt->channel = channel;
  mevt->notenum = notenum;
  if (mevt->notenum < 0 || mevt->notenum >= MAX_MIDI_NOTES) {
    printf("MIDI: Bad MIDI note #%d!\n",mevt->notenum);
    mevt->notenum = 0;
  }
  mevt->vel = vel;
  if (vel > 0) {
    mevt->down = 1;
    note_def_port[mevt->notenum] = mevt->outport = echoport;
    note_patch[mevt->notenum] = curpatch;
  } else {
    mevt->down = 0;
    mevt->outport = note_def_port[mevt->notenum];
  }
  if (app->getCFG()->IsDebugInfo())
    printf("MIDI: Note %d %s, channel %d velocity %d\n",
           mevt->notenum,
           (mevt->down ? "on" : "off"),
           mevt->channel, mevt->vel);
  app->getEMG()->BroadcastEventNow(mevt, this);
}

void MidiIO::ReceivePitchBendEvent (int channel, int value) {
  // Store incoming bender value
  curbender = value;
  // Perform tune
  value += bendertune;
  
  MIDIPitchBendInputEvent *mevt = (MIDIPitchBendInputEvent *)
    Event::GetEventByType(T_EV_Input_MIDIPitchBend);
  mevt->outport = echoport;
  mevt->channel = channel;
  mevt->val = value;
  if (app->getCFG()->IsDebugInfo())
    printf("MIDI: Pitchbend channel %d value %d\n",
           mevt->channel,mevt->val);
  app->getEMG()->BroadcastEventNow(mevt, this);
}         

void MidiIO::ReceiveChannelPressureEvent (int channel, int value) {
  MIDIChannelPressureInputEvent *mevt = (MIDIChannelPressureInputEvent *)
    Event::GetEventByType(T_EV_Input_MIDIChannelPressure);
  mevt->outport = echoport;
  mevt->channel = channel;
  mevt->val = value;
  if (app->getCFG()->IsDebugInfo())
    printf("MIDI: Channel pressure channel %d value %d\n",
           mevt->channel,mevt->val);
  app->getEMG()->BroadcastEventNow(mevt, this);
}         

void MidiIO::ReceiveProgramChangeEvent (int channel, int value) {
  MIDIProgramChangeInputEvent *mevt = (MIDIProgramChangeInputEvent *)
    Event::GetEventByType(T_EV_Input_MIDIProgramChange);
  mevt->outport = echoport;
  mevt->channel = channel;
  mevt->val = value;
  if (app->getCFG()->IsDebugInfo())
    printf("MIDI: Program change channel %d value %d\n",
           mevt->channel,mevt->val);
  app->getEMG()->BroadcastEventNow(mevt, this);
}         

void MidiIO::ReceiveControlChangeEvent (int channel, int ctrl, int value) {
  MIDIControllerInputEvent *mevt = (MIDIControllerInputEvent *)
    Event::GetEventByType(T_EV_Input_MIDIController);
  mevt->outport = echoport;
  mevt->channel = channel;
  mevt->ctrl = ctrl;
  mevt->val = value;
  if (app->getCFG()->IsDebugInfo())
    printf("MIDI: Controller %d channel %d value %d\n",mevt->ctrl,
           mevt->channel,mevt->val);
  app->getEMG()->BroadcastEventNow(mevt, this);
}

MidiIO::~MidiIO() {
  delete[] note_def_port;
  delete[] note_patch;
  if (in_ports != 0)
    delete[] in_ports;
  if (out_ports != 0)
    delete[] out_ports;
  if (bp != 0)
    delete[] bp;
    
#ifdef __MACOSX__
  if (out_sources != 0)
    delete[] out_sources;
#endif
}

void MidiIO::SetMIDIForPatch (int def_port, PatchItem *patch) {
  if (CRITTERS)
    printf("MIDI: Setup MIDI for Patch (Port: %d Patch: %s)\n",def_port,
           (patch != 0 ? patch->name : "(none)"));

  // Move away from another patch?
  if (curpatch != 0 && patch != curpatch) {
    if (curpatch->IsCombi()) {
      // Bypass settings
      for (int i = 0; i < curpatch->numzones; i++) {
        CombiZone *z = curpatch->GetZone(i);
        if (z->bypasscc != 0) {
          int z_port = (z->port_r ? z->port : echoport),
            c = (z->bypasschannel == -1 ? z->channel : z->bypasschannel);
          BypassInfo *b = getBP(z_port-1,c);
          if (b != 0)
            b->active = 1;  // Activate auto-bypass as we are leaving this patch
          else
            printf("MIDI: Can't find BypassInfo struct for p/c [%d/%d]\n",z_port-1,c);
        }
      }
    } else {
      // Bypass settings
      if (curpatch->bypasscc != 0) {
        int c = (curpatch->bypasschannel == -1 ? echochan : curpatch->bypasschannel);
        BypassInfo *b = getBP(echoport-1,c);
        if (b != 0)
          b->active = 1;  // Activate auto-bypass as we are leaving this patch
        else
          printf("MIDI: Can't find BypassInfo struct for p/c [%d/%d]\n",echoport-1,c);
      }
    }
  }
  
  // Save default port
  char outportschanged = 0,
    usefluid = 0; // Enable FluidSynth?
  if (def_port >= 0 && def_port <= numouts) {
    if (def_port != echoport) {
      echoport = def_port;
      outportschanged = 1;
      if (echoport == 0)
        usefluid = 1;
    }
  } else
    printf("MIDI: Invalid port #%d (valid range 0-%d)\n",
           def_port,numouts);

  if (patch != 0) {
    if (patch->IsCombi()) {
      // Store reference to patch, to get zones later
      if (patch != curpatch) {
        curpatch = patch;

        // Check if FluidSynth port is referenced-
        outportschanged = 1;
        usefluid = 0;
        for (int i = 0; i < patch->numzones && !usefluid; i++) {
          CombiZone *z = patch->GetZone(i);
          if ((!z->port_r && echoport == 0) || // Port 0 is FluidSynth
              (z->port_r && z->port == 0))   
            usefluid = 1;
        }

        if (CRITTERS)
          printf("MIDI: COMBI PATCH!\n");
          
        // Bypass settings
        for (int i = 0; i < curpatch->numzones; i++) {
          CombiZone *z = curpatch->GetZone(i);
          if (z->bypasscc != 0) {
            int z_port = (z->port_r ? z->port : echoport),
              c = (z->bypasschannel == -1 ? z->channel : z->bypasschannel);
            BypassInfo *b = getBP(z_port-1,c);
            if (b != 0) {
              b->active = 0;  // Only bypass when we switch away from another patch
              b->bypasscc = z->bypasscc;
              b->bypasslen1 = (nframes_t) (z->bypasstime1*app->getAUDIO()->get_srate());
              b->bypasslen2 = (nframes_t) (z->bypasstime2*app->getAUDIO()->get_srate());
            } else
              printf("MIDI: Can't find BypassInfo struct for p/c [%d/%d]\n",z_port-1,c);
          }
        }
      }
    } else {
      // Easy, single channel patch
      curpatch = patch;
      echochan = patch->channel;
      
      // Bypass settings
      if (patch->bypasscc != 0) {
        int c = (curpatch->bypasschannel == -1 ? echochan : curpatch->bypasschannel);
        BypassInfo *b = getBP(echoport-1,c);
        if (b != 0) {
          b->active = 0;  // Only bypass when we switch away from another patch
          b->bypasscc = patch->bypasscc;
          b->bypasslen1 = (nframes_t) (patch->bypasstime1*app->getAUDIO()->get_srate());
          b->bypasslen2 = (nframes_t) (patch->bypasstime2*app->getAUDIO()->get_srate());
        } else
          printf("MIDI: Can't find BypassInfo struct for p/c [%d/%d]\n",echoport-1,c);
      }
    }
  }

  if (outportschanged) { // If ports changed
    // Enable/Disable FluidSynth engine
    FluidSynthEnableEvent *fsevt = (FluidSynthEnableEvent *)
      Event::GetEventByType(T_EV_FluidSynthEnable);
    fsevt->enable = usefluid;
    app->getEMG()->BroadcastEventNow(fsevt, this);
  }
}

void MidiIO::ReceiveEvent(Event *ev, EventProducer *from) {
  // Handle special events for MIDI
  switch (ev->GetType()) {
  case T_EV_SetMidiTuning :
    {
      SetMidiTuningEvent *tev = (SetMidiTuningEvent *) ev;
      
      // OK!
      if (CRITTERS)
        printf("MIDI: Received SetMidiTuning "
               "(new tuning: %d)\n", tev->tuning);
      SetBenderTune(tev->tuning);
    }
    return;
    
#if 0
  case T_EV_SetMidiEchoPort :
    {
      SetMidiEchoPortEvent *pev = (SetMidiEchoPortEvent *) ev;
      
      // OK!
      if (CRITTERS)
        printf("MIDI: Received SetMidiEchoPort "
               "(port #: %d)\n", pev->echoport);
      if (pev->echoport >= 0 && pev->echoport <= numouts)
        echoport = pev->echoport;
      else
        printf("MIDI: Invalid port #%d (valid range 0-%d)\n",
               pev->echoport,numouts);
    }
    return;

  case T_EV_SetMidiEchoChannel :
    {
      SetMidiEchoChannelEvent *cev = (SetMidiEchoChannelEvent *) ev;
      
      // OK!
      if (CRITTERS)
        printf("MIDI: Received SetMidiEchoChannel "
               "(channel #: %d)\n", cev->echochannel);
      echochan = cev->echochannel;
    }
    return;
#endif
    
  default:
    break;
  }

  // Unhandled event?-- echo back to MIDI outs
  EchoEvent(ev);
}

int MidiIO::EchoEventToPortChannel (Event *ev, int port, int channel, int bypasschannel) {
  FloConfig *fs = app->getCFG(); 
  static int midi_clock_count = 0;

  // DEBUG
  // printf("ECHO EVENT -> Port: %d Channel: %d\n",port,channel);

  int ret = -1;
  switch (ev->GetType()) {
  case T_EV_Input_MIDIClock :
    {
      // MIDIClockInputEvent *clkevt = (MIDIClockInputEvent *) ev;
      OutputClock(ret = port);

      midi_clock_count++;
      if (midi_clock_count >= MIDI_CLOCK_FREQUENCY) {
        midi_clock_count = 0;
        if (CRITTERS)
          printf("MIDI: Quarter Note Clock\n");
      }
    } 
    break;

  case T_EV_Input_MIDIStartStop :
    {
      MIDIStartStopInputEvent *ssevt = (MIDIStartStopInputEvent *) ev;
      if (ssevt->start) {
        midi_clock_count = 0;
        OutputSPP(ret = port);  // Output song position pointer first
        OutputStart(port);      // Then start message
      } else
        OutputStop(ret = port);
    } 
    break;
    
  case T_EV_Input_MIDIController :
    {
      MIDIControllerInputEvent *mcev = (MIDIControllerInputEvent *) ev;
      int p = (port == -1 ? mcev->outport-1 : port),
        c = (channel == -1 ? mcev->channel : channel),
        bc = (bypasschannel == -1 ? c : bypasschannel);
      ret = p;
      
      OutputController(p,c,
                       mcev->ctrl,
                       mcev->val);

      if (mcev->ctrl == MIDI_CC_SUSTAIN) {
        BypassInfo *b = getBP(p,bc);
        if (b != 0) {
          // printf ("sustain: %d\n",mcev->val);
          b->sp = mcev->val;
          if (b->sp == 0 && b->numheld == 0)
            b->releasecnt = app->getRP()->GetSampleCnt(); // Register release time of notes as when sustain pedal is released
        } else
          printf("MIDI: Can't find BypassInfo struct for p/c [%d/%d]\n",p,c);
      }      
    }
    break;
    
  case T_EV_Input_MIDIProgramChange :
    {
      MIDIProgramChangeInputEvent *mpev = (MIDIProgramChangeInputEvent *) ev;
      OutputProgramChange(ret = (port == -1 ? mpev->outport-1 : port),
                          (channel == -1 ? mpev->channel : channel),
                          mpev->val);
    } 
    break;

  case T_EV_Input_MIDIChannelPressure :
    {
      MIDIChannelPressureInputEvent *mpev = 
        (MIDIChannelPressureInputEvent *) ev;
      OutputChannelPressure(ret = (port == -1 ? mpev->outport-1 : port),
                            (channel == -1 ? mpev->channel : channel),
                            mpev->val);
    }
    break;
    
  case T_EV_Input_MIDIPitchBend :
    {
      MIDIPitchBendInputEvent *mpev = (MIDIPitchBendInputEvent *) ev;
      OutputPitchBend(ret = (port == -1 ? mpev->outport-1 : port),
                      (channel == -1 ? mpev->channel : channel),
                      mpev->val);
    }
    break;
    
  case T_EV_Input_MIDIKey :
    {
      MIDIKeyInputEvent *mkev = (MIDIKeyInputEvent *) ev;      
      int p = (port == -1 ? mkev->outport-1 : port),
        c = (channel == -1 ? mkev->channel : channel),
        bc = (bypasschannel == -1 ? c : bypasschannel);
      ret = p;

      OutputNote(p,c,
                 mkev->down,
                 mkev->notenum + fs->transpose,
                 mkev->vel);

      BypassInfo *b = getBP(p,bc);
      if (b != 0) {
        // Keep track of # of keys held down- assume no duplicates or repeated note offs
        if (mkev->down) {
          b->notepresscnt = app->getRP()->GetSampleCnt();
          b->numheld++;
        } else {
          b->numheld--;
          if (b->numheld <= 0) {
            b->numheld = 0;
            if (b->sp == 0) // Only register release time if sustain pedal is released
              b->releasecnt = app->getRP()->GetSampleCnt();
          }
        }

        // printf ("held: %d\n",b->numheld);
      } else
        printf("MIDI: Can't find BypassInfo struct for p/c [%d/%d]\n",p,c);

#if 0
      // For poorly behaved softsynths, double up on Note Offs
      if (!mkev->down) 
        OutputNote(ret = (port == -1 ? mkev->outport-1 : port),
                   (channel == -1 ? mkev->channel : channel),
                   mkev->down,
                   mkev->notenum + fs->transpose,
                   mkev->vel);
#endif
    }
    break;

  default:
    break;
  }

  return ret;
};

void MidiIO::EchoEvent (Event *ev) {
  // Handle MIDI event echos..
  if (ev->GetType() == T_EV_Input_MIDIClock ||
      ev->GetType() == T_EV_Input_MIDIStartStop) {
    // MIDI sync message- send out to all ports set to transmit MIDI sync
    int *xmitports = app->getCFG()->GetMIDISyncOuts(),
      numxmitports = app->getCFG()->GetNumMIDISyncOuts();
    for (int i = 0; i < numxmitports; i++) {
      OutputStartOnPort();
      int port = EchoEventToPortChannel(ev,xmitports[i],-1,-1);
      OutputEndOnPort(port);
    } 
  } else if (!ev->echo) {
    // This event is generated from the configuration explicitly- not
    // an automatic echo of an unhandled MIDI event--
    // So don't send it through our current patch settings.
    // Send to the exact port and channel specified

    OutputStartOnPort();
    int port = EchoEventToPortChannel(ev,-1,-1,-1);
    OutputEndOnPort(port);
  } else {
    // Echo of unused MIDI event- use patch logic
    
    // Handle special note off case- ensure note off sent to right place(s)
    PatchItem *ev_patch = 0;
    int ev_port = 0;
    if (ev->GetType() == T_EV_Input_MIDIKey &&
        !((MIDIKeyInputEvent *) ev)->down &&
        note_patch[((MIDIKeyInputEvent *) ev)->notenum] != 0) {
      // Use patch which was used when note was pressed
      ev_patch = note_patch[((MIDIKeyInputEvent *) ev)->notenum];
      ev_port = note_def_port[((MIDIKeyInputEvent *) ev)->notenum];
    } else {
      // Use current patch for all other events
      ev_patch = curpatch;
      ev_port = echoport;
    }
    
    // DEBUG
    // printf("MIDI: Echo port: %d Patch: %p\n",ev_port,ev_patch);
    
    if (ev_patch != 0 && ev_patch->IsCombi()) {
      // Combi patch- keyboard split into zones, possible multichannel output
      
      int curport = -1;
      for (int i = 0; i < ev_patch->numzones; i++) {
        CombiZone *z = ev_patch->GetZone(i);
        
        int notenum = -1;
        MIDIKeyInputEvent *note = 0;
        if (ev->GetType() == T_EV_Input_MIDIKey) {
          note = (MIDIKeyInputEvent *) ev;
          notenum = note->notenum;
        }
        
        if (notenum == -1 || (notenum >= z->kr_lo && notenum <= z->kr_hi)) {
          int z_port = (z->port_r ? z->port : echoport);
          if (curport == -1 || z_port != curport) {
            // Starting with a new port
            if (curport > 0 && curport <= numouts)
              OutputEndOnPort(curport-1);
            
            if (z_port > 0 && z_port <= numouts)
              OutputStartOnPort();
            curport = z_port;
          }
          
          if (curport > 0 && curport <= numouts)
            EchoEventToPortChannel(ev,curport-1,z->channel,z->bypasschannel);
          else if (curport == 0) {
#if USE_FLUIDSYNTH
            app->getFLUIDP()->ReceiveMIDIEvent(ev);
#endif
          }
        }
      }
    } else {   
      if (ev_port > 0 && ev_port <= numouts) {
        // Single channel output- easy case
        OutputStartOnPort();
        EchoEventToPortChannel(ev,ev_port-1,
                               (echochan != -1 && ev_patch != 0 ? 
                                ev_patch->channel : echochan),ev_patch->bypasschannel);
        OutputEndOnPort(ev_port-1);
      } else if (ev_port == 0) {
#if USE_FLUIDSYNTH
        app->getFLUIDP()->ReceiveMIDIEvent(ev);
#endif
      }
    }
  }
};

void MidiIO::SendBankProgramChangeToPortChannel (int bank, int program, 
                                                 int port, int channel) {
  if (bank != -1) {
    OutputController(port,channel,MIDI_BANKCHANGE_MSB,bank / 128);
    OutputController(port,channel,MIDI_BANKCHANGE_MSB,bank % 128);
  }
  if (program != -1)
    OutputProgramChange(port,channel,program);
};

void MidiIO::SendBankProgramChange (PatchItem *patch) {
  printf("MIDI: Program Change\n");

  if (patch != 0) {
    if (patch->IsCombi()) {
      // Combi, send individual zone bank/patch changes
      for (int i = 0; i < patch->numzones; i++) {
        CombiZone *z = patch->GetZone(i);
        
        int z_port = (z->port_r ? z->port : echoport);
        if (z_port > 0 && z_port <= numouts)
          SendBankProgramChangeToPortChannel(z->bank, z->prog,
                                             z_port-1, z->channel);
      }
    } else {
      // Single patch, send bank/patch change
      if (echoport > 0 && echoport <= numouts) 
        SendBankProgramChangeToPortChannel(patch->bank, patch->prog,
                                           echoport-1, patch->channel);
    }
  }  
};

BypassInfo *MidiIO::getBP(int port, int channel) { 
  // printf("MIDI: getBP: %d/%d\n",port,channel);
  if (port >= 0 && port < app->getCFG()->GetNumMIDIOuts() &&
	  channel >= 0 && channel < MAX_MIDI_CHANNELS)
	  return &bp[port*MAX_MIDI_CHANNELS + channel];
  else
    return 0;
}

void MidiIO::CheckBypass() {
  // Save scanning through bypass info array by only checking periodically (since bypassing unused channels isn't *that* time critical)
  nframes_t now = app->getRP()->GetSampleCnt();
  if (now - lastchecktime > checkfreq) {
    // printf("CHECK!\n");

    // Run through all ports/channels and test time
    BypassInfo *b = bp;
    for (int p = 0; p < app->getCFG()->GetNumMIDIOuts(); p++)
      for (int c = 0; c < MAX_MIDI_CHANNELS; c++, b++) {
        if (b->active && b->bypasscc != 0 && !b->bypassed) {
          // Time since last note press is greater than sustain time?? Then bypass
          if ((b->bypasslen1 > 0 && now - b->notepresscnt >= b->bypasslen1) || 
          // Time after last note release is greater than release time?? Then bypass
              (b->bypasslen2 > 0 && b->sp == 0 && b->numheld == 0 && now - b->releasecnt >= b->bypasslen2)) {
            // Bypass port/channel
            /* printf("dnotepress: %d dnoterelease: %d\n",now - b->notepresscnt, now - b->releasecnt);
            printf("len1: %d len2: %d\n",b->bypasslen1,b->bypasslen2);
            printf("bypass p/c: %d/%d\n",p,c); */
            
            b->bypassed = 1;
            OutputController(p,c,
                             b->bypasscc,
                             127);
          } 
        }
      }      
      
    lastchecktime = now;
  }
};

void MidiIO::CheckUnbypass() {
  // Look at current patch, make sure all used channels are unbypassed
  if (curpatch != 0) {
    if (curpatch->IsCombi()) {
      // Bypass settings
      for (int i = 0; i < curpatch->numzones; i++) {
        CombiZone *z = curpatch->GetZone(i);
        if (z->bypasscc != 0) {
          int z_port = (z->port_r ? z->port : echoport),
            c = (z->bypasschannel == -1 ? z->channel : z->bypasschannel);
          BypassInfo *b = getBP(z_port-1,c);
          if (b != 0) {
            if (b->bypassed) {
              // Unbypass 
              // printf("unbypass p/c: %d/%d\n",echoport-1,echochan);
              b->bypassed = 0;
              OutputController(z_port-1,c,
                               b->bypasscc,
                               0);
            }
          } else
            printf("MIDI: Can't find BypassInfo struct for p/c [%d/%d]\n",z_port-1,c);
        }
      }      
    } else {
      // Bypass settings
      if (curpatch->bypasscc != 0) {
        int p = echoport-1,
          bc = (curpatch->bypasschannel == -1 ? echochan : curpatch->bypasschannel);
        BypassInfo *b = getBP(p,bc);
        if (b != 0) {
          if (b->bypassed) {
            // Unbypass 
            // printf("unbypass p/c: %d/%d\n",echoport-1,echochan);
            b->bypassed = 0;
            OutputController(p,bc,
                             b->bypasscc,
                             0);
          }
        } else
          printf("MIDI: Can't find BypassInfo struct for p/c [%d/%d]\n",echoport-1,echochan);
      }
    }
  }
};