Hello everyone,
I’m trying to find out what’s the cleanest and reliable way to send audio from an external class to my AudioProcessor. I have this Metronome class:
class Metronome : public juce::HighResolutionTimer {
public:
Metronome();
void hiResTimerCallback();
static int nBeats;
private:
ImproSettings improSettings;
};
In this callback I would like to add something as sendAudio() which maybe could fill a buffer in the AudioProcessor class and plays it in the processBlock() function at the right time.
How could I do that?
Feel free to revolutionize my approach if you think it’s not best practice.
Are you making a plugin? You might be able to skirt round sharing data between threads in real time by counting the time in the audio thread.
There might be some clues here:
If you’re in control in the caller of processBlock, you might try passing in the timing information to the AudioProcessor in the audio thread then generating the audio in processBlock.
Judging by the name of your class, timing is all important. Might be worth considering passing a MIDI buffer rather than audio since the MIDI events are time stamped.
I’m not sure, but from your post it’s not obvious why you want to generate the metronome independently from the audio stream. I can’t think of a good reason, so the rest of my answer assumes there is none.
In that case: never ever use a timer to make a sequencer or metronome! Do it on the audio thread simply by counting samples and doing some math (e.g. a quarter note at 120 bpm and 44.1kHz is 22050 samples). You want the audio stream to be tight, so the best timer you have is the number of samples you process. In return you’ll get sample-accurate timing, save a lot of hassle communicating between (partly realtime) threads, and it will also work when rendering offline.
If you’re making a plugin, it’s very likely you don’t even have to implement a metronome yourself. You can get accurate musical timing information from the host via AudioProcessor::getPlayHead() directly in your processBlock().
Hello Hugo, I was developing a standalone application, that’s why I needed a metronome…
I went for the raw and “counting samples” way, generating sound directly in the processBlock function at the right time.
Thanks for your suggestions!