aboutsummaryrefslogtreecommitdiff
path: root/src/midiio/src/MidiOutput.cpp
blob: d78460f61a789c890afa0aa5fef4cddbfd7217b7 (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
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
//
// Programmer:    Craig Stuart Sapp <craig@ccrma.stanford.edu> 
// Creation Date: 18 December 1997
// Last Modified: Mon Jan 26 23:54:36 GMT-0800 1998
// Last Modified: Tue Feb  2 08:30:28 PST 1999
// Last Modified: Sun Jul 18 18:52:29 PDT 1999 (added RPN functions)
// Last Modified: Sun Dec  9 15:01:33 PST 2001 (switched con/des code)
// Filename:      ...sig/code/control/MidiOutput/MidiOutput.cpp
// Web Address:   http://sig.sapp.org/src/sig/MidiOutput.cpp
// Syntax:        C++
//
// Description:   The MIDI output interface for MIDI synthesizers/equipment
//                which has many convienience functions defined for
//                various types of MIDI output.
//

#include "MidiOutput.h"
#include <iostream>
#include <iomanip>

#define RECORD_ASCII     (0)
#define RECORD_BINARY    (1)
#define RECORD_MIDI_FILE (2)


// declaration of static variables
SigTimer    MidiOutput::timer;
Array<int>* MidiOutput::rpn_lsb_status = NULL;
Array<int>* MidiOutput::rpn_msb_status = NULL;
int         MidiOutput::objectCount    = 0;


//////////////////////////////
//
// MidiOutput::MidiOutput --
//


MidiOutput::MidiOutput(void) : MidiOutPort() {
   outputRecordQ = 0;

   if (objectCount == 0) {
      initializeRPN();
   }
   objectCount++;
}



MidiOutput::MidiOutput(int aPort, int autoOpen) : MidiOutPort(aPort, autoOpen) {
   outputRecordQ = 0;

   if (objectCount == 0) {
      initializeRPN();
   }
   objectCount++;
}



//////////////////////////////
//
// MidiOutput::~MidiOutput
//

MidiOutput::~MidiOutput() {
   objectCount--;
   if (objectCount == 0) {
      deinitializeRPN();
   } else if (objectCount < 0) {
      std::cout << "Error in MidiOutput decontruction" << std::endl; 
   }

}



//////////////////////////////
//
// MidiOutput::cont -- send a controller command MIDI message.
//
//    channel = the Midi channel ofset from 0 [0..15]
//    controller = the continuous controller number [0..127]
//    data = the value of the specified controller [0..127]
//

int MidiOutput::cont(int channel, int controller, int data) {
   return send(0xb0 | (channel & 0x0f), controller, data);
}



//////////////////////////////
//
// MidiOutput::off -- sends a Note Off MIDI message (0x80).
//
//    channel = MIDI channel to send note on. range is [0..15]
//    keynum = MIDI key number to play (middle C = 60, C# = 61, etc.) [0..127]
//    velocity = release velocity of the note, 127 = quickest possible
//
// Note: The more common method of turning off a note is to use the
//	play() function (midi command 0x90) but send an attack velocity of 0.
//

int MidiOutput::off(int channel, int keynum, int releaseVelocity) {
   return send(0x80 | (channel & 0x0f), keynum, releaseVelocity);
}



//////////////////////////////
//
// MidiOutput::pc -- send a patch change MIDI message. changes the timbre
//    on the specified channel.  
//
//    channel = MIDI channel to which to send the patch change [0..15]
//    timbre = the voice to select on the specified channel [0..127]
//

int MidiOutput::pc(int channel, int timbre) {
   return send(0xc0 | (channel & 0x0f), timbre);
}



//////////////////////////////
//
// MidiOutput::play -- sends a Note On/Off MIDI message.
//
//    channel = MIDI channel to send note on. range is [0..15]
//    keynum = MIDI key number to play (middle C = 60, C# = 61, etc.) [0..127]
//    velocity = attack velocity of the note, 0 = 0ff, 127 = loudest possible
//

int MidiOutput::play(int channel, int keynum, int velocity) {
   return send(0x90 | (channel & 0x0f), keynum, velocity);
}



//////////////////////////////
//
// MidiOutput::pw -- Pitch Wheel: send a MIDI pitch bend.
//      Parameters are:
//         1. channel   -- MIDI channel offset from 0.
//         2. mostByte  -- most significant 7 bits (coarse tuning)
//         3. leastByte -- least significant 7 bits (fine tuning)
//

int MidiOutput::pw(int channel, int mostByte, int leastByte) {
   return send(0xe0 | (channel & 0x0f), leastByte, mostByte);
}



//////////////////////////////
//
// MidiOutput::pw -- Pitch Wheel: 14 bit number given as input
//      range for 14 bit number is 0 to 16383.
//

int MidiOutput::pw(int channel, int tuningData) {
   uchar greaterBits = (uchar)((tuningData >> 7) & 0x7f);
   uchar lesserBits = (uchar)(tuningData & 0x7f);
   return pw(channel, greaterBits, lesserBits);
}



//////////////////////////////
//
// MidiOutput::pw -- Pitch Wheel: range between -1 to 1 given as input.
//      Range is then converted to a 14 bit number.
//      +1 = highest value of pitch wheel
//       0 = rest position of pitch wheel
//      -1 = lowest value of pitch wheel
//

int MidiOutput::pw(int channel, double tuningData) {
   if (tuningData < -1.0 || tuningData > 1.0) {
      std::cerr << "Error: pitch wheel data is out of range: " << tuningData << std::endl;
      exit(1);
   }

   int output = (int)((tuningData+1.0)/2.0*16383 + 0.5);
   return pw(channel, output);
}



//////////////////////////////
//
// MidiOutput::recordStart
//

void MidiOutput::recordStart(char *filename, int format) {
   if (outputRecordQ) {  // already recording, so close old file
      recordStop();
   }

   outputRecordFile.open(filename, std::ios::out);
   if (!outputRecordFile) {   // open file failed
      std::cerr << "Error: cannot open file " << filename << std::endl;
      outputRecordQ = 0;
   } else {
      outputRecordQ = 1;
   }

   if (outputRecordQ) {
      switch (format) {
         case RECORD_ASCII:   // ascii
            outputRecordType = RECORD_ASCII;
            outputRecordFile <<"; delta time/MIDI output at delta time" << std::endl;
            break;
         case RECORD_BINARY:   // binary
            outputRecordType = RECORD_BINARY;
            // record the magic number for binary format
            outputRecordFile << (uchar)0xf8 << (uchar)0xf8
                       << (uchar)0xf8 << (uchar)0xf8;
            break;
         case RECORD_MIDI_FILE: // standard MIDI file, type 0
         default:
            outputRecordType = RECORD_MIDI_FILE;
            // header stuff to be written here
            break;
      }
   }

   lastFlushTime = timer.getTime();
}



//////////////////////////////
//
// MidiOutput::recordStop
//

void MidiOutput::recordStop(void) {
   if (outputRecordQ) {
      outputRecordQ = 0;
      outputRecordFile.close();
   }
}



//////////////////////////////
//
// MidiOutput::reset -- sends the MIDI command 0xFF which
//      should force the MIDI devices on the other side of the
//      MIDI cable into their power-on reset condition, clear running
//      status, turn off any sounding notes, set Local Control on, and
//      otherwise clean up the state of things.
//

void MidiOutput::reset(void) {
   send(0xff, 0, 0);
}



//////////////////////////////
//
// MidiOutput::send -- send a byte to the MIDI port but record it
//	first.
//

int MidiOutput::send(int command, int p1, int p2) {
   if (outputRecordQ) {
      switch (outputRecordType) {
         case 0:   // ascii
            writeOutputAscii(command, p1, p2);
            break;
         case 1:   // binary
            writeOutputBinary(command, p1, p2);
            break;
         case 2:   // standard MIDI file type 0
            writeOutputMidifile(command, p1, p2);
            break;
      }
      lastFlushTime = timer.getTime();  // only keep track if recording
   }
   return rawsend(command, p1, p2);
}


int MidiOutput::send(int command, int p1) {
   if (outputRecordQ) {
      switch (outputRecordType) {
         case 0:   // ascii
            writeOutputAscii(command, p1, -1);
            break;
         case 1:   // binary
            writeOutputBinary(command, p1, -1);
            break;
         case 2:   // standard MIDI file type 0
            writeOutputMidifile(command, p1, -1);
            break;
      }
      lastFlushTime = timer.getTime();  // only keep track if recording
   }
   return rawsend(command, p1);
}


int MidiOutput::send(int command) {
   if (outputRecordQ) {
      switch (outputRecordType) {
         case 0:   // ascii
            writeOutputAscii(command, -1, -1);
            break;
         case 1:   // binary
            writeOutputBinary(command, -1, -1);
            break;
         case 2:   // standard MIDI file type 0
            writeOutputMidifile(command, -1, -1);
            break;
      }
      lastFlushTime = timer.getTime();  // only keep track if recording
   }
   return rawsend(command);
}



//////////////////////////////
//
// MidiOutput::silence -- send a note off to all notes on all channels.
//    default value: aChannel = -1
//

void MidiOutput::silence(int aChannel) {
   int keyno;
   if (aChannel == -1) {
      for (int channel=0; channel<16; channel++) {
         for (keyno=0; keyno<128; keyno++) {
            play(channel, keyno, 0);
         }
      }
   } else {
      for (keyno=0; keyno<128; keyno++) {
         play(aChannel, keyno, 0);
      }
   }
}



//////////////////////////////
//
// MidiOutput::sustain -- set the MIDI sustain continuous controller on or off.
//	Equivalent to the command cont(channel, 0x40, status).
//

void MidiOutput::sustain(int channel, int status) {
   if (status) {  // turn on sustain
      cont(channel, 0x40, 127);
   } else {       // turn off sustain
      cont(channel, 0x40, 0);
   }
}



///////////////////////////////
//
// MidiOutput::sysex -- sends a system exclusive MIDI message.
//	you must supply the 0xf0 at the start of the array
//	and the 0xf7 at the end of the array.
//

int MidiOutput::sysex(char* data, int length) {
   return rawsend((uchar*)data, length);
}


int MidiOutput::sysex(uchar* data, int length) {
   return rawsend(data, length);
}


///////////////////////////////////////////////////////////////////////////
//
// RPN functions
//


//////////////////////////////
//
// NRPN -- sends a Non-registered parameter number where:
//   parameter #1: channel (0-15)
//   parameter #2: NRPN MSB indicator (controller #99 data)
//   parameter #3: NRPN LSB indicator (controller #98 data)
//   parameter #4: NRPN MSB data      (controller #6  data)
//  [parameter #5: NRPN LSB data      (controller #38 data)]
// or:
//   parameter #1: channel (0-15)
//   parameter #2: NRPN MSB indicator (controller #99 data)
//   parameter #3: NRPN LSB indicator (controller #98 data)
//   parameter #4: NRPN Floating point in range (-1..1) (ccont#6 and #38 data)
//
//
// NRPN (Non-registered parameter number) -- General MIDI and 
//    Extended MIDI mess.  It becomes the receiving synthesizer's
//    responsibility to determine the meaning of continuous
//    controller (ccont) #6 from data sent with cconts #98,99 (for
//    NRPNS) and cconts #100,101 (for RPNS).  NRPN parameters
//    are not reset when the ccont#121 is sent to reset controllers.
//
// NRPN's are "non-standard" meaning that any synthesizer could
//    do whatever they want with a given NRPN;  However, the 
//    GS and XG specifications are given in the table further below.
//
// The data for NRPNs are transfered to synthesizer with 
//    data slider ccont #6(MSB) and ccont #38(LSB).  Also data increment
//    ccont#96 (data increment) and ccont#97 (data decrement) are in
//    relation to the RPN or NRPN in effect.  Increment and Decrement
//    are not recommend to use with RPN's because of confusion in the
//    MIDI industry over which 7-bit data (#6 or #38) to increment.
//  
// Once you have selected an NRPN on a given channel, the
//    channel will apply subsequent Data Entry to the 
//    selected parameter.  After making the necessary settings
//    you should set NRPN to NULL to reduce the risk of 
//    operational errors.  a NUL RPN will disable the previous
//    values of either RPN or NRPN data.
// 
// The following NRPN values are supported in Yamaha's XG specification:
//    CCont #98 = LSB of NRPN parameter ID
//    CCont #99 = MSB of NRPN parameter ID
// 
// NRPN
// MSB LSB                           Data Range
// #99 #98    Parameter              (ccont#6=MSB, ccont#38=LSB)
// === ===== ====================== ======================
//   1   8  Vibrato Rate            -64.. 0..+63 logical range or (-50..+50)
//                                    0..64..127 MIDI data range  ( 14..114)
//   1   9  Vibrato Depth           same ranges as above      
//   1  10  Vibrato Delay           same ranges as above      
//   1  32  Filter Cutoff Freq.     same ranges as above      
//   1  33  Filter Resonance        same ranges as above      
//   1  99  EG Attack Time          same ranges as above      
//   1 100  EG Decay Time           same ranges as above      
//   1 102  EG Release Time         same ranges as above
//  20  xx  Drum Filter Cutoff Freq same ranges as above          
//          xx = drum MIDI key number
//  21  xx  Drum Filter Resonance   same ranges as above          
//          xx = drum MIDI key number
//  22  xx  Drum EG Attack Rage     same ranges as above          
//          xx = drum MIDI key number
//  23  xx  Drum EG Decay Rate      same ranges as above          
//          xx = drum MIDI key number
//  24  xx  Drum Pitch Coarse       same ranges as above          
//          xx = drum MIDI key number
//  25  xx  Drum Pitch Fine         same ranges as above          
//          xx = drum MIDI key number
//  26  xx  Drum Level                0..64..127 MIDI data range
//          xx = drum MIDI key number
//  28  xx  Drum Pan                  Random, Left..Center..Right
//                                    0.......1.....64......127 MIDI data range
//          xx = drum MIDI key number
//  29  xx  Drum Reverb Send Level    0..64..127 MIDI data range
//          xx = drum MIDI key number
//  30  xx  Drum Chorus Send Level    0..64..127 MIDI data range
//          xx = drum MIDI key number
//  31  xx  Drum Variation Send Level 0..64..127 MIDI data range
//          xx = drum MIDI key number
// 127 127  Null RPN (disables RPN/NRPN parameters from being altered).
//
//

int MidiOutput::NRPN(int channel, int nrpn_msb, int nrpn_lsb, 
      int data_msb, int data_lsb) {
   channel  = channel  & 0x0f;
   nrpn_msb = nrpn_msb & 0x7f;
   nrpn_lsb = nrpn_msb & 0x7f;
   data_msb = nrpn_msb & 0x7f;
   data_lsb = nrpn_msb & 0x7f;
 
   int status = 1;

   // check to see if the nrpn_msb and nrpn_lsb are the same
   // as the last call to this function, if not, then send
   // the appropriate MIDI controller values.
   if (rpn_msb_status[getPort()][channel] != nrpn_msb) {
      status &= cont(channel, 99, nrpn_msb);
      rpn_msb_status[getPort()][channel] = nrpn_msb;
   }
   if (rpn_lsb_status[getPort()][channel] != nrpn_lsb) {
      status &= cont(channel, 98, nrpn_lsb);
      rpn_lsb_status[getPort()][channel] = nrpn_lsb;
   }

   // now that the NRPN state is set, send the NRPN data values
   // but do not bother sending any data if the Null RPN is in effect.
   if (nrpn_msb != 127 && nrpn_lsb != 127) {
      status &= cont(channel, 6, data_msb);
      status &= cont(channel, 38, data_msb);
   }

   return status;
}


int MidiOutput::NRPN(int channel, int nrpn_msb, int nrpn_lsb, int data_msb) {
   channel  = channel  & 0x0f;
   nrpn_msb = nrpn_msb & 0x7f;
   nrpn_lsb = nrpn_msb & 0x7f;
   data_msb = nrpn_msb & 0x7f;
 
   int status = 1;

   // check to see if the nrpn_msb and nrpn_lsb are the same
   // as the last call to this function, if not, then send
   // the appropriate MIDI controller values.
   if (rpn_msb_status[getPort()][channel] != nrpn_msb) {
      status &= cont(channel, 99, nrpn_msb);
      rpn_msb_status[getPort()][channel] = nrpn_msb;
   }
   if (rpn_lsb_status[getPort()][channel] != nrpn_lsb) {
      status &= cont(channel, 98, nrpn_lsb);
      rpn_lsb_status[getPort()][channel] = nrpn_lsb;
   }

   // now that the NRPN state is set, send the NRPN data value,
   // but do not bother sending any data if the Null RPN is in effect.
   if (nrpn_msb != 127 && nrpn_lsb != 127) {
      status &= cont(channel, 6, data_msb);
   }

   return status;
}
 

int MidiOutput::NRPN(int channel, int nrpn_msb, int nrpn_lsb, double data) {
   channel  = channel  & 0x0f;
   nrpn_msb = nrpn_msb & 0x7f;
   nrpn_lsb = nrpn_msb & 0x7f;
   if (data < -1.0) {
      data = -1.0;
   } else if (data > 1.0) {
      data = 1.0;
   }

   int status = 1;

   // check to see if the nrpn_msb and nrpn_lsb are the same
   // as the last call to this function, if not, then send
   // the appropriate MIDI controller values.
   if (rpn_msb_status[getPort()][channel] != nrpn_msb) {
      status &= cont(channel, 99, nrpn_msb);
      rpn_msb_status[getPort()][channel] = nrpn_msb;
   }
   if (rpn_lsb_status[getPort()][channel] != nrpn_lsb) {
      status &= cont(channel, 98, nrpn_lsb);
      rpn_lsb_status[getPort()][channel] = nrpn_lsb;
   }

   // convert data into 14 bit number
   int data14 = (int)((data+1.0)/2.0*16383 + 0.5);

   // send the NRPN data values, two message of 7 bits each
   // but do not bother sending any data if the Null RPN is in effect.
   if (nrpn_msb != 127 && nrpn_lsb != 127) {
      status &= cont(channel,  6, data14 >> 7);
      status &= cont(channel, 38, data14 & 0x7f);
   }

   return status;
}


//////////
//
// Convenience functions for use of NRPN function.  Note that these
// are "Non-Registered" Parameter Numbers which means that each
// synthesizer manufacture can do whatever they want, so these
// functions might not behave the way you expect them to do so.
// Yamaha XG and Roland GS NRPN specifications are given below.
//

int MidiOutput::NRPN_null(int channel) {
   return NRPN(channel, 127, 127, 0);
}

int MidiOutput::NRPN_vibratoRate(int channel, int value) {
   // value in range -64..+63
   return NRPN(channel, 1, 8, value+64);
}

int MidiOutput::NRPN_vibratoRate(int channel, double value) {
   // value in range -1.0..+1.0
   return NRPN(channel, 1, 8, value);
}

int MidiOutput::NRPN_vibratoDepth(int channel, int value) {
   // value in range -64..+63
   return NRPN(channel, 1, 9, value+64);
}

int MidiOutput::NRPN_vibratoDepth(int channel, double value) {
   // value in range -1.0..+1.0
   return NRPN(channel, 1, 9, value);
}

int MidiOutput::NRPN_vibratoDelay(int channel, int value) {
   // value in range -64..+63
   return NRPN(channel, 1, 32, value+64);
}

int MidiOutput::NRPN_vibratoDelay(int channel, double value) {
   // value in range -1.0..+1.0
   return NRPN(channel, 1, 32, value);
}

int MidiOutput::NRPN_filterCutoff(int channel, int value) {
   // value in range -64..+63
   return NRPN(channel, 1, 33, value+64);
}

int MidiOutput::NRPN_filterCutoff(int channel, double value) {
   // value in range -1.0..+1.0
   return NRPN(channel, 1, 33, value);
}

int MidiOutput::NRPN_attack(int channel, int value) {
   // value in range -64..+63
   return NRPN(channel, 1, 99, value+64);
}

int MidiOutput::NRPN_attack(int channel, double value) {
   // value in range -1.0..+1.0
   return NRPN(channel, 1, 99, value);
}

int MidiOutput::NRPN_decay(int channel, int value) {
   // value in range -64..+63
   return NRPN(channel, 1, 100, value+64);
}

int MidiOutput::NRPN_decay(int channel, double value) {
   // value in range -1.0..+1.0
   return NRPN(channel, 1, 100, value);
}

int MidiOutput::NRPN_release(int channel, int value) {
   // value in range -64..+63
   return NRPN(channel, 1, 102, value+64);
}

int MidiOutput::NRPN_release(int channel, double value) {
   // value in range -1.0..+1.0
   return NRPN(channel, 1, 102, value);
}

int MidiOutput::NRPN_drumFilterCutoff(int drum, int value) {
   // value in range -64..+63
   return NRPN(9, 20, drum, value+64);
}

int MidiOutput::NRPN_drumFilterCutoff(int drum, double value) {
   // value in range -1.0..+1.0
   return NRPN(9, 20, drum, value);
}

int MidiOutput::NRPN_drumFilterResonance(int drum, int value) {
   // value in range -64..+63
   return NRPN(9, 21, drum, value+64);
}

int MidiOutput::NRPN_drumFilterResonance(int drum, double value) {
   // value in range -1.0..+1.0
   return NRPN(9, 21, drum, value);
}

int MidiOutput::NRPN_drumAttack(int drum, int value) {
   // value in range -64..+63
   return NRPN(9, 22, drum, value+64);
}

int MidiOutput::NRPN_drumAttack(int drum, double value) {
   // value in range -1.0..+1.0
   return NRPN(9, 22, drum, value);
}

int MidiOutput::NRPN_drumDecay(int drum, int value) {
   // value in range -64..+63
   return NRPN(9, 23, drum, value+64);
}

int MidiOutput::NRPN_drumDecay(int drum, double value) {
   // value in range -1.0..+1.0
   return NRPN(9, 23, drum, value);
}

int MidiOutput::NRPN_drumPitch(int drum, int value) {
   // value in range -64..+63
   return NRPN(9, 24, drum, value+64);
}

int MidiOutput::NRPN_drumPitch(int drum, double value) {
   // value in range -1.0..+1.0
   return NRPN(9, 24, drum, value);
}

int MidiOutput::NRPN_drumLevel(int drum, int value) {
   // value in range -64..+63
   return NRPN(9, 26, drum, value+64);
}

int MidiOutput::NRPN_drumLevel(int drum, double value) {
   // value in range -1.0..+1.0
   return NRPN(9, 26, drum, value);
}

int MidiOutput::NRPN_drumPan(int drum, int value) {
   return NRPN(9, 28, drum, value+64);
}

int MidiOutput::NRPN_drumPan(int drum, double value) {
   // value in range -1.0..+1.0
   return NRPN(9, 28, drum, value);
}

int MidiOutput::NRPN_drumReverb(int drum, int value) {
   // note offset from 0 not -64
   return NRPN(9, 29, drum, value);
}

int MidiOutput::NRPN_drumReverb(int drum, double value) {
   // value in range -1.0..+1.0
   return NRPN(9, 29, drum, value);
}

int MidiOutput::NRPN_drumChorus(int drum, int value) {
   // note offset from 0 not -64
   return NRPN(9, 30, drum, value);
}

int MidiOutput::NRPN_drumChorus(int drum, double value) {
   // value in range -1.0..+1.0
   return NRPN(9, 30, drum, value);
}

int MidiOutput::NRPN_drumVariation(int drum, int value) {
   // note offset from 0 not -64
   return NRPN(9, 31, drum, value);
}

int MidiOutput::NRPN_drumVariation(int drum, double value) {
   // value in range -1.0..+1.0
   return NRPN(9, 31, drum, value);
}

//
// Convenience functions for use of NRPN function.
//
//////////



//////////////////////////////
//
// RPN -- sends a registered parameter number where:
//   parameter #1: channel (0-15)
//   parameter #2: RPN MSB indicator (controller #101 data)
//   parameter #3: RPN LSB indicator (controller #100 data)
//   parameter #4: RPN MSB data      (controller #6  data)
//  [parameter #5: RPN LSB data      (controller #38 data)]
// or:
//   parameter #1: channel (0-15)
//   parameter #2: NRPN MSB indicator (controller #99 data)
//   parameter #3: NRPN LSB indicator (controller #98 data)
//   parameter #4: NRPN Floating point in range (-1..1) (ccont#6 and #38 data)
//
//
// RPN (registered parameter number) -- General MIDI and 
//    Extended MIDI mess.  It becomes the receiving synthesizer's
//    responsibility to determine the meaning of continuous
//    controller (ccont) #6 from data sent with cconts #100,101 (for
//    RPNS) and cconts #98,99 (for NRPNS).  
//
// The data for RPNs are transfered to synthesizer with 
//    data slider ccont #6(MSB) and ccont #38(LSB).  Also data increment
//    ccont#96 (data increment) and ccont#97 (data decrement) are in
//    relation to the RPN or NRPN in effect.  Increment and Decrement
//    are not recommend to use with RPN's because of confusion in the
//    MIDI industry over which 7-bit data (#6 or #38) to increment.
//  
// Once you have selected an RPN on a given channel, the
//    channel will apply subsequent Data Entry to the 
//    selected parameter.  After making the necessary settings
//    you should set RPN's to NULL to reduce the risk of 
//    operational errors.  a NULL RPN will disable the previous
//    values of either RPN or NRPN data.
// 
// The following RPN values are registered:
//    CCont #100 = LSB of RPN parameter ID
//    CCont #101 = MSB of RPN parameter ID
// 
// RPN                               Data Range
// MSB LSB     Parameter              (ccont#6=MSB, ccont#38=LSB)
// === ===== ====================== ======================

//   0   0   Pitchbend Sensitivity  0-127 (default 2) (LSB ignored)
//                                  (The number of +/- half steps in
//                                  pitch wheel range).
//   0   1   Fine Tune              -64.. 0..+63 logical range
//                                    0..64..127 MIDI data range
//   0   2   Coarse Tune            same range as above.
//   0   3   Change Tuning Program  0..127
//   0   4   Change Tuning Bank     0..127
//   

int MidiOutput::RPN(int channel, int rpn_msb, int rpn_lsb, 
      int data_msb, int data_lsb) {
   channel  = channel & 0x0f;
   rpn_msb  = rpn_msb & 0x7f;
   rpn_lsb  = rpn_msb & 0x7f;
   data_msb = rpn_msb & 0x7f;
   data_lsb = rpn_msb & 0x7f;
 
   int status = 1;

   // check to see if the rpn_msb and rpn_lsb are the same
   // as the last call to this function, if not, then send
   // the appropriate MIDI controller values.
   if (rpn_msb_status[getPort()][channel] != rpn_msb) {
      status &= cont(channel, 101, rpn_msb);
      rpn_msb_status[getPort()][channel] = rpn_msb;
   }
   if (rpn_lsb_status[getPort()][channel] != rpn_lsb) {
      status &= cont(channel, 100, rpn_lsb);
      rpn_lsb_status[getPort()][channel] = rpn_lsb;
   }

   // now that the RPN state is set, send the RPN data values
   // but do not bother sending any data if the Null RPN is in effect.
   if (rpn_msb != 127 && rpn_lsb != 127) {
      status &= cont(channel, 6, data_msb);
      status &= cont(channel, 38, data_msb);
   }

   return status;
}


int MidiOutput::RPN(int channel, int rpn_msb, int rpn_lsb, int data_msb) {
   channel  = channel & 0x0f;
   rpn_msb  = rpn_msb & 0x7f;
   rpn_lsb  = rpn_msb & 0x7f;
   data_msb = rpn_msb & 0x7f;
 
   int status = 1;

   // check to see if the rpn_msb and rpn_lsb are the same
   // as the last call to this function, if not, then send
   // the appropriate MIDI controller values.
   if (rpn_msb_status[getPort()][channel] != rpn_msb) {
      status &= cont(channel, 101, rpn_msb);
      rpn_msb_status[getPort()][channel] = rpn_msb;
   }
   if (rpn_lsb_status[getPort()][channel] != rpn_lsb) {
      status &= cont(channel, 100, rpn_lsb);
      rpn_lsb_status[getPort()][channel] = rpn_lsb;
   }

   // now that the RPN state is set, send the RPN data value,
   // but do not bother sending any data if the Null RPN is in effect.
   if (rpn_msb != 127 && rpn_lsb != 127) {
      status &= cont(channel, 6, data_msb);
   }

   return status;
}
 

int MidiOutput::RPN(int channel, int rpn_msb, int rpn_lsb, double data) {
   channel = channel & 0x0f;
   rpn_msb = rpn_msb & 0x7f;
   rpn_lsb = rpn_msb & 0x7f;
   if (data < -1.0) {
      data = -1.0;
   } else if (data > 1.0) {
      data = 1.0;
   }

   int status = 1;

   // check to see if the rpn_msb and rpn_lsb are the same
   // as the last call to this function, if not, then send
   // the appropriate MIDI controller values.
   if (rpn_msb_status[getPort()][channel] != rpn_msb) {
      status &= cont(channel, 101, rpn_msb);
      rpn_msb_status[getPort()][channel] = rpn_msb;
   }
   if (rpn_lsb_status[getPort()][channel] != rpn_lsb) {
      status &= cont(channel, 100, rpn_lsb);
      rpn_lsb_status[getPort()][channel] = rpn_lsb;
   }

   // convert data into 14 bit number
   int data14 = (int)((data+1.0)/2.0*16383 + 0.5);

   // send the RPN data values, two message of 7 bits each
   // but do not bother sending any data if the Null RPN is in effect.
   if (rpn_msb != 127 && rpn_lsb != 127) {
      status &= cont(channel,  6, data14 >> 7);
      status &= cont(channel, 38, data14 & 0x7f);
   }

   return status;
}


//////////
//
// Convenience functions for use of RPN function. 
//

int MidiOutput::RPN_null(void) {
   int status = 1;
   for (int i=0; i<16; i++) {
      status &= RPN_null(i);
   }
   return status;
}

int MidiOutput::RPN_null(int channel) {
   return RPN(channel, 127, 127, 0);
}

int MidiOutput::pbRange(int channel, int steps) {
   // default value for pitch bend sensitivity is 2 semitones.
   return RPN(channel, 0, 0, steps);
}

int MidiOutput::tuneFine(int channel, int cents) {
   // data from -64 to + 63
   return RPN(channel, 0, 1, cents+64);
}

int MidiOutput::fineTune(int channel, int cents) {
   return tuneFine(channel, cents);
}

int MidiOutput::tuneCoarse(int channel, int steps) {
   // data from -64 to + 63
   return RPN(channel, 0, 1, steps+64);
}

int MidiOutput::coarseTune(int channel, int steps) {
   return tuneCoarse(channel, steps);
}

int MidiOutput::tuningProgram(int channel, int program) {
   return RPN(channel, 0, 3, program);
}

int MidiOutput::tuningBank(int channel, int bank) {
   return RPN(channel, 0, 4, bank);
}


///////////////////////////////////////////////////////////////////////////
//
// private functions
//


//////////////////////////////
//
// MidiOutput::initializeRPN -- set up the RPN status arrays
//   ignores initiaization request if already initialized.
//

void MidiOutput::initializeRPN(void) {
   int i, channel;

   if (rpn_lsb_status == NULL) {
      rpn_lsb_status = new Array<int>[getNumPorts()];
      for (i=0; i<getNumPorts(); i++) {
         rpn_lsb_status[i].setSize(16);      
         rpn_lsb_status[i].allowGrowth(0);      
         for (channel=0; channel<16; channel++) {
            rpn_lsb_status[i][channel] = 127;
         }
      }
   }

   if (rpn_msb_status == NULL) {
      rpn_msb_status = new Array<int>[getNumPorts()];
      for (i=0; i<getNumPorts(); i++) {
         rpn_msb_status[i].setSize(16);      
         rpn_msb_status[i].allowGrowth(0);      
         for (channel=0; channel<16; channel++) {
            rpn_msb_status[i][channel] = 127;
         }
      }
   }
}



//////////////////////////////
//
// MidiOutput::deinitializeRPN -- destroy the RPN status arrays
//    do nothing if the arrays are not initialized
//

void MidiOutput::deinitializeRPN(void) {
   if (rpn_msb_status != NULL) {
      delete [] rpn_msb_status;
      rpn_msb_status = NULL;
   }

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



//////////////////////////////
//
// MidiOutput::writeOutputAscii
//

void MidiOutput::writeOutputAscii(int command, int p1, int p2) {
   outputRecordFile << std::dec;
   outputRecordFile.width(6);
   outputRecordFile << (timer.getTime()-lastFlushTime) <<'\t';
   outputRecordFile << "0x" << std::hex;
   outputRecordFile.width(2);
   outputRecordFile << command << ' ';
   outputRecordFile << std::dec; 
   outputRecordFile.width(3);
   outputRecordFile << p1 << ' ';
   outputRecordFile << std::dec; 
   outputRecordFile.width(3);
   outputRecordFile<< p2;
   outputRecordFile << std::endl;
}



//////////////////////////////
//
// MidiOutput::writeOutputBinary
//

void MidiOutput::writeOutputBinary(int command, int p1, int p2) {
   // don't store 0xf8 command since it will be used to mark the end of the 
   if (command == 0xf8) return;

   // write the delta time (four bytes)
   outputRecordFile.writeBigEndian((ulong)(timer.getTime() - lastFlushTime));

   // write midi data 
   // don't store 0xf8 command since it will be used to mark the end of the 
   // delta time data.
   outputRecordFile << (uchar)p1;
   outputRecordFile << (uchar)p2;
   outputRecordFile << (uchar)0xf8;
}



//////////////////////////////
//
// MidiOutput::writeOutputMidifile
//

void MidiOutput::writeOutputMidifile(int command, int p1, int p2) {
   // not yet implemented
}



// md5sum:	1c518e5130ac9ba0d79c4e9ce7fa41cf  - MidiOutput.cpp =css= 20030102