-=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- (c) WidthPadding Industries 1987 0|357|0 -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=-
Socoder -> Basic Basement -> Time synchronization problem

Sun, 19 Oct 2008, 09:11
For some weird reason, I wanted to make a realtime application that makes operations exactly 20000 times per second. To see if it's possible I made this program (in FreeBASIC):

This program, I thought, should add 1 every |edit| 1/20000th |edit|of a second, so i should get 100000 in the end. But I get results like these instead:
|edit| 98066, 98489, 98505... Oops, fixed a bug (>= instead of >) but they are still not coherent and near 100000 enough. |edit|

How should it be done? How do sound generators and players synchronize?
Sun, 19 Oct 2008, 09:24
I don't know FreeBASIC, but I'd guess the processor isn't fast enough...

If you want to synchronise sound, then you could divide the difference between the last and current timer by the update time you want, thus telling you how many of these timespans have gone by, whatever the processor speed, and base it off that (if that makes any sense).

A mushroom a day keeps the doctor away...

Keep It Simple, Shroom!
Sun, 19 Oct 2008, 09:55
Yeah, your programs just not going fast enough to do what it is your trying to do.

I'm not exactly sure what it is you're trying to do, but there are other ways to do it.

Let's say you wanted to do a beatmania style game.. Or, Cowbell Hero like I did a couple of months back.

What I did was first quickly run through the .xm file.. I coded my own reader that flicked through the file, read all the playable notes, and kept track of each note's millisecs() value.. that is, if you start at 0, and imaginarily play through, you can add "millisecs delay per line", and use that millisecs to track each note..

Then, ingame, start the song, start the millisecs timer, and when "Note's millisecs" is near "Actual millisecs", then you display that note on the screen. At the point that the two meet, that's "PERFECT!".. near that's "Good" (give or take 50 mills), then great, then whatever, then "you suck"..

*The above code is pseudocode only!

''Load, Next List!''
Sun, 19 Oct 2008, 09:55
First are you running that code over 5 seconds? If your increment once every 20th of a second, for 5 seconds, that is 100 increments not 10,000.

Anywho bear in mind that you'll never get 10,000 because the value is incremented after 1/20th of a second. I expect it's higher on a faster CPU simply because it can schedule and so run your loop more times in a second. By checking more often your more likely to be closer to the moment when it's just over 1/20th of a second.

Plus if you are incrementing every 1/20,000 of a second then I doubt the CPU is accurate enough to time it well.
Sun, 19 Oct 2008, 10:05
Oops, you are right, JL. It was 1/20000. And, thanks, Shroom and Jay, I'll think less realtime (or learn some assembly language).
Tue, 21 Oct 2008, 08:18
Pio, another problem is the fact that the computer's clock is not very accurate, in terms of timing. What I mean is, if you were to time something over an hour, the computer's clock does that just fine. But if you want to do something every millisecond, you're plum out of luck. I think its accuracy only approaches like 15 to 20 milliseconds. But, that is quite fine for any type of game you're making.

Vesuvius web game
Tue, 21 Oct 2008, 08:52
Your PC is able to be far more accurate then 15 milliseconds, but it depends greatly on how your recording it. I've seen languages out there who's library can only pickup a minimum difference of 7 milliseconds.

On my 900mhz celeron though this Java app manages to check the time over 500 times within a millisecond.

Tue, 30 Dec 2008, 08:51
You don't need to learn assembly language, after using freebasic quite a lot for the last few years I find properly optimised FB to be every bit as fast as good C++ code.

If you're using it for sound as you said in the first post, why re-invent the wheel?

There are wrappers out there for almost any kind of sound format you can think of.

bass, bassmod, sid, ufmod, minifmod, protrekkr, suntronic, felix schmidt, etc etc, all have been done and converted for use with freebasic.

If you're limiting to 20,000 steps for movement of some kind, you just have to accept that not all computers are going to be able to do it and use a delta timing system instead.

You can get really accurate information using queryperformancecounter (see nehe's tutorial No.21) , for most things you'll do though, timing the last frame using the internal timer will be sufficient, just store the timer value in a double at the beginning of the loop, execute the loop, sleep for 1 or 2 millisecs to give some time back to the os and then look at the timer again to get the elapsed time in a fraction of millisecs which you can use as some delta value.