IN SHORT:
I have a method being called at 44,100 times per second. I would like to know what to do in a situation where I need a sound to be ticked at the 13781.25th method call – thats a 192 Beats Per Minute). I have the option to round off that number and make the sound tick at the 13781st method call which would mean i am making the sound tick at 0.25th too early. this equates to being 0.00000566893424 of a second too early.
after a 100 ticks im sure the delay will certainly add up. Are there any smart work arounds that sort of keeps track of the delay and when the delay gets over a certain point then maybe ± some figures to keep the beat on track again?
Here is my code below so far..
int counter; // used to track down the amount of times the method has been called
signalMethod(){
if(counter % ceil(2,646,000/10) == 0){ //the ceil function turns the decimal point into a whole number so it can be used for analysis. but this will cut off delay.. and over time im sure it will add up.. which will cause the beats to fluctuate...
playSound();
}
counter++;
}
Your problem is just another variation of the problem addressed “Bresenham’s Algorithm”, but instead of incrementing the Y coordinate of a line drawn with a certain slope (the slope would be the beat frequency), you’re emitting ticks.
Wikipedia on Bresenham’s Algorithm