plugins/include/sdktools_sound.inc
author David Anderson <dvander@alliedmods.net>
Sat May 17 05:12:52 2008 +0000 (2008-05-17)
changeset 2056 f1fcde46ac7c
parent 1952 b7aa2b626807
child 2289 44045ead61cd
permissions -rw-r--r--
added amb1119 - sm_revote command
     1 /**
     2  * vim: set ts=4 :
     3  * =============================================================================
     4  * SourceMod (C)2004-2008 AlliedModders LLC.  All rights reserved.
     5  * =============================================================================
     6  *
     7  * This file is part of the SourceMod/SourcePawn SDK.
     8  *
     9  * This program is free software; you can redistribute it and/or modify it under
    10  * the terms of the GNU General Public License, version 3.0, as published by the
    11  * Free Software Foundation.
    12  * 
    13  * This program is distributed in the hope that it will be useful, but WITHOUT
    14  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
    15  * FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
    16  * details.
    17  *
    18  * You should have received a copy of the GNU General Public License along with
    19  * this program.  If not, see <http://www.gnu.org/licenses/>.
    20  *
    21  * As a special exception, AlliedModders LLC gives you permission to link the
    22  * code of this program (as well as its derivative works) to "Half-Life 2," the
    23  * "Source Engine," the "SourcePawn JIT," and any Game MODs that run on software
    24  * by the Valve Corporation.  You must obey the GNU General Public License in
    25  * all respects for all other code used.  Additionally, AlliedModders LLC grants
    26  * this exception to all derivative works.  AlliedModders LLC defines further
    27  * exceptions, found in LICENSE.txt (as of this writing, version JULY-31-2007),
    28  * or <http://www.sourcemod.net/license.php>.
    29  *
    30  * Version: $Id$
    31  */
    32 
    33 #if defined _sdktools_sound_included
    34   #endinput
    35 #endif
    36 #define _sdktools_sound_included
    37 
    38 /**
    39  * Sound should be from the target client.
    40  */
    41 #define SOUND_FROM_PLAYER		-2
    42 
    43 /**
    44  * Sound should be from the listen server player.
    45  */
    46 #define SOUND_FROM_LOCAL_PLAYER	-1
    47 
    48 /**
    49  * Sound is from the world.
    50  */
    51 #define SOUND_FROM_WORLD		0
    52 
    53 /**
    54  * Sound channels.
    55  */
    56 enum
    57 {
    58 	SNDCHAN_REPLACE = -1,		/**< Unknown */
    59 	SNDCHAN_AUTO = 0,			/**< Auto */
    60 	SNDCHAN_WEAPON = 1,			/**< Weapons */
    61 	SNDCHAN_VOICE = 2,			/**< Voices */
    62 	SNDCHAN_ITEM = 3,			/**< Items */
    63 	SNDCHAN_BODY = 4,			/**< Player? */
    64 	SNDCHAN_STREAM = 5,			/**< "Stream channel from the static or dynamic area" */
    65 	SNDCHAN_STATIC = 6,			/**< "Stream channel from the static area" */
    66 	SNDCHAN_VOICE_BASE = 7,		/**< "Channel for network voice data" */
    67 	SNDCHAN_USER_BASE = 135		/**< Anything >= this is allocated to game code */
    68 };
    69 
    70 /**
    71  * Sound flags for the sound emitter system.
    72  */
    73 enum
    74 {
    75 	SND_NOFLAGS= 0,			/**< Nothing */
    76 	SND_CHANGEVOL = 1,		/**< Change sound volume */
    77 	SND_CHANGEPITCH = 2,	/**< Change sound pitch */
    78 	SND_STOP = 3,			/**< Stop the sound */
    79 	SND_SPAWNING = 4,		/**< Used in some cases for ambients */
    80 	SND_DELAY = 5,			/**< Sound has an initial delay */
    81 	SND_STOPLOOPING = 6,	/**< Stop looping all sounds on the entity */
    82 	SND_SPEAKER = 7,		/**< Being played by a mic through a speaker */
    83 	SND_SHOULDPAUSE = 8,	/**< Pause if game is paused */
    84 };
    85 
    86 /**
    87  * Various predefined sound levels in dB.
    88  */
    89 enum
    90 {
    91 	SNDLEVEL_NONE = 0,			/**< None */
    92 	SNDLEVEL_RUSTLE = 20,		/**< Rustling leaves */
    93 	SNDLEVEL_WHISPER = 25,		/**< Whispering */
    94 	SNDLEVEL_LIBRARY = 30,		/**< In a library */
    95 	SNDLEVEL_FRIDGE = 45,		/**< Refridgerator */
    96 	SNDLEVEL_HOME = 50,			/**< Average home (3.9 attn) */
    97 	SNDLEVEL_CONVO = 60,		/**< Normal conversation (2.0 attn) */
    98 	SNDLEVEL_DRYER = 60,		/**< Clothes dryer */
    99 	SNDLEVEL_DISHWASHER = 65,	/**< Dishwasher/washing machine (1.5 attn) */
   100 	SNDLEVEL_CAR = 70,			/**< Car or vacuum cleaner (1.0 attn) */
   101 	SNDLEVEL_NORMAL = 75,		/**< Normal sound level */
   102 	SNDLEVEL_TRAFFIC = 75,		/**< Busy traffic (0.8 attn) */
   103 	SNDLEVEL_MINIBIKE = 80,		/**< Mini-bike, alarm clock (0.7 attn) */
   104 	SNDLEVEL_SCREAMING = 90,	/**< Screaming child (0.5 attn) */
   105 	SNDLEVEL_TRAIN = 100,		/**< Subway train, pneumatic drill (0.4 attn) */
   106 	SNDLEVEL_HELICOPTER = 105,	/**< Helicopter */
   107 	SNDLEVEL_SNOWMOBILE = 110,	/**< Snow mobile */
   108 	SNDLEVEL_AIRCRAFT = 120,	/**< Auto horn, aircraft */
   109 	SNDLEVEL_RAIDSIREN = 130,	/**< Air raid siren */
   110 	SNDLEVEL_GUNFIRE = 140,		/**< Gunshot, jet engine (0.27 attn) */
   111 	SNDLEVEL_ROCKET = 180,		/**< Rocket launching (0.2 attn) */
   112 };
   113 
   114 #define SNDVOL_NORMAL		1.0		/**< Normal volume */
   115 #define SNDPITCH_NORMAL		100		/**< Normal pitch */
   116 #define SNDPITCH_LOW		95		/**< A low pitch */
   117 #define SNDPITCH_HIGH		120		/**< A high pitch */
   118 #define SNDATTN_NONE		0.0		/**< No attenuation */
   119 #define SNDATTN_NORMAL		0.8		/**< Normal attenuation */
   120 #define SNDATTN_STATIC		1.25	/**< Static attenuation? */
   121 #define SNDATTN_RICOCHET	1.5		/**< Ricochet effect */
   122 #define SNDATTN_IDLE		2.0		/**< Idle attenuation? */
   123  
   124 /**
   125  * Prefetches a sound.
   126  *
   127  * @param name		Sound file name relative to the "sounds" folder.
   128  * @noreturn
   129  */
   130 native PrefetchSound(const String:nane[]);
   131 
   132 /**
   133  * This function is not known to work, and may crash.  You should 
   134  * not use it.  It is provided for backwards compatibility only.
   135  *
   136  * @param name		Sound file name relative to the "sounds" folder.
   137  * @return			Duration in seconds.
   138  */
   139 #pragma deprecated Does not work, may crash.
   140 native Float:GetSoundDuration(const String:name[]);
   141 
   142 /**
   143  * Emits an ambient sound.
   144  *
   145  * @param name		Sound file name relative to the "sounds" folder.
   146  * @param pos		Origin of sound.
   147  * @param entity	Entity index to associate sound with.
   148  * @param level		Sound level (from 0 to 255).
   149  * @param flags		Sound flags.
   150  * @param vol		Volume (from 0.0 to 1.0).
   151  * @param pitch		Pitch (from 0 to 255).
   152  * @param delay		Play delay.
   153  * @noreturn
   154  */
   155 native EmitAmbientSound(const String:name[],
   156 						const Float:pos[3],
   157 						entity = SOUND_FROM_WORLD,
   158 						level = SNDLEVEL_NORMAL,
   159 						flags = SND_NOFLAGS,
   160 						Float:vol = SNDVOL_NORMAL,
   161 						pitch = SNDPITCH_NORMAL,
   162 						Float:delay = 0.0);
   163 
   164 /**
   165  * Fades a client's volume level toward silence or a given percentage.
   166  *
   167  * @param client	Client index.
   168  * @param percent	Fade percentage.
   169  * @param outtime	Fade out time, in seconds.
   170  * @param holdtime	Hold time, in seconds.
   171  * @param intime	Fade in time, in seconds.
   172  * @noreturn
   173  * @error			Invalid client index or client not in game.
   174  */
   175 native FadeClientVolume(client, Float:percent, Float:outtime, Float:holdtime, Float:intime);
   176 
   177 /**
   178  * Stops a sound.
   179  *
   180  * @param entity	Entity index.
   181  * @param channel	Channel number.
   182  * @param name		Sound file name relative to the "sounds" folder.
   183  * @noreturn
   184  */
   185 native StopSound(entity, channel, const String:name[]);
   186 
   187 /**
   188  * Emits a sound to a list of clients.
   189  *
   190  * @param clients		Array of client indexes.
   191  * @param numClients	Number of clients in the array.
   192  * @param sample		Sound file name relative to the "sounds" folder.
   193  * @param entity		Entity to emit from.
   194  * @param channel		Channel to emit with.
   195  * @param level			Sound level.
   196  * @param flags			Sound flags.
   197  * @param volume		Sound volume.
   198  * @param pitch			Sound pitch.
   199  * @param speakerentity	Unknown.
   200  * @param origin		Sound origin.
   201  * @param dir			Sound direction.
   202  * @param updatePos		Unknown (updates positions?)
   203  * @param soundtime		Alternate time to play sound for.
   204  * @param ...			Optional list of Float[3] arrays to specify additional origins.
   205  * @noreturn
   206  * @error				Invalid client index.
   207  */
   208 native EmitSound(const clients[],
   209 				 numClients,
   210 				 const String:sample[],
   211 				 entity = SOUND_FROM_PLAYER,
   212 				 channel = SNDCHAN_AUTO,
   213 				 level = SNDLEVEL_NORMAL,
   214 				 flags = SND_NOFLAGS,
   215 				 Float:volume = SNDVOL_NORMAL,
   216 				 pitch = SNDPITCH_NORMAL,
   217 				 speakerentity = -1,
   218 				 const Float:origin[3] = NULL_VECTOR,
   219 				 const Float:dir[3] = NULL_VECTOR,
   220 				 bool:updatePos = true,
   221 				 Float:soundtime = 0.0,
   222 				 any:...);
   223 
   224 /**
   225  * Emits a sentence to a list of clients.
   226  *
   227  * @param clients		Array of client indexes.
   228  * @param numClients	Number of clients in the array.
   229  * @param sentence		Sentence index (from PrecacheSenteFile).
   230  * @param entity		Entity to emit from.
   231  * @param channel		Channel to emit with.
   232  * @param level			Sound level.
   233  * @param flags			Sound flags.
   234  * @param volume		Sound volume.
   235  * @param pitch			Sound pitch.
   236  * @param speakerentity	Unknown.
   237  * @param origin		Sound origin.
   238  * @param dir			Sound direction.
   239  * @param updatePos		Unknown (updates positions?)
   240  * @param soundtime		Alternate time to play sound for.
   241  * @param ...			Optional list of Float[3] arrays to specify additional origins.
   242  * @noreturn
   243  * @error				Invalid client index.
   244  */
   245 native EmitSentence(const clients[],
   246 				 numClients,
   247 				 sentence,
   248 				 entity,
   249 				 channel = SNDCHAN_AUTO,
   250 				 level = SNDLEVEL_NORMAL,
   251 				 flags = SND_NOFLAGS,
   252 				 Float:volume = SNDVOL_NORMAL,
   253 				 pitch = SNDPITCH_NORMAL,
   254 				 speakerentity = -1,
   255 				 const Float:origin[3] = NULL_VECTOR,
   256 				 const Float:dir[3] = NULL_VECTOR,
   257 				 bool:updatePos = true,
   258 				 Float:soundtime = 0.0,
   259 				 any:...);
   260 
   261 /**
   262  * Called when an ambient sound is about to be emitted to one or more clients.
   263  *
   264  * NOTICE: all parameters can be overwritten to modify the default behavior.
   265  *
   266  * @param sample	Sound file name relative to the "sounds" folder.
   267  * @param entity	Entity index associated to the sound.
   268  * @param volume	Volume (from 0.0 to 1.0).
   269  * @param level		Sound level (from 0 to 255).
   270  * @param pitch		Pitch (from 0 to 255).
   271  * @param pos		Origin of sound.
   272  * @param flags		Sound flags.
   273  * @param delay		Play delay.
   274  * @return		Plugin_Continue to allow the sound to be played, Plugin_Stop to block it, 
   275  *			  Plugin_Changed when any parameter has been modified.
   276  */
   277 functag AmbientSHook Action:public(String:sample[PLATFORM_MAX_PATH], &entity, &Float:volume, &level, &pitch, Float:pos[3], &flags, &Float:delay);
   278 
   279 /**
   280  * Called when a sound is going to be emitted to one or more clients.
   281  * NOTICE: all params can be overwritten to modify the default behaviour.
   282  *
   283  * @param clients		Array of client indexes.
   284  * @param numClients		Number of clients in the array (modify this value if you add/remove elements from the client array).
   285  * @param sample		Sound file name relative to the "sounds" folder.
   286  * @param entity		Entity emitting the sound.
   287  * @param channel		Channel emitting the sound.
   288  * @param volume		Sound volume.
   289  * @param level			Sound level.
   290  * @param pitch			Sound pitch.
   291  * @param flags			Sound flags.
   292  * @return			Plugin_Continue to allow the sound to be played, Plugin_Stop to block it, 
   293  *				  Plugin_Changed when any parameter has been modified.
   294  */
   295 functag NormalSHook Action:public(clients[64], &numClients, String:sample[PLATFORM_MAX_PATH], &entity, &channel, &Float:volume, &level, &pitch, &flags);
   296 
   297 /**
   298  * Hooks all played ambient sounds.
   299  *
   300  * @param hook		Function to use as a hook.
   301  * @noreturn
   302  * @error		Invalid function hook.
   303  */
   304 native AddAmbientSoundHook(AmbientSHook:hook);
   305 
   306 /**
   307  * Hooks all played normal sounds.
   308  *
   309  * @param hook		Function to use as a hook.
   310  * @noreturn
   311  * @error		Invalid function hook.
   312  */
   313 native AddNormalSoundHook(NormalSHook:hook);
   314 
   315 /**
   316  * Unhooks all played ambient sounds.
   317  *
   318  * @param hook		Function used for the hook.
   319  * @noreturn
   320  * @error		Invalid function hook.
   321  */
   322 native RemoveAmbientSoundHook(AmbientSHook:hook);
   323 
   324 /**
   325  * Unhooks all played normal sounds.
   326  *
   327  * @param hook		Function used for the hook.
   328  * @noreturn
   329  * @error		Invalid function hook.
   330  */
   331 native RemoveNormalSoundHook(NormalSHook:hook);
   332 				 
   333 /**
   334  * Wrapper to emit sound to one client.
   335  *
   336  * @param client		Client index.
   337  * @param sample		Sound file name relative to the "sounds" folder.
   338  * @param entity		Entity to emit from.
   339  * @param channel		Channel to emit with.
   340  * @param level			Sound level.
   341  * @param flags			Sound flags.
   342  * @param volume		Sound volume.
   343  * @param pitch			Sound pitch.
   344  * @param speakerentity	Unknown.
   345  * @param origin		Sound origin.
   346  * @param dir			Sound direction.
   347  * @param updatePos		Unknown (updates positions?)
   348  * @param soundtime		Alternate time to play sound for.
   349  * @noreturn
   350  * @error				Invalid client index.
   351  */
   352 stock EmitSoundToClient(client,
   353 				 const String:sample[],
   354 				 entity = SOUND_FROM_PLAYER,
   355 				 channel = SNDCHAN_AUTO,
   356 				 level = SNDLEVEL_NORMAL,
   357 				 flags = SND_NOFLAGS,
   358 				 Float:volume = SNDVOL_NORMAL,
   359 				 pitch = SNDPITCH_NORMAL,
   360 				 speakerentity = -1,
   361 				 const Float:origin[3] = NULL_VECTOR,
   362 				 const Float:dir[3] = NULL_VECTOR,
   363 				 bool:updatePos = true,
   364 				 Float:soundtime = 0.0)
   365 {
   366 	new clients[1];
   367 	clients[0] = client;
   368 	/* Save some work for SDKTools and remove SOUND_FROM_PLAYER references */
   369 	entity = (entity == SOUND_FROM_PLAYER) ? client : entity;
   370 	EmitSound(clients, 1, sample, entity, channel, 
   371 		level, flags, volume, pitch, speakerentity,
   372 		origin, dir, updatePos, soundtime);
   373 }
   374 
   375 /**
   376  * Wrapper to emit sound to all clients.
   377  *
   378  * @param sample		Sound file name relative to the "sounds" folder.
   379  * @param entity		Entity to emit from.
   380  * @param channel		Channel to emit with.
   381  * @param level			Sound level.
   382  * @param flags			Sound flags.
   383  * @param volume		Sound volume.
   384  * @param pitch			Sound pitch.
   385  * @param speakerentity	Unknown.
   386  * @param origin		Sound origin.
   387  * @param dir			Sound direction.
   388  * @param updatePos		Unknown (updates positions?)
   389  * @param soundtime		Alternate time to play sound for.
   390  * @noreturn
   391  * @error				Invalid client index.
   392  */
   393 stock EmitSoundToAll(const String:sample[],
   394 				 entity = SOUND_FROM_PLAYER,
   395 				 channel = SNDCHAN_AUTO,
   396 				 level = SNDLEVEL_NORMAL,
   397 				 flags = SND_NOFLAGS,
   398 				 Float:volume = SNDVOL_NORMAL,
   399 				 pitch = SNDPITCH_NORMAL,
   400 				 speakerentity = -1,
   401 				 const Float:origin[3] = NULL_VECTOR,
   402 				 const Float:dir[3] = NULL_VECTOR,
   403 				 bool:updatePos = true,
   404 				 Float:soundtime = 0.0)
   405 {
   406 	new maxClients = GetMaxClients();
   407 	new clients[maxClients];
   408 	new total = 0;
   409 	
   410 	for (new i=1; i<=maxClients; i++)
   411 	{
   412 		if (IsClientInGame(i))
   413 		{
   414 			clients[total++] = i;
   415 		}
   416 	}
   417 	
   418 	if (!total)
   419 	{
   420 		return;
   421 	}
   422 	
   423 	EmitSound(clients, total, sample, entity, channel, 
   424 		level, flags, volume, pitch, speakerentity,
   425 		origin, dir, updatePos, soundtime);
   426 }
   427 
   428 /**
   429  * Converts an attenuation value to a sound level.  
   430  * This function is from the HL2SDK.
   431  *
   432  * @param attn		Attenuation value.
   433  * @return			Integer sound level.
   434  */
   435 stock ATTN_TO_SNDLEVEL(Float:attn)
   436 {
   437 	if (attn > 0.0)
   438 	{
   439 		return RoundFloat(50.0 + (20.0 / attn));
   440 	}
   441 	return 0;
   442 }