Forum Rule: Always post complete source code & details to reproduce any issue!
Results 1 to 9 of 9

Thread: MIDI Clock in.

  1. #1

    MIDI Clock in.

    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

  2. #2
    Senior Member ETMoody3's Avatar
    Join Date
    Mar 2014
    Location
    New Ulm, Mn
    Posts
    177
    Try using the callback

    Code:
    MIDI.setHandleClock(doSomethingAtClock);
    
    void doSomethingAtClock() { //your secret sauce here }

  3. #3
    Senior Member ETMoody3's Avatar
    Join Date
    Mar 2014
    Location
    New Ulm, Mn
    Posts
    177

  4. #4
    Quote Originally Posted by ETMoody3 View Post
    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.

  5. #5
    Quote Originally Posted by ETMoody3 View Post
    Thank you!

  6. #6
    Senior Member mortonkopf's Avatar
    Join Date
    Apr 2013
    Location
    London, uk
    Posts
    950
    @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.

  7. #7
    Quote Originally Posted by mortonkopf View Post
    @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.

  8. #8
    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:
    Process Timings: Max time (MicroSecs)
    Play Beats
    1
    SetPixels
    712
    CheckPots
    83
    CheckSwitches
    1
    setDrums
    1
    Clear Pixels
    638
    Increment Beat Counters
    1
    setVolumes
    11
    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.

  9. #9
    Thanks for everyone's help. Here it is in action.

    TD-3 with Launchpad Flappy Bird Note Generator Jam - Volca FM and TE606


    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.



Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •