Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <MIDI.h>
- #include "MIDIBuddy.h"
- // MIDI.getType();
- // returns a kMIDIType such as NoteOn or NoteOff
- // MIDI.getChannel();
- // returns the channel of the message stored in the structure 1-16 <----this is unexpected.
- // MIDI.getData1();
- // returns the first peice of data, usually pitch
- // MIDI.getData2();
- // returns the second peice of data, usually velocity
- // creation of a class which is just an array of a midi channel class
- MCArray chArray;
- // initiallizes a the number channels as 4
- byte numChannels = 4;
- boolean newMidi = true;
- // these define the channel status feedback pins
- // they go high when the channel is currently playing a note
- const int ledChOne = 5;
- const int ledChTwo = 6;
- const int ledChThree =7;
- const int ledChFour = 8;
- // these deifne the number of channels to distribute the midi commands to
- // if 3 HIGH then 4 Channels, if 4 HIGH then 2 Channels, no HIGH: 3 Channels
- // allows use of a simple center off switch to control 3 options
- const int selectPinOne = 3;
- const int selectPinTwo = 4;
- // these define the pins used to select the priority algorithm
- // if pin 2 is high, then drop note by time, if pin 9 is high then drop high note.
- // if no pins high, MONO MODE
- const int modeSelectOne = 2;
- const int modeSelectTwo = 9;
- void setup(){
- MIDI.begin(MIDI_CHANNEL_OMNI);
- MIDI.setInputChannel(MIDI_CHANNEL_OMNI);
- MIDI.disconnectCallbackFromType(NoteOn);
- MIDI.disconnectCallbackFromType(NoteOff);
- MIDI.disconnectCallbackFromType(PitchBend);
- MIDI.turnThruOff();
- pinMode(ledChOne, OUTPUT);
- pinMode(ledChTwo, OUTPUT);
- pinMode(ledChThree, OUTPUT);
- pinMode(ledChFour, OUTPUT);
- pinMode(selectPinOne, INPUT);
- pinMode(selectPinTwo, INPUT);
- pinMode(modeSelectOne, INPUT);
- pinMode(modeSelectTwo, INPUT);
- }
- void loop(){
- byte nxtChan = 0x00;
- numChannels = getNumberOfChannels();
- // read next available midi message in cue
- if(MIDI.read()){
- byte curChannel = MIDI.getChannel();
- kMIDIType curCommand = MIDI.getType();
- byte curPitch = MIDI.getData1();
- byte curVelocity = MIDI.getData2();
- //comvert 0x00 velocity to note off
- if(curChannel < 5 && curCommand == NoteOn && curVelocity == 0x00){
- curCommand = NoteOff;
- }
- // if the message is on a channel that the distributor does not use
- // echo the midi message out
- if(curChannel > 4){
- // echos the incoming message back out.
- MIDI.send(curCommand, curPitch, curVelocity, curChannel);
- }
- // if the message is within the range of channels the distributor uses but not the incoming
- else if(curChannel > 1){
- return;
- }
- // if the distributor is in mono mode
- else if(!digitalRead(modeSelectOne) && !digitalRead(modeSelectTwo)){
- for(int i = 0x00; i < numChannels; i++){
- MIDI.send(curCommand, curPitch, curVelocity, i +1);
- updateLEDs(i, curCommand, ledChOne, ledChTwo, ledChThree, ledChFour);
- }
- }
- // if the message is a pitch bend
- else if(curCommand == PitchBend){
- for(int i = 0x00; i < numChannels; i++){
- MIDI.send(curCommand, curPitch, curVelocity, i +1);
- }
- }
- // if the message is note on
- else if(curCommand == NoteOn){
- byte killNote = 0xFF;
- // determine next channel to send on
- if(digitalRead(modeSelectOne)){
- nxtChan = chArray.nextNotePlayTime(killNote, numChannels);
- }
- else if(digitalRead(modeSelectTwo)){
- nxtChan = chArray.nextNotePlayPitch(killNote, numChannels);
- }
- // kill channel note of necessary
- if(killNote != 0xFF){
- MIDI.sendNoteOff(killNote, 0x00, nxtChan +1);
- }
- // play note
- MIDI.sendNoteOn(curPitch, curVelocity, nxtChan +1);
- // update the channel array
- chArray.updateStatus(curCommand, curPitch, nxtChan);
- updateLEDs(nxtChan, curCommand, ledChOne, ledChTwo, ledChThree, ledChFour);
- }
- // if the message is note off
- else if(curCommand == NoteOff){
- // locate matching note currently being played
- nxtChan = chArray.noteOffMatch(curPitch);
- // send note off command
- if(nxtChan != 0xFF){
- MIDI.sendNoteOff(curPitch, curVelocity, nxtChan +1);
- }
- // update the channel array
- chArray.updateStatus(curCommand, curPitch, nxtChan);
- updateLEDs(nxtChan, curCommand, ledChOne, ledChTwo, ledChThree, ledChFour);
- }
- }
- }
- // updates the feedback LEDs
- void updateLEDs(const byte nxtChan, const kMIDIType inCommand, const int& ledChOne, const int& ledChTwo, const int& ledChThree, const int& ledChFour){
- if(inCommand == NoteOn){
- switch (nxtChan){
- case 0x00:
- digitalWrite(ledChOne, HIGH);
- break;
- case 0x01:
- digitalWrite(ledChTwo, HIGH);
- break;
- case 0x02:
- digitalWrite(ledChThree, HIGH);
- break;
- case 0x03:
- digitalWrite(ledChFour, HIGH);
- break;
- }
- }
- if(inCommand == NoteOff){
- switch (nxtChan){
- case 0x00:
- digitalWrite(ledChOne, LOW);
- break;
- case 0x01:
- digitalWrite(ledChTwo, LOW);
- break;
- case 0x02:
- digitalWrite(ledChThree, LOW);
- break;
- case 0x03:
- digitalWrite(ledChFour, LOW);
- break;
- }
- }
- }
- // checks the pins for the selected number of channesl 2,3,4
- byte getNumberOfChannels(){
- if(digitalRead(selectPinOne)){
- return 0x04;
- }
- else if(digitalRead(selectPinTwo)){
- return 0x02;
- }
- else{
- return 0x03;
- }
- }
- //compare the that last processed midi data with midi data currently in MIDI que.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement