SHARE
TWEET

MIDI Distributor REDUX

charlieslick Dec 7th, 2014 177 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <MIDI.h>
  2. #include "MIDIBuddy.h"
  3.  
  4. // MIDI.getType();
  5. // returns a kMIDIType such as NoteOn or NoteOff
  6.  
  7. // MIDI.getChannel();
  8. // returns the channel of the message stored in the structure 1-16 <----this is unexpected.
  9.  
  10. // MIDI.getData1();
  11. // returns the first peice of data, usually pitch
  12.  
  13. // MIDI.getData2();
  14. // returns the second peice of data, usually velocity
  15.  
  16.  
  17. // creation of a class which is just an array of a midi channel class
  18. MCArray chArray;
  19.  
  20. // initiallizes a the number channels as 4
  21. byte numChannels = 4;
  22. boolean newMidi = true;
  23.  
  24. // these define the channel status feedback pins
  25. // they go high when the channel is currently playing a note
  26.  
  27. const int ledChOne = 5;
  28. const int ledChTwo = 6;
  29. const int ledChThree =7;
  30. const int ledChFour = 8;
  31.  
  32. // these deifne the number of channels to distribute the midi commands to
  33. // if 3 HIGH then 4 Channels, if 4 HIGH then 2 Channels, no HIGH: 3 Channels
  34. // allows use of a simple center off switch to control 3 options
  35. const int selectPinOne = 3;
  36. const int selectPinTwo = 4;
  37.  
  38. // these define the pins used to select the priority algorithm
  39. // if pin 2 is high, then drop note by time, if pin 9 is high then drop high note.
  40. // if no pins high, MONO MODE
  41. const int modeSelectOne = 2;
  42. const int modeSelectTwo = 9;
  43.  
  44.  
  45.  
  46.  
  47. void setup(){
  48.   MIDI.begin(MIDI_CHANNEL_OMNI);
  49.   MIDI.setInputChannel(MIDI_CHANNEL_OMNI);
  50.   MIDI.disconnectCallbackFromType(NoteOn);
  51.   MIDI.disconnectCallbackFromType(NoteOff);
  52.   MIDI.disconnectCallbackFromType(PitchBend);
  53.   MIDI.turnThruOff();
  54.  
  55.   pinMode(ledChOne, OUTPUT);
  56.   pinMode(ledChTwo, OUTPUT);
  57.   pinMode(ledChThree, OUTPUT);
  58.   pinMode(ledChFour, OUTPUT);
  59.  
  60.   pinMode(selectPinOne, INPUT);
  61.   pinMode(selectPinTwo, INPUT);
  62.   pinMode(modeSelectOne, INPUT);
  63.   pinMode(modeSelectTwo, INPUT);
  64.  
  65. }
  66. void loop(){
  67.   byte nxtChan = 0x00;
  68.   numChannels = getNumberOfChannels();
  69.  
  70.   // read next available midi message in cue
  71.   if(MIDI.read()){
  72.     byte curChannel = MIDI.getChannel();
  73.     kMIDIType curCommand = MIDI.getType();
  74.     byte curPitch = MIDI.getData1();
  75.     byte curVelocity = MIDI.getData2();
  76.    
  77.     //comvert 0x00 velocity to note off
  78.     if(curChannel < 5 && curCommand == NoteOn && curVelocity == 0x00){
  79.       curCommand = NoteOff;
  80.     }
  81.    
  82.    
  83.     // if the message is on a channel that the distributor does not use
  84.     // echo the midi message out
  85.     if(curChannel > 4){
  86.       // echos the incoming message back out.
  87.       MIDI.send(curCommand, curPitch, curVelocity, curChannel);
  88.     }
  89.    
  90.     // if the message is within the range of channels the distributor uses but not the incoming
  91.     else if(curChannel > 1){
  92.       return;
  93.     }
  94.    
  95.     // if the distributor is in mono mode
  96.     else if(!digitalRead(modeSelectOne) && !digitalRead(modeSelectTwo)){
  97.       for(int i = 0x00; i < numChannels; i++){
  98.         MIDI.send(curCommand, curPitch, curVelocity, i +1);
  99.        updateLEDs(i, curCommand, ledChOne, ledChTwo, ledChThree, ledChFour);
  100.       }
  101.     }
  102.     // if the message is a pitch bend
  103.     else if(curCommand == PitchBend){
  104.        for(int i = 0x00; i < numChannels; i++){
  105.         MIDI.send(curCommand, curPitch, curVelocity, i +1);
  106.       }
  107.     }
  108.    
  109.     // if the message is note on
  110.     else if(curCommand == NoteOn){
  111.       byte killNote = 0xFF;
  112.       // determine next channel to send on
  113.      
  114.       if(digitalRead(modeSelectOne)){
  115.         nxtChan = chArray.nextNotePlayTime(killNote, numChannels);
  116.       }
  117.       else if(digitalRead(modeSelectTwo)){
  118.         nxtChan = chArray.nextNotePlayPitch(killNote, numChannels);
  119.       }
  120.       // kill channel note of necessary
  121.       if(killNote != 0xFF){
  122.         MIDI.sendNoteOff(killNote, 0x00, nxtChan +1);
  123.       }
  124.       // play note
  125.       MIDI.sendNoteOn(curPitch, curVelocity, nxtChan +1);
  126.       // update the channel array
  127.       chArray.updateStatus(curCommand, curPitch, nxtChan);
  128.       updateLEDs(nxtChan, curCommand, ledChOne, ledChTwo, ledChThree, ledChFour);
  129.        
  130.     }
  131.    
  132.     // if the message is note off
  133.     else if(curCommand == NoteOff){
  134.       // locate matching note currently being played
  135.       nxtChan = chArray.noteOffMatch(curPitch);
  136.      
  137.       // send note off command
  138.       if(nxtChan != 0xFF){
  139.         MIDI.sendNoteOff(curPitch, curVelocity, nxtChan +1);
  140.       }
  141.      
  142.       // update the channel array
  143.       chArray.updateStatus(curCommand, curPitch, nxtChan);
  144.       updateLEDs(nxtChan, curCommand, ledChOne, ledChTwo, ledChThree, ledChFour);
  145.     }
  146.   }
  147. }
  148.  
  149. // updates the feedback LEDs
  150. void updateLEDs(const byte nxtChan, const kMIDIType inCommand, const int& ledChOne, const int& ledChTwo, const int& ledChThree, const int& ledChFour){
  151.   if(inCommand == NoteOn){
  152.     switch (nxtChan){
  153.       case 0x00:
  154.       digitalWrite(ledChOne, HIGH);
  155.       break;
  156.       case 0x01:
  157.       digitalWrite(ledChTwo, HIGH);
  158.       break;
  159.       case 0x02:
  160.       digitalWrite(ledChThree, HIGH);
  161.       break;
  162.       case 0x03:
  163.       digitalWrite(ledChFour, HIGH);
  164.       break;
  165.     }
  166.   }
  167.     if(inCommand == NoteOff){
  168.     switch (nxtChan){
  169.       case 0x00:
  170.       digitalWrite(ledChOne, LOW);
  171.       break;
  172.       case 0x01:
  173.       digitalWrite(ledChTwo, LOW);
  174.       break;
  175.       case 0x02:
  176.       digitalWrite(ledChThree, LOW);
  177.       break;
  178.       case 0x03:
  179.       digitalWrite(ledChFour, LOW);
  180.       break;
  181.     }
  182.   }
  183. }
  184.  
  185. // checks the pins for the selected number of channesl 2,3,4
  186. byte getNumberOfChannels(){
  187.   if(digitalRead(selectPinOne)){
  188.     return 0x04;
  189.   }
  190.   else if(digitalRead(selectPinTwo)){
  191.     return 0x02;
  192.   }
  193.   else{
  194.     return 0x03;
  195.   }
  196. }
  197.  
  198. //compare the that last processed midi data with midi data currently in MIDI que.
RAW Paste Data
Top