AlliedModders

AlliedModders (https://forums.alliedmods.net/index.php)
-   Code Snippets/Tutorials (https://forums.alliedmods.net/forumdisplay.php?f=83)
-   -   ConvertFloatToSeconds Stock (Useful for Songs) (https://forums.alliedmods.net/showthread.php?t=303108)

edon1337 11-22-2017 11:06

ConvertFloatToSeconds Stock (Useful for Songs)
 
Today, I was making a Song Player and my array of Songs had a dimension just for the music length, most of people don't know the exact length of a song in seconds so they write it in a 'float' form (Example 3:15), the way YouTube displays it. So I was in need of a stock that would Convert that 'float' length to seconds but couldn't find any, that's why I'm posting this stock here so other's can find what they need.

3:15

3 = Number of minutes.
15 = Number of seconds.

Stock:

PHP Code:

ConvertFloatToSeconds( const Float:fTime )
{
    new 
iSecondsiMinutesszExtraSeconds], iExtraSecondsszString];

    
float_to_strfTimeszStringcharsmaxszString ) );

    
szExtraSeconds] = szString];
    
addszExtraSecondscharsmaxszExtraSeconds ), szString] );

    
iExtraSeconds str_to_numszString] );

    
iMinutes floatroundfTimefloatround_floor );

    
iSeconds iMinutes 60;

    return 
iSeconds += iExtraSeconds;


The reason I needed this stock was that to make the song more relaxable by lowering the volume of the game sounds when the music started and letting the player feel the music. So I needed this stock to create a task to be executed when music would end and increase the game volume of the player again.

Usage of the stock:

PHP Code:

#define MUSIC_LENGTH 3.15

set_taskfloatConvertFloatToSecondsMUSIC_LENGTH ), "OnTaskEndSong"id_) ); 

Test Logs:
Code:
L 11/22/2017 - 16:52:21: [DEBUG] Old Float Value: 3.14 | New Converted Value: 194 L 11/22/2017 - 16:52:21: [DEBUG] Old Float Value: 0.26 | New Converted Value: 26 L 11/22/2017 - 16:52:21: [DEBUG] Old Float Value: 3.06 | New Converted Value: 186

TheWhitesmith 11-22-2017 11:44

Re: ConvertFloatToSeconds Stock (Useful for Songs)
 
Try
PHP Code:

iMinutes floatroundfTimefloatround_floor 

If I'm not wrong the error does not come from float_to_str, but from floatround because if the 2nd option is left blank, it will use "floatround_round" as default, and HERE it says "floatround_round : rounds to the nearest integer"

klippy 11-22-2017 12:55

Re: ConvertFloatToSeconds Stock (Useful for Songs)
 
I don't get why would anyone store the song length of 3:15 as 3.15. 195.0 is much more more appropriate and manageable.

TheWhitesmith 11-22-2017 13:42

Re: ConvertFloatToSeconds Stock (Useful for Songs)
 
Quote:

Originally Posted by KliPPy (Post 2562235)
I don't get why would anyone store the song length of 3:15 as 3.15. 195.0 is much more more appropriate and manageable.

Lack of mathematics? doesn't know that 1 minute is 60 second? Lazy to convert minutes to seconds? lol

edon1337 11-22-2017 14:07

Re: ConvertFloatToSeconds Stock (Useful for Songs)
 
Quote:

Originally Posted by KliPPy (Post 2562235)
I don't get why would anyone store the song length of 3:15 as 3.15. 195.0 is much more more appropriate and manageable.

In YouTube, songs length are displayed in minute:seconds format. Furthermore, when you want to print the length of a video in chat, if you use 'Video Length: 195.0 seconds' they'll get confused lol. It's better for the readability. I personally needed this stock so I posted it here for other people. I don't get what you're trying to say with '3:15 as 3.15', how would you convert 3:15 to seconds? It's not even a number.

Quote:

Originally Posted by TheWhitesmith (Post 2562222)
Try
PHP Code:

iMinutes floatroundfTimefloatround_floor 

If I'm not wrong the error does not come from float_to_str, but from floatround because if the 2nd option is left blank, it will use "floatround_round" as default, and HERE it says "floatround_round : rounds to the nearest integer"

I'll try your method, thanks :).

klippy 11-22-2017 15:32

Re: ConvertFloatToSeconds Stock (Useful for Songs)
 
Quote:

Originally Posted by edon1337 (Post 2562254)
In YouTube, songs length are displayed in minute:seconds format. Furthermore, when you want to print the length of a video in chat, if you use 'Video Length: 195.0 seconds' they'll get confused lol. It's better for the readability. I personally needed this stock so I posted it here for other people. I don't get what you're trying to say with '3:15 as 3.15', how would you convert 3:15 to seconds? It's not even a number.



I'll try your method, thanks :).

That's the thing - the minute:seconds format is a string. In your example you defined MUSIC_LENGTH to 3.15.
It's more logical to store a timespan as the number of seconds. 3.15 is not the number of seconds, 195.0 is. It's much easier to convert 195.0 to that string representation than 3.15. You'll find that in any programming language / library that can work with timespans. It's also really easy to convert "3:15" back to 195.

fysiks 11-22-2017 18:52

Re: ConvertFloatToSeconds Stock (Useful for Songs)
 
This is completely wrong and shouldn't be used by anyone.

The time 3:15 (3 minutes, 15 seconds) is 3.25 minutes. As others mentioned, time should be stored in a plugin as seconds. When you need to use time as an input or
output, you can convert it to/from the colon notation (as a string).

I recommend that you either convert this thread to functions for doing the conversion to and from the colon notation or remove the thread entirely so that people new to plugin scripting aren't confused.

ddhoward 11-22-2017 21:08

Re: ConvertFloatToSeconds Stock (Useful for Songs)
 
Quote:

Originally Posted by edon1337 (Post 2562254)
how would you convert 3:15 to seconds? It's not even a number.

Grab the value before the : and multiply it by 60. Then add the value that was after the :.

If you wanted to go the opposite direction, take the total seconds value and divide it by 60. That's your minutes. Then take the total seconds value and modulo it by 60. There's the rest of the seconds.

Here's the equivalent code that you'd find in a Sourcemod plugin, with some tweaks to make it work if they included hours.

PHP Code:


stock int HMSDurationToSeconds
(char[] duration) {
    
char timeunits[3][8]
    
int numStrings ExplodeString(duration":"timeunitssizeof(timeunits), sizeof(timeunits[]));
    if (!
numStrings) return 0;
    
    
int numSeconds;
    
numSeconds += StringToInt(timeunits[0]);
    if (
numStrings 1) {
        
numSeconds *= 60;
        
numSeconds += StringToInt(timeunits[1]);
    }
    if (
numStrings 2) {
        
numSeconds *= 60;
        
numSeconds += StringToInt(timeunits[2]);
    }
    return 
numSeconds;
}

stock void SecondsToHMSDuration(int totalSecondschar[] timeString) {
    
int hours totalSeconds / (60*60);
    
int minutes totalSeconds % (60 60) / 60;
    
int seconds totalSeconds 60;
    
    if (
hoursFormat(timeString10"%i:%02i:%02i",  hoursminutesseconds); 
    else 
Format(timeString10"%i:%02i"minutesseconds); 



fysiks 11-23-2017 02:19

Re: ConvertFloatToSeconds Stock (Useful for Songs)
 
@ddhoward, going from duration to hours, minutes, and seconds can be done more elegantly without any subtraction or multiplication:

Code:

new iDays= iDuration / 86400
new iHours = iDuration % 86400 / 3600
new iMinutes = iDuration % 3600 / 60
new iSeconds = iDuration % 60


ddhoward 11-23-2017 15:47

Re: ConvertFloatToSeconds Stock (Useful for Songs)
 
Quote:

Originally Posted by fysiks (Post 2562339)
@ddhoward, going from duration to hours, minutes, and seconds can be done more elegantly without any subtraction or multiplication:

Code:

new iDays= iDuration / 86400
new iHours = iDuration % 86400 / 3600
new iMinutes = iDuration % 3600 / 60
new iSeconds = iDuration % 60


Haha, excellent. I knew there was an easier way to do it, I just couldn't think of it in my sleep-deprived state. I'll update my post now.


All times are GMT -4. The time now is 01:57.

Powered by vBulletin®
Copyright ©2000 - 2024, vBulletin Solutions, Inc.