I'm trying to build a device that takes MIDI in on one channel and spits it out verbatim on a different channel.
I have a sequencer with one MIDI out controlling two synths. Both synths receive MIDI on channel 1 only, no way to change. In order to control the synths independently, I want to send MIDI on channel 1 for synth 1, and channel 16 for synth 2. Then have a 1-in 2-out MIDI thru, send one out to synth 1 receiving channel 1 data, and the other on channel 16 to the Arduino, change it into channel 1, and send it along to synth 2. Like this:
→ synth 1, channel 1
sequencer, channels 1 and 16 → MIDI split
→ Arduino, channel 16 → synth 2, channel 1
I have something working so far, attached below. It does exactly what I want it to do: takes MIDI in on channel n and sends it out on channel n. There's one encoder used for both input and output channel selection, depending on the encoder switch, and an OLED screen. It took a bit of hair pulling but seems to work fine... except there is just one problem left:
When I hit the play button on the sequencer controlling the device, the device plays back at 4 times the BPM! What's going on? Any suggestions for where to look? Any thoughts on my code so far? Thanks for your time!
#include <MIDI.h>
#include <Adafruit_SSD1306.h>
#include <splash.h>
#include <Encoder.h>
#include <Bounce2.h>
#define OLED_RESET -1
MIDI_CREATE_DEFAULT_INSTANCE();
Encoder theEnc(2,3);
Bounce debouncer = Bounce();
Adafruit_SSD1306 display(OLED_RESET);
int encSw = 4;
bool encSwState = false;
int midiInputChannel;
int midiOutputChannel;
long encOld1 = 0;
long encNew1;
long encOld2 = 0;
long encNew2;
int inputDs;
int outputDs;
void setup() {
midiInputChannel = 1;
midiOutputChannel = 1;
debouncer.attach(encSw, INPUT_PULLUP);
debouncer.interval(35);
display.begin(SSD1306_SWITCHCAPVCC, 0x3C);
display.clearDisplay();
display.setTextColor(WHITE);
display.setTextSize(1);
display.setCursor(11,1);
display.print("INPUT");
display.setCursor(16,11);
display.setTextSize(3);
display.print(midiInputChannel);
display.print(" ");
display.setTextSize(1);
display.setCursor(81,1);
display.print("OUTPUT");
display.setTextSize(3);
display.setCursor(85,11);
display.print(midiOutputChannel);
display.display();
MIDI.begin(MIDI_CHANNEL_OMNI);
}
void loop() {
MIDI.setInputChannel(midiInputChannel);
debouncer.update();
if (debouncer.fell()) {
encSwState = !encSwState;
}
if (encSwState == false) {
encNew1 = theEnc.read()/4;
if (encNew1 > encOld1) {
midiInputChannel++;
if (midiInputChannel > 16) {
midiInputChannel = 16;
}
}
if (encNew1 < encOld1) {
midiInputChannel--;
if (midiInputChannel < 1) {
midiInputChannel = 1;
}
}
encOld1 = encNew1;
} //input
if (encSwState == true) {
encNew2 = theEnc.read()/4;
if (encNew2 > encOld2) {
midiOutputChannel++;
if (midiOutputChannel > 16) {
midiOutputChannel = 16;
}
}
if (encNew2 < encOld2) {
midiOutputChannel--;
if (midiOutputChannel < 1) {
midiOutputChannel = 1;
}
}
encOld2 = encNew2;
} //output
if (inputDs != midiInputChannel || outputDs != midiOutputChannel) {
updateDisplay();
}
if (MIDI.read()) {
MIDI.send(MIDI.getType(),
MIDI.getData1(),
MIDI.getData2(),
midiOutputChannel);
}
} //loop
void updateDisplay() {
display.clearDisplay();
display.setTextColor(WHITE);
display.setTextSize(1);
display.setCursor(11,1);
display.print("INPUT");
display.setCursor(16,11);
display.setTextSize(3);
display.print(midiInputChannel);
display.print(" ");
display.setTextSize(1);
display.setCursor(81,1);
display.print("OUTPUT");
display.setTextSize(3);
display.setCursor(85,11);
display.print(midiOutputChannel);
display.display();
inputDs = midiInputChannel;
outputDs = midiOutputChannel;
} //display
