Can anyone give me advice on how to generate the appropriate MIDI sync or timecode to get an external application to sync to the Arduino?
I know I can setup Ableton Live to sync a different source.
I already have MIDI working (sending notes) via a conventional MIDI cable, but I have no experience with sync'ing. My Arduino program has a tempo (BPM) so I know the duration of a quarternote etc.
Ah, I forgot, you will need a good timer to send Midi Sync Clock to the host, here's what I use:
void timerStart()
{
TCCR1A = TCCR1B = 0;
bitWrite(TCCR1B, CS11, 1);
bitWrite(TCCR1B, WGM12, 1);
timerSetFrequency();
bitWrite(TIMSK1, OCIE1A, 1);
}
void timerSetFrequency()
{
// Calculates the Frequency for the Timer, used by the PPQ clock (Pulses Per Quarter Note) //
// This uses the 16-bit Timer1, unused by the Arduino, unless you use the analogWrite or Tone functions //
#define frequency (((midiClockBPM)*(PPQ))/60)
OCR1A = (F_CPU/ 8) / frequency - 1;
}
void timerStop(void)
{
bitWrite(TIMSK1, OCIE1A, 0);
TCCR1A = TCCR1B = OCR1A = 0;
}
ISR(TIMER1_COMPA_vect)
{
MSerial.write(0xF8); // Midi Clock Sync Tick
}
For PPQ use 24, as its the standard clock format. Unless Live let you chose 96.
I was having a hard time getting my head around the way interrupts work and I looked in the 'Arduino Cookbook' and found a reference to a library called MsTimer2 (Arduino Playground - MsTimer2). This is much simpler. I just need two functions, one to set the tempo (beats per minute) and one to send the sync.
This works with Ableton Live.
Thanks,
Dr Speed
#include <MsTimer2.h>
define PPQ 24
// when the sequencer starts, set the timer with the new tempo, something like 75 or 125, a normal bpm.
void setTimer(int beatsPerMinute){
// sync period in milliSeconds
period = ((1000L * 60)/beatsPerMinute)/PPQ;