This is a piece of code for Arduino to decode MidiTimeCode.
It can be usefull for some people that wants to deal with MTC.
/*
Midi Time Code Reader.
For information about MTC : http://home.roadrunner.com/~jgglatt/tech/mtc.htm
*/
#include <stdio.h> // sprintf belongs to this lib
enum { F24 = 0, F25 = 2, F30DF = 4, F30 = 6 }; // Frames type
byte h, m, s, f; // hour, minutes, secondes, frames time code
byte frameType; // speed of MTC : 24fps / 25fps / 30 drop frame / 30 fps
byte buf[64]; // Serial Midi Buffer
byte tc[20]; // array to old timecode
void setup()
{
Serial3.begin(31250); // I use Arduino Mega TX3/RX3 serial / Modify for other arduino hardware
Serial.begin(115200); // To trace and display timeocde
}
void loop()
{
int bytestoRead;
bytestoRead = Serial3.available();
if (bytestoRead>0)
{
for (int i=0; i<bytestoRead; i++)
{
buf[i]=Serial3.read();
if (buf[i]!=0xF1)
{
int indice;
indice = (buf[i] & 0xf0)>>4;
if (indice>7)
continue;
tc[indice]= buf[i] & 0x0f;
if (indice==7)
{
char toDisp[30];
char typeStr[10];
byte frameType;
frameType = tc[7] & 0x06;
h = (tc[7] & 0x01)*16 + tc[6];
m = tc[5]*16+tc[4];
s = tc[3]*16+tc[2];
f = tc[1]*16+tc[0];
if (h>23) h = 23;
if (m>59) m = 59;
if (s>59) s = 59;
if (f>30) f = 30;
switch(frameType)
{
case F24:
strcpy(typeStr, "24 fps"); break;
case F25:
strcpy(typeStr, "25 fps"); break;
case F30DF:
strcpy(typeStr, "30df fps"); break;
case F30:
strcpy(typeStr, "30 fps"); break;
default:
strcpy(typeStr, "Error"); break;
}
sprintf(toDisp, "TC %02d:%02d:%02d.%02d / %s", h, m, s, f, typeStr);
Serial.println(toDisp);
}
}
}
}
delay(50); // Save time for other things. Be carefull, every 2 frames as MTC works is around 8/100 of seconde !
}
Hope it can help few of you.
Grag