Problem with timer's frequency

Hello. My friend and me are making drum-machine using juce now and we need using of timers, but we have faced a problem. We need float frequency in our HighResTimer, but all timers in juce require only “int” type of frequency. What can we do with this problem or how can we solve it?

Can you elaborate, what you intend to do with the HighResolutionTimer? It takes milliseconds as argument, and if that isn’t accurate enough, I have a gut feeling you are using it for the wrong purpose.
For sample accurate events, use the audio as synchronisation master, i.e. counting samples in the processBlock or getNextAudioBlock call.

HTH

1 Like

our drum-machine do not need to play audio, it must send midi messages with certain frequency, so we use timers for this purpose

All startTimerHz does is divide the value by 1000 and call startTimer, you could do the same by adding a function like so…

void startTimerHz (float timerFrequencyHz) noexcept
{
    startTimer (roundToInt (1000.f / timerFrequencyHz));
}

it helped a little bit, but it is still not enough accurately

If you need pinpoint accuracy I think you’ll be better off handling it in processBlock, there is a MidiBuffer in there for reading/writing MIDI events.

If you’re sending MIDI to an external device via a MidiOutput, then 1ms is accurate enough - any more precision will be lost by the MIDI drivers anyway. And HighResolutionTimer will do the job just fine (or you can use a high-priority thread yourself if you want to do slightly better)

If you’re building a plugin then you definitely do not want a timer of any sort - like the others who replied in this thread said, you should be counting samples because everything happens in blocks.

2 Likes