D
deciduous
Guest
Hello friends!
Yesterday I started a fun new project that involves some tricky high precision timing, and I'm having some trouble finding a way to keep it running at a fixed rate.
I'm creating a beat sequencer that uses a BPM value to find a rate at which to move through 16 steps every bar. If my BPM is set to 120 then it should sequentially trigger each step every 125 milliseconds. Then I have boolean arrays of audio samples that each have 16 indexes. If an element is set to true and its index matches the step number when it is triggered, the audio sample will be played. Here is a video demonstrating what I'm talking about:
So far this works in terms of execution, but not in terms of optimization. I'd love to find some way to make it essentially framerate independent, because it will occasionally lag which is a problem if you want something to stay precisely on beat.
Here is the code for managing the timing.
I've seen delta_time come up in a lot of my google searches, but either I can't implement it in such a way that works for what I'm trying to do, or I don't quite understand how to use it in this case (this is my guess).
Does anyone have any suggestions or ideas?
Thanks very much in advance!
Yesterday I started a fun new project that involves some tricky high precision timing, and I'm having some trouble finding a way to keep it running at a fixed rate.
I'm creating a beat sequencer that uses a BPM value to find a rate at which to move through 16 steps every bar. If my BPM is set to 120 then it should sequentially trigger each step every 125 milliseconds. Then I have boolean arrays of audio samples that each have 16 indexes. If an element is set to true and its index matches the step number when it is triggered, the audio sample will be played. Here is a video demonstrating what I'm talking about:
So far this works in terms of execution, but not in terms of optimization. I'd love to find some way to make it essentially framerate independent, because it will occasionally lag which is a problem if you want something to stay precisely on beat.
Here is the code for managing the timing.
Code:
//create event
bpm = 120;
step_time = (1 / ((bpm / 60) * 4)) * 1000; //in milliseconds
start_time = current_time;
trigger_step = false;
enum states
{
PLAYING,
PAUSED,
STOPPED
}
current_state = states.STOPPED;
Code:
//step event
switch(current_state)
{
case states.PLAYING:
if(current_time - start_time >= step_time)
{
trigger_step = true;
start_time = current_time;
}
else
trigger_step = false;
break;
case states.PAUSED:
break;
case states.STOPPED:
break;
}
Does anyone have any suggestions or ideas?
Thanks very much in advance!
Last edited by a moderator: