Hi,
I am new to Juce development, and far from being a C++ expert. I'm experimenting a bit with this library, and so far I've been able to build quite a few funny (and basic) things.
One thing I cannot manage to get working is a small metronome. I know there already are some posts here, but some are old, some are without any answer... so it was no real help for me.
Here is what I want to do: when I click on a button, I launch a very basic metronome that is always producing the same midi sound, with the same interval of time.
Here the code I came with, it is called when my button is clicked.
int tempoInBpm = 150; MidiOutput* midiOutput = MidiOutput::openDevice(0); MidiMessageSequence midiMessageSequence; MidiBuffer midiBuffer; for(int i=0; i<999; i++){ MidiMessage midiMessage(153, 56, 100/*, i*1000*/); midiBuffer.addEvent(midiMessage, i); } midiOutput->startBackgroundThread(); midiOutput->sendBlockOfMessages(midiBuffer, Time::getMillisecondCounter() + 2, tempoInBpm/(double)60);
This is quite a dummy code (the loop towards 999 is just to test), but it does not really work: the accuracy of the time intervals between two notes is not good. Almost, but not 100% exact.
I saw posts about people trying to send their midi messages in a Timer, and they were answered "do not use Timer, but Audio Thread instead". Actually, as you see, I am not using any Timer.
I also saw a post about the internal behaviour of "MidiOutput::sendBlockOfMessages" that was using a Timer, so being not reliable, but is it really the case? Is it still the case with the last version of Juce (this is the one I am using)?
Moreover, if "MidiOutput::sendBlockOfMessages" is not reliable, what is the purpose of this method?
Is is really mandatory for accuracy to send Midi messages in the Audio Thread? How can I simply achieve this?
Thanks for your help!
ps: disclaimer: Sorry for the indentation of the code, I am a native Java and PHP developper, I have always coded this way, and not really wanting to change for the time being
disclaimer2: I am French, so sorry for the uncorrect language...