gmcl_sound - Extra clientside sound functions

http://chrisaster.net/files/gmcl_sound.zip (Source included)

Functions:

sound.GetAll() – Creates a table of all sounds currently playing and gives the following information:

  • Guid
  • Volume
  • Pitch
  • Radius
  • Channel
  • From server
  • From speaker ent
  • Ignoring DSP
  • Source can move
  • Length
  • Direction
  • Origin

sound.StopAll() – Stops all sounds that are currently playing
sound.StopByGuid(guid) – Stops a specific sound by its guid
sound.SetVolByGuid(guid, volume) – Sets the volume of a specific sound by its guid (volume = 0.0 -> 1.0)
sound.IsPlayingByGuid(guid) – Checks if a sound is playing by its guid

Examples:



require("sound")

//List all sounds currently playing

for k, v in pairs(sound.GetAll()) do
	if v.Name != NULL then
		print(v.Name)
	end
end

//Show where sounds are being emitted from:

hook.Add("HUDPaint", "SoundPos", function() 
	for k, v in pairs(sound.GetAll()) do
		if v.Origin != NULL then
			local pos = Vector(v.Origin.x, v.Origin.y, v.Origin.z) 
			cam.Start3D(EyePos(), EyeAngles())
				render.SetMaterial(Material("hlmv/debugmrmwireframe"))
				render.DrawSprite(pos, 2, 2, Color(0, 0, 0, 255))		
			cam.End3D()
		end
	end 
end )

//Stop individual sounds:

for k, v in pairs(sound.GetAll()) do
	if v.Name == "lasthuman.mp3" then
		sound.StopByGuid(k)
	end
end

//Check if a sound if playing

for k, v in pairs(sound.GetAll()) do
	if v.Name == "lasthuman.mp3" then
		print("Last human music is currently playing!")
		break
	end
end

//Stop a sound played from a folder

for k, v in pairs(sound.GetAll()) do 
	if v.Name == "gu_zs\\humanlose.mp3" then 
		sound.StopByGuid(k) 
	end 
end


Thanks to:

  • AzuiSleet for helping prevent occasional crashes
  • Overv and FlapJack for testing
  • Levibreak for showing how to stop sounds that have been played from a folder

Please post any bugs you may come across in this thread.

I am 12 and what is this?

Sounds rather useful actually, if it’s real.

Tis real.

Epic.

Yep, it’s epic. :slight_smile:

Hm, very nice. Maybe move some of the functionality to an oop approach?

Tell me how to make a table of userdata and I’ll be happy to :slight_smile:

Hm, at first I though the engine used sound objects instead of ids. You may need to create a class inside the module and work with that. Jinto has a good example for assigning mtatables to userdata. Download this.

Or you could create a lua based class either as a separate script or inside the module.

I’ll try to whip up some code for you.

Can’t find any code in there that will help me return a table of userdata. I’m aiming for something similar to player.GetAll() at the moment.

Well, create a new table, loop through your array and SetMember your value with the current index as the key? Or you don’t know how to pass your userdata as a viable type? If the later, then…



ILuaObject* obj = g_Lua->GetNewTable( );
obj->SetUserData( yourobject );
obj->SetMetaTable( yourmetatable );


The odbc library shows how to manipulate metatables.

I’ve been notified of a bug with sounds in folders, however I can’t seem to find out what’s causing it.

Example:



] play gu_zs\humanlose.mp3
] lua_run_cl for k, v in pairs(sound.GetAll()) do if v.Name == "gu_zs\humanlose.mp3" then sound.StopByGuid(k) else Msg(v.Name) end end
gu_zs\humanlose.mp3


Any ideas?



] play gu_zs\humanlose.mp3
] lua_run_cl for k, v in pairs(sound.GetAll()) do if v.Name == "gu_zs\\humanlose.mp3" then sound.StopByGuid(k) else Msg(v.Name) end end
gu_zs\humanlose.mp3


Try that.

Yep, that works perfectly.

Thanks, I’ll add that as an example.

Just remember that \ exits the string, so that if you need a real backslash, you need \. :V

BUMP.

So, it would be possible to get a sound that’s currently playing (within the first few milliseconds-when you can’t even HEAR the sound) and replace/modify it?

-snip-

Yes. Also, for more examples see my MP3 player (http://www.facepunch.com/showthread.php?t=755611). The module is not exactly the same, but it uses some of the gmcl_sound functions.

So, how would you do that?