From 7d69fbaf24fdbaaff5c7df7490d1884f7339af4e Mon Sep 17 00:00:00 2001 From: stepa2 Date: Tue, 30 May 2023 17:08:17 +0300 Subject: [PATCH] E2 sound extensions (#2578) * Exported E2 SoundCore functions * Added modified parts of xtrasound core https://steamcommunity.com/sharedfiles/filedetails/?id=2221932128 * Added emitsound core * Replaced tabs with spaces * Refactoring fix * Another refactoring fix I really need to sometimes test *before* pushing * Merged emitsound into sound core, un-exported soundcore internals * Fixed `emitSound` and `emitSoundStop` documentation --- .../gmod_wire_expression2/core/sound.lua | 116 ++++++++++++++++++ lua/wire/client/e2descriptions.lua | 23 ++++ 2 files changed, 139 insertions(+) diff --git a/lua/entities/gmod_wire_expression2/core/sound.lua b/lua/entities/gmod_wire_expression2/core/sound.lua index 4f311c76..4b38944c 100644 --- a/lua/entities/gmod_wire_expression2/core/sound.lua +++ b/lua/entities/gmod_wire_expression2/core/sound.lua @@ -8,6 +8,9 @@ local wire_expression2_maxsounds = CreateConVar( "wire_expression2_maxsounds", 1 local wire_expression2_sound_burst_max = CreateConVar( "wire_expression2_sound_burst_max", 8, {FCVAR_ARCHIVE} ) local wire_expression2_sound_burst_rate = CreateConVar( "wire_expression2_sound_burst_rate", 0.1, {FCVAR_ARCHIVE} ) +-- _level_max: Sets the maximum soundLevel we can set on a sound. 140 is maximum to begin with, a more non-obnoxious level is maybe around 110. +local wire_expression2_sound_level_max = CreateConVar( "wire_expression2_sound_level_max", 110, {FCVAR_ARCHIVE} ) + --------------------------------------------------------------- -- Helper functions --------------------------------------------------------------- @@ -44,6 +47,7 @@ local function getSound( self, index ) return self.data.sound_data.sounds[index] end + local function soundStop(self, index, fade) local sound = getSound( self, index ) if not sound then return end @@ -214,6 +218,118 @@ e2function number soundDuration(string sound) end __e2setcost(nil) +-- From https://steamcommunity.com/sharedfiles/filedetails/?id=2221932128, modified +---------------------------------------------------- +-- soundLevel, soundDSP (Monkatraz) +---------------------------------------------------- + +__e2setcost(5) + +e2function void soundDSP( index, dsp ) + local sound = getSound( self, index ) + if not sound then return end + -- We need to apply the DSP while the sound is stopped + sound:Stop() + sound:SetDSP( math.Clamp( dsp, 0, 34 ) ) -- clamped up to 34 because anything past 34 produces the sound of the letter E + sound:Play() +end +e2function void soundDSP( string index, dsp ) = e2function void soundDSP( index, dsp ) + +e2function void soundLevel( index, level ) + local sound = getSound( self, index ) + if not sound then return end + -- We need to set the level while the sound is stopped + sound:Stop() + sound:SetSoundLevel( math.Clamp( level, 0, wire_expression2_sound_level_max:GetInt() ) ) + sound:Play() +end +e2function void soundLevel( string index, level ) = e2function void soundLevel( index, level ) + +---------------------------------------------------- +-- Other stuff (Tim) +---------------------------------------------------- + +-- GETs for the above + +__e2setcost(2) + +e2function number soundDSP( index ) + local sound = getSound( self, index ) + if not sound then return 0 end + return sound:GetDSP() or 0 +end +e2function number soundDSP( string index ) = e2function number soundDSP( index ) + +e2function number soundLevel( index ) + local sound = getSound( self, index ) + if not sound then return 0 end + return sound:GetSoundLevel() +end +e2function number soundLevel( string index ) = e2function number soundLevel( index ) + +-- Extras (GETs) + +e2function number soundPitch( index ) + local sound = getSound( self, index ) + if not sound then return 0 end + return sound:GetPitch() +end +e2function number soundPitch( string index ) = e2function number soundPitch( index ) + +e2function number soundVolume( index ) + local sound = getSound( self, index ) + if not sound then return 0 end + return sound:GetVolume() +end +e2function number soundVolume( string index ) = e2function number soundVolume( index ) + +e2function number soundPlaying( index ) + local sound = getSound( self, index ) + if not sound then return 0 end + return sound:IsPlaying() and 1 or 0 +end +e2function number soundPlaying( string index ) = e2function number soundPlaying( index ) + +-- EmitSound + +local function EmitSound(e2, ent, snd, level, pitch, volume) + if not isAllowed(e2) then return end + + if not IsValid(ent) then return e2:throw("Invalid entity!", nil) end + if not isOwner(e2, ent) then return e2:throw("You do not own this entity!", nil) end + + local maxlevel = wire_expression2_sound_level_max:GetInt() + if level ~= nil and level > maxlevel then + level = maxlevel + end + + ent:EmitSound(snd, level, pitch, volume) +end + +__e2setcost(20) +e2function void entity:emitSound(string soundName, number soundLevel, number pitchPercent, number volume) + EmitSound(self, this, soundName, soundLevel, pitchPercent, volume) +end + +e2function void entity:emitSound(string soundName, number soundLevel, number pitchPercent) + EmitSound(self, this, soundName, soundLevel, pitchPercent) +end + +e2function void entity:emitSound(string soundName, number soundLevel) + EmitSound(self, this, soundName, soundLevel) +end + +e2function void entity:emitSound(string soundName) + EmitSound(self, this, soundName) +end + +e2function void entity:emitSoundStop(string soundName) + if not IsValid(this) then return self:throw("Invalid entity!", nil) end + if not isOwner(self, this) then return self:throw("You do not own this entity!", nil) end + + this:StopSound(soundName) +end + --------------------------------------------------------------- registerCallback("construct", function(self) diff --git a/lua/wire/client/e2descriptions.lua b/lua/wire/client/e2descriptions.lua index 74e865de..eb8ecdb1 100644 --- a/lua/wire/client/e2descriptions.lua +++ b/lua/wire/client/e2descriptions.lua @@ -1510,6 +1510,29 @@ E2Helper.Descriptions["soundVolume(snn)"] = "soundVolume(string Index, Volume, F E2Helper.Descriptions["soundVolume(sn)"] = "soundVolume(string Index, Volume), where Volume is a number between 0 and 1. Default Volume is 1" E2Helper.Descriptions["soundVolume(nn)"] = "soundVolume(integer Index, Volume), where Volume is a number between 0 and 1. Default Volume is 1" E2Helper.Descriptions["soundVolume(nnn)"] = "soundVolume(integer Index, Volume, FadeTime), where Volume is a number between 0 and 1. Default Volume is 1" +E2Helper.Descriptions["soundDSP(nn)"] = "Sets the DSP effect for the sound at the index, then restarts the sound" +E2Helper.Descriptions["soundDSP(sn)"] = "Sets the DSP effect for the sound at the index, then restarts the sound" +E2Helper.Descriptions["soundLevel(nn)"] = "Sets the sound's level in dB, then restarts the sound. This affects how far away the sound can be heard." +E2Helper.Descriptions["soundLevel(sn)"] = "Sets the sound's level in dB, then restarts the sound. This affects how far away the sound can be heard." +E2Helper.Descriptions["soundDSP(n)"] = "Returns the DSP effect for the sound at the index (default 0)" +E2Helper.Descriptions["soundDSP(s)"] = "Returns the DSP effect for the sound at the index (default 0)" +E2Helper.Descriptions["soundLevel(n)"] = "Returns the sound level in dB for the sound at the index" +E2Helper.Descriptions["soundLevel(s)"] = "Returns the sound level in dB for the sound at the index" +E2Helper.Descriptions["soundPitch(n)"] = "Returns the pitch of the sound at the index" +E2Helper.Descriptions["soundPitch(s)"] = "Returns the pitch of the sound at the index" +E2Helper.Descriptions["soundVolume(n)"] = "Returns the volume of the sound at the index" +E2Helper.Descriptions["soundVolume(s)"] = "Returns the volume of the sound at the index" +E2Helper.Descriptions["soundPlaying(n)"] = "Returns 1 if the sound at the index is playing, 0 if not" +E2Helper.Descriptions["soundPlaying(s)"] = "Returns 1 if the sound at the index is playing, 0 if not" + +do + local EmitSoundDoc = "Plays sound on entity. Note that one file can only be played once in a time." + E2Helper.Descriptions["emitSound(e:s)"] = EmitSoundDoc + E2Helper.Descriptions["emitSound(e:sn)"] = EmitSoundDoc + E2Helper.Descriptions["emitSound(e:snn)"] = EmitSoundDoc + E2Helper.Descriptions["emitSound(e:snnn)"] = EmitSoundDoc +end +E2Helper.Descriptions["emitSoundStop(e:s)"] = "Stops sound played with 'emitSound'" -- UTF-8 E2Helper.Descriptions["toUnicodeChar(...)"] = "Returns the UTF-8 string from the given Unicode code-points"