How can I calculate gate length from midi clock?


I'm currently programming a step sequencer. The code is pretty much by now, so I will not post the code right away. I'd rather explain everything first and would then post the appropriate portions of the code, if necessary.

By now I'm using the .read() method on the midi input to filter out the midi clock bytes. For each received clock a function is executed. In this function a counter is incremented and reset to 0 as soon as it reaches 96. Therefore I receive 96 ticks each bar. Now, my sequencer sends a Midi Note On for every 8th note (or every 12th tick). So if counter%12==0, a new Note is triggered. For now the Note Off is triggered if the counter%11==0. So I have 11 stages to adjust the Note Off. E.g.: I could send Note Off on counter%6 to reduce the gate length to about half.

Though what I want to do, is to adjust the Note Off without steps. In other words I want to have a pot controlling the gate length from 0% to 100% of the length between the note On of the corresponding Note and the Note On of the next note.

Does anyone know of any projects that had to deal with this kind of problem? Any code examples that I should look at?
At the moment I'm thinking of measuring the milliseconds between two clock ticks in order to calculate the bpm and send the midi Note Off after a given amount of milliseconds. On the other hand, this seems kind of unstable... Does anyone have a different idea on how to solve this problem?

Thanks in advance!

with a pot you would have 0…1023 = 1024 steps to configure it,
suppose every step <==> 2 milliseconds you could send NoteOff almost any time between from 0 to 2 seconds
you probably need to take the running average of the pot to remove some noise.

If one step of the pot-meter equals 1 beat, you can do 0…1023 beats, but here also the noise might give undesired results.

Best is to check for an rotary encoder, as these have no “noise” like a pot and if interrupt driven are way faster than analogRead.

Posting code always helps to understand the problem.

Thanks for the answer, but that's not what I mean. I'm actually using a rotary encoder. Still, what do you mean by 'a beat'? Do you mean a clock tick? This is exactly the problem. I can vary the gate length just by clock ticks. For example, I can change the note length of an 8th step from 1 clock tick to 11 ticks until the note off is sent and the next note is triggered. This is something that I already have figured out.

What I'm looking for instead is a possibility to change the note length without steps. So that the length of a note can be adjusted smoothly between 1ms and the beginning of the next note on message.