PocketMatrix forums

A read-only archive of the PocketMatrix forums (2001–2014). Posting and accounts are closed.

BeatsPerMinute useage in code running smooth

:? hmm.... H'm workin on this drum-pattern editor for PocketPC and now it's just about done... The only thing i just cant figure out is how i'm supposed to make the BPM run smoothly. At this time i'm using a SetTimer(win,1,60000/BPM/4,(TIMERPROC)Updatesong);... I think the reason why it isnt running smooth is that the program loop slows down the process and Phantom told me something about high resolution timers but i couldnt figure it out. No i'm not a pro. :lol: Just tryin to make something i can use for mixing in my band and it's GOTTA run smooth. :x well well. please help. thx!
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
I've not done any of this before, so I may be talking out of my bum here.

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 :cry:

Cheers,

Refractor
Fuzzajakka · 16 Apr 2002, 14:57 UTC — Sorry but i dont think i got that...
:lol: hehe! as i said i'm no pro, but i checked the GetTickCount thingy you told me about but all i saw was some jibberish in the help saying it counts how many milliseconds have elapsed since the system was started... Maby i missed something here that makes me look stupid(happened alot before) but hey! i'm no c++ guru... Well well. Thanks. Hehe! i'll still try to think of how i COULD use that GetTickCount thingy though....
Fuzzajakka · 16 Apr 2002, 15:01 UTC — Oh! Another reply. =)
:) thanks for the reply. No i use 135 bpm but the 4 is cause i make trance and i want to have as many sounds each beat as posieble. :D i HATE flat boring beats. guess that explains the dividing by 4. and i think i got what you meant now. about the GetTickCount... in the beginning of the loop save the value, and at the end take the new value and subtract the old one from it and i got the time that's elapsed right? :roll:
Simply keep the value *before* the loop, and subtract it at the end...

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
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
:D

Fuzzajakka, Dan's too modest - his is "cleaner/nicer" than mine, go with that. :)

Cheers,

Refractor
Fuzzajakka · 16 Apr 2002, 15:56 UTC — Awesome!
=D =D =D THANKS!!! ok. so it's workin now... cant thank you enough! :P :lol: :twisted: ALLRIGHT!!! SHOWTIME!!! okok... easy.... Well... Thanks for the help!