plugins/include/sdktools_sound.inc
author David Anderson <dvander@alliedmods.net>
Mon Oct 29 00:33:20 2007 +0000 (2007-10-29)
changeset 1652 eacd2002cda8
parent 1330 ab94316bb52b
child 1733 61f9b414c2d1
permissions -rw-r--r--
fixed nonsense documentation comment
     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.
     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.
   128  * @noreturn
   129  */
   130 native PrefetchSound(const String:nane[]);
   131 
   132 /**
   133  * Gets a sound's duration (does not work for mp3s).
   134  *
   135  * @param name		Sound file name.
   136  * @return			Duration in seconds.
   137  */
   138 native Float:GetSoundDuration(const String:name[]);
   139 
   140 /**
   141  * Emits an ambient sound.
   142  *
   143  * @param entity	Entity index to associate sound with.
   144  * @param pos		Origin of sound.
   145  * @param name		Sound file name.
   146  * @param level		Sound level (from 0 to 255).
   147  * @param flags		Sound flags.
   148  * @param vol		Volume (from 0.0 to 1.0).
   149  * @param pitch		Pitch (from 0 to 255).
   150  * @param delay		Play delay.
   151  * @noreturn
   152  */
   153 native EmitAmbientSound(const String:name[],
   154 						const Float:pos[3],
   155 						entity = SOUND_FROM_WORLD,
   156 						level = SNDLEVEL_NORMAL,
   157 						flags = SND_NOFLAGS,
   158 						Float:vol = SNDVOL_NORMAL,
   159 						pitch = SNDPITCH_NORMAL,
   160 						Float:delay = 0.0);
   161 
   162 /**
   163  * Fades a client's volume level toward silence or a given percentage.
   164  *
   165  * @param client	Client index.
   166  * @param percent	Fade percentage.
   167  * @param outtime	Fade out time, in seconds.
   168  * @param holdtime	Hold time, in seconds.
   169  * @param intime	Fade in time, in seconds.
   170  * @noreturn
   171  * @error			Invalid client index or client not in game.
   172  */
   173 native FadeClientVolume(client, Float:percent, Float:outtime, Float:holdtime, Float:intime);
   174 
   175 /**
   176  * Stops a sound.
   177  *
   178  * @param entity	Entity index.
   179  * @param channel	Channel number.
   180  * @param name		Sound name.
   181  * @noreturn
   182  */
   183 native StopSound(entity, channel, const String:name[]);
   184 
   185 /**
   186  * Emits a sound to a list of clients.
   187  *
   188  * @param clients		Array of client indexes.
   189  * @param numClients	Number of clients in the array.
   190  * @param entity		Entity to emit from.
   191  * @param channel		Channel to emit with.
   192  * @param sample		Sound name.
   193  * @param level			Sound level.
   194  * @param flags			Sound flags.
   195  * @param volume		Sound volume.
   196  * @param pitch			Sound pitch.
   197  * @param speakerentity	Unknown.
   198  * @param origin		Sound origin.
   199  * @param dir			Sound direction.
   200  * @param updatePos		Unknown (updates positions?)
   201  * @param soundtime		Alternate time to play sound for.
   202  * @param ...			Optional list of Float[3] arrays to specify additional origins.
   203  * @noreturn
   204  * @error				Invalid client index.
   205  */
   206 native EmitSound(const clients[],
   207 				 numClients,
   208 				 const String:sample[],
   209 				 entity = SOUND_FROM_PLAYER,
   210 				 channel = SNDCHAN_AUTO,
   211 				 level = SNDLEVEL_NORMAL,
   212 				 flags = SND_NOFLAGS,
   213 				 Float:volume = SNDVOL_NORMAL,
   214 				 pitch = SNDPITCH_NORMAL,
   215 				 speakerentity = -1,
   216 				 const Float:origin[3] = NULL_VECTOR,
   217 				 const Float:dir[3] = NULL_VECTOR,
   218 				 bool:updatePos = true,
   219 				 Float:soundtime = 0.0,
   220 				 any:...);
   221 
   222 /**
   223  * Emits a sentence to a list of clients.
   224  *
   225  * @param clients		Array of client indexes.
   226  * @param numClients	Number of clients in the array.
   227  * @param entity		Entity to emit from.
   228  * @param channel		Channel to emit with.
   229  * @param sentence		Sentence index (from PrecacheSenteFile).
   230  * @param level			Sound level.
   231  * @param flags			Sound flags.
   232  * @param volume		Sound volume.
   233  * @param pitch			Sound pitch.
   234  * @param speakerentity	Unknown.
   235  * @param origin		Sound origin.
   236  * @param dir			Sound direction.
   237  * @param updatePos		Unknown (updates positions?)
   238  * @param soundtime		Alternate time to play sound for.
   239  * @param ...			Optional list of Float[3] arrays to specify additional origins.
   240  * @noreturn
   241  * @error				Invalid client index.
   242  */
   243 native EmitSentence(const clients[],
   244 				 numClients,
   245 				 sentence,
   246 				 entity,
   247 				 channel = SNDCHAN_AUTO,
   248 				 level = SNDLEVEL_NORMAL,
   249 				 flags = SND_NOFLAGS,
   250 				 Float:volume = SNDVOL_NORMAL,
   251 				 pitch = SNDPITCH_NORMAL,
   252 				 speakerentity = -1,
   253 				 const Float:origin[3] = NULL_VECTOR,
   254 				 const Float:dir[3] = NULL_VECTOR,
   255 				 bool:updatePos = true,
   256 				 Float:soundtime = 0.0,
   257 				 any:...);
   258 
   259 				 
   260 /**
   261  * Wrapper to emit sound to one client.
   262  *
   263  * @param client		Client index.
   264  * @param entity		Entity to emit from.
   265  * @param channel		Channel to emit with.
   266  * @param sample		Sound name.
   267  * @param level			Sound level.
   268  * @param flags			Sound flags.
   269  * @param volume		Sound volume.
   270  * @param pitch			Sound pitch.
   271  * @param speakerentity	Unknown.
   272  * @param origin		Sound origin.
   273  * @param dir			Sound direction.
   274  * @param updatePos		Unknown (updates positions?)
   275  * @param soundtime		Alternate time to play sound for.
   276  * @noreturn
   277  * @error				Invalid client index.
   278  */
   279 stock EmitSoundToClient(client,
   280 				 const String:sample[],
   281 				 entity = SOUND_FROM_PLAYER,
   282 				 channel = SNDCHAN_AUTO,
   283 				 level = SNDLEVEL_NORMAL,
   284 				 flags = SND_NOFLAGS,
   285 				 Float:volume = SNDVOL_NORMAL,
   286 				 pitch = SNDPITCH_NORMAL,
   287 				 speakerentity = -1,
   288 				 const Float:origin[3] = NULL_VECTOR,
   289 				 const Float:dir[3] = NULL_VECTOR,
   290 				 bool:updatePos = true,
   291 				 Float:soundtime = 0.0)
   292 {
   293 	new clients[1];
   294 	clients[0] = client;
   295 	/* Save some work for SDKTools and remove SOUND_FROM_PLAYER references */
   296 	entity = (entity == SOUND_FROM_PLAYER) ? client : entity;
   297 	EmitSound(clients, 1, sample, entity, channel, 
   298 		level, flags, volume, pitch, speakerentity,
   299 		origin, dir, updatePos, soundtime);
   300 }
   301 
   302 /**
   303  * Wrapper to emit sound to all clients.
   304  *
   305  * @param entity		Entity to emit from.
   306  * @param channel		Channel to emit with.
   307  * @param sample		Sound name.
   308  * @param level			Sound level.
   309  * @param flags			Sound flags.
   310  * @param volume		Sound volume.
   311  * @param pitch			Sound pitch.
   312  * @param speakerentity	Unknown.
   313  * @param origin		Sound origin.
   314  * @param dir			Sound direction.
   315  * @param updatePos		Unknown (updates positions?)
   316  * @param soundtime		Alternate time to play sound for.
   317  * @noreturn
   318  * @error				Invalid client index.
   319  */
   320 stock EmitSoundToAll(const String:sample[],
   321 				 entity = SOUND_FROM_PLAYER,
   322 				 channel = SNDCHAN_AUTO,
   323 				 level = SNDLEVEL_NORMAL,
   324 				 flags = SND_NOFLAGS,
   325 				 Float:volume = SNDVOL_NORMAL,
   326 				 pitch = SNDPITCH_NORMAL,
   327 				 speakerentity = -1,
   328 				 const Float:origin[3] = NULL_VECTOR,
   329 				 const Float:dir[3] = NULL_VECTOR,
   330 				 bool:updatePos = true,
   331 				 Float:soundtime = 0.0)
   332 {
   333 	new maxClients = GetMaxClients();
   334 	new clients[maxClients];
   335 	new total = 0;
   336 	
   337 	for (new i=1; i<=maxClients; i++)
   338 	{
   339 		if (IsClientInGame(i))
   340 		{
   341 			clients[total++] = i;
   342 		}
   343 	}
   344 	
   345 	if (!total)
   346 	{
   347 		return;
   348 	}
   349 	
   350 	EmitSound(clients, total, sample, entity, channel, 
   351 		level, flags, volume, pitch, speakerentity,
   352 		origin, dir, updatePos, soundtime);
   353 }
   354 
   355 /**
   356  * Converts an attenuation value to a sound level.  
   357  * This function is from the HL2SDK.
   358  *
   359  * @param attn		Attenuation value.
   360  * @return			Integer sound level.
   361  */
   362 stock ATTN_TO_SNDLEVEL(Float:attn)
   363 {
   364 	if (attn > 0.0)
   365 	{
   366 		return RoundFloat(50.0 + (20.0 / attn));
   367 	}
   368 	return 0;
   369 }