Skip to content

Instantly share code, notes, and snippets.

@nutrun
Last active April 13, 2019 19:30
Show Gist options
  • Save nutrun/d129e4d8ddc33c2f183f77b4b95e8f41 to your computer and use it in GitHub Desktop.
Save nutrun/d129e4d8ddc33c2f183f77b4b95e8f41 to your computer and use it in GitHub Desktop.
#include <SdFat.h>
#include <MD_MIDIFile.h>
#include <SoftwareSerial.h>
#define MIDI_OUT_TX_PIN 9
#define SD_SELECT_PIN 10
unsigned short play = 0;
byte midiInByte;
SdFat SD;
MD_MIDIFile midiFile;
SoftwareSerial midiOutSerial = SoftwareSerial();
void midiCallback(midi_event *midiEvent);
void midiSilence(void);
void setup() {
// MIDI in
Serial.begin(31250);
while (!Serial);
// MIDI out
midiOutSerial.setTX(MIDI_OUT_TX_PIN);
midiOutSerial.begin(31250);
while (!midiOutSerial);
if (!SD.begin(SD_SELECT_PIN, SPI_HALF_SPEED)) {
while (1);
}
midiFile.begin(&SD);
midiFile.setMidiHandler(midiCallback);
midiFile.looping(true);
midiFile.setFilename("TEST.mid");
if (!midiFile.load()) {
while (1);
}
}
void loop() {
if (Serial.available()) {
midiInByte = Serial.read();
if (midiInByte == 0xFA) { // Start
midiFile.restart();
play = 1;
} else if (midiInByte == 0xFC) { // Stop
midiSilence();
play = 0;
}
if (play == 1) {
if (midiInByte == 0xF8) { // clock
// HELP :)
}
}
}
}
void midiCallback(midi_event *e) {
midiOutSerial.write(e->data[0] | e->channel);
midiOutSerial.write(&e->data[1], e->size - 1);
}
void midiSilence(void) {
midi_event ev;
ev.size = 0;
ev.data[ev.size++] = 0xb0;
ev.data[ev.size++] = 120;
ev.data[ev.size++] = 0;
for (ev.channel = 0; ev.channel < 16; ev.channel++) {
midiCallback(&ev);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment