-
Notifications
You must be signed in to change notification settings - Fork 56
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #378 from thecraftianman/sound-lib
Add sound library + fully clientsided sounds
- Loading branch information
Showing
19 changed files
with
372 additions
and
145 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,116 @@ | ||
local Sounds = ACF.Utilities.Sounds | ||
|
||
do -- Valid sound check | ||
local file = file | ||
local isstring = isstring | ||
local Folder = "sound/%s" | ||
local ValidSounds = {} | ||
|
||
function Sounds.IsValidSound(Name) | ||
if not isstring(Name) then return false end | ||
|
||
local Path = Folder:format(Name:Trim()) | ||
local Valid = ValidSounds[Path] | ||
|
||
if Valid == nil then | ||
Valid = file.Exists(Path, "GAME") | ||
|
||
ValidSounds[Path] = Valid | ||
end | ||
|
||
return Valid | ||
end | ||
end | ||
|
||
do -- Playing regular sounds | ||
function Sounds.PlaySound(Origin, Path, Level, Pitch, Volume) | ||
Volume = ACF.Volume * Volume | ||
|
||
if isentity(Origin) and IsValid(Origin) then | ||
Origin:EmitSound(Path, Level, Pitch, Volume) | ||
elseif isvector(Origin) then | ||
sound.Play(Path, Origin, Level, Pitch, Volume) | ||
end | ||
end | ||
|
||
net.Receive("ACF_Sounds", function() | ||
local IsEnt = net.ReadBool() | ||
local Origin = IsEnt and net.ReadEntity() or net.ReadVector() | ||
local Path = net.ReadString() | ||
local Level = net.ReadUInt(7) | ||
local Pitch = net.ReadUInt(8) | ||
local Volume = net.ReadFloat() | ||
|
||
if not Sounds.IsValidSound(Path) then return end | ||
|
||
Sounds.PlaySound(Origin, Path, Level, Pitch, Volume) | ||
end) | ||
end | ||
|
||
do -- Processing adjustable sounds (for example, engine noises) | ||
local IsValid = IsValid | ||
|
||
function Sounds.UpdateAdjustableSound(Origin, Pitch, Volume) | ||
if not IsValid(Origin) then return end | ||
|
||
local Sound = Origin.Sound | ||
if not Sound then return end | ||
|
||
Volume = Volume * ACF.Volume | ||
|
||
if Sound:IsPlaying() then | ||
Sound:ChangePitch(Pitch, 0) | ||
Sound:ChangeVolume(Volume, 0) | ||
else | ||
Sound:PlayEx(Volume, Pitch) | ||
end | ||
end | ||
|
||
function Sounds.CreateAdjustableSound(Origin, Path, Pitch, Volume) | ||
if not IsValid(Origin) then return end | ||
if Origin.Sound then return end | ||
|
||
local Sound = CreateSound(Origin, Path) | ||
Origin.Sound = Sound | ||
|
||
-- Ensuring that the sound can't stick around if the server doesn't properly ask for it to be destroyed | ||
Origin:CallOnRemove("ACF_ForceStopAdjustableSound", function(Entity) | ||
Sounds.DestroyAdjustableSound(Entity) | ||
end) | ||
|
||
Sounds.UpdateAdjustableSound(Origin, Pitch, Volume) | ||
end | ||
|
||
function Sounds.DestroyAdjustableSound(Origin) | ||
local Current = Origin.Sound | ||
if not Current then return end | ||
|
||
Current:Stop() | ||
Origin.Sound = nil | ||
end | ||
|
||
net.Receive("ACF_Sounds_Adjustable", function() | ||
local Origin = net.ReadEntity() | ||
local ShouldStop = net.ReadBool() | ||
|
||
if ShouldStop then | ||
Sounds.DestroyAdjustableSound(Origin) | ||
else | ||
local Pitch = net.ReadUInt(8) | ||
local Volume = net.ReadFloat() | ||
|
||
Sounds.UpdateAdjustableSound(Origin, Pitch, Volume) | ||
end | ||
end) | ||
|
||
net.Receive("ACF_Sounds_AdjustableCreate", function() | ||
local Origin = net.ReadEntity() | ||
local Path = net.ReadString() | ||
local Pitch = net.ReadUInt(8) | ||
local Volume = net.ReadFloat() | ||
|
||
if not Sounds.IsValidSound(Path) then return end | ||
|
||
Sounds.CreateAdjustableSound(Origin, Path, Pitch, Volume) | ||
end) | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
local Sounds = ACF.Utilities.Sounds | ||
|
||
util.AddNetworkString("ACF_Sounds") | ||
util.AddNetworkString("ACF_Sounds_Adjustable") | ||
util.AddNetworkString("ACF_Sounds_AdjustableCreate") | ||
|
||
function Sounds.SendSound(Origin, Path, Level, Pitch, Volume) | ||
if not IsValid(Origin) then return end | ||
|
||
local IsEnt = isentity(Origin) | ||
local Pos | ||
|
||
-- Set default Gmod level/pitch values if not present | ||
Level = Level or 75 | ||
Pitch = Pitch or 100 | ||
|
||
net.Start("ACF_Sounds") | ||
net.WriteBool(IsEnt) | ||
if IsEnt then | ||
net.WriteEntity(Origin) | ||
Pos = Origin:GetPos() | ||
else | ||
net.WriteVector(Origin) | ||
Pos = Origin | ||
end | ||
net.WriteString(Path) | ||
net.WriteUInt(Level, 7) | ||
net.WriteUInt(Pitch, 8) | ||
net.WriteFloat(Volume) | ||
net.SendPAS(Pos) | ||
end | ||
|
||
function Sounds.CreateAdjustableSound(Origin, Path, Pitch, Volume) | ||
if not IsValid(Origin) then return end | ||
|
||
net.Start("ACF_Sounds_AdjustableCreate") | ||
net.WriteEntity(Origin) | ||
net.WriteString(Path) | ||
net.WriteUInt(Pitch, 8) | ||
net.WriteFloat(Volume) | ||
net.SendPAS(Origin:GetPos()) | ||
end | ||
|
||
function Sounds.SendAdjustableSound(Origin, ShouldStop, Pitch, Volume) | ||
if not IsValid(Origin) then return end | ||
|
||
ShouldStop = ShouldStop or false | ||
local Time = CurTime() | ||
Origin.ACF.SoundTimer = Origin.ACF.SoundTimer or Time | ||
|
||
-- Slowing down the rate of sending a bit | ||
if Origin.ACF.SoundTimer <= Time or ShouldStop then | ||
net.Start("ACF_Sounds_Adjustable") | ||
net.WriteEntity(Origin) | ||
net.WriteBool(ShouldStop) | ||
if not ShouldStop then | ||
net.WriteUInt(Pitch, 8) | ||
net.WriteFloat(Volume) | ||
end | ||
net.SendPAS(Origin:GetPos()) | ||
Origin.ACF.SoundTimer = Time + 0.1 | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.