SHARE
TWEET

Untitled

a guest Nov 17th, 2019 68 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #define PLUGIN_VERSION      "1.0"
  2.  
  3. #pragma semicolon 1
  4. #pragma newdecls required
  5.  
  6. #include <sourcemod>
  7. #include <sdktools>
  8.  
  9. #define CVAR_FLAGS              FCVAR_NOTIFY
  10.  
  11. bool g_bVoice_Talking;
  12.  
  13. public Plugin myinfo =
  14. {
  15.     name = "[ANY] Voice - Sound Attenuation",
  16.     author = "SilverShot",
  17.     description = "Reduces game volume when someone talks.",
  18.     version = PLUGIN_VERSION,
  19.     url = ""
  20. }
  21.  
  22. /*
  23.     Requred:
  24.    
  25.     1. VoiceHook extension by Accelerator74
  26.     https://forums.alliedmods.net/showthread.php?t=279231
  27.    
  28.     or
  29.    
  30.     2. VoiceannounceEX plugin by Franc1sco
  31.     https://forums.alliedmods.net/showthread.php?t=245384
  32.    
  33.     TODO:
  34.     retrieve ambient entities
  35.     add silence to a weapon
  36.    
  37.     env_soundscape
  38.    
  39.     ambient_generic
  40.     GetEntPropString(entity, Prop_Data, "m_iszSound", sSound, sizeof(sSound));
  41.     int len = strlen(sSound);
  42.     if (len > 4 && (StrEqual(sSound[len-3], "mp3") || StrEqual(sSound[len-3], "wav")))
  43.    
  44.    
  45.    
  46. */
  47.  
  48. ConVar g_hCvarAllow;
  49. ConVar g_hCvarAmbFactor;
  50. ConVar g_hCvarNormFactor;
  51.  
  52. bool g_bCvarAllow;
  53. float g_fAmbFactor;
  54. float g_fNormFactor;
  55. //Handle g_hTimer;
  56.  
  57. public void OnPluginStart()
  58. {
  59.     g_hCvarAllow =          CreateConVar(   "l4d_sound_atten_allow",            "1",            "0=Plugin Off, 1=Plugin On.", CVAR_FLAGS);
  60.     g_hCvarAmbFactor =      CreateConVar(   "l4d_sound_atten_factor_ambience",  "0.5",          "Decrease ambient sounds volume by this factor when someone is speaking", CVAR_FLAGS);
  61.     g_hCvarNormFactor =     CreateConVar(   "l4d_sound_atten_factor_normal",    "0.5",          "Decrease normal sounds volume by this factor when someone is speaking", CVAR_FLAGS);
  62.    
  63.    
  64.    
  65.     //RegConsoleCmd("sm_test", CmdTest, "");
  66.  
  67.     g_hCvarAllow.AddChangeHook(ConVarChanged_Cvars);
  68.     g_hCvarAmbFactor.AddChangeHook(ConVarChanged_Cvars);
  69.     g_hCvarNormFactor.AddChangeHook(ConVarChanged_Cvars);
  70.    
  71.     GetCvars();
  72. }
  73.  
  74. public Action CmdTest(int client, int args)
  75. {
  76.     char sSound[PLATFORM_MAX_PATH];
  77.     int ent = -1;
  78.    
  79.     while (-1 != (ent = FindEntityByClassname(ent, "ambient_generic")))
  80.     {
  81.         GetEntPropString(ent, Prop_Data, "m_iszSound", sSound, sizeof(sSound));
  82.         //SetEntProp(ent, Prop_Send, "nv_m_ambientVolume", 1);
  83.        
  84.        
  85.         //SetEntProp(ent, Prop_Data, "m_iVolume", 1);
  86.         //SetEntProp(ent, Prop_Data, "m_iSoundLevel", 1);
  87.        
  88.         //AcceptEntityInput(ent, "StopSound");
  89.         //SetVariantFloat(2.0);
  90.         //AcceptEntityInput(ent, "Volume");
  91.        
  92.         PrintToChatAll("found: %i - %s. Level: %i", ent, sSound, GetEntProp(ent, Prop_Data, "m_iSoundLevel"));
  93.     }
  94.    
  95.     PrintToChatAll("sound search completed");
  96.    
  97.     //int len = strlen(sSound);
  98.     //if (len > 4 && (StrEqual(sSound[len-3], "mp3") || StrEqual(sSound[len-3], "wav")))
  99.  
  100.     return Plugin_Handled;
  101. }
  102.  
  103. public void ConVarChanged_Cvars(Handle convar, const char[] oldValue, const char[] newValue)
  104. {
  105.     GetCvars();
  106. }
  107.  
  108. void GetCvars()
  109. {
  110.     g_bCvarAllow = g_hCvarAllow.BoolValue;
  111.     g_fAmbFactor = g_hCvarAmbFactor.FloatValue;
  112.     g_fNormFactor = g_hCvarNormFactor.FloatValue;
  113.    
  114.     SetHook();
  115. }
  116.  
  117. void SetHook()
  118. {
  119.     static bool bHooked;
  120.    
  121.     if (g_bCvarAllow) {
  122.         if (!bHooked) {
  123.             AddNormalSoundHook(view_as<NormalSHook>(SoundHookNormal));
  124.             AddAmbientSoundHook(view_as<AmbientSHook>(SoundHookAmbient));
  125.             //AddTempEntHook("Impact", TE_Hook_ShotgunShot);
  126.             //AddTempEntHook("Shotgun Shot", TE_Hook_ShotgunShot);
  127.             //AddTempEntHook("Fire Bullets", TE_Hook_ShotgunShot);
  128.  
  129.             /*
  130.             if (g_hTimer == INVALID_HANDLE)
  131.             {
  132.                 if (GetExtensionFileStatus("VoiceHook.ext") == 1)
  133.                 {
  134.                     g_hTimer = CreateTimer(2.0, Timer_UpdateSpeaking, _, TIMER_REPEAT);
  135.                 }
  136.             }
  137.             */
  138.             bHooked = true;
  139.         }
  140.     } else {
  141.         if (bHooked) {
  142.             RemoveNormalSoundHook (view_as<NormalSHook>(SoundHookNormal));
  143.             RemoveAmbientSoundHook(view_as<AmbientSHook>(SoundHookAmbient));
  144.            
  145.             /*
  146.             if (g_hTimer != INVALID_HANDLE)
  147.             {
  148.                 KillTimer(g_hTimer);
  149.                 g_hTimer = INVALID_HANDLE;
  150.             }
  151.             */
  152.             bHooked = false;
  153.         }
  154.     }
  155. }
  156.  
  157. public Action TE_Hook_ShotgunShot(const char[] te_name, const int[] Players, int numClients, float delay)
  158. {
  159.     PrintToChatAll(te_name);
  160.     return Plugin_Stop;
  161. }
  162.  
  163. /*
  164. public Action Timer_UpdateSpeaking(Handle timer)
  165. {
  166.     g_bVoice_Talking = false;
  167. }
  168.  
  169. public void OnClientSpeaking(int client) // VoiceHook
  170. {
  171.     g_bVoice_Talking = true;
  172. }
  173. */
  174.  
  175. public void OnClientSpeakingStart(int client) // VoiceHook v3.0
  176. {
  177.     g_bVoice_Talking = true;
  178. }
  179.  
  180. public void OnClientSpeakingEx(int client) // VoiceannounceEX
  181. {
  182.     g_bVoice_Talking = true;
  183. }
  184.  
  185. public void OnClientSpeakingEnd(int client) // VoiceannounceEX & VoiceHook v3.0
  186. {
  187.     g_bVoice_Talking = false;
  188. }
  189.  
  190. 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)
  191. {
  192.     if( g_bVoice_Talking )
  193.     {
  194.         //PrintToChatAll("Normal: \x03%s \x01decreased: (volume) \x04%.2f => %.2f\x01, (level) \x04%i => %i", sample, volume, volume * g_fNormFactor, level, RoundToCeil(level * g_fNormFactor));
  195.         volume *= g_fNormFactor;
  196.         level = RoundToCeil(level * g_fNormFactor);
  197.         flags |= SND_CHANGEVOL;
  198.         return Plugin_Changed;
  199.     }
  200.     return Plugin_Continue;
  201. }
  202.  
  203. 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)
  204. {
  205.     if( g_bVoice_Talking )
  206.     {
  207.         //PrintToChatAll("Ambient: \x03%s \x01decreased: (volume) \x04%.2f => %.2f\x01, (level) \x04%i => %i", sample, volume, volume * g_fNormFactor, level, RoundToCeil(level * g_fNormFactor));
  208.         volume *= g_fAmbFactor;
  209.         level = RoundToCeil(level * g_fNormFactor);
  210.         flags |= SND_CHANGEVOL;
  211.         return Plugin_Changed;
  212.     }
  213.     return Plugin_Continue;
  214. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top