I’ve been doing research into the various methods that JUCE offers for sleep / wait functions, to help me build my sequencer. I’ve looked at
HighResolutionTimer, but I suspect that there are still more JUCE methods that offer timed callbacks. I need some advice from more experienced programmers, because getting the right sort of timer seems like it will be critical for my program, and so far I’ve only really been working with GUI and Component related JUCE code.
I understand that the right type of timer depends entirely on the situation, so I’ll describe the shape of my project here. If there are important details that I’m missing, please point this out.
My project is a sequencer only, with no direct audio capability of its own. Once the prototype is complete, Phase 1 is to get it working with OSC and / or MIDI, so that it can communicate with other audio software (ie. PD) to work as a master sequencer. Phase 2 is to build in VST hosting capabilities, and also give it the ability to play samples stored in a sample bank. Phase 2 is very far away and I probably won’t get close to it for another year or two. However, I wouldn’t want to jinx myself at this early stage by writing code that will create headaches for phase 2 (ie. code that is not thread-safe).
The sequencer I am building is not a regular grid sequencer which sends out ticks at regular intervals (a la
Timer). Instead, the user cues custom messages, which are delayed by any number of milliseconds and spit out again at the right time. Crucially, the system needs to be able to deal with any number of messages with different delay times, all running in parallel. (I have an idea about how this might work: cued messaged are stored in an ordered list, and the timer picks them off the front one by one (side question: does such a class already exist?))
All data is stored in a
ValueTreehierarchy which is quite complex (already implemented), and there will be a certain amount of string parsing and interpreting done on the cued messages (not yet implemented). I can provide more details here if necessary; I’m bringing up ValueTrees here because I know they are not thread safe…
One question which seems crucial here is do I need to utilize multiple threads? Given that my sequencer won’t be working with audio directly, I am hoping that I can keep everything on the message thread, use
Time:: waitForMillisecondCounter(), and keep the code-base nice and simple. However, I don’t want my program to freeze up, and as I mentioned already, I don’t want to be creating barriers for myself in the phase 2, and so perhaps I should be thinking about more robust approaches at this stage.
If people here recommend that I use multiple threads, then I’m going to respond with a few follow-up questions about how I might do that. But for now, the questions are:
- Should I put my timer on its own thread?
- Which timer should I use?