Move MIDI functions into separate files

This commit is contained in:
John Stäck 2019-03-01 13:51:14 +01:00
parent a63ac4e3b9
commit 1d8cd1a3ef
3 changed files with 231 additions and 217 deletions

290
NuEVI.ino
View file

@ -7,6 +7,8 @@
#include <Adafruit_SSD1306.h> #include <Adafruit_SSD1306.h>
#include <Filters.h> // for the breath signal LP filtering, https://github.com/edgar-bonet/Filters #include <Filters.h> // for the breath signal LP filtering, https://github.com/edgar-bonet/Filters
#include "midi.h"
/* /*
NAME: NuEVI NAME: NuEVI
WRITTEN BY: JOHAN BERGLUND WRITTEN BY: JOHAN BERGLUND
@ -579,6 +581,7 @@ byte breathLedBrightness = 2000; // up to 4095, PWM
byte portamLedBrightness = 2000; // up to 4095, PWM byte portamLedBrightness = 2000; // up to 4095, PWM
Adafruit_MPR121 touchSensor = Adafruit_MPR121(); // This is the 12-input touch sensor Adafruit_MPR121 touchSensor = Adafruit_MPR121(); // This is the 12-input touch sensor
FilterOnePole breathFilter;
//_______________________________________________________________________________________________ SETUP //_______________________________________________________________________________________________ SETUP
@ -719,6 +722,8 @@ void setup() {
display.begin(SSD1306_SWITCHCAPVCC, 0x3C); // initialize with the I2C addr 0x3D (for the 128x64) display.begin(SSD1306_SWITCHCAPVCC, 0x3C); // initialize with the I2C addr 0x3D (for the 128x64)
// init done // init done
breathFilter.setFilter(LOWPASS, filterFreq, 0.0); // create a one pole (RC) lowpass filter
// Show image buffer on the display hardware. // Show image buffer on the display hardware.
// Since the buffer is intialized with an Adafruit splashscreen // Since the buffer is intialized with an Adafruit splashscreen
// internally, this will display the splashscreen. // internally, this will display the splashscreen.
@ -784,22 +789,18 @@ void setup() {
//_______________________________________________________________________________________________ MAIN LOOP //_______________________________________________________________________________________________ MAIN LOOP
void loop() { void loop() {
mainLoop();
}
void mainLoop() {
FilterOnePole breathFilter( LOWPASS, filterFreq ); // create a one pole (RC) lowpass filter
while (1){
breathFilter.input(analogRead(breathSensorPin)); breathFilter.input(analogRead(breathSensorPin));
pressureSensor = constrain((int)breathFilter.output(),0,4095); // Get the filtered pressure sensor reading from analog pin A0, input from sensor MP3V5004GP pressureSensor = constrain((int)breathFilter.output(),0,4095); // Get the filtered pressure sensor reading from analog pin A0, input from sensor MP3V5004GP
//pressureSensor = analogRead(A0); //pressureSensor = analogRead(A0);
//pressureSensor = smooth(analogRead(0), filterVal, smoothedVal); // second parameter determines smoothness - 0 is off, .9999 is max smooth //pressureSensor = smooth(analogRead(0), filterVal, smoothedVal); // second parameter determines smoothness - 0 is off, .9999 is max smooth
if (mainState == NOTE_OFF) { if (mainState == NOTE_OFF) {
if (activeMIDIchannel != MIDIchannel) activeMIDIchannel = MIDIchannel; // only switch channel if no active note if (activeMIDIchannel != MIDIchannel) {
activeMIDIchannel = MIDIchannel; // only switch channel if no active note
midiSetChannel(activeMIDIchannel);
}
if ((activePatch != patch) && doPatchUpdate){ if ((activePatch != patch) && doPatchUpdate){
activePatch = patch; activePatch = patch;
usbMIDI.sendProgramChange(activePatch-1,activeMIDIchannel); midiSendProgramChange(activePatch);
dinMIDIsendProgramChange(activePatch-1,activeMIDIchannel-1);
slurSustain = 0; slurSustain = 0;
parallelChord = 0; parallelChord = 0;
subOctaveDouble = 0; subOctaveDouble = 0;
@ -864,11 +865,10 @@ void mainLoop() {
else if (!K1 && K2 && !K3 && K4) {patch = patch +10; doPatchUpdate = 1; } //f#30 send patch change +10 else if (!K1 && K2 && !K3 && K4) {patch = patch +10; doPatchUpdate = 1; } //f#30 send patch change +10
else if (!K1 && !K2 && !K3 &&K4) { patch++; doPatchUpdate = 1; } //g31 increment and send patch change else if (!K1 && !K2 && !K3 &&K4) { patch++; doPatchUpdate = 1; } //g31 increment and send patch change
if (!K1 && !K2 && K3 &&!K4) { //send reverb pitchlatch value if (!K1 && !K2 && K3 &&!K4) { //send reverb pitchlatch value
reverb = ((pitchlatch - 36) * 2); reverb = ((pitchlatch - 36) * 2);
if (reverb > 127) {reverb = 127;} if (reverb > 127) {reverb = 127;}
if (reverb < 0) {reverb = 0;} if (reverb < 0) {reverb = 0;}
usbMIDI.sendControlChange(91,reverb, activeMIDIchannel); midiSendControlChange(91,reverb);
dinMIDIsendControlChange(91,reverb, activeMIDIchannel - 1);
} }
} }
@ -876,47 +876,35 @@ void mainLoop() {
// breathCC value is from cclist[] which assigns controller number // breathCC value is from cclist[] which assigns controller number
if (K1) { //turn on midi volume if (K1) { //turn on midi volume
breathCC = 3; breathCC = 3;
usbMIDI.sendControlChange(7,0, activeMIDIchannel); //midi vol to 0 midiSendControlChange(7,0); //midi vol to 0
dinMIDIsendControlChange(7,0, activeMIDIchannel - 1); midiSendControlChange(11,127); //midi expression to 127
usbMIDI.sendControlChange(11,127, activeMIDIchannel); //midi expression to 127
dinMIDIsendControlChange(11,127, activeMIDIchannel - 1);
} }
if (K3) { //turn on midi breath controller if (K3) { //turn on midi breath controller
breathCC = 2; breathCC = 2;
usbMIDI.sendControlChange(7,127, activeMIDIchannel); //midi vol to 127 midiSendControlChange(7,127); //midi vol to 127
dinMIDIsendControlChange(7,127, activeMIDIchannel - 1); midiSendControlChange(11,127); //midi expression to 127
usbMIDI.sendControlChange(11,127, activeMIDIchannel); //midi expression to 127
dinMIDIsendControlChange(11,127, activeMIDIchannel - 1);
} }
if (K4) { //sb turn on midi expression if (K4) { //sb turn on midi expression
breathCC = 4; breathCC = 4;
usbMIDI.sendControlChange(7,127, activeMIDIchannel); //midi vol to 127 midiSendControlChange(7,127); //midi vol to 127
dinMIDIsendControlChange(7,127, activeMIDIchannel - 1); midiSendControlChange(11,0); //midi expression to 0
usbMIDI.sendControlChange(11,0, activeMIDIchannel); //midi expression to 0
dinMIDIsendControlChange(11,0, activeMIDIchannel - 1);
} }
if (K2) { //2v turn on aftertouch if (K2) { //2v turn on aftertouch
breathAT = 1; breathAT = 1;
usbMIDI.sendControlChange(7,127, activeMIDIchannel); //midi vol to 127 midiSendControlChange(7,127); //midi vol to 127
dinMIDIsendControlChange(7,127, activeMIDIchannel - 1); midiSendControlChange(11,127); //midi expression to 0
usbMIDI.sendControlChange(11,127, activeMIDIchannel); //midi expression to 0
dinMIDIsendControlChange(11,127, activeMIDIchannel - 1);
} }
else { breathAT = 0; } else { breathAT = 0; }
if (K5) { //1tr turn on velocity if (K5) { //1tr turn on velocity
velocity = 0; velocity = 0;
usbMIDI.sendControlChange(7,127, activeMIDIchannel); //midi vol to 127 midiSendControlChange(7,127); //midi vol to 127
dinMIDIsendControlChange(7,127, activeMIDIchannel - 1); midiSendControlChange(11,127); //midi expression to 0
usbMIDI.sendControlChange(11,127, activeMIDIchannel); //midi expression to 0
dinMIDIsendControlChange(11,127, activeMIDIchannel - 1);
} }
else { velocity = 127; } else { velocity = 127; }
if (!K1 && !K3 && !K4) { if (!K1 && !K3 && !K4) {
breathCC = 0; breathCC = 0;
usbMIDI.sendControlChange(7,127, activeMIDIchannel); //midi vol to 127 midiSendControlChange(7,127); //midi vol to 127
dinMIDIsendControlChange(7,127, activeMIDIchannel - 1); midiSendControlChange(11,127); //midi expression to 127
usbMIDI.sendControlChange(11,127, activeMIDIchannel); //midi expression to 127
dinMIDIsendControlChange(11,127, activeMIDIchannel - 1);
} }
} }
} }
@ -986,41 +974,33 @@ void mainLoop() {
breath(); // send breath data breath(); // send breath data
fingeredNote=noteValueCheck(fingeredNote); fingeredNote=noteValueCheck(fingeredNote);
if (priority){ // mono prio to last chord note if (priority){ // mono prio to last chord note
usbMIDI.sendNoteOn(fingeredNote, velocitySend, activeMIDIchannel); // send Note On message for new note midiSendNoteOn(fingeredNote, velocitySend); // send Note On message for new note
dinMIDIsendNoteOn(fingeredNote, velocitySend, activeMIDIchannel - 1);
} }
if (parallelChord){ if (parallelChord){
for (int i=0; i < addedIntervals; i++){ for (int i=0; i < addedIntervals; i++){
usbMIDI.sendNoteOn(noteValueCheck(fingeredNote+slurInterval[i]), velocitySend, activeMIDIchannel); // send Note On message for new note midiSendNoteOn(noteValueCheck(fingeredNote+slurInterval[i]), velocitySend); // send Note On message for new note
dinMIDIsendNoteOn(noteValueCheck(fingeredNote+slurInterval[i]), velocitySend, activeMIDIchannel - 1);
} }
} }
if (slurSustain){ if (slurSustain){
usbMIDI.sendControlChange(64,127, activeMIDIchannel); midiSendControlChange(64,127);
dinMIDIsendControlChange(64,127, activeMIDIchannel - 1);
slurBase = fingeredNote; slurBase = fingeredNote;
addedIntervals = 0; addedIntervals = 0;
} }
if (subOctaveDouble){ if (subOctaveDouble){
usbMIDI.sendNoteOn(noteValueCheck(fingeredNote-12), velocitySend, activeMIDIchannel); midiSendNoteOn(noteValueCheck(fingeredNote-12), velocitySend);
dinMIDIsendNoteOn(noteValueCheck(fingeredNote-12), velocitySend, activeMIDIchannel - 1);
if (parallelChord){ if (parallelChord){
for (int i=0; i < addedIntervals; i++){ for (int i=0; i < addedIntervals; i++){
usbMIDI.sendNoteOn(noteValueCheck(fingeredNote+slurInterval[i]-12), velocitySend, activeMIDIchannel); // send Note On message for new note midiSendNoteOn(noteValueCheck(fingeredNote+slurInterval[i]-12), velocitySend); // send Note On message for new note
dinMIDIsendNoteOn(noteValueCheck(fingeredNote+slurInterval[i]-12), velocitySend, activeMIDIchannel - 1);
} }
} }
} }
if (rotatorOn){ if (rotatorOn){
usbMIDI.sendNoteOn(noteValueCheck(fingeredNote+parallel), velocitySend, activeMIDIchannel); // send Note On message for new note midiSendNoteOn(noteValueCheck(fingeredNote+parallel), velocitySend); // send Note On message for new note
dinMIDIsendNoteOn(noteValueCheck(fingeredNote+parallel), velocitySend, activeMIDIchannel - 1);
if (currentRotation < 3) currentRotation++; else currentRotation = 0; if (currentRotation < 3) currentRotation++; else currentRotation = 0;
usbMIDI.sendNoteOn(noteValueCheck(fingeredNote+rotations[currentRotation]), velocitySend, activeMIDIchannel); // send Note On message for new note midiSendNoteOn(noteValueCheck(fingeredNote+rotations[currentRotation]), velocitySend); // send Note On message for new note
dinMIDIsendNoteOn(noteValueCheck(fingeredNote+rotations[currentRotation]), velocitySend, activeMIDIchannel - 1);
} }
if (!priority){ // mono prio to base note if (!priority){ // mono prio to base note
usbMIDI.sendNoteOn(fingeredNote, velocitySend, activeMIDIchannel); // send Note On message for new note midiSendNoteOn(fingeredNote, velocitySend); // send Note On message for new note
dinMIDIsendNoteOn(fingeredNote, velocitySend, activeMIDIchannel - 1);
} }
activeNote=fingeredNote; activeNote=fingeredNote;
mainState = NOTE_ON; mainState = NOTE_ON;
@ -1035,38 +1015,30 @@ void mainLoop() {
// Value has fallen below threshold - turn the note off // Value has fallen below threshold - turn the note off
activeNote=noteValueCheck(activeNote); activeNote=noteValueCheck(activeNote);
if (priority){ if (priority){
usbMIDI.sendNoteOff(activeNote, velocitySend, activeMIDIchannel); // send Note Off message midiSendNoteOff(activeNote); // send Note Off message
dinMIDIsendNoteOff(activeNote, velocitySend, activeMIDIchannel - 1);
} }
if (parallelChord){ if (parallelChord){
for (int i=0; i < addedIntervals; i++){ for (int i=0; i < addedIntervals; i++){
usbMIDI.sendNoteOff(noteValueCheck(activeNote+slurInterval[i]), velocitySend, activeMIDIchannel); // send Note On message for new note midiSendNoteOff(noteValueCheck(activeNote+slurInterval[i])); // send Note On message for new note
dinMIDIsendNoteOff(noteValueCheck(activeNote+slurInterval[i]), velocitySend, activeMIDIchannel - 1);
} }
} }
if (subOctaveDouble){ if (subOctaveDouble){
usbMIDI.sendNoteOff(noteValueCheck(activeNote-12), velocitySend, activeMIDIchannel); midiSendNoteOff(noteValueCheck(activeNote-12));
dinMIDIsendNoteOff(noteValueCheck(activeNote-12), velocitySend, activeMIDIchannel - 1);
if (parallelChord){ if (parallelChord){
for (int i=0; i < addedIntervals; i++){ for (int i=0; i < addedIntervals; i++){
usbMIDI.sendNoteOff(noteValueCheck(activeNote+slurInterval[i]-12), velocitySend, activeMIDIchannel); // send Note On message for new note midiSendNoteOff(noteValueCheck(activeNote+slurInterval[i]-12)); // send Note On message for new note
dinMIDIsendNoteOff(noteValueCheck(activeNote+slurInterval[i]-12), velocitySend, activeMIDIchannel - 1);
} }
} }
} }
if (rotatorOn){ if (rotatorOn){
usbMIDI.sendNoteOff(noteValueCheck(activeNote+parallel), velocitySend, activeMIDIchannel); // send Note Off message for old note midiSendNoteOff(noteValueCheck(activeNote+parallel)); // send Note Off message for old note
dinMIDIsendNoteOff(noteValueCheck(activeNote+parallel), velocitySend, activeMIDIchannel - 1); midiSendNoteOff(noteValueCheck(activeNote+rotations[currentRotation])); // send Note Off message for old note
usbMIDI.sendNoteOff(noteValueCheck(activeNote+rotations[currentRotation]), velocitySend, activeMIDIchannel); // send Note Off message for old note
dinMIDIsendNoteOff(noteValueCheck(activeNote+rotations[currentRotation]), velocitySend, activeMIDIchannel - 1);
} }
if (!priority){ if (!priority){
usbMIDI.sendNoteOff(activeNote, velocitySend, activeMIDIchannel); // send Note Off message midiSendNoteOff(activeNote); // send Note Off message
dinMIDIsendNoteOff(activeNote, velocitySend, activeMIDIchannel - 1);
} }
if (slurSustain){ if (slurSustain){
usbMIDI.sendControlChange(64,0, activeMIDIchannel); midiSendControlChange(64,0);
dinMIDIsendControlChange(64,0, activeMIDIchannel - 1);
} }
breathLevel=0; breathLevel=0;
mainState = NOTE_OFF; mainState = NOTE_OFF;
@ -1091,75 +1063,60 @@ void mainLoop() {
} }
activeNote=noteValueCheck(activeNote); activeNote=noteValueCheck(activeNote);
if ((parallelChord || subOctaveDouble || rotatorOn) && priority){ // poly playing, send old note off before new note on if ((parallelChord || subOctaveDouble || rotatorOn) && priority){ // poly playing, send old note off before new note on
usbMIDI.sendNoteOff(activeNote, velocitySend, activeMIDIchannel); // send Note Off message for old note midiSendNoteOff(activeNote); // send Note Off message for old note
dinMIDIsendNoteOff(activeNote, velocitySend, activeMIDIchannel - 1);
} }
if (parallelChord){ if (parallelChord){
for (int i=0; i < addedIntervals; i++){ for (int i=0; i < addedIntervals; i++){
usbMIDI.sendNoteOff(noteValueCheck(activeNote+slurInterval[i]), velocitySend, activeMIDIchannel); // send Note Off message for old note midiSendNoteOff(noteValueCheck(activeNote+slurInterval[i])); // send Note Off message for old note
dinMIDIsendNoteOff(noteValueCheck(activeNote+slurInterval[i]), velocitySend, activeMIDIchannel - 1);
} }
} }
if (subOctaveDouble){ if (subOctaveDouble){
usbMIDI.sendNoteOff(noteValueCheck(activeNote-12), velocitySend, activeMIDIchannel); // send Note Off message for old note midiSendNoteOff(noteValueCheck(activeNote-12)); // send Note Off message for old note
dinMIDIsendNoteOff(noteValueCheck(activeNote-12), velocitySend, activeMIDIchannel - 1);
if (parallelChord){ if (parallelChord){
for (int i=0; i < addedIntervals; i++){ for (int i=0; i < addedIntervals; i++){
usbMIDI.sendNoteOff(noteValueCheck(activeNote+slurInterval[i]-12), velocitySend, activeMIDIchannel); // send Note Off message for old note midiSendNoteOff(noteValueCheck(activeNote+slurInterval[i]-12)); // send Note Off message for old note
dinMIDIsendNoteOff(noteValueCheck(activeNote+slurInterval[i]-12), velocitySend, activeMIDIchannel - 1);
} }
} }
} }
if (rotatorOn){ if (rotatorOn){
usbMIDI.sendNoteOff(noteValueCheck(activeNote+parallel), velocitySend, activeMIDIchannel); // send Note Off message for old note midiSendNoteOff(noteValueCheck(activeNote+parallel)); // send Note Off message for old note
dinMIDIsendNoteOff(noteValueCheck(activeNote+parallel), velocitySend, activeMIDIchannel - 1); midiSendNoteOff(noteValueCheck(activeNote+rotations[currentRotation])); // send Note Off message for old note
usbMIDI.sendNoteOff(noteValueCheck(activeNote+rotations[currentRotation]), velocitySend, activeMIDIchannel); // send Note Off message for old note
dinMIDIsendNoteOff(noteValueCheck(activeNote+rotations[currentRotation]), velocitySend, activeMIDIchannel - 1);
} }
if ((parallelChord || subOctaveDouble || rotatorOn) && !priority){ // poly playing, send old note off before new note on if ((parallelChord || subOctaveDouble || rotatorOn) && !priority){ // poly playing, send old note off before new note on
usbMIDI.sendNoteOff(activeNote, velocitySend, activeMIDIchannel); // send Note Off message for old note midiSendNoteOff(activeNote); // send Note Off message for old note
dinMIDIsendNoteOff(activeNote, velocitySend, activeMIDIchannel - 1);
} }
fingeredNote=noteValueCheck(fingeredNote); fingeredNote=noteValueCheck(fingeredNote);
if (priority){ if (priority){
usbMIDI.sendNoteOn(fingeredNote, velocitySend, activeMIDIchannel); // send Note On message for new note midiSendNoteOn(fingeredNote, velocitySend); // send Note On message for new note
dinMIDIsendNoteOn(fingeredNote, velocitySend, activeMIDIchannel - 1);
} }
if (parallelChord){ if (parallelChord){
for (int i=0; i < addedIntervals; i++){ for (int i=0; i < addedIntervals; i++){
usbMIDI.sendNoteOn(noteValueCheck(fingeredNote+slurInterval[i]), velocitySend, activeMIDIchannel); // send Note On message for new note midiSendNoteOn(noteValueCheck(fingeredNote+slurInterval[i]), velocitySend); // send Note On message for new note
dinMIDIsendNoteOn(noteValueCheck(fingeredNote+slurInterval[i]), velocitySend, activeMIDIchannel - 1);
} }
} }
if (subOctaveDouble){ if (subOctaveDouble){
usbMIDI.sendNoteOn(noteValueCheck(fingeredNote-12), velocitySend, activeMIDIchannel); // send Note On message for new note midiSendNoteOn(noteValueCheck(fingeredNote-12), velocitySend); // send Note On message for new note
dinMIDIsendNoteOn(noteValueCheck(fingeredNote-12), velocitySend, activeMIDIchannel - 1);
if (parallelChord){ if (parallelChord){
for (int i=0; i < addedIntervals; i++){ for (int i=0; i < addedIntervals; i++){
usbMIDI.sendNoteOn(noteValueCheck(fingeredNote+slurInterval[i]-12), velocitySend, activeMIDIchannel); // send Note On message for new note midiSendNoteOn(noteValueCheck(fingeredNote+slurInterval[i]-12), velocitySend); // send Note On message for new note
dinMIDIsendNoteOn(noteValueCheck(fingeredNote+slurInterval[i]-12), velocitySend, activeMIDIchannel - 1);
} }
} }
} }
if (rotatorOn){ if (rotatorOn){
usbMIDI.sendNoteOn(noteValueCheck(fingeredNote+parallel), velocitySend, activeMIDIchannel); // send Note On message for new note midiSendNoteOn(noteValueCheck(fingeredNote+parallel), velocitySend); // send Note On message for new note
dinMIDIsendNoteOn(noteValueCheck(fingeredNote+parallel), velocitySend, activeMIDIchannel - 1);
if (currentRotation < 3) currentRotation++; else currentRotation = 0; if (currentRotation < 3) currentRotation++; else currentRotation = 0;
usbMIDI.sendNoteOn(noteValueCheck(fingeredNote+rotations[currentRotation]), velocitySend, activeMIDIchannel); // send Note On message for new note midiSendNoteOn(noteValueCheck(fingeredNote+rotations[currentRotation]), velocitySend); // send Note On message for new note
dinMIDIsendNoteOn(noteValueCheck(fingeredNote+rotations[currentRotation]), velocitySend, activeMIDIchannel - 1);
} }
if (!priority){ if (!priority){
usbMIDI.sendNoteOn(fingeredNote, velocitySend, activeMIDIchannel); // send Note On message for new note midiSendNoteOn(fingeredNote, velocitySend); // send Note On message for new note
dinMIDIsendNoteOn(fingeredNote, velocitySend, activeMIDIchannel - 1);
} }
if (!parallelChord && !subOctaveDouble && !rotatorOn){ // mono playing, send old note off after new note on if (!parallelChord && !subOctaveDouble && !rotatorOn){ // mono playing, send old note off after new note on
usbMIDI.sendNoteOff(activeNote, velocitySend, activeMIDIchannel); // send Note Off message midiSendNoteOff(activeNote); // send Note Off message
dinMIDIsendNoteOff(activeNote, velocitySend, activeMIDIchannel - 1);
} }
if (slurSustain){ if (slurSustain){
@ -1204,7 +1161,6 @@ void mainLoop() {
lastFingering=fingeredNote; lastFingering=fingeredNote;
//do menu stuff //do menu stuff
menu(); menu();
}
} }
//_______________________________________________________________________________________________ FUNCTIONS //_______________________________________________________________________________________________ FUNCTIONS
@ -1329,86 +1285,6 @@ int patchLimit(int value){
if (value < 1) return 1; else if (value > 128) return 128; else return value; if (value < 1) return 1; else if (value > 128) return 128; else return value;
} }
void midiPanic(){ // all notes off
usbMIDI.sendControlChange(123, 0, activeMIDIchannel);
dinMIDIsendControlChange(123, 0, activeMIDIchannel - 1);
for (int i = 0; i < 128; i++){
usbMIDI.sendNoteOff(i,0,activeMIDIchannel);
dinMIDIsendNoteOff(i,0,activeMIDIchannel - 1);
delay(2);
}
}
//**************************************************************
void midiReset(){ // reset controllers
usbMIDI.sendControlChange(7, 100, activeMIDIchannel);
dinMIDIsendControlChange(7, 100, activeMIDIchannel - 1);
usbMIDI.sendControlChange(11, 127, activeMIDIchannel);
dinMIDIsendControlChange(11, 127, activeMIDIchannel - 1);
}
//**************************************************************
// Send a three byte din midi message
void midiSend3B(byte midistatus, byte data1, byte data2) {
Serial3.write(midistatus);
Serial3.write(data1);
Serial3.write(data2);
}
//**************************************************************
// Send a two byte din midi message
void midiSend2B(byte midistatus, byte data) {
Serial3.write(midistatus);
Serial3.write(data);
}
//**************************************************************
// Send din pitchbend
void dinMIDIsendPitchBend(int pb, byte ch) {
int pitchLSB = pb & 0x007F;
int pitchMSB = (pb >>7) & 0x007F;
midiSend3B((0xE0 | ch), pitchLSB, pitchMSB);
}
//**************************************************************
// Send din control change
void dinMIDIsendControlChange(byte ccNumber, int cc, byte ch) {
midiSend3B((0xB0 | ch), ccNumber, cc);
}
//**************************************************************
// Send din note on
void dinMIDIsendNoteOn(byte note, int vel, byte ch) {
midiSend3B((0x90 | ch), note, vel);
}
//**************************************************************
// Send din note off
void dinMIDIsendNoteOff(byte note, int vel, byte ch) {
midiSend3B((0x80 | ch), note, vel);
}
//**************************************************************
// Send din aftertouch
void dinMIDIsendAfterTouch(byte value, byte ch) {
midiSend2B((0xD0 | ch), value);
}
//**************************************************************
// Send din program change
void dinMIDIsendProgramChange(byte value, byte ch) {
midiSend2B((0xC0 | ch), value);
}
//************************************************************** //**************************************************************
void statusLEDs() { void statusLEDs() {
@ -1441,21 +1317,18 @@ void breath(){
if (breathCCval != oldbreath){ // only send midi data if breath has changed from previous value if (breathCCval != oldbreath){ // only send midi data if breath has changed from previous value
if (breathCC){ if (breathCC){
// send midi cc // send midi cc
usbMIDI.sendControlChange(ccList[breathCC], breathCCval, activeMIDIchannel); midiSendControlChange(ccList[breathCC], breathCCval);
dinMIDIsendControlChange(ccList[breathCC], breathCCval, activeMIDIchannel - 1);
} }
if (breathAT){ if (breathAT){
// send aftertouch // send aftertouch
usbMIDI.sendAfterTouch(breathCCval, activeMIDIchannel); midiSendAfterTouch(breathCCval);
dinMIDIsendAfterTouch(breathCCval, activeMIDIchannel - 1);
} }
oldbreath = breathCCval; oldbreath = breathCCval;
} }
if (breathCCvalHires != oldbreathhires){ if (breathCCvalHires != oldbreathhires){
if ((breathCC > 4) && (breathCC < 9)){ // send high resolution midi if ((breathCC > 4) && (breathCC < 9)){ // send high resolution midi
usbMIDI.sendControlChange(ccList[breathCC]+32, breathCCvalFine, activeMIDIchannel); midiSendControlChange(ccList[breathCC]+32, breathCCvalFine);
dinMIDIsendControlChange(ccList[breathCC]+32, breathCCvalFine, activeMIDIchannel - 1);
} }
oldbreathhires = breathCCvalHires; oldbreathhires = breathCCvalHires;
} }
@ -1556,12 +1429,7 @@ void pitch_bend(){
//Serial.println(oldpb); //Serial.println(oldpb);
if (pitchBend != oldpb){// only send midi data if pitch bend has changed from previous value if (pitchBend != oldpb){// only send midi data if pitch bend has changed from previous value
#if defined(NEWTEENSYDUINO) midiSendPitchBend(pitchBend);
usbMIDI.sendPitchBend(pitchBend-8192, activeMIDIchannel); // newer teensyduino "pitchBend-8192" older just "pitchBend"... strange thing to change
#else
usbMIDI.sendPitchBend(pitchBend, activeMIDIchannel);
#endif
dinMIDIsendPitchBend(pitchBend, activeMIDIchannel - 1);
oldpb=pitchBend; oldpb=pitchBend;
} }
} }
@ -1609,31 +1477,27 @@ void extraController(){
if (!extracIsOn) { if (!extracIsOn) {
extracIsOn=1; extracIsOn=1;
if (extraCT == 4){ //Sustain ON if (extraCT == 4){ //Sustain ON
usbMIDI.sendControlChange(64,127, activeMIDIchannel); midiSendControlChange(64, 127);
dinMIDIsendControlChange(64,127, activeMIDIchannel - 1);
} }
} }
if (extraCT == 1){ //Send modulation if (extraCT == 1){ //Send modulation
int extracCC = map(constrain(exSensor,extracThrVal,extracMaxVal),extracThrVal,extracMaxVal,1,127); int extracCC = map(constrain(exSensor,extracThrVal,extracMaxVal),extracThrVal,extracMaxVal,1,127);
if (extracCC != oldextrac){ if (extracCC != oldextrac){
usbMIDI.sendControlChange(1,extracCC, activeMIDIchannel); midiSendControlChange(1, extracCC);
dinMIDIsendControlChange(1,extracCC, activeMIDIchannel - 1);
} }
oldextrac = extracCC; oldextrac = extracCC;
} }
if (extraCT == 2){ //Send foot pedal (CC#4) if (extraCT == 2){ //Send foot pedal (CC#4)
int extracCC = map(constrain(exSensor,extracThrVal,extracMaxVal),extracThrVal,extracMaxVal,1,127); int extracCC = map(constrain(exSensor,extracThrVal,extracMaxVal),extracThrVal,extracMaxVal,1,127);
if (extracCC != oldextrac){ if (extracCC != oldextrac){
usbMIDI.sendControlChange(4,extracCC, activeMIDIchannel); midiSendControlChange(4, extracCC);
dinMIDIsendControlChange(4,extracCC, activeMIDIchannel - 1);
} }
oldextrac = extracCC; oldextrac = extracCC;
} }
if ((extraCT == 3) && (breathCC != 9)){ //Send filter cutoff (CC#74) if ((extraCT == 3) && (breathCC != 9)){ //Send filter cutoff (CC#74)
int extracCC = map(constrain(exSensor,extracThrVal,extracMaxVal),extracThrVal,extracMaxVal,1,127); int extracCC = map(constrain(exSensor,extracThrVal,extracMaxVal),extracThrVal,extracMaxVal,1,127);
if (extracCC != oldextrac){ if (extracCC != oldextrac){
usbMIDI.sendControlChange(74,extracCC, activeMIDIchannel); midiSendControlChange(74, extracCC);
dinMIDIsendControlChange(74,extracCC, activeMIDIchannel - 1);
} }
oldextrac = extracCC; oldextrac = extracCC;
} }
@ -1642,28 +1506,24 @@ void extraController(){
if (extraCT == 1){ //MW if (extraCT == 1){ //MW
if (oldextrac != 0){ if (oldextrac != 0){
//send modulation 0 //send modulation 0
usbMIDI.sendControlChange(1,0, activeMIDIchannel); midiSendControlChange(1, 0);
dinMIDIsendControlChange(1,0, activeMIDIchannel - 1);
oldextrac = 0; oldextrac = 0;
} }
} else if (extraCT == 2){ //FP } else if (extraCT == 2){ //FP
if (oldextrac != 0){ if (oldextrac != 0){
//send foot pedal 0 //send foot pedal 0
usbMIDI.sendControlChange(4,0, activeMIDIchannel); midiSendControlChange(4, 0);
dinMIDIsendControlChange(4,0, activeMIDIchannel - 1);
oldextrac = 0; oldextrac = 0;
} }
} else if ((extraCT == 3) && (breathCC != 9)){ //CF } else if ((extraCT == 3) && (breathCC != 9)){ //CF
if (oldextrac != 0){ if (oldextrac != 0){
//send filter cutoff 0 //send filter cutoff 0
usbMIDI.sendControlChange(74,0, activeMIDIchannel); midiSendControlChange(74, 0);
dinMIDIsendControlChange(74,0, activeMIDIchannel - 1);
oldextrac = 0; oldextrac = 0;
} }
} else if (extraCT == 4){ //SP } else if (extraCT == 4){ //SP
//send sustain off //send sustain off
usbMIDI.sendControlChange(64,0, activeMIDIchannel); midiSendControlChange(64, 0);
dinMIDIsendControlChange(64,0, activeMIDIchannel - 1);
} }
} }
} }
@ -1687,8 +1547,7 @@ void portamento_(){
void portOn(){ void portOn(){
if (portamento == 2){ // if portamento midi switching is enabled if (portamento == 2){ // if portamento midi switching is enabled
usbMIDI.sendControlChange(CCN_PortOnOff, 127, activeMIDIchannel); midiSendControlChange(CCN_PortOnOff, 127);
dinMIDIsendControlChange(CCN_PortOnOff, 127, activeMIDIchannel - 1);
} }
portIsOn=1; portIsOn=1;
} }
@ -1699,8 +1558,7 @@ void port(){
int portCC; int portCC;
portCC = map(constrain(biteSensor,portamThrVal,portamMaxVal),portamThrVal,portamMaxVal,0,127); portCC = map(constrain(biteSensor,portamThrVal,portamMaxVal),portamThrVal,portamMaxVal,0,127);
if (portCC!=oldport){ if (portCC!=oldport){
usbMIDI.sendControlChange(CCN_Port, portCC, activeMIDIchannel); midiSendControlChange(CCN_Port, portCC);
dinMIDIsendControlChange(CCN_Port, portCC, activeMIDIchannel - 1);
} }
oldport = portCC; oldport = portCC;
} }
@ -1709,12 +1567,10 @@ void port(){
void portOff(){ void portOff(){
if (oldport != 0){ //did a zero get sent? if not, then send one if (oldport != 0){ //did a zero get sent? if not, then send one
usbMIDI.sendControlChange(CCN_Port, 0, activeMIDIchannel); midiSendControlChange(CCN_Port, 0);
dinMIDIsendControlChange(CCN_Port, 0, activeMIDIchannel - 1);
} }
if (portamento == 2){ // if portamento midi switching is enabled if (portamento == 2){ // if portamento midi switching is enabled
usbMIDI.sendControlChange(CCN_PortOnOff, 0, activeMIDIchannel); midiSendControlChange(CCN_PortOnOff, 0);
dinMIDIsendControlChange(CCN_PortOnOff, 0, activeMIDIchannel - 1);
} }
portIsOn=0; portIsOn=0;
oldport = 0; oldport = 0;

29
midi.h Normal file
View file

@ -0,0 +1,29 @@
#ifndef __MIDI_H
#define __MIDI_H
//Enable use of USB and serial MIDI
#define USE_MIDI_USB
#define USE_MIDI_SERIAL
//Define which serial device to use
#define MidiSerial Serial3
//Set / get current midi channel
void midiSetChannel(int channel);
int midiGetChannel();
void midiSendProgramChange(int patch);
void midiSendControlChange(int ccParam, int ccValue);
void midiSendNoteOn(byte note, int velocity);
void midiSendNoteOff(byte note);
void midiSendAfterTouch(byte value);
void midiSendPitchBend(int value);
void midiReset(); // reset controllers
void midiPanic(); // turn all notes off
void dinMIDIsendPitchBend(int pb, byte ch); // Send din pitchbend
void dinMIDIsendAfterTouch(byte value, byte ch); // Send din aftertouch
#endif

129
midi.ino Normal file
View file

@ -0,0 +1,129 @@
#include "midi.h"
//Need this define here for now
#define NEWTEENSYDUINO
int midiChannel = 1;
void midiSetChannel(int channel) {
midiChannel = constrain(channel, 1, 16);
}
int midiGetChannel() {
return midiChannel;
}
void midiSendProgramChange(int patch) {
usbMIDI.sendProgramChange(patch-1, midiChannel);
dinMIDIsendProgramChange(patch-1, midiChannel-1);
}
void midiSendControlChange(int ccParam, int ccValue) {
usbMIDI.sendControlChange(ccParam, ccValue, midiChannel);
dinMIDIsendControlChange(ccParam, ccValue, midiChannel - 1);
}
void midiSendNoteOn(byte note, int velocity) {
usbMIDI.sendNoteOn(note, velocity, midiChannel);
dinMIDIsendNoteOn(note, velocity, midiChannel - 1);
}
void midiSendNoteOff(byte note) {
//Always send velocity 0 on note off to avoid confusing some synthesizers
usbMIDI.sendNoteOn(note, 0, midiChannel);
dinMIDIsendNoteOn(note, 0, midiChannel - 1);
}
void midiSendAfterTouch(byte value) {
usbMIDI.sendAfterTouch(value, midiChannel);
dinMIDIsendAfterTouch(value, midiChannel - 1);
}
void midiSendPitchBend(int value) {
#if defined(NEWTEENSYDUINO)
usbMIDI.sendPitchBend(value-8192, midiChannel); // newer teensyduino "pitchBend-8192" older just "pitchBend"... strange thing to change
#else
usbMIDI.sendPitchBend(value, midiChannel);
#endif
dinMIDIsendPitchBend(value, midiChannel - 1);
}
void midiReset() { // reset controllers
midiSendControlChange(7, 100);
midiSendControlChange(11, 127);
}
void midiPanic() { // all notes off
midiSendControlChange(123, 0);
for (int i = 0; i < 128; i++){
midiSendNoteOff(i);
delay(2);
}
}
//Serial midi functions
// Send a three byte din midi message
void midiSend3B(byte midistatus, byte data1, byte data2) {
MidiSerial.write(midistatus);
MidiSerial.write(data1);
MidiSerial.write(data2);
}
//**************************************************************
// Send a two byte din midi message
void midiSend2B(byte midistatus, byte data) {
MidiSerial.write(midistatus);
MidiSerial.write(data);
}
//**************************************************************
// Send din pitchbend
void dinMIDIsendPitchBend(int pb, byte ch) {
int pitchLSB = pb & 0x007F;
int pitchMSB = (pb >>7) & 0x007F;
midiSend3B((0xE0 | ch), pitchLSB, pitchMSB);
}
//**************************************************************
// Send din control change
void dinMIDIsendControlChange(byte ccNumber, int cc, byte ch) {
midiSend3B((0xB0 | ch), ccNumber, cc);
}
//**************************************************************
// Send din note on
void dinMIDIsendNoteOn(byte note, int vel, byte ch) {
midiSend3B((0x90 | ch), note, vel);
}
//**************************************************************
// Send din note off
void dinMIDIsendNoteOff(byte note, int vel, byte ch) {
midiSend3B((0x80 | ch), note, vel);
}
//**************************************************************
// Send din aftertouch
void dinMIDIsendAfterTouch(byte value, byte ch) {
midiSend2B((0xD0 | ch), value);
}
//**************************************************************
// Send din program change
void dinMIDIsendProgramChange(byte value, byte ch) {
midiSend2B((0xC0 | ch), value);
}