Advertisement
charlieslick

midiDistributorREDO

Oct 30th, 2014
176
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 6.14 KB | None | 0 0
  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
  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. // these are used to make sure incoming midi is new message
  45. kMIDIType prevType = InvalidType;
  46. byte prevPitch = 0x00;
  47. byte prevVelocity = 0x00;
  48.  
  49.  
  50. void setup(){
  51.   MIDI.begin(MIDI_CHANNEL_OMNI);
  52.   MIDI.setInputChannel(MIDI_CHANNEL_OMNI);
  53.   MIDI.disconnectCallbackFromType(NoteOn);
  54.   MIDI.disconnectCallbackFromType(NoteOff);
  55.   MIDI.disconnectCallbackFromType(PitchBend);
  56.   MIDI.turnThruOff();
  57.  
  58.   pinMode(ledChOne, OUTPUT);
  59.   pinMode(ledChTwo, OUTPUT);
  60.   pinMode(ledChThree, OUTPUT);
  61.   pinMode(ledChFour, OUTPUT);
  62.  
  63.   pinMode(selectPinOne, INPUT);
  64.   pinMode(selectPinTwo, INPUT);
  65.   pinMode(modeSelectOne, INPUT);
  66.   pinMode(modeSelectTwo, INPUT);
  67.  
  68. }
  69. void loop(){
  70.   byte nxtChan = 0x00;
  71.   numChannels = getNumberOfChannels();
  72.  
  73.   // read next available midi message in cue
  74.   MIDI.read();
  75.  
  76.   newMidi = isNewMidi(prevType, prevPitch, prevVelocity, MIDI.getType(),MIDI.getData1(),MIDI.getData2());
  77.   if(newMidi){
  78.     prevType = MIDI.getType();
  79.     prevPitch= MIDI.getData1();
  80.     prevVelocity=MIDI.getData2();
  81.    
  82.     // if the message is on a channel that the distributor does not use
  83.     // echo the midi message out
  84.     if(MIDI.getChannel() > 0x03){
  85.       // echos the incoming message back out.
  86.       MIDI.send(MIDI.getType(), MIDI.getData1(), MIDI.getData2(), MIDI.getChannel());
  87.     }
  88.    
  89.     // if the message is within the range of channels the distributor uses
  90.     else if(MIDI.getChannel() > 0x00){
  91.     }
  92.    
  93.     // if the distributor is in mono mode
  94.     else if(!digitalRead(modeSelectOne) && !digitalRead(modeSelectTwo)){
  95.       for(int i = 0x00; i < numChannels; i++){
  96.         MIDI.send(MIDI.getType(), MIDI.getData1(), MIDI.getData2(), i);
  97.        updateLEDs(i, MIDI.getType(), ledChOne, ledChTwo, ledChThree, ledChFour);
  98.       }
  99.     }
  100.     // if the message is a pitch bend
  101.     else if(MIDI.getType() == PitchBend){
  102.        for(int i = 0x00; i < numChannels; i++){
  103.         MIDI.send(MIDI.getType(), MIDI.getData1(), MIDI.getData2(), i);
  104.       }
  105.     }
  106.    
  107.     // if the message is note on
  108.     else if(MIDI.getType() == NoteOn && MIDI.getData2() != 0x00){
  109.       byte killNote = 0x00;
  110.       // determine next channel to send on
  111.      
  112.       if(digitalRead(modeSelectOne)){
  113.         nxtChan = chArray.nextNotePlayTime(killNote, numChannels);
  114.       }
  115.       else if(digitalRead(modeSelectTwo)){
  116.         nxtChan = chArray.nextNotePlayPitch(killNote, numChannels);
  117.       }
  118.       // kill channel note of necessary
  119.       MIDI.sendNoteOff(killNote, 0x00, nxtChan);
  120.       // play note
  121.       MIDI.sendNoteOn(MIDI.getData1(), MIDI.getData2(), nxtChan);
  122.       // update the channel array
  123.       chArray.updateStatus(MIDI.getType(), MIDI.getData1(), nxtChan);
  124.       updateLEDs(nxtChan, MIDI.getType(), ledChOne, ledChTwo, ledChThree, ledChFour);
  125.        
  126.     }
  127.    
  128.     // if the message is note off
  129.     else if(MIDI.getType() == NoteOff || (MIDI.getType() == NoteOn && MIDI.getData2() == 0x00)){
  130.       // locate matching note currently being played
  131.       nxtChan = chArray.noteOffMatch(MIDI.getData1());
  132.      
  133.       // send note off command
  134.       if(nxtChan != 0xFF){
  135.         MIDI.sendNoteOff(MIDI.getData1(), MIDI.getData2(), nxtChan);
  136.       }
  137.      
  138.       // update the channel array
  139.       chArray.updateStatus(MIDI.getType(), MIDI.getData1(), nxtChan);
  140.       updateLEDs(nxtChan, MIDI.getType(), ledChOne, ledChTwo, ledChThree, ledChFour);
  141.     }
  142.   }
  143. }
  144.  
  145. // updates the feedback LEDs
  146. void updateLEDs(const byte nxtChan, const kMIDIType inCommand, const int& ledChOne, const int& ledChTwo, const int& ledChThree, const int& ledChFour){
  147.   if(inCommand == NoteOn){
  148.     switch (nxtChan){
  149.       case 0x00:
  150.       digitalWrite(ledChOne, HIGH);
  151.       break;
  152.       case 0x01:
  153.       digitalWrite(ledChTwo, HIGH);
  154.       break;
  155.       case 0x02:
  156.       digitalWrite(ledChThree, HIGH);
  157.       break;
  158.       case 0x03:
  159.       digitalWrite(ledChFour, HIGH);
  160.       break;
  161.     }
  162.   }
  163.     if(inCommand == NoteOff){
  164.     switch (nxtChan){
  165.       case 0x00:
  166.       digitalWrite(ledChOne, LOW);
  167.       break;
  168.       case 0x01:
  169.       digitalWrite(ledChTwo, LOW);
  170.       break;
  171.       case 0x02:
  172.       digitalWrite(ledChThree, LOW);
  173.       break;
  174.       case 0x03:
  175.       digitalWrite(ledChFour, LOW);
  176.       break;
  177.     }
  178.   }
  179. }
  180.  
  181. // checks the pins for the selected number of channesl 2,3,4
  182. byte getNumberOfChannels(){
  183.   if(digitalRead(selectPinOne)){
  184.     return 0x04;
  185.   }
  186.   else if(digitalRead(selectPinTwo)){
  187.     return 0x02;
  188.   }
  189.   else{
  190.     return 0x03;
  191.   }
  192. }
  193.  
  194. //compare the that last processed midi data with midi data currently in MIDI que.
  195. boolean isNewMidi(const kMIDIType prevType, const byte prevPitch, const byte prevVelocity, const kMIDIType curType, const byte curPitch, const byte curVelocity){
  196.   if(prevType != curType){
  197.     return true;
  198.   }
  199.   if(prevPitch != curPitch){
  200.     return true;
  201.   }
  202.   if(prevVelocity !=curVelocity){
  203.     return true;
  204.   }
  205.   return false;
  206. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement