Advertisement
charlieslick

MIDIdistributor

Aug 25th, 2014
291
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.07 KB | None | 0 0
  1. #include "charlesMIDI.h"
  2.  
  3. // You cannot change the
  4.  
  5.  
  6.  
  7. MidiWord currentMessage;
  8.  
  9. MCArray midiChArray;
  10.  
  11. // set midi in to Channel 1,  unfortunately because of all the
  12. // "greater than" and "less than" logic, you cannot change the midi input channel
  13. const byte MIDI_IN_CHANNEL = 0x00;
  14.  
  15. // these define the channel status feedback pins
  16. // they go high when the channel is currently playing a note
  17.  
  18. const int ledChOne = 5;
  19. const int ledChTwo = 6;
  20. const int ledChThree =7;
  21. const int ledChFour = 8;
  22.  
  23. // these deifne the number of channels to distribute the midi commands to
  24. // if 3 HIGH then 4 Channels, if 4 HIGH then 2 Channels, no HIGH: 3 Channels
  25. // allows use of a simple center off switch to control 3 options
  26. const int selectPinOne = 3;
  27. const int selectPinTwo = 4;
  28.  
  29. // these define the pins used to select the priority algorithm
  30. // if pin 2 is high, then drop note by time, if pin 9 is high then drop high note.
  31. // if no pins high, MONO MODE
  32. const int modeSelectOne = 2;
  33. const int modeSelectTwo = 9;
  34.  
  35. void setup(){
  36.   // MIDI baud rate
  37.   Serial.begin(31250);
  38.   pinMode(ledChOne, OUTPUT);
  39.   pinMode(ledChTwo, OUTPUT);
  40.   pinMode(ledChThree, OUTPUT);
  41.   pinMode(ledChFour, OUTPUT);
  42.  
  43.   pinMode(selectPinOne, INPUT);
  44.   pinMode(selectPinTwo, INPUT);
  45.   pinMode(modeSelectOne, INPUT);
  46.   pinMode(modeSelectTwo, INPUT);
  47.  
  48. }
  49. void loop(){
  50.  
  51.   // tempCh is passed into function to return channel the note shall be played on
  52.   byte tempCh;
  53.  
  54.   // initialize current command, used to store current midi command
  55.   byte currentCommand = 0x00;
  56.  
  57.   // check how many channels are in play
  58.   byte numberOfChannels = getNumberOfChannels();
  59.  
  60.   // read capture midi "word"
  61.   currentMessage.getWord();
  62.  
  63.   // get the current midi command from incoming word
  64.   currentCommand = currentMessage.getCommand();
  65.  
  66.   // if pitch bend command outside of the range channels, just send it on through
  67.   if(currentCommand >= 0xE0 + numberOfChannels){
  68.     currentMessage.writeWord();
  69.   }
  70.   // if pitch bend for receive channel. bend all notes.
  71.   else if (currentCommand == 0xE0){
  72.     for(int i = 0x00; i < numberOfChannels; i++){
  73.       currentMessage.writeWord(i);
  74.     }
  75.   }
  76.  
  77.   // if it is a command for a channel that our distributor does not occupy
  78.   // send it on through
  79.   else if(currentCommand >= noteOff + numberOfChannels && currentCommand < noteOn){
  80.     currentMessage.writeWord();
  81.   }
  82.   else if(currentCommand >= noteOn + numberOfChannels){
  83.     currentMessage.writeWord();
  84.   }
  85.  
  86.   // if mono mode is engaged. send message out on all selected channels
  87.   else if(!digitalRead(modeSelectOne) && !digitalRead(modeSelectTwo)){
  88.     for(int i = 0x00; i < numberOfChannels; i++){
  89.       currentMessage.writeWord(i);
  90.      updateLEDs(currentMessage, i, ledChOne, ledChTwo, ledChThree, ledChFour);
  91.     }
  92.   }
  93.  
  94.   // for all non-mono-mode midi messages
  95.   else if(currentCommand == noteOn + MIDI_IN_CHANNEL){
  96.    // kill note is used in case when all channels are currently playing a note
  97.    // one of those notes must be dropped,  this is the kill note.
  98.    MidiWord killNote;
  99.    // checks the switch status for sort algorithm
  100.    if(digitalRead(modeSelectOne)){
  101.     tempCh = midiChArray.nextNotePlayTime(killNote, numberOfChannels);
  102.    }
  103.    else if(digitalRead(modeSelectTwo)){
  104.      tempCh = midiChArray.nextNotePlayPitch(killNote, numberOfChannels);
  105.    }
  106.    killNote.writeWord(tempCh);  
  107.    currentMessage.writeWord(tempCh);
  108.    midiChArray.updateStatus(currentMessage, tempCh);
  109.    updateLEDs(currentMessage, tempCh, ledChOne, ledChTwo, ledChThree, ledChFour);
  110.   }
  111.  
  112.   // regular mode note off
  113.   else if(currentCommand == noteOff + MIDI_IN_CHANNEL){
  114.     tempCh = midiChArray.noteOffMatch(currentMessage.getPitch());
  115.     currentMessage.writeWord(tempCh);
  116.     midiChArray.updateStatus(currentMessage, tempCh);
  117.     updateLEDs(currentMessage, tempCh, ledChOne, ledChTwo, ledChThree, ledChFour);
  118.   }
  119.  
  120.  
  121.  
  122. }
  123.  
  124. boolean flipState(boolean current){
  125.   if(current){
  126.     return LOW;
  127.   }
  128.   else{
  129.     return HIGH;
  130.   }
  131. }
  132.  
  133. void updateLEDs(const MidiWord& inWord, const byte& tempCh, const int& ledChOne, const int& ledChTwo, const int& ledChThree, const int& ledChFour){
  134.   byte inCommand = inWord.getCommand();
  135.   if(inCommand == noteOn){
  136.     switch (tempCh){
  137.       case 0x00:
  138.       digitalWrite(ledChOne, HIGH);
  139.       break;
  140.       case 0x01:
  141.       digitalWrite(ledChTwo, HIGH);
  142.       break;
  143.       case 0x02:
  144.       digitalWrite(ledChThree, HIGH);
  145.       break;
  146.       case 0x03:
  147.       digitalWrite(ledChFour, HIGH);
  148.       break;
  149.     }
  150.   }
  151.     if(inCommand == noteOff){
  152.     switch (tempCh){
  153.       case 0x00:
  154.       digitalWrite(ledChOne, LOW);
  155.       break;
  156.       case 0x01:
  157.       digitalWrite(ledChTwo, LOW);
  158.       break;
  159.       case 0x02:
  160.       digitalWrite(ledChThree, LOW);
  161.       break;
  162.       case 0x03:
  163.       digitalWrite(ledChFour, LOW);
  164.       break;
  165.     }
  166.   }
  167. }
  168.  
  169. byte getNumberOfChannels(){
  170.   if(digitalRead(selectPinOne)){
  171.     return 0x04;
  172.   }
  173.   else if(digitalRead(selectPinTwo)){
  174.     return 0x02;
  175.   }
  176.   else{
  177.     return 0x03;
  178.   }
  179. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement