Getting tight timing on music using settimer()
-
I’ve been having a faff making music recently. Started off checking/doing stuff per frame, in the loop. As we all know though, if you start doing too much and your framerate drops, your music slows down too, and it sounds like butt.
So moved to a time() based system. Even if your framerate slows, time keeps on ticking at the same rate, so things might get a little screwy sometimes, but at least it won’t sloooow doooownnn...
It still sounds a little off though, and starts missing beats at high tempos, and I think it’s still to do with framerate, update() and the loop. 1 frame’s worth of time slop might explain it, so I thought I’d put all my audio decision making and note playing into a function to be called with settimer(), then it’d be totally disconnected from whatever 60fps is, and would hopefully sound rock-solid and never skip a beat.
What do y’all think?
-
@toxibunny After I tried several things to get samples played at the right moment, I came to the conclusion that the settimer function is nice in theory but I got the best results using pure time() function and taking the diff to the last note/sample event.
-
Yeah, I prefer using time() as well. As an added precaution I call my music function twice in each update loop. The music function starts by checking the difference between current time() and last time a note was played. If the difference is less than the constant, nothing happens, so it shouldn’t affect performance.
-
It seems like you’re both doing the same thing, but I don’t understand it.
I think mine basically checks to see if time() is within a certain small amount past the start of a regular period, and makes sure it only happens once per period.
What exactly are you doing?
-
Actually, I’ve described mine totally wrong. I’m actually multiplying time() up, int()ing it, and then later on, seeing if the modulo of something == 0. That’s how it loops around. I wonder if it would help to use floor() or ceil() instead of int()..
-
It’s basically the same as yours, but I have a “music counter” which I increase with one if a certain amount of time has passed. Then I do modulo on the counter to trigger the sounds.
-
Well I’ve been having another listen, and actually it’s okay as it is. My program is crashing fuze though. No sign of a memory leak. Could it be the settimer()?
-
Removing the custom timer seems to have fixed the crash. Edit: putting the timer back in works fine too, so now I don’t know why it was crashing, I only know that it doesn’t do it now. Cosmic rays? Poltergeist activity? I don’t know..
-
@toxibunny If possible, could you share some code to reproduce this crash? I'd like to investigate it.
-
@Willpowered I have tried to reproduce the crash, but haven’t managed. Even undoing my fixes doesn’t seem to do it. Sorry!