I use a timer (named metronome) to create a metronome effect.
Play method:
Metronome.Period = (60/val(TextField1.text))*1000
Metronome.Enabled = true
Metronome (timer) action
clickSound.play
But when started, usually the first and second click are not correct, then after the third clic it works ok
Any idea to make a smart start?
Now to avoid the first wrong click, when the window opens I force to play the sound with volume=0, this avoids the first click, but seems not very elegant.
[quote]
, when the window opens I force to play the sound with volume=0, this avoids the first click, but seems not very elegant.[/quote]
I do this with all of the sounds in my apps. There is a hesitation when the sound first loads that I have not found a way to avoid.
Even forcing a silence play, the metronome (timer) sometimes is wrong and plays the first two clicks not even,
Maybe I should try another aooroach to make a metronome?
Unfortunately I don’t think there is a way to get absolute perfect timing accuracy in Xojo. I would love to be corrected on that (yay hidden features!)
Timer.Period is more of a “no more frequent than this” setting, rather than a “trigger at this point.” If something else in your app consumes more execution time than the timer’s period, the timer will run it’s Action code the next time it can.
If you set a lower period, and check if enough time has passed between metronome clicks before playing one you can get closer to accurate. This workaround would still suffer from the same issue though, and a metronome that clicks off rhythm isn’t what you want!
You may be able to get even more accurate by taking the method from above and implementing it in a thread. A thread with a while true infinite loop would always run, so you take out the “no more frequent than” step of a timer. You essentially implement this by counting ticks within the thread and deciding if you want to play the sound. But it still suffers from the issue where if anything else in your app takes more processing time it’ll offset the next time the thread is allowed to execute.
you could create a timer with a low value (very fast timer) and check Xojo.Core.Date for the actual timing.
If it’s within a given time range using Xojo.core.dateinterval, you can raise an event.
The simplest would be making a subclass of timer and handle this within.