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
- // 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;
- // these are used to make sure incoming midi is new message
- kMIDIType prevType = InvalidType;
- byte prevPitch = 0x00;
- byte prevVelocity = 0x00;
- 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
- MIDI.read();
- newMidi = isNewMidi(prevType, prevPitch, prevVelocity, MIDI.getType(),MIDI.getData1(),MIDI.getData2());
- if(newMidi){
- prevType = MIDI.getType();
- prevPitch= MIDI.getData1();
- prevVelocity=MIDI.getData2();
- // if the message is on a channel that the distributor does not use
- // echo the midi message out
- if(MIDI.getChannel() > 0x03){
- // echos the incoming message back out.
- MIDI.send(MIDI.getType(), MIDI.getData1(), MIDI.getData2(), MIDI.getChannel());
- }
- // if the message is within the range of channels the distributor uses
- else if(MIDI.getChannel() > 0x00){
- }
- // if the distributor is in mono mode
- else if(!digitalRead(modeSelectOne) && !digitalRead(modeSelectTwo)){
- for(int i = 0x00; i < numChannels; i++){
- MIDI.send(MIDI.getType(), MIDI.getData1(), MIDI.getData2(), i);
- updateLEDs(i, MIDI.getType(), ledChOne, ledChTwo, ledChThree, ledChFour);
- }
- }
- // if the message is a pitch bend
- else if(MIDI.getType() == PitchBend){
- for(int i = 0x00; i < numChannels; i++){
- MIDI.send(MIDI.getType(), MIDI.getData1(), MIDI.getData2(), i);
- }
- }
- // if the message is note on
- else if(MIDI.getType() == NoteOn && MIDI.getData2() != 0x00){
- byte killNote = 0x00;
- // 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
- MIDI.sendNoteOff(killNote, 0x00, nxtChan);
- // play note
- MIDI.sendNoteOn(MIDI.getData1(), MIDI.getData2(), nxtChan);
- // update the channel array
- chArray.updateStatus(MIDI.getType(), MIDI.getData1(), nxtChan);
- updateLEDs(nxtChan, MIDI.getType(), ledChOne, ledChTwo, ledChThree, ledChFour);
- }
- // if the message is note off
- else if(MIDI.getType() == NoteOff || (MIDI.getType() == NoteOn && MIDI.getData2() == 0x00)){
- // locate matching note currently being played
- nxtChan = chArray.noteOffMatch(MIDI.getData1());
- // send note off command
- if(nxtChan != 0xFF){
- MIDI.sendNoteOff(MIDI.getData1(), MIDI.getData2(), nxtChan);
- }
- // update the channel array
- chArray.updateStatus(MIDI.getType(), MIDI.getData1(), nxtChan);
- updateLEDs(nxtChan, MIDI.getType(), 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.
- boolean isNewMidi(const kMIDIType prevType, const byte prevPitch, const byte prevVelocity, const kMIDIType curType, const byte curPitch, const byte curVelocity){
- if(prevType != curType){
- return true;
- }
- if(prevPitch != curPitch){
- return true;
- }
- if(prevVelocity !=curVelocity){
- return true;
- }
- return false;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement