Replacing entity:EmitSound with sound.PlayURL?

I have some code here borrowed from swamp onions. I would like to stream files in .mp3 rather than fastdl’ing wav files to clients. Anyone know how to modify this code to use PlayURL rather than EmitSound? Thanks for your time.


function SWEP:Reload()
	if self.reloading then return end
	self.reloading=true
	timer.Simple(3, function() self.reloading=false end)
	local pitch = 100 + (self.SoundPitchMod or 0) + (self.Owner:Crouching() and 40 or 0)
	self:EmitSound("mywavfile.wav", 90, pitch + math.Rand(-5,5))
	if SERVER then
		net.Start("VapeTalking")
		net.WriteEntity(self.Owner)
		net.WriteFloat(CurTime() + (2.2*100/pitch))
		net.Broadcast()
	end
end

IGModAudioChannel, which is what sound.PlayURL uses for in-game manipulation, cannot alter pitch to my knowledge, so changing to use that will strip away that feature. Either way, PlayURL should only be used for streaming dynamic audio files (TTS, radio) – FastDL should be for sounds you use consistently, like weapon sounds. This guarantees they are played (a website being down could prevent PlayURL from functioning) and played without delay. At the very least, you should almost never use anything but Entity:EmitSound() for entities to maintain proper sound networking/PAS management. Also, the code you posted isn’t properly predicted and could be a lot more structurally cohesive. I went ahead and refactored it, although, I don’t have access to the full code, so more refactoring would have to be done to the entire base.


SWEP.ReloadCooldown = 3 -- Cooldown time between Reload function calls

SWEP.ReloadSound = {
	Sound = "mywavfile.wav", -- Sound file/scape to play
	Level = 90, -- Sound range. 90 is actually quite loud ("Screaming child, passing motorcycle, convertible ride on freeway")
	PitchModifier = {-5, 5}, -- Can be table to randomly select a float in-between, or a number
	CrouchAdditive = 40 -- Pitch to add while crouched
}

function SWEP:Initialize()
	-- Other initialize code here, call baseclass if necessary
	self.m_flSoundPitchMod = 0
end

-- Could use NW(2)Var instead
function SWEP:SetupDataTables()
	-- Other datatable code here, call baseclass and adjust slot if necessary
	self:NetworkVar("Float", 0, "NextReload")
end

function SWEP:CanReload()
	return self:GetNextReload() <= CurTime() and self:GetOwner():IsValid()
end

function SWEP:Reload()
	if (self:CanReload()) then
		local tReloadSound = self.ReloadSound
		local flPitch = 100 + self.m_flSoundPitchMod
		local pPlayer = self:GetOwner()
		
		if (pPlayer:Crouching()) then
			flPitch = tReloadSound.CrouchAdditive
		end
		
		local PitchModifier = self.PitchModifier
		
		-- Support table and number modifiers
		if (istable(PitchModifier)) then
			-- util.SharedRandom guarentees 
			flPitch = flPitch + util.SharedRandom(self:GetClass(), PitchModifier[1], PitchModifier[2], self:EntIndex())
		elseif (isnumber(PitchModifier)) then
			flPitch = flPitch + PitchModifier
		end
		
		-- Would be better to use a sound script, but that would take away
		-- The ability change the pitch on the fly (m_flSoundPitchMod)
		self:EmitSound(tReloadSound.Sound, tReloadSound.Level, flPitch)
		
		local flCurTime = CurTime()
		self:SetNextReload(flCurTime + self.ReloadCooldown)
		
		if (SERVER) then
			net.Start("VapeTalking")
				net.WriteEntity(self:GetOwner())
				net.WriteFloat(flCurTime + flPitch)
			net.Broadcast() -- This could definitely be done better
		end
	end
end

Thanks so much man, Ill try and learn from your code to the best of my ability.