Tuesday, May 19, 2015

Control Playback/Recording in Avid Pro Tools with an Arduino or Teensy

I would like my Ghz Warrior to be able to sync with Pro Tools. The way I envision this feature, is to be as little as possible in touch with the computer. So instead of using Pro Tools as a Master, and slave the sequencer to it, I would like the Ghz Warrior to be the Master clock and the computer coordinates with that. Since USB hubs are cheap while MIDI DIN hubs are expensive, this should be accomplished through MIDI over USB.

While Pro Tools can send MIDI Real Time Messages (MIDI Clock) and MIDI Time Code (MTC) messages, it can only receive the latter. A quick look at the MIDI reference sheets, and the wikipedia page for better readability, reveals that a full MTC consists of two so-called frames. The combination of both frames encodes the actual second, minute and hour of the song/video/you-name-it that is playing, as well as 2 bits to communicate how many frames will actually be sent per second (FPS). This information is split into eight quarter frames (how was ONE frame defined in the first place?), each consisting of the status byte 0xF1, a zero bit, and then 7 bits encoding the described information in the following way:

  #    Data Byte    Description
  0    0000 ffff    Frame number lsbits
  1    0001 000f    Frame number msbit
  2    0010 ssss    Second lsbits
  3    0011 00ss    Second msbits
  4    0100 mmmm    Minute lsbits
  5    0101 00mm    Minute msbits
  6    0110 hhhh    Hour lsbits
  7    0111 0rrh    Rate and hour msbit

The Digital Warrior, and therefore my Ghz Warrior, is built on the Teensy platform, and I am actually truly grateful to Paul Stofregen and Robin Coon for this great system. In particular MIDI functionalities are extremely easy to implement, and a library for MIDI over USB comes with the installer that well integrates the system into the Arduino IDE. Unfortunately, what is missing in that library are routines to send MTC messages (and Real Time Messages, but more about that in another post).

I quickly wrote two routines to implement sending MTC with the library. To use that functionality on your Teensy board, copy paste the following lines to the usb_midi.h file of the midi_usb library:

On Mac OSX and for the Teensy 3.x boards, you will find the usb_midi.h file by right-clicking on your Arduino application and then selecting Show Package Contents from the drop down menu. In the now opening folder structure, usb_midi.h is located in Contents/Java/hardware/teensy/avr/cores/teensy3/

For the Teensy 2.x platforms, the file is in Contents/Java/hardware/teensy/avr/cores/usb:midi/

Make sure to select the right folder according to the hardware you have (selected in the Arduino IDE).

A fully modified version of the file for the Teensy 3.x is also available on my GitHub page. Further, I have put there an example sketch that synchronises your board with Pro Tools (Arduino/Teensy = Master, Pro Tools = Slave) and have the play/record status of Pro Tools respond to button presses. As a bonus, the option to use a standard serial connection without the need to use above library tweaks are included. By that you can also sync out-of-the-box Arduinos with Pro Tools.

Presets in the examples are a button connected to pin 4, and MTCs with 25 FPS.
For this, in Pro Tools, go to Setup/Peripherals... and set the MTC Reader Port under Synchronization to any:

Then, open Setup/Session and select 25 as the Timecode Rate:

Finally, put Pro Tools online by activating the corresponding button in the Transport window:

When you compile the sketch for the Teensy, make sure to select USB Type: "MIDI" in the Tools menu of the IDE, and everything should work out of the box.


If you use an Arduino, or compile the Teensy with the USB Type: "Serial" option, you will need to use a serial to midi bridge. This converts the incoming serial commands to actual MIDI messages in your computer. I found the free Hairless quite useful for this.

In Hairless, select your Arduino/Teensy in the Serial Port section, and the IAC Driver as a MIDI Out. Activate the Bridge and you are also done on this route :)


gio said... Best Blogger Tips[Reply to comment]Best Blogger Templates

Hello, I'm Gio.
I used your code lines with Teensy 3.1 to send MTC over USB to a PC and it works very well.
Next step I'm looking to a way to receive MTC over USB from a PC, but in the library there are no callbacks function to use.
I modified a little bit usb.midi.c file, and right now I can see just if MTC is present or not, but I can't decode it. I'm not quite experienced with programming to get it properly decode...
Any suggestion could be appreciated.
Regards. Gio

Kar G said... Best Blogger Tips[Reply to comment]Best Blogger Templates

Hi Gio,
so far I was not interested in receiving MTC. However, I can look into it the next couple of days and will post it here for you...
, Karg

gio said... Best Blogger Tips[Reply to comment]Best Blogger Templates

Hi Karg, very happy to hear from you.

I tried to add a couple of lines to usb_midi.c file;

if (type1 == 0x02 ) {
usb_midi_msg_type = 9; // 9 = MTC

Using usbMIDI.getType(), I can see if MTC from USB is present, but at the moment I'm not able to decode it in a proper way. I understand Quarter Frame MTC format, but my programming skills are not so good to make Teesny does the same...



Kar G said... Best Blogger Tips[Reply to comment]Best Blogger Templates

Hi gio,
no problem. Your code is actually right, there are just some parts missing. I posted everything you need in a new blog entry. Don't hesitate to ask if you have any further questions...
, Karg

gio said... Best Blogger Tips[Reply to comment]Best Blogger Templates

Amazing... I appreciate a lot.

The first good thing it's works. The second one now I know the steps to follow to add functions to library.

thank you again.


Kar G said... Best Blogger Tips[Reply to comment]Best Blogger Templates

You are most welcome :)
, Karg