Advertisement
charlieslick

midibuddy.h

Dec 7th, 2014
251
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.48 KB | None | 0 0
  1. #include <MIDI.h>
  2. #include <arduino.h>
  3.  
  4. class MidiChannel{
  5.   private:
  6.   byte curPitch;
  7.   boolean curState;
  8.   unsigned long startTime;
  9.  
  10.   public:
  11.   MidiChannel();
  12.   byte getCurPitch() const;
  13.   boolean getCurState() const;
  14.   unsigned long getStartTime() const;
  15.   void setCurPitch(const byte inCurPitch);
  16.   void setCurState(const byte inState);
  17.   void setStartTime();
  18.  
  19. };
  20.  
  21.  
  22. MidiChannel::MidiChannel(){
  23.    curPitch = 0x00;
  24.    curState = false;
  25.    startTime = millis();
  26. }
  27.  
  28. byte MidiChannel::getCurPitch() const{
  29.   return curPitch;
  30. }
  31.  
  32. boolean MidiChannel::getCurState() const{
  33.   return curState;
  34.  }
  35.  
  36. unsigned long MidiChannel::getStartTime() const{
  37.   return startTime;
  38. }
  39.  
  40. void MidiChannel::setCurPitch(const byte inCurPitch){
  41.   curPitch = inCurPitch;
  42. }
  43.  
  44. void MidiChannel::setCurState(const boolean inState){
  45.   curState = inState;
  46. }
  47.  
  48. void MidiChannel::setStartTime(){
  49.   startTime = millis();
  50. }
  51.  
  52. class MCArray{
  53.   private:
  54.   MidiChannel Array[4];
  55.  
  56.   public:
  57.   MCArray();
  58.   byte noteOffMatch(const byte inPitch) const;
  59.   void updateStatus(const kMIDIType curMessage, byte pitch, byte messageChannel);
  60.   byte nextNotePlayPitch(byte& killPitch, byte numberOfChannels) const;
  61.   byte nextNotePlayTime(byte& killPitch, byte numberOfChannels) const;
  62.  
  63.  
  64. };
  65.  
  66. MCArray::MCArray(){
  67. }
  68.  
  69. // check the channels with current status on and match pitch to determine note send off.
  70. byte MCArray::noteOffMatch(const byte inPitch) const{
  71.   for(int i = 0x00; i < 4; i++){
  72.     if(Array[i].getCurState() == true && Array[i].getCurPitch() == inPitch){
  73.       return i;
  74.     }
  75.   }
  76.   // incase the note has already been turned off
  77.   return 0xFF;
  78.  
  79. }
  80. void MCArray::updateStatus(const kMIDIType curMessage, byte pitch, byte messageChannel){
  81.   if(curMessage == NoteOn){
  82.     Array[messageChannel].setCurPitch(pitch);
  83.     Array[messageChannel].setCurState(true);
  84.     Array[messageChannel].setStartTime();
  85.   }
  86.  
  87.   if(curMessage == NoteOff){
  88.     Array[messageChannel].setCurPitch(0x00);
  89.     Array[messageChannel].setCurState(false);
  90.   }
  91. }
  92.  
  93.  
  94. // use an algorithm to find which note should be played next
  95. // killnote is in case all channels are in use.
  96. byte MCArray::nextNotePlayPitch(byte& killPitch, byte numberOfChannels) const{
  97.  
  98.   // check if all notes are being played
  99.   for(int i = 0x00; i < numberOfChannels; i++){
  100.     if (Array[i].getCurState() == false){
  101.       return i;
  102.     }
  103.   }
  104.  
  105.   // check play times
  106.   byte dropChannel = 0x00;
  107.   byte maxPitch = Array[0x00].getCurPitch();
  108.   for(int i = 0x01; i < numberOfChannels; i++){
  109.     if(maxPitch < Array[i].getCurPitch()){
  110.       dropChannel = i;
  111.       maxPitch = Array[i].getCurPitch();
  112.     }
  113.   }
  114.   killPitch = maxPitch;
  115.   return dropChannel;
  116. }
  117.  
  118. // use an algorithm to find which note should be played next
  119. // killnote is in case all channels are in use.
  120. byte MCArray::nextNotePlayTime(byte& killPitch, byte numberOfChannels) const{
  121.  
  122.   // check if all notes are being played
  123.   for(int i = 0x00; i < numberOfChannels; i++){
  124.     if (Array[i].getCurState() == false){
  125.       return i;
  126.     }
  127.   }
  128.  
  129.   // check play times
  130.   byte dropChannel = 0x00;
  131.   killPitch = Array[0x00].getCurPitch();
  132.   unsigned long lowTime = Array[0x00].getStartTime();
  133.   for(int i = 0x01; i < numberOfChannels; i++){
  134.     if(lowTime > Array[i].getStartTime()){
  135.       dropChannel = i;
  136.       lowTime = Array[i].getCurPitch();
  137.       killPitch = Array[i].getCurPitch();
  138.     }
  139.   }
  140.   return dropChannel;
  141. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement