Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #define PLUGIN_VERSION "1.0"
- #pragma semicolon 1
- #pragma newdecls required
- #include <sourcemod>
- #include <sdktools>
- #define CVAR_FLAGS FCVAR_NOTIFY
- bool g_bVoice_Talking;
- public Plugin myinfo =
- {
- name = "[ANY] Voice - Sound Attenuation",
- author = "SilverShot",
- description = "Reduces game volume when someone talks.",
- version = PLUGIN_VERSION,
- url = ""
- }
- /*
- Requred:
- 1. VoiceHook extension by Accelerator74
- https://forums.alliedmods.net/showthread.php?t=279231
- or
- 2. VoiceannounceEX plugin by Franc1sco
- https://forums.alliedmods.net/showthread.php?t=245384
- TODO:
- retrieve ambient entities
- add silence to a weapon
- env_soundscape
- ambient_generic
- GetEntPropString(entity, Prop_Data, "m_iszSound", sSound, sizeof(sSound));
- int len = strlen(sSound);
- if (len > 4 && (StrEqual(sSound[len-3], "mp3") || StrEqual(sSound[len-3], "wav")))
- */
- ConVar g_hCvarAllow;
- ConVar g_hCvarAmbFactor;
- ConVar g_hCvarNormFactor;
- bool g_bCvarAllow;
- float g_fAmbFactor;
- float g_fNormFactor;
- //Handle g_hTimer;
- public void OnPluginStart()
- {
- g_hCvarAllow = CreateConVar( "l4d_sound_atten_allow", "1", "0=Plugin Off, 1=Plugin On.", CVAR_FLAGS);
- g_hCvarAmbFactor = CreateConVar( "l4d_sound_atten_factor_ambience", "0.5", "Decrease ambient sounds volume by this factor when someone is speaking", CVAR_FLAGS);
- g_hCvarNormFactor = CreateConVar( "l4d_sound_atten_factor_normal", "0.5", "Decrease normal sounds volume by this factor when someone is speaking", CVAR_FLAGS);
- //RegConsoleCmd("sm_test", CmdTest, "");
- g_hCvarAllow.AddChangeHook(ConVarChanged_Cvars);
- g_hCvarAmbFactor.AddChangeHook(ConVarChanged_Cvars);
- g_hCvarNormFactor.AddChangeHook(ConVarChanged_Cvars);
- GetCvars();
- }
- public Action CmdTest(int client, int args)
- {
- char sSound[PLATFORM_MAX_PATH];
- int ent = -1;
- while (-1 != (ent = FindEntityByClassname(ent, "ambient_generic")))
- {
- GetEntPropString(ent, Prop_Data, "m_iszSound", sSound, sizeof(sSound));
- //SetEntProp(ent, Prop_Send, "nv_m_ambientVolume", 1);
- //SetEntProp(ent, Prop_Data, "m_iVolume", 1);
- //SetEntProp(ent, Prop_Data, "m_iSoundLevel", 1);
- //AcceptEntityInput(ent, "StopSound");
- //SetVariantFloat(2.0);
- //AcceptEntityInput(ent, "Volume");
- PrintToChatAll("found: %i - %s. Level: %i", ent, sSound, GetEntProp(ent, Prop_Data, "m_iSoundLevel"));
- }
- PrintToChatAll("sound search completed");
- //int len = strlen(sSound);
- //if (len > 4 && (StrEqual(sSound[len-3], "mp3") || StrEqual(sSound[len-3], "wav")))
- return Plugin_Handled;
- }
- public void ConVarChanged_Cvars(Handle convar, const char[] oldValue, const char[] newValue)
- {
- GetCvars();
- }
- void GetCvars()
- {
- g_bCvarAllow = g_hCvarAllow.BoolValue;
- g_fAmbFactor = g_hCvarAmbFactor.FloatValue;
- g_fNormFactor = g_hCvarNormFactor.FloatValue;
- SetHook();
- }
- void SetHook()
- {
- static bool bHooked;
- if (g_bCvarAllow) {
- if (!bHooked) {
- AddNormalSoundHook(view_as<NormalSHook>(SoundHookNormal));
- AddAmbientSoundHook(view_as<AmbientSHook>(SoundHookAmbient));
- //AddTempEntHook("Impact", TE_Hook_ShotgunShot);
- //AddTempEntHook("Shotgun Shot", TE_Hook_ShotgunShot);
- //AddTempEntHook("Fire Bullets", TE_Hook_ShotgunShot);
- /*
- if (g_hTimer == INVALID_HANDLE)
- {
- if (GetExtensionFileStatus("VoiceHook.ext") == 1)
- {
- g_hTimer = CreateTimer(2.0, Timer_UpdateSpeaking, _, TIMER_REPEAT);
- }
- }
- */
- bHooked = true;
- }
- } else {
- if (bHooked) {
- RemoveNormalSoundHook (view_as<NormalSHook>(SoundHookNormal));
- RemoveAmbientSoundHook(view_as<AmbientSHook>(SoundHookAmbient));
- /*
- if (g_hTimer != INVALID_HANDLE)
- {
- KillTimer(g_hTimer);
- g_hTimer = INVALID_HANDLE;
- }
- */
- bHooked = false;
- }
- }
- }
- public Action TE_Hook_ShotgunShot(const char[] te_name, const int[] Players, int numClients, float delay)
- {
- PrintToChatAll(te_name);
- return Plugin_Stop;
- }
- /*
- public Action Timer_UpdateSpeaking(Handle timer)
- {
- g_bVoice_Talking = false;
- }
- public void OnClientSpeaking(int client) // VoiceHook
- {
- g_bVoice_Talking = true;
- }
- */
- public void OnClientSpeakingStart(int client) // VoiceHook v3.0
- {
- g_bVoice_Talking = true;
- }
- public void OnClientSpeakingEx(int client) // VoiceannounceEX
- {
- g_bVoice_Talking = true;
- }
- public void OnClientSpeakingEnd(int client) // VoiceannounceEX & VoiceHook v3.0
- {
- g_bVoice_Talking = false;
- }
- public Action SoundHookNormal(int clients[64], int &numClients, char sample[PLATFORM_MAX_PATH], int &entity, int &channel, float &volume, int &level, int &pitch, int &flags)
- {
- if( g_bVoice_Talking )
- {
- //PrintToChatAll("Normal: \x03%s \x01decreased: (volume) \x04%.2f => %.2f\x01, (level) \x04%i => %i", sample, volume, volume * g_fNormFactor, level, RoundToCeil(level * g_fNormFactor));
- volume *= g_fNormFactor;
- level = RoundToCeil(level * g_fNormFactor);
- flags |= SND_CHANGEVOL;
- return Plugin_Changed;
- }
- return Plugin_Continue;
- }
- public Action SoundHookAmbient(int clients[64], int &numClients, char sample[PLATFORM_MAX_PATH], int &entity, int &channel, float &volume, int &level, int &pitch, int &flags)
- {
- if( g_bVoice_Talking )
- {
- //PrintToChatAll("Ambient: \x03%s \x01decreased: (volume) \x04%.2f => %.2f\x01, (level) \x04%i => %i", sample, volume, volume * g_fNormFactor, level, RoundToCeil(level * g_fNormFactor));
- volume *= g_fAmbFactor;
- level = RoundToCeil(level * g_fNormFactor);
- flags |= SND_CHANGEVOL;
- return Plugin_Changed;
- }
- return Plugin_Continue;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement