MIDI Clock in.

Status
Not open for further replies.

Expensive Notes

Well-known member
Hi, I made a Drum Machine (TEE606) based on a Teensy 4.0 and an Audio Board. It works well and I implemented my first MIDI in, however, I wasn't sure how to implement MIDI clock in. The problem is having the Teensy constantly monitor the incoming Serial1 pin and not miss any clock bytes. I thought about using interrupts but I think this might be tricky based on what I have read here. A friend of mine suggested using a State Machine Approach. I also looked at an old approach that modified MIDI.h but it looks like that is out of date now as it is from 2011 https://github.com/2kohm/Teensy-USB-MIDI-CLOCK. In the end I send a CC of 110 between the two Teensys I am using every beat and it works. However I am curious if anyone could recommend an approach they have used to receive MIDI clock that works with Teensy.

Thanks,
John

Here it is in action...
https://www.youtube.com/watch?v=-UC9Nm_uOmI
 
Try using the callback

Code:
MIDI.setHandleClock(doSomethingAtClock);

void doSomethingAtClock() { //your secret sauce here }
 
Try using the callback

Code:
MIDI.setHandleClock(doSomethingAtClock);

void doSomethingAtClock() { //your secret sauce here }
Excellent. Thanks, I have been avoiding using the MIDI library until now just because. This will get me using it, much appreciated.
 
@Expensive Notes - there's nothing too mysterious in checking for the clocks coming in, as the callbacks rely on "MIDI.read();" which is done in the loop. So, in effect, you are indeed "having the Teensy constantly monitor the incoming Serial". the trick is to manage latency, so have very short jobs for the callback function to do when a clock is read. checking in on the incoming serial shouldn't be a problem.
 
@Expensive Notes - there's nothing too mysterious in checking for the clocks coming in, as the callbacks rely on "MIDI.read();" which is done in the loop. So, in effect, you are indeed "having the Teensy constantly monitor the incoming Serial". the trick is to manage latency, so have very short jobs for the callback function to do when a clock is read. checking in on the incoming serial shouldn't be a problem.

Makes sense. In a way it seems like a State Machine method. Thanks. I have measured the time it takes to check the Pots and switches and the Pots are the ones that take the most time (74 microseconds), I expect due to the ADC conversion. I am thinking I could speed that process up by reducing the bits needed and also not doing all 4 at the one time. I just need to do some calculations on the minimum time needed between MIDI clocks at the fastest BPM.
 
Thanks for your help. It was relatively easy to implement as most of the functions I used were very quick. If anyone wants to know:
At a BPM of 250 (which is plenty fast enough) the clock pulses come every 10ms.
Here are the maximum times I measured for each of the following:
[FONT=&quot]Process Timings:[/FONT]
[FONT=&quot]Max time (MicroSecs)[/FONT]
[FONT=&quot]Play Beats[/FONT]
[FONT=&quot]1[/FONT]​
[FONT=&quot]SetPixels[/FONT]
[FONT=&quot]712[/FONT]​
[FONT=&quot]CheckPots[/FONT]
[FONT=&quot]83[/FONT]​
[FONT=&quot]CheckSwitches[/FONT]
[FONT=&quot]1[/FONT]​
[FONT=&quot]setDrums[/FONT]
[FONT=&quot]1[/FONT]​
[FONT=&quot]Clear Pixels[/FONT]
[FONT=&quot]638[/FONT]​
[FONT=&quot]Increment Beat Counters[/FONT]
[FONT=&quot]1[/FONT]​
[FONT=&quot]setVolumes[/FONT]
[FONT=&quot]11[/FONT]​
This is a total of 1.448ms which gives plenty of wriggle room in a 10ms time window. Interesting that the biggest time hogs were setting the Glowbit Rainbow RGB pixels. About 10 times as long as checking 4 pots.
 
Thanks for everyone's help. Here it is in action.

[FONT=&quot]TD-3 with Launchpad Flappy Bird Note Generator Jam - Volca FM and TE606[/FONT]
[FONT=&quot]
[/FONT]
[FONT=&quot]The Launchpad Mini is controlled by a Teensy microcontroller which also sends notes to the Volca FM and TD-3. It also sends clock to the TEE606 which is playing it’s own patterns. The notes are played based on me playing Flappy Bird very badly. My poor ability with Flappy Bird actually helps to make the music more interesting.[/FONT]
[FONT=&quot]
[/FONT]
[FONT=&quot]
[/FONT]
 
Status
Not open for further replies.
Back
Top