![]() |
|
Timer Precision |
Paladin
Member #6,645
December 2005
![]() |
Ok I already asked how to use timers for my needs and such and it does work, but it's not precise. The reason it needs to be exactly precise is I'm creating a metronome where you can change the beats per minute to anything. This is the main code I am using:
That is my code right now. It works awesome, it's just the fact that the beats aren't accurate and there isn't really a difference unless you change the beats by a large margin(maybe around 30). I was wondering if there was a much more accurate way to do this? Or is my method just not accurate itself and is there a better way? Thank you. |
Richard Phipps
Member #1,632
November 2001
![]() |
Allegro's timers aren't too precise.. Here's the timing code I use: .hpp file #include <allegro.h> void start_timer(void); void reset_timer(void); int check_timer(int frac_sec); .cpp file
Once you've called start_timer, you can do say time = check_timer(100); Which will check with a precision of 100 ticks per second. You should be able to go up to at least (250) ticks per second precision. Just remember to call reset_timer before you do another check if you've found a new tick.. I.e.: if (check_timer(100) > 0) { // do stuff.. reset_timer(); }
Hope that helps. |
Paladin
Member #6,645
December 2005
![]() |
How am I supposed to implement this with my code? Do I call check_timer(); in the main while loop? Or do I replace the if(timer < 0) line in my main loop with a different variable? |
Richard Phipps
Member #1,632
November 2001
![]() |
Change: while(timer > 0) { --timer; handleMetronome(); } to: timer = check_timer(100); while (timer > 0) { reset_timer(); handleMetronome(); } You would have to add a start_timer() call before the main loop and use a timer global variable in your program that other functions can use (like you have now with an allegro timer. (or you could pass it to your functions as a variable). |
Paladin
Member #6,645
December 2005
![]() |
Ok, I have this timer code of yours put in at the top of my source and it works great, but the problem is that it plays the sound very fast. What type of time measurement am I dealing with so I can change my algorithm to find the pause inbetween each sound? |
Richard Phipps
Member #1,632
November 2001
![]() |
I'm not sure what you mean.. I don't know music calculations. But the value you call check_timer() with is the accuracy. I.e. using check_timer(1); would mean that the value returned would be the number of seconds since the last reset_timer call. 10 would be 10'ths of a second, etc.. So if check timer(100) returns 100 then 1 second has elaped. Is that helpful? |
Paladin
Member #6,645
December 2005
![]() |
Yeah that makes sense. I'm simply trying to get the pause inbetween each beat by finding the beats per second (from beats per minute). So I can change that value to whatever I want to make it more accurate? EDIT: I am also noticing that I cannot do anything when the program starts. I can't press any keys or click on anything with my mouse. I think it's because of the sound playing, but I don't think it should be doing that. |
Richard Phipps
Member #1,632
November 2001
![]() |
Quote: So I can change that value to whatever I want to make it more accurate? Yes.. Quote: I am also noticing that I cannot do anything when the program starts. I can't press any keys or click on anything with my mouse. I think it's because of the sound playing, but I don't think it should be doing that.
No idea with this problem. |
Paladin
Member #6,645
December 2005
![]() |
Ok everything is fine except the metronome code now. I'm not sure if I'm handling it correctly or not. Main Loop: Code to make sound occur every so often void handleMetronome() { if(check_timer(100) >= pause) { play_sample(click, 255, 128, 1000, FALSE); reset_timer(); } } Can I use the check_timer twice like that or am I doing it incorrectly? |
Richard Phipps
Member #1,632
November 2001
![]() |
Not sure about the sound issue. Quote: Can I use the check_timer twice like that or am I doing it incorrectly? No that's fine. However, wouldn't it be easier to do a fixed rate check, i.e. check_timer(200); And then have your metronome data take this into account with the song's tempo? |
Paladin
Member #6,645
December 2005
![]() |
Is this what you mean? void handleMetronome() { mettimer = check_timer(100); if(mettimer >= 100) { play_sample(click, 255, 128, 1000, FALSE); reset_timer(); } } This is the main loop at the moment: It's still not working either way though. Sorry to seem stupid. |
Mark Oates
Member #1,146
March 2001
![]() |
this is the way I've done it for my sequencer, and the playback is pretty acurate. To change the tempo, just call set_playback_tempo() and get_playback_tempo(). In my sequencer, however, nothing is being drawn to the screen during playback. This is to ensure acurate timing.
the only strange thing is when I change MIDI drivers, I sometimes have to set the odd multiplier to 7.0 . -- |
Paladin
Member #6,645
December 2005
![]() |
I'm trying to find a method more accurate than the allegro default timers actually. None of my code is working. >< |
GullRaDriel
Member #3,861
September 2003
![]() |
Check this thread for HighRes Timer. Here is my own wrapper. "Code is like shit - it only smells if it is not yours" |
Richard Phipps
Member #1,632
November 2001
![]() |
GullRaDriel: Did you read this thread? I already posted high res timer code.. |
GullRaDriel
Member #3,861
September 2003
![]() |
Quote: I already posted high res timer code.. I know, but there are C/C++, RDTSC, and lots of various way of using GTOD & QPC. I was not trying to say that your code is not right, I was trying to help. Quote: GullRaDriel: Did you read this thread? I am at work, and I did not took the time to read the whole thread. Last, be sure I would have put your code into the thread if only I was able to edit it. My excuse, dear RP. "Code is like shit - it only smells if it is not yours" |
|