plugins/include/sdktools_sound.inc
author Phong Dang <teame06@gmail.com>
Wed Aug 01 18:22:53 2007 +0000 (2007-08-01)
changeset 1245 bb0611ab4bed
parent 1048 9f5605daf52d
child 1330 ab94316bb52b
permissions -rw-r--r--
Updated license header.
     1 /**
     2  * vim: set ts=4 :
     3  * ===============================================================
     4  * SourceMod (C)2004-2007 AlliedModders LLC.  All rights reserved.
     5  * ===============================================================
     6  *
     7  *  This file is part of the SourceMod/SourcePawn SDK.  This file may only be used 
     8  * or modified under the Terms and Conditions of its License Agreement, which is found 
     9  * in LICENSE.txt.  The Terms and Conditions for making SourceMod extensions/plugins 
    10  * may change at any time.  To view the latest information, see:
    11  *   http://www.sourcemod.net/license.php
    12  *
    13  * Version: $Id$
    14  */
    15 
    16 #if defined _sdktools_sound_included
    17   #endinput
    18 #endif
    19 #define _sdktools_sound_included
    20 
    21 /**
    22  * Sound should be from the target client.
    23  */
    24 #define SOUND_FROM_PLAYER		-2
    25 
    26 /**
    27  * Sound should be from the listen server player.
    28  */
    29 #define SOUND_FROM_LOCAL_PLAYER	-1
    30 
    31 /**
    32  * Sound is from the world.
    33  */
    34 #define SOUND_FROM_WORLD		0
    35 
    36 /**
    37  * Sound channels.
    38  */
    39 enum
    40 {
    41 	SNDCHAN_REPLACE = -1,		/**< Unknown */
    42 	SNDCHAN_AUTO = 0,			/**< Auto */
    43 	SNDCHAN_WEAPON = 1,			/**< Weapons */
    44 	SNDCHAN_VOICE = 2,			/**< Voices */
    45 	SNDCHAN_ITEM = 3,			/**< Items */
    46 	SNDCHAN_BODY = 4,			/**< Player? */
    47 	SNDCHAN_STREAM = 5,			/**< "Stream channel from the static or dynamic area" */
    48 	SNDCHAN_STATIC = 6,			/**< "Stream channel from the static area" */
    49 	SNDCHAN_VOICE_BASE = 7,		/**< "Channel for network voice data" */
    50 	SNDCHAN_USER_BASE = 135		/**< Anything >= this is allocated to game code */
    51 };
    52 
    53 /**
    54  * Sound flags for the sound emitter system.
    55  */
    56 enum
    57 {
    58 	SND_NOFLAGS= 0,			/**< Nothing */
    59 	SND_CHANGEVOL = 1,		/**< Change sound volume */
    60 	SND_CHANGEPITCH = 2,	/**< Change sound pitch */
    61 	SND_STOP = 3,			/**< Stop the sound */
    62 	SND_SPAWNING = 4,		/**< Used in some cases for ambients */
    63 	SND_DELAY = 5,			/**< Sound has an initial delay */
    64 	SND_STOPLOOPING = 6,	/**< Stop looping all sounds on the entity */
    65 	SND_SPEAKER = 7,		/**< Being played by a mic through a speaker */
    66 	SND_SHOULDPAUSE = 8,	/**< Pause if game is paused */
    67 };
    68 
    69 /**
    70  * Various predefined sound levels in dB.
    71  */
    72 enum
    73 {
    74 	SNDLEVEL_NONE = 0,			/**< None */
    75 	SNDLEVEL_RUSTLE = 20,		/**< Rustling leaves */
    76 	SNDLEVEL_WHISPER = 25,		/**< Whispering */
    77 	SNDLEVEL_LIBRARY = 30,		/**< In a library */
    78 	SNDLEVEL_FRIDGE = 45,		/**< Refridgerator */
    79 	SNDLEVEL_HOME = 50,			/**< Average home (3.9 attn) */
    80 	SNDLEVEL_CONVO = 60,		/**< Normal conversation (2.0 attn) */
    81 	SNDLEVEL_DRYER = 60,		/**< Clothes dryer */
    82 	SNDLEVEL_DISHWASHER = 65,	/**< Dishwasher/washing machine (1.5 attn) */
    83 	SNDLEVEL_CAR = 70,			/**< Car or vacuum cleaner (1.0 attn) */
    84 	SNDLEVEL_NORMAL = 75,		/**< Normal sound level */
    85 	SNDLEVEL_TRAFFIC = 75,		/**< Busy traffic (0.8 attn) */
    86 	SNDLEVEL_MINIBIKE = 80,		/**< Mini-bike, alarm clock (0.7 attn) */
    87 	SNDLEVEL_SCREAMING = 90,	/**< Screaming child (0.5 attn) */
    88 	SNDLEVEL_TRAIN = 100,		/**< Subway train, pneumatic drill (0.4 attn) */
    89 	SNDLEVEL_HELICOPTER = 105,	/**< Helicopter */
    90 	SNDLEVEL_SNOWMOBILE = 110,	/**< Snow mobile */
    91 	SNDLEVEL_AIRCRAFT = 120,	/**< Auto horn, aircraft */
    92 	SNDLEVEL_RAIDSIREN = 130,	/**< Air raid siren */
    93 	SNDLEVEL_GUNFIRE = 140,		/**< Gunshot, jet engine (0.27 attn) */
    94 	SNDLEVEL_ROCKET = 180,		/**< Rocket launching (0.2 attn) */
    95 };
    96 
    97 #define SNDVOL_NORMAL		1.0		/**< Normal volume */
    98 #define SNDPITCH_NORMAL		100		/**< Normal pitch */
    99 #define SNDPITCH_LOW		95		/**< A low pitch */
   100 #define SNDPITCH_HIGH		120		/**< A high pitch */
   101 #define SNDATTN_NONE		0.0		/**< No attenuation */
   102 #define SNDATTN_NORMAL		0.8		/**< Normal attenuation */
   103 #define SNDATTN_STATIC		1.25	/**< Static attenuation? */
   104 #define SNDATTN_RICOCHET	1.5		/**< Ricochet effect */
   105 #define SNDATTN_IDLE		2.0		/**< Idle attenuation? */
   106  
   107 /**
   108  * Prefetches a sound.
   109  *
   110  * @param name		Sound file name.
   111  * @noreturn
   112  */
   113 native PrefetchSound(const String:nane[]);
   114 
   115 /**
   116  * Gets a sound's duration (does not work for mp3s).
   117  *
   118  * @param name		Sound file name.
   119  * @return			Duration in seconds.
   120  */
   121 native Float:GetSoundDuration(const String:name[]);
   122 
   123 /**
   124  * Emits an ambient sound.
   125  *
   126  * @param entity	Entity index to associate sound with.
   127  * @param pos		Origin of sound.
   128  * @param name		Sound file name.
   129  * @param level		Sound level (from 0 to 255).
   130  * @param flags		Sound flags.
   131  * @param vol		Volume (from 0.0 to 1.0).
   132  * @param pitch		Pitch (from 0 to 255).
   133  * @param delay		Play delay.
   134  * @noreturn
   135  */
   136 native EmitAmbientSound(const String:name[],
   137 						const Float:pos[3],
   138 						entity = SOUND_FROM_WORLD,
   139 						level = SNDLEVEL_NORMAL,
   140 						flags = SND_NOFLAGS,
   141 						Float:vol = SNDVOL_NORMAL,
   142 						pitch = SNDPITCH_NORMAL,
   143 						Float:delay = 0.0);
   144 
   145 /**
   146  * Fades a client's volume level toward silence or a given percentage.
   147  *
   148  * @param client	Client index.
   149  * @param percent	Fade percentage.
   150  * @param outtime	Fade out time, in seconds.
   151  * @param holdtime	Hold time, in seconds.
   152  * @param intime	Fade in time, in seconds.
   153  * @noreturn
   154  * @error			Invalid client index or client not in game.
   155  */
   156 native FadeClientVolume(client, Float:percent, Float:outtime, Float:holdtime, Float:intime);
   157 
   158 /**
   159  * Stops a sound.
   160  *
   161  * @param entity	Entity index.
   162  * @param channel	Channel number.
   163  * @param name		Sound name.
   164  * @noreturn
   165  */
   166 native StopSound(entity, channel, const String:name[]);
   167 
   168 /**
   169  * Emits a sound to a list of clients.
   170  *
   171  * @param clients		Array of client indexes.
   172  * @param numClients	Number of clients in the array.
   173  * @param entity		Entity to emit from.
   174  * @param channel		Channel to emit with.
   175  * @param sample		Sound name.
   176  * @param level			Sound level.
   177  * @param flags			Sound flags.
   178  * @param volume		Sound volume.
   179  * @param pitch			Sound pitch.
   180  * @param speakerentity	Unknown.
   181  * @param origin		Sound origin.
   182  * @param dir			Sound direction.
   183  * @param updatePos		Unknown (updates positions?)
   184  * @param soundtime		Alternate time to play sound for.
   185  * @param ...			Optional list of Float[3] arrays to specify additional origins.
   186  * @noreturn
   187  * @error				Invalid client index.
   188  */
   189 native EmitSound(const clients[],
   190 				 numClients,
   191 				 const String:sample[],
   192 				 entity = SOUND_FROM_PLAYER,
   193 				 channel = SNDCHAN_AUTO,
   194 				 level = SNDLEVEL_NORMAL,
   195 				 flags = SND_NOFLAGS,
   196 				 Float:volume = SNDVOL_NORMAL,
   197 				 pitch = SNDPITCH_NORMAL,
   198 				 speakerentity = -1,
   199 				 const Float:origin[3] = NULL_VECTOR,
   200 				 const Float:dir[3] = NULL_VECTOR,
   201 				 bool:updatePos = true,
   202 				 Float:soundtime = 0.0,
   203 				 any:...);
   204 
   205 /**
   206  * Emits a sentence to a list of clients.
   207  *
   208  * @param clients		Array of client indexes.
   209  * @param numClients	Number of clients in the array.
   210  * @param entity		Entity to emit from.
   211  * @param channel		Channel to emit with.
   212  * @param sentence		Sentence index (from PrecacheSenteFile).
   213  * @param level			Sound level.
   214  * @param flags			Sound flags.
   215  * @param volume		Sound volume.
   216  * @param pitch			Sound pitch.
   217  * @param speakerentity	Unknown.
   218  * @param origin		Sound origin.
   219  * @param dir			Sound direction.
   220  * @param updatePos		Unknown (updates positions?)
   221  * @param soundtime		Alternate time to play sound for.
   222  * @param ...			Optional list of Float[3] arrays to specify additional origins.
   223  * @noreturn
   224  * @error				Invalid client index.
   225  */
   226 native EmitSentence(const clients[],
   227 				 numClients,
   228 				 sentence,
   229 				 entity,
   230 				 channel = SNDCHAN_AUTO,
   231 				 level = SNDLEVEL_NORMAL,
   232 				 flags = SND_NOFLAGS,
   233 				 Float:volume = SNDVOL_NORMAL,
   234 				 pitch = SNDPITCH_NORMAL,
   235 				 speakerentity = -1,
   236 				 const Float:origin[3] = NULL_VECTOR,
   237 				 const Float:dir[3] = NULL_VECTOR,
   238 				 bool:updatePos = true,
   239 				 Float:soundtime = 0.0,
   240 				 any:...);
   241 
   242 				 
   243 /**
   244  * Wrapper to emit sound to one client.
   245  *
   246  * @param client		Client index.
   247  * @param entity		Entity to emit from.
   248  * @param channel		Channel to emit with.
   249  * @param sample		Sound name.
   250  * @param level			Sound level.
   251  * @param flags			Sound flags.
   252  * @param volume		Sound volume.
   253  * @param pitch			Sound pitch.
   254  * @param speakerentity	Unknown.
   255  * @param origin		Sound origin.
   256  * @param dir			Sound direction.
   257  * @param updatePos		Unknown (updates positions?)
   258  * @param soundtime		Alternate time to play sound for.
   259  * @noreturn
   260  * @error				Invalid client index.
   261  */
   262 stock EmitSoundToClient(client,
   263 				 const String:sample[],
   264 				 entity = SOUND_FROM_PLAYER,
   265 				 channel = SNDCHAN_AUTO,
   266 				 level = SNDLEVEL_NORMAL,
   267 				 flags = SND_NOFLAGS,
   268 				 Float:volume = SNDVOL_NORMAL,
   269 				 pitch = SNDPITCH_NORMAL,
   270 				 speakerentity = -1,
   271 				 const Float:origin[3] = NULL_VECTOR,
   272 				 const Float:dir[3] = NULL_VECTOR,
   273 				 bool:updatePos = true,
   274 				 Float:soundtime = 0.0)
   275 {
   276 	new clients[1];
   277 	clients[0] = client;
   278 	/* Save some work for SDKTools and remove SOUND_FROM_PLAYER references */
   279 	entity = (entity == SOUND_FROM_PLAYER) ? client : entity;
   280 	EmitSound(clients, 1, sample, entity, channel, 
   281 		level, flags, volume, pitch, speakerentity,
   282 		origin, dir, updatePos, soundtime);
   283 }
   284 
   285 /**
   286  * Wrapper to emit sound to all clients.
   287  *
   288  * @param entity		Entity to emit from.
   289  * @param channel		Channel to emit with.
   290  * @param sample		Sound name.
   291  * @param level			Sound level.
   292  * @param flags			Sound flags.
   293  * @param volume		Sound volume.
   294  * @param pitch			Sound pitch.
   295  * @param speakerentity	Unknown.
   296  * @param origin		Sound origin.
   297  * @param dir			Sound direction.
   298  * @param updatePos		Unknown (updates positions?)
   299  * @param soundtime		Alternate time to play sound for.
   300  * @noreturn
   301  * @error				Invalid client index.
   302  */
   303 stock EmitSoundToAll(const String:sample[],
   304 				 entity = SOUND_FROM_PLAYER,
   305 				 channel = SNDCHAN_AUTO,
   306 				 level = SNDLEVEL_NORMAL,
   307 				 flags = SND_NOFLAGS,
   308 				 Float:volume = SNDVOL_NORMAL,
   309 				 pitch = SNDPITCH_NORMAL,
   310 				 speakerentity = -1,
   311 				 const Float:origin[3] = NULL_VECTOR,
   312 				 const Float:dir[3] = NULL_VECTOR,
   313 				 bool:updatePos = true,
   314 				 Float:soundtime = 0.0)
   315 {
   316 	new maxClients = GetMaxClients();
   317 	new clients[maxClients];
   318 	new total = 0;
   319 	
   320 	for (new i=1; i<=maxClients; i++)
   321 	{
   322 		if (IsClientInGame(i))
   323 		{
   324 			clients[total++] = i;
   325 		}
   326 	}
   327 	
   328 	if (!total)
   329 	{
   330 		return;
   331 	}
   332 	
   333 	EmitSound(clients, total, sample, entity, channel, 
   334 		level, flags, volume, pitch, speakerentity,
   335 		origin, dir, updatePos, soundtime);
   336 }
   337 
   338 /**
   339  * Converts an attenuation value to a sound level.  
   340  * This function is from the HL2SDK.
   341  *
   342  * @param attn		Attenuation value.
   343  * @return			Integer sound level.
   344  */
   345 stock ATTN_TO_SNDLEVEL(Float:attn)
   346 {
   347 	if (attn > 0.0)
   348 	{
   349 		return RoundFloat(50.0 + (20.0 / attn));
   350 	}
   351 	return 0;
   352 }