SNES Doombox MIDI in Arduino code

#include <MIDI.h> //get it here http://sourceforge.net/projects/arduinomidilib/files/Releases/
const int tonePin = 2;
const int LEDpin = 13;
int voicecount = 0;
float Freq = 440;
unsigned int notetone = 0;
float PBval = 0;
int note = 0;
float Multiplier = 1;
int offnote;
unsigned int frequency[128] = {8, 9, 9, 10, 10, 11, 12, 12, 13, 14, 15, 15, 16, 17, 18, 19, 21, 22, 23, 24, 26, 28, 29, 31, 33, 35, 37, 39, 41, 44, 46, 49, 52, 55, 58, 62, 65, 69, 73, 78, 82, 87, 92, 98, 104, 110, 117, 123, 131, 139, 147, 156, 165, 175, 185, 196, 208, 220, 233, 247, 262, 277, 294, 311, 330, 349, 370, 392, 415, 440, 466, 494, 523, 554, 587, 622, 659, 698, 740, 784, 831, 880, 932, 988, 1047, 1109, 1175, 1245, 1319, 1397, 1480, 1568, 1661, 1760, 1865, 1976, 2093, 2217, 2349, 2489, 2637, 2794, 2960, 3136, 3322, 3520, 3729, 3951, 4186, 4435, 4699, 4978, 5274, 5588, 5920, 5920, 6645, 7040, 7459, 7902, 8372, 8870, 9397, 9956, 10548, 11175, 11840, 12544};
unsigned int voices[36] = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
int notescan = 0;
int releasednote;
void setup(){
pinMode(tonePin, OUTPUT); //sound out
pinMode(LEDpin, OUTPUT); //blinks when we ket notes
MIDI.begin(MIDI_CHANNEL_OMNI);
//MIDI.turnThruOff();
}
void loop(){
if(MIDI.read()){
if(MIDI.getType()==NoteOn){ //if a note is pressed
digitalWrite(LEDpin, HIGH); //turn on the led
voicecount++; // and add 1 to the voice counter
note = MIDI.getData1(); // what note was it ?
voices[voicecount] = note; // store what note it is in an array so we can find it when its released
notetone = (frequency[note]); //look up what frequency that note corresponds to
Freq = notetone * Multiplier; //multiply it by what ever the current pitchbend value is
tone(tonePin,Freq); // play the tone brah
}
//PITCH BEND
if(MIDI.getType()==PitchBend){ //if we recieve a pitchbend message
PBval = ((MIDI.getData2()<<7)+MIDI.getData1()); //what is the pitch bend val? (we have to add together 2 7bit values
if (PBval < 8192){ // code to translate data to a multiplier for frequency
PBval = map(PBval,0,8191,4096,8191);
Multiplier = PBval/8192;
} else {
Multiplier = PBval/8192;
}
Freq = notetone*Multiplier;
if (voicecount > 0){
tone(tonePin,Freq); //restart the tone at the new frequency
}
}
//NOTE OFFS
if(MIDI.getType()==NoteOff){
voicecount–; // if we recieve a noteoff subtract one from the voice counter
offnote = MIDI.getData1(); //what note was it
for (int x=0; (x <= 34) && notescan != offnote; x++){                  //scan through played notes to see witch one was released
notescan = voices[x];
releasednote = x;
}
notescan = 0;
//DEBUG
if (releasednote == voicecount+1){                                  //check if it was the last pressed note that was released
note = voices[voicecount];                                               //if it was the last note that was released then play the after that
notetone = (frequency[note]);
Freq = notetone * Multiplier;
tone(tonePin,Freq);
}                                                                          // we want to remove released note from the array and shift all following entries down one
if(voicecount<1) { //what to do if voicecount is 0
noTone(tonePin);
digitalWrite(LEDpin, LOW);
}
//CLEANUP
for(int i = releasednote; i < 19; i++){ //remove last entry in the note array, and move all entries up one
voices[i] = voices[i+1];
}
}
}
}

Leave a Reply

Your email address will not be published. Required fields are marked *

*


Booking

want to book a concert or a workshop contact me here: email

News

NEWS

Fantasy Mansion

releases June 23, 2017.

Preorder here

Planck Midi/CV controller register interest here

More info on Planck here

NEW Christmas track 2016

Ravé Maria

Forest Moon Music Video

Dead Cats Coming Oct2015

on Metronomicon Audio

New EP out NOW!!!1

New VST ploggin!