Allegro.cc - Online Community

Allegro.cc Forums » Programming Questions » milliseconds to minutes:seconds:ms

This thread is locked; no one can reply to it. rss feed Print
 1   2 
milliseconds to minutes:seconds:ms
Steve Terry
Member #1,989
March 2002
avatar

I'm using fmod and there is a function FSOUND_Stream_GetTime(stream) which returns the number of milliseconds since the beginning of the track. The only problem is that it's not 60milliseconds to a second and so forth. Instead it's just a single number from 0 to infinity. How do I break this single number into minutes, seconds, and milliseconds in the 60:60:60 format. Sorry if this is a bit offtopic.

___________________________________
[ Facebook ]
Microsoft is not the Borg collective. The Borg collective has got proper networking. - planetspace.de
Bill Gates is in fact Shawn Hargreaves' ßî+çh. - Gideon Weems

Derezo
Member #1,666
April 2001
avatar

I thought it was 1000ms -> 1s?

I'm not sure how to break down the ms to 60, exactly.. but, I usually break it down to 100 instead. Just divide by 10.

ms = time%1000; // 1000 ms in 1 sec.
s = (time/1000)%60; // 60 sec in 1 min..
min = (time/60000)%60; // 60 min in 1 hour..

Ok, was simpler than I thought.. so I edited my post to include code :P
I tried it out, and passed 70000ms.. and it broke it down to 1 minute 10 seconds, which is correct..

"He who controls the stuffing controls the Universe"

Steve Terry
Member #1,989
March 2002
avatar

yeah the problem I'm getting it's coming out like 00:00:78 instead of 00:01:18, etc.

___________________________________
[ Facebook ]
Microsoft is not the Borg collective. The Borg collective has got proper networking. - planetspace.de
Bill Gates is in fact Shawn Hargreaves' ßî+çh. - Gideon Weems

Matthew Leverton
Supreme Loser
January 1999
avatar

How about this?

int t = FSOUND_Stream_GetTime(stream);
int minutes, seconds, ms;

minutes = t / (60 * 1000);
t -= minutes * (60 * 1000);
seconds = t / 1000;
ms = t - (seconds * 1000);

printf("%02d:%02d:%d", minutes, seconds, ms);

Untested, and not optimized, but something along those lines should work.

Edit: If you want ms to be 0 to 59, just scale it.

ms = ms * 60 / 1000;

Korval
Member #1,538
September 2001
avatar

Quote:

The only problem is that it's not 60milliseconds to a second

That's because there aren't 60 milliseconds to a second. Nor should there be. The prefix "milli" is used to denote 1/1000ths, not 1/60ths. To get seconds, divide the milliseconds by 1000.

What Matthew wrote should get you what you need. If, for some odd reason, you really want 1/60ths of a second, you should be able to convert the code that computes the variable "ms" into something that gives you 1/60ths of a second.

Rash
Member #2,374
May 2002
avatar

Sometimes the term jiffy is used to denote 1/60th of a second.

Steve Terry
Member #1,989
March 2002
avatar

alrighty that worked.. what's wrong with the 00:00:00 format instead of 00:00:0000, personally I like it all 1/60.

___________________________________
[ Facebook ]
Microsoft is not the Borg collective. The Borg collective has got proper networking. - planetspace.de
Bill Gates is in fact Shawn Hargreaves' ßî+çh. - Gideon Weems

Matthew Leverton
Supreme Loser
January 1999
avatar

You are losing precision, but it really doesn't matter. Whatever looks best visually is what should be used. I personally would make the ms display as 00-99 (or not at all).

Bruce Perry
Member #270
April 2000

The ms display won't be 0-99. If it's 0-99, they're centiseconds.

Personally I've never seen seconds subdivided into 60; it's usually 100 or 1000. Sometimes it's 24.

I'd recommend minutes:seconds.centiseconds or minutes:seconds.milliseconds (note the use of . not : ;) ).

int ms = (LONG_LONG)al_duh_get_position(dp)*1000>>16; /* :P */
int secs, mins/*, hours*/;

secs = ms / 1000; ms %= 1000;
mins = secs / 60; secs %= 60;
/* hours = mins / 60; mins %= 60; */

/* By all means replace this with text_printf().
 * %02d will print a trailing zero for <10.
 * %2d will print a trailing space for <10.
 * %d would not print a trailing space.
 */
printf("%2d:%02d.%03d", mins, secs, ms);
/* printf("%2d:%02d:%02d.%03d", hours, mins, secs, ms); */

If you'd like to use centiseconds, replace 1000 with 100 and ms with cs everywhere, and change %03d to %02d. (Using FMOD's function, divide by 10, obviously.)

--
Bruce "entheh" Perry [ Web site | DUMB | Set Up Us The Bomb !!! | Balls ]
Programming should be fun. That's why I hate C and C++.
The brxybrytl has you.

Paul Pridham
Member #250
April 2000
avatar

I use this for my game's countdown clock:

hours=(countdown/(UPDATE_RATE*60*60))%24;
mins=(countdown/(UPDATE_RATE*60))%60;
secs=(countdown/(UPDATE_RATE))%60;
msecs=countdown%UPDATE_RATE;

This works in 1/100ths of a second (UPDATE_RATE=100, countdown is in 1/100ths), but just change UPDATE_RATE and countdown to whatever you need.

Steve Terry
Member #1,989
March 2002
avatar

hey enthen does DUMB support FFT output, I heard you a long time ago talking about finding the algorithm and whatnot, but have you actually implimented it yet? Oh and I think I will take the mm:ss.ms where ms = 1/100s method.. it looks nicer.

___________________________________
[ Facebook ]
Microsoft is not the Borg collective. The Borg collective has got proper networking. - planetspace.de
Bill Gates is in fact Shawn Hargreaves' ßî+çh. - Gideon Weems

Thomas Fjellstrom
Member #476
June 2000
avatar

why 100? milliseconds are 1/1000th... my little wav player thats builting to an editor for a file called a pak file (kinda like a dat file, but has the nesesary api to add and write them, and adding more formats are a breeze.) I use 1/1000ths and it looks fine...

--
Thomas Fjellstrom - [website] - [email] - [Allegro Wiki] - [Allegro TODO]
"If you can't think of a better solution, don't try to make a better solution." -- weapon_S
"The less evidence we have for what we believe is certain, the more violently we defend beliefs against those who don't agree" -- https://twitter.com/neiltyson/status/592870205409353730

Steve Terry
Member #1,989
March 2002
avatar

hmm interesting replies.. I never thought that displaying a timer would be so complex.. maybe I should specify a config file for my media player that allows the user to set the time format.

[edit]
Oh and I added mp3 id3 v1.0 reading for the media player as well.... only I use fopen and fread which is really slow... my 300 or so files take about 12 seconds to load which is way to much... I need to get this down to about 2 seconds or people will think the program hung up, only I don't know if allegros packfile reading is any faster.
[/edit]

___________________________________
[ Facebook ]
Microsoft is not the Borg collective. The Borg collective has got proper networking. - planetspace.de
Bill Gates is in fact Shawn Hargreaves' ßî+çh. - Gideon Weems

Thomas Fjellstrom
Member #476
June 2000
avatar

if anything allegro's pakkfile reading will be slower. :)

Winamp takes just as long to read the id3 tags.. you just have to do it a bit at a time... say every MSG_IDLE do a couple files :)

--
Thomas Fjellstrom - [website] - [email] - [Allegro Wiki] - [Allegro TODO]
"If you can't think of a better solution, don't try to make a better solution." -- weapon_S
"The less evidence we have for what we believe is certain, the more violently we defend beliefs against those who don't agree" -- https://twitter.com/neiltyson/status/592870205409353730

Steve Terry
Member #1,989
March 2002
avatar

ahhh brilliant.. that should work.. though I can't really impliment that at the moment because of HW.. but brilliant non-theless I would of never thought of that.. it would update just like winamp does as well... possibly I could do it for only those in the view of the playlist. It's very doable as well, not much to change...

___________________________________
[ Facebook ]
Microsoft is not the Borg collective. The Borg collective has got proper networking. - planetspace.de
Bill Gates is in fact Shawn Hargreaves' ßî+çh. - Gideon Weems

Thomas Fjellstrom
Member #476
June 2000
avatar

w00t. Someone said I'm brilliant! hehe. It feels nice to feel needed... ;)

Now if only my game would get to a point people could play it... :(

--
Thomas Fjellstrom - [website] - [email] - [Allegro Wiki] - [Allegro TODO]
"If you can't think of a better solution, don't try to make a better solution." -- weapon_S
"The less evidence we have for what we believe is certain, the more violently we defend beliefs against those who don't agree" -- https://twitter.com/neiltyson/status/592870205409353730

Bruce Perry
Member #270
April 2000

Ack! PLEASE stop referring to 100ths of seconds as milliseconds! They're not milliseconds, they're centiseconds!

No, DUMB does not do any FFT or similar algorithms yet. I implemented it in ML, which has a tendency to be one of the slowest languages around (although apparently it "can now run as fast as C"... :P ) Anyway I didn't really understand what I wrote. ;D

DUMB's .mod support is on its way though. DUMB CVS also does length calculation. It won't take much more work to allow you to stop a module from looping (it'll use a system that's already in place for the length calculation). So progress is being made ;)

I'm dumb!, I really didn't mean to turn this thread into a DUMB plug thread as well ::)

--
Bruce "entheh" Perry [ Web site | DUMB | Set Up Us The Bomb !!! | Balls ]
Programming should be fun. That's why I hate C and C++.
The brxybrytl has you.

Paul Pridham
Member #250
April 2000
avatar

If you wrote your FFT in ML (PolyML was it?), you should consider porting it over to O'Caml, which is generally faster than g++. It's also very nifty.

Thomas Fjellstrom
Member #476
June 2000
avatar

hehe... pushing other 'intelectual' languages are you paul? I'm dumb!.

--
Thomas Fjellstrom - [website] - [email] - [Allegro Wiki] - [Allegro TODO]
"If you can't think of a better solution, don't try to make a better solution." -- weapon_S
"The less evidence we have for what we believe is certain, the more violently we defend beliefs against those who don't agree" -- https://twitter.com/neiltyson/status/592870205409353730

Steve Terry
Member #1,989
March 2002
avatar

TF... I used the MSG_IDLE technique... worked beautifully.. better than I expected.. simply awsome, you don't even knotice much of a drag down and it updates em fast. The only way I can see it working is to scroll all teh way to the bottom, select a file, close the dialog, and reload it... in about 6 seconds it hits the bottom, this also lets me do more complex things with the filenames in case an ID3 tag doesn't exist too, like the filename parsing you did... without any performance hit whatsoever... god I love it. Happy happy happy.....:P;D:P

[edit]
oh.. I forgot to ask in all the excitement.. does MSG_IDLE sometimes get called before MSG_START... I found that if I returned D_EXIT if fopen failed then the program would randomly close the dialog window.. but if I return D_O_K and don't update my tracker variable, then it works just fine.. it sorta just skips out for a second and comes back later to finish what it tried.. but failed at the first time.
[/edit]

___________________________________
[ Facebook ]
Microsoft is not the Borg collective. The Borg collective has got proper networking. - planetspace.de
Bill Gates is in fact Shawn Hargreaves' ßî+çh. - Gideon Weems

Bruce Perry
Member #270
April 2000

Yep, it was Poly/ML. Cool, I'll try O'Caml then (it should be installed on here, though it might be an out-of-date version). However, unless the compiler can really work miracles, I doubt it'll do better than gcc on an algorithm that can work in place in a procedural implementation :P

Why do you mention g++ in particular? It's my understanding that compiled C++ code used to be slower than compiled C code (assuming the source file is understandable as both), but not any more...

For reference, here's the thread in which I implemented FFT. (I wonder if I should clone Steve Terry now ;) )

By the way, XMMS is much faster than Winamp at reading the ID3 tags. Hooray for Linux's file system! ;D

--
Bruce "entheh" Perry [ Web site | DUMB | Set Up Us The Bomb !!! | Balls ]
Programming should be fun. That's why I hate C and C++.
The brxybrytl has you.

Steve Terry
Member #1,989
March 2002
avatar

Hmm I wonder what fmod wrote their FFT functions in.. it's quite fast too. I bet I still have some source on my HD from earlier versions before they just sent out the dlls and libs only. Yes and XMMS is cool, it has some nice visuals too. Oh and sadly this topic is getting off topic unless we start talking about programming again...... I think I'll shut up.

___________________________________
[ Facebook ]
Microsoft is not the Borg collective. The Borg collective has got proper networking. - planetspace.de
Bill Gates is in fact Shawn Hargreaves' ßî+çh. - Gideon Weems

Paul Pridham
Member #250
April 2000
avatar

Thomas: If I was an intellectual, I'd reply "yes" to that. ;)

Ben: I mentioned g++ because of this: http://www.bagley.org/~doug/shootout/ ... O'Caml did very well.

Anyway, definitely look at O'Caml. It's doubly cool because O'Caml is also a scripting language as well as a compiler, as well as giving you the productivity increases that you already know about from using ML.

Steve Terry
Member #1,989
March 2002
avatar

clone me?? um.. ok.. but I think there is enough of just one of me around... but I bet two of us would code a lot faster... what about writing a DUMB wrapper for my GUI :) since I can't seem to get DUMB ogg installed :( it would help you promote DUMB even more ;D and promote my GUI... maybe, it's still got a long ways to go before it's finished, but making little media players and dialogs helps me find/fix bugs and impliment new features. I wonder if I can move my version back to .30 or something.. I think I overestimated because the more I look at it.. the more I have to do.

___________________________________
[ Facebook ]
Microsoft is not the Borg collective. The Borg collective has got proper networking. - planetspace.de
Bill Gates is in fact Shawn Hargreaves' ßî+çh. - Gideon Weems

Thomas Fjellstrom
Member #476
June 2000
avatar

Steve: Cool. I just thought that would be the best way, short of rewriting everything to use threads ;)

Paul: It just seems you like those languages that are mostly used by scientists (who don't code much) ;) and not by 'pro' coders.

--
Thomas Fjellstrom - [website] - [email] - [Allegro Wiki] - [Allegro TODO]
"If you can't think of a better solution, don't try to make a better solution." -- weapon_S
"The less evidence we have for what we believe is certain, the more violently we defend beliefs against those who don't agree" -- https://twitter.com/neiltyson/status/592870205409353730

 1   2 


Go to: