Best way to handle TempoClocks

Imagine a ‘Play’ button on a DAW or similar. You press play. You press stop. You press play again. Each time you press ‘Play’, it starts playback immediately, not waiting for a beat on a clock.

Would you achieve this by creating a separate TempoClock each time the user presses play? Is there any issue around instantiating a lot of `TempoClock’s, or anything I should consider?

Dunno why, this area gets me a bit spooked.

go ahead and create a new TempoClock every time, as long as you remember to stop it
in general don’t worry about anything unless you have to :slight_smile:

1 Like

or are you asking about like sequencer transport type stuff?

As s2s2s says, as long as you stop clocks when you’re finished with them, there’s no harm in creating clocks.

Every TempoClock creates a c++ thread in the backend. Stopping the clock removes the thread. If you create clocks but don’t stop them, then those threads will accumulate, using CPU and memory resources (probably negligible if the clocks are idle, but still, it’s better not to let clock objects leak). If you clean up dead clocks, there shouldn’t be any build-up of resource use.

There is one other way: this isn’t often done (and it shouldn’t be done carelessly): you can set a TempoClock’s beats at any time. So when you hit play in your interface, you could set myClock.beats = 0 and then start scheduling activities starting from 0. You might need to reset the meter if you’re changing meters too.

The caveat is this: if you’re at beat 500 and something is scheduled for beat 501, you expect it to happen soon. If you set beats = 0 before that happens, then the thing will still be scheduled for 501, but that’s now over 500 beats later (not soon). So a large change in beats should happen only when the queue is empty, ideally. If you’re thinking of a DAW style transport, the queue should be cleared when you stop, so that condition shouldn’t be too hard to meet. (Or, clear out the queue just before resetting.)

No opinion about which is better :wink: just pointing out a hidden feature of TempoClock.

hjh

1 Like

Thanks both, that was really helpful. And my instincts were right; TempoClocks do need to be handled correctly.