Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <MIDI.h>
- #include <arduino.h>
- class MidiChannel{
- private:
- byte curPitch;
- boolean curState;
- unsigned long startTime;
- public:
- MidiChannel();
- byte getCurPitch() const;
- boolean getCurState() const;
- unsigned long getStartTime() const;
- void setCurPitch(const byte inCurPitch);
- void setCurState(const byte inState);
- void setStartTime();
- };
- MidiChannel::MidiChannel(){
- curPitch = 0x00;
- curState = false;
- startTime = millis();
- }
- byte MidiChannel::getCurPitch() const{
- return curPitch;
- }
- boolean MidiChannel::getCurState() const{
- return curState;
- }
- unsigned long MidiChannel::getStartTime() const{
- return startTime;
- }
- void MidiChannel::setCurPitch(const byte inCurPitch){
- curPitch = inCurPitch;
- }
- void MidiChannel::setCurState(const boolean inState){
- curState = inState;
- }
- void MidiChannel::setStartTime(){
- startTime = millis();
- }
- class MCArray{
- private:
- MidiChannel Array[4];
- public:
- MCArray();
- byte noteOffMatch(const byte inPitch) const;
- void updateStatus(const kMIDIType curMessage, byte pitch, byte messageChannel);
- byte nextNotePlayPitch(byte& killPitch, byte numberOfChannels) const;
- byte nextNotePlayTime(byte& killPitch, byte numberOfChannels) const;
- };
- MCArray::MCArray(){
- }
- // check the channels with current status on and match pitch to determine note send off.
- byte MCArray::noteOffMatch(const byte inPitch) const{
- for(int i = 0x00; i < 4; i++){
- if(Array[i].getCurState() == true && Array[i].getCurPitch() == inPitch){
- return i;
- }
- }
- // incase the note has already been turned off
- return 0xFF;
- }
- void MCArray::updateStatus(const kMIDIType curMessage, byte pitch, byte messageChannel){
- if(curMessage == NoteOn){
- Array[messageChannel].setCurPitch(pitch);
- Array[messageChannel].setCurState(true);
- Array[messageChannel].setStartTime();
- }
- if(curMessage == NoteOff){
- Array[messageChannel].setCurPitch(0x00);
- Array[messageChannel].setCurState(false);
- }
- }
- // use an algorithm to find which note should be played next
- // killnote is in case all channels are in use.
- byte MCArray::nextNotePlayPitch(byte& killPitch, byte numberOfChannels) const{
- // check if all notes are being played
- for(int i = 0x00; i < numberOfChannels; i++){
- if (Array[i].getCurState() == false){
- return i;
- }
- }
- // check play times
- byte dropChannel = 0x00;
- byte maxPitch = Array[0x00].getCurPitch();
- for(int i = 0x01; i < numberOfChannels; i++){
- if(maxPitch < Array[i].getCurPitch()){
- dropChannel = i;
- maxPitch = Array[i].getCurPitch();
- }
- }
- killPitch = maxPitch;
- return dropChannel;
- }
- // use an algorithm to find which note should be played next
- // killnote is in case all channels are in use.
- byte MCArray::nextNotePlayTime(byte& killPitch, byte numberOfChannels) const{
- // check if all notes are being played
- for(int i = 0x00; i < numberOfChannels; i++){
- if (Array[i].getCurState() == false){
- return i;
- }
- }
- // check play times
- byte dropChannel = 0x00;
- killPitch = Array[0x00].getCurPitch();
- unsigned long lowTime = Array[0x00].getStartTime();
- for(int i = 0x01; i < numberOfChannels; i++){
- if(lowTime > Array[i].getStartTime()){
- dropChannel = i;
- lowTime = Array[i].getCurPitch();
- killPitch = Array[i].getCurPitch();
- }
- }
- return dropChannel;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement