AlliedModders

AlliedModders (https://forums.alliedmods.net/index.php)
-   Scripting Help (https://forums.alliedmods.net/forumdisplay.php?f=11)
-   -   Precache sounds from file (https://forums.alliedmods.net/showthread.php?t=89248)

FiFiX 04-04-2009 04:26

Precache sounds from file
 
Hello. How to mace function that will precache sounds from .ini file. FE:
In sounds.ini I've got:
Code:

"sounds/FiFiX/1.wav"
"sounds/FiFiX/2.mp3"
"sounds/FiFiX/3.wav"
"sounds/FiFiX/4.mp3"

And I want to play it radomly by command.

TheRadiance 04-04-2009 05:19

Re: Precache sounds from file
 
PHP Code:

#include < amxmodx >
#include < amxmisc >

new g_szPrecached 32 ] [ 64 ]

public 
plugin_init ( )
{
    
register_plugin "PrecacheSound""1.0""FiFiX" )
    
set_task random_float 5.015.0 ), "Task_PlaySounds")
}

public 
plugin_precache ( )
{
    new 
szPath 64 ]
    
get_configsdir szPathsizeof szPath ) - )
    
format szPathsizeof szPath ) - 1"%s/sounds.ini"szPath )

    if ( !
file_exists szPath ) )
        return

    new 
iCount

    
for ( new 0file_size szPath); ++ )
    {
        new 
szText 64 ]
        new 
iLen
        read_file 
szPathaszTextsizeof szText ) - 1iLen )

        if ( ( 
szText ] != ';' ) && (szText ] != '/' ) && file_exists szText ) )
        {
            
g_szPrecached iCount++ ] = szText
            precache_sound 
szText )
        }
    }
}

public 
Task_PlaySounds ( )
{
    
client_cmd 0"spk ^"%s^""g_szPrecached random 32 ) ] )
    
set_task random_float 5.015.0 ), "Task_PlaySounds")



ConnorMcLeod 04-04-2009 05:22

Re: Precache sounds from file
 
Better to use "new" files natives rather than read_file, but it's less important in plugin_precache.

FiFiX 04-04-2009 05:23

Re: Precache sounds from file
 
@up
What it exactly mean?

And how to radomly play sounds?
Also would be great if amx will log sounds which it precached.

TheRadiance 04-04-2009 05:28

Re: Precache sounds from file
 
Quote:

Better to use "new" files natives rather than read_file, but it's less important in plugin_precache.
I know, but i've never used "new" file natives ( don't know how :P ), because of this i'm always use old method :).
Quote:

And how to radomly play sounds?
Also would be great if amx will log sounds which it precached.
I've added a console command, check the script.

FiFiX 04-04-2009 05:39

Re: Precache sounds from file
 
But I want play it from plugin.
It would be:
Code:

#include < amxmodx >
#include < amxmisc >

new g_szPrecached [ 32 ] [ 64 ]

public
plugin_init ( )
{
   
register_plugin ( "PrecacheSound", "1.0", "FiFiX" )
}

public
plugin_precache ( )
{
    new
szPath [ 64 ]
   
get_configsdir ( szPath, sizeof ( szPath ) - 1 )
   
format ( szPath, sizeof ( szPath ) - 1, "%s/sounds.ini", szPath )

    if ( !
file_exists ( szPath ) )
        return

    new
iCount

   
for ( new a = 0; a < file_size ( szPath, 1 ); a ++ )
    {
        new
szText [ 64 ]
        new
iLen
        read_file
( szPath, a, szText, sizeof ( szText ) - 1, iLen )

        if ( (
szText [ 0 ] != ';' ) && (szText [ 0 ] != '/' ) && file_exists ( szText ) )
        {
           
g_szPrecached [ iCount++ ] = szText
            precache_sound
( szText )
        }
    }
}

public
Command_PlaySound ( )
{

    client_cmd
( 0, "spk ^"%s^"", g_szPrecached [ random ( 32 ) ] )

    return
PLUGIN_HANDLED
}


And in plugin just add:
Code:

Command_PlaySound();

TheRadiance 04-04-2009 05:44

Re: Precache sounds from file
 
Quote:

But I want play it from plugin.
Check my script again.

FiFiX 04-04-2009 05:46

Re: Precache sounds from file
 
Heh Okay, Look for my edited. I want to place line in plugin hmm f.e. When player connect, and when he disconnects.
In:
Code:

public client_connect(id)
I'll just add:
Code:

Command_PlaySound( );

ConnorMcLeod 04-04-2009 05:48

Re: Precache sounds from file
 
Example on hw to precache .mp3 files :

PHP Code:

public plugin_precache() {
    new 
configfile[64]
    
get_configsdir(configfile63)
    
format(configfile63"%s/loadingsongs.ini"configfile)

// should be useless
//    if(!file_exists(configfile))
//        return

//    g_Sounds = ArrayCreate(128)

    
new File fopen(configfile"rt")
    if(!
File)
        return
    new 
Text[256], Sound[128]
    while(!
feof(File))
    {
        
fgets(FileText255)
        
trim(Text)
        if(!
Text[0] || Text[0] == ';' || (Text[0] == '/' && Text[1] == '/'))
            continue
        
parse(TextSound127)
        if(!
equaliSound[strlen(Sound)-4], ".mp3"))
        {
    
//        log_amx("Fichier %s ne porte pas l'extension ^".mp3^"", Sound)
            
continue
        }
        if(!
file_exists(Sound))
        {
    
//        log_amx("Fichier %s inexistant", Sound)
            
continue
        }
//        ArrayPushString(g_Sounds, Sound)
        
precache_generic(Sound)
    }
    
fclose(File)



FiFiX 04-08-2009 17:26

Re: Precache sounds from file
 
Quote:

Originally Posted by FiFiX (Post 796947)
But I want play it from plugin.
It would be:
Code:

#include < amxmodx >
#include < amxmisc >

new g_szPrecached [ 32 ] [ 64 ]

public
plugin_init ( )
{
   
register_plugin ( "PrecacheSound", "1.0", "FiFiX" )
}

public
plugin_precache ( )
{
    new
szPath [ 64 ]
   
get_configsdir ( szPath, sizeof ( szPath ) - 1 )
   
format ( szPath, sizeof ( szPath ) - 1, "%s/sounds.ini", szPath )

    if ( !
file_exists ( szPath ) )
        return

    new
iCount

   
for ( new a = 0; a < file_size ( szPath, 1 ); a ++ )
    {
        new
szText [ 64 ]
        new
iLen
        read_file
( szPath, a, szText, sizeof ( szText ) - 1, iLen )

        if ( (
szText [ 0 ] != ';' ) && (szText [ 0 ] != '/' ) && file_exists ( szText ) )
        {
           
g_szPrecached [ iCount++ ] = szText
            precache_sound
( szText )
        }
    }
}

public
Command_PlaySound ( )
{

    client_cmd
( 0, "spk ^"%s^"", g_szPrecached [ random ( 32 ) ] )

    return
PLUGIN_HANDLED
}


And in plugin just add:
Code:

Command_PlaySound();

Hey, how to add here line which will log precached files into log_amx and how to make if extention is different than mp3 or wav plugin logs unknow extention and precache another file.


All times are GMT -4. The time now is 02:21.

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