Skip to content

Instantly share code, notes, and snippets.

@asus4
Created November 22, 2014 11:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save asus4/2a6da939cd87be657cb4 to your computer and use it in GitHub Desktop.
Save asus4/2a6da939cd87be657cb4 to your computer and use it in GitHub Desktop.
Load midi file from SD card. and Convert to Motor
//
// Midi2Motor.ino
//
// Using Libiraries
//
// Standard MIDI Files (SMF) Processing Library
// GPLv3
// https://arduinocode.codeplex.com/releases/view/115256
//
// SdFat
// https://github.com/greiman/SdFat
#include <SdFat.h>
#include <MD_MIDIFile.h>
#define DEBUGS(s) Serial.print(s)
#define DEBUG(s, x) { Serial.print(F(s)); Serial.print(x); }
#define DEBUGX(s, x) { Serial.print(F(s)); Serial.print(x, HEX); }
#define SERIAL_RATE 57600
#define SD_SELECT 53
#define ARRAY_SIZE(a) (sizeof(a)/sizeof(a[0]))
char *loopfile = "releaser.mid";
const int MOTOR_OUTPUTS_SIZE = 16;
// const int MOTOR_OUTPUTS[16] = {2,3,5,6,7,8,9,10,11,12,13,13,13,13,13,13}; // for UNO TEST
const int MOTOR_OUTPUTS[16] = {4,5,6,7,8,9,10,11,22,24,26,28,30,32,34,36}; // for MEGA
SdFat SD;
MD_MIDIFile SMF;
void midiCallback(midi_event *pev)
{
DEBUG("\nM T", pev->track);
DEBUG(": Ch ", pev->channel+1);
DEBUGS(" Data");
for (uint8_t i=0; i<pev->size; i++)
{
DEBUGX(" ", pev->data[i]);
}
// Find note event
if(pev->data[0] == 0x90) {
int track = pev->track;
int noteOn = pev->data[2] > 0 ? 1 : 0;
DEBUG("\ntrack ", track);
DEBUG(" note ", noteOn);
digitalWrite(MOTOR_OUTPUTS[track-1], noteOn);
}
}
void setup(void)
{
// Initialize motor pin mode
for(int i=0; i<MOTOR_OUTPUTS_SIZE; i++) {
pinMode(MOTOR_OUTPUTS[i], OUTPUT);
}
// SD
int err;
Serial.begin(SERIAL_RATE);
DEBUGS("\n[MidiFile Looper]");
// Initialize SD
if (!SD.begin(SD_SELECT, SPI_FULL_SPEED))
{
DEBUGS("\nSD init fail!");
while (true) ;
}
// Initialize MIDIFile
SMF.begin(&SD);
SMF.setMidiHandler(midiCallback);
SMF.looping(true);
// use the next file name and play it
DEBUG("\nFile: ", loopfile);
SMF.setFilename(loopfile);
err = SMF.load();
if (err != -1)
{
DEBUG("\nSMF load Error ", err);
while (true);
}
}
void loop(void)
{
// play the file
if (!SMF.isEOF())
{
SMF.getNextEvent();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment