MIDI Clock should drive a simple background change

Hi everyone!

This is my first post into this forum. Thank u for having me ;).

I’m trying to code a simple sketch: An alternating black/white background synced with Abletons clock. The snyc over midiBus and IAC works find I think. But when I run the sketch. I’m experiencing a lot of drop outs. So to say: You don’t see the “metro” (black and white) repeating reaguraly. The midiBus sends for every quarter note 24 pulses (24ppqn). Then the way I do is simple; I just calcualte the rest of 24 with “%” and divide by 24 and load this into a float called “lfo”. After this I just check when the float “lfo” is 0 and if yes I draw a white background. But it doesen’t seems to work. :frowning:

Can you help me to solve this problem? I’m really stuck. Maybe I’m overviewing something. Maybe it’s an update problem and the lfo is just jumping from number to number and maybe skips the 0 from time to time? I really dont know!!

Thank you so much!

To make it simple - this is the code (btw: ignore the movie library and code):

    Basic tutorial to create animations in sync with a MIDI clock
    by: luiscript
    Important info about MIDI clock standard:
    clock     0xF8 (248)
    start     0xFA (250)
    continue  0xFB (251)
    stop      0xFC (252)

// download this library or you would get an error
  import themidibus.*;
//video library
  import processing.video.*;

// this is or MIDI bus object
  MidiBus myBus;

// counts the total of quarter notes
  int timing = 0; 
// this will be our LFO
  float lfo = 0.0;
  int counter = 0;
  int timing2;
// Video Load
  Movie movie;

void setup() {
  movie = new Movie(this, "blur.mp4");

  //this prints all the available MIDI ports 
  //select the MIDI port of your preference
  //in my case port 0 named "Bus 1"
  myBus = new MidiBus(this, 1, "Bus 1"); 

//this function will be called when raw MIDI data arrives
void rawMidi(byte[] data) {  
  if(data[0] == (byte)0xFC) {         // TRUE when MIDI clock stops.
    // reset timing when clock stops to stay in sync for the next start
    timing = 0;
  } else if(data[0] == (byte)0xF8) {  // TRUE every MIDI clock pulse
    //we need to increase timing every pulse to get the total count
    // MIDI clock sends 24 ppqn (pulses per quarter note)
    // with this formula, lfo will oscillate between 0 and 1 every quarter note
    lfo = (timing % 24 ) / 24.0;
  if (lfo == 0){
    println("LFO: " + lfo);
    println("new " + counter);
    counter = counter + 1;
    // now you can use lfo to easily animate whatever you want in sync with BPM

void movieEvent(Movie m) {

void draw() {
  if (lfo == 0){
    //image(movie, 0, 0, width, height);
  //since we have lfo oscillating between 0 and 1, we can use it to animate the size of this ellipse in sync
  //ellipse(width/2, height/2, 100 * lfo, 100 * lfo);
1 Like

Please format your Code correctly with the </> sign at the top.

Also, you might want to try using < 1 instead of == 0, cause the variable is a float.

Ex :

int timing = 127;

float lfo = (timing%24)/24; // 127%24 = 7, 7/24 = 0.291. 

Although it technically should give back a 0/24 = 0, but who knows what happens…

So, idk… also take Note that i never worked with Midi, so i don‘t really know what‘s going on :sweat_smile:

1 Like

As Lexyth explained, this won’t work.

To see why try printing its value to the console with print() and see what lfo is doing.

1 Like

thank you guys for the help. I tried it it with just checking lfo for a greater range of values something like if “lfo is > 12 then” do this and this. thats all haha. thank you again for the help!

1 Like

Hi littlecat! Can you post your final code that works? Thanks!!