BeatsPerMinute useage in code running smooth
For time-critical applications you are better off not using a message-based technique as you are now.
The idea is to have a main loop and not relinquish control to the system. You can use GetTickCount to get a millisecond value to determine how much time elapses in each loop. Use Sleep to give up unused clock cycles (Sleeping saves battery life, allows other multi-tasking applications to still function properly, and is necessary if you take the multithreaded route below).
Regarding the GUI; you can either create a new thread to do the above mentioned processing in, or you can use the main thread, and add a Message Pump to your loop. Let me know if you want to go the latter route and I'll post some code.
Dan East
The idea is to have a main loop and not relinquish control to the system. You can use GetTickCount to get a millisecond value to determine how much time elapses in each loop. Use Sleep to give up unused clock cycles (Sleeping saves battery life, allows other multi-tasking applications to still function properly, and is necessary if you take the multithreaded route below).
Regarding the GUI; you can either create a new thread to do the above mentioned processing in, or you can use the main thread, and add a Message Pump to your loop. Let me know if you want to go the latter route and I'll post some code.
Dan East
I've not done any of this before, so I may be talking out of my bum here.
Firstly, what's the /4 for? You really want 4 updates for every "beat"?
Say for 60BPM you want:
60000/60 = 1000ms between beats, not /4=250ms.
The main problem will be if you're doing a lot in your callback function, or if Windows isn't giving your task much time for some reason. Can you raise the priority of your task?
Unless you're giving it a silly amount of beats per second, I'm not sure a higher resolution timer would help (unless the granularity of the SetTimer callback isn't really approaching 1ms).
In the "good old" days, I'd have latched the code to a clock interrupt/vector, but I'm guessing that's not possible through PocketPC
Cheers,
Refractor
60000/BPM/4
Firstly, what's the /4 for? You really want 4 updates for every "beat"?
Say for 60BPM you want:
60000/60 = 1000ms between beats, not /4=250ms.
The main problem will be if you're doing a lot in your callback function, or if Windows isn't giving your task much time for some reason. Can you raise the priority of your task?
Unless you're giving it a silly amount of beats per second, I'm not sure a higher resolution timer would help (unless the granularity of the SetTimer callback isn't really approaching 1ms).
In the "good old" days, I'd have latched the code to a clock interrupt/vector, but I'm guessing that's not possible through PocketPC
Cheers,
Refractor
Simply keep the value *before* the loop, and subtract it at the end...
You probably want something like:
You probably want something like:
- Code:
// try and hit the top of "stuff" once per second
unsigned long resolution = 1000;
unsigned long old_value=GetTickCount();
while(bail_out==false)
{
while((GetTickCount()-old_value)<resolution)
{
// do nothing for a while..
// you could sleep() too
}
old_value=GetTickCount();
// do "stuff" (maybe call your UpdateSong procedure here)
// set the bail_out flag to true if you need to
}
It's all relative. All you need to know is how many milliseconds occured from one point in your code to another. Here's the rough idea off the top of my head
Dan East
- Code:
DWORD dwLastTime=GetTickCount();
//dwFrequency is the number of beats per second
DWORD dwFrequency=4;
//Precalc the number of milliseconds we need to sleep
DWORD dwPeriod=1000/dwFrequency;
//Main loop
for (;;) {
//Do processing / play sound / whatever here
//Do message loop to keep our GUI responsive here
//Now we sleep to wait out the remainer of the time until the next iteration
DWORD dwCurTime=GetTickCount();
//dwElapsed is the number of milliseconds elapsed since our last iteration
int dElapsed=dwCurTime-dwLastTime;
dwLastTime=dwCurTime;
if (dwElapsed < dwPeriod)
Sleep(dwPeriod-dwElapsed);
}
Dan East
We must have been typing at the same time.
Either of those examples would work.
Dan East
Dan East
Fuzzajakka, Dan's too modest - his is "cleaner/nicer" than mine, go with that.
Cheers,
Refractor
=D =D =D THANKS!!! ok. so it's workin now... cant thank you enough!
ALLRIGHT!!! SHOWTIME!!! okok... easy.... Well... Thanks for the help!