make playsound volume distance dependent
Moderator: Moderators
make playsound volume distance dependent
Currently when you do play-sound in a script the sound is heard all over the map at the same volume no matter where the camera is. It is directional tough, somehow. These sounds need to work exactly like weapon fire/explosion sounds... this shouldn't really be hard to fix if you know the code just make these sounds do the same thing as all the others??? copy paste?
This is really needed to put in random noises and stuff, like in supcom, would add a ton to immersion possibilty.
This is really needed to put in random noises and stuff, like in supcom, would add a ton to immersion possibilty.
As the PLAY_SOUND code in rts/Sim/Units/COB/CobThread.h
appears to have a fixed number of parameters, you may want
to setup a custom GET instead (which would also need a tie in
to the sound file names, and check for 0's in the args?).
Alternatively, you could use LuaCob.
The lua sound command is setup as such:
PlaySound("filename"[, volume[, x, y, z]]) -- [] mean optional
So it can be used as follows:
PlaySound("test") -- local sound; full volume (1.0)
PlaySound("test", 0.5) -- local sound; half volume
PlaySound("test", 0.5, 0, 100, 0) -- world sound @ 0,100,0; half volume
With LuaCob, you could already get what you want:
COB: call-script lua_PlaySound(SOUND_ID, 50, 0, 100, 0)
In LuaCob/main.lua
This is untested code, but the theory is sound 
Actually, I've hidden one important detail, the PlaySound() call is only
available in the unsynced lua code, so it would have to be relayed using
SendToUnsynced().
appears to have a fixed number of parameters, you may want
to setup a custom GET instead (which would also need a tie in
to the sound file names, and check for 0's in the args?).
Alternatively, you could use LuaCob.
The lua sound command is setup as such:
PlaySound("filename"[, volume[, x, y, z]]) -- [] mean optional
So it can be used as follows:
PlaySound("test") -- local sound; full volume (1.0)
PlaySound("test", 0.5) -- local sound; half volume
PlaySound("test", 0.5, 0, 100, 0) -- world sound @ 0,100,0; half volume
With LuaCob, you could already get what you want:
COB: call-script lua_PlaySound(SOUND_ID, 50, 0, 100, 0)
In LuaCob/main.lua
Code: Select all
PlaySound(id, volume, x, y, z)
local file = soundMap[id]
if (not file) then return end
if (not volume) then
Spring.PlaySound(file)
return
end
if ((not x) or (not y) or (not z)) then
Spring.PlaySound(file, volume)
return
end
Spring.PlaySound(file, volume, x, y, z)
return
end

Actually, I've hidden one important detail, the PlaySound() call is only
available in the unsynced lua code, so it would have to be relayed using
SendToUnsynced().
Ok, is it really a good idea to have this go through LUA calls? I mean, if I use it for footsteps, and generally for a LOT of little sounds... wouldn't it be slow as hell??
Anyway I'm trying this out, but I have no clue how to do it...
1. I realised I need like a LUA compiler or something since .lua files look like garbage in notepad, so I found one "LUAEdit" that works on windsows, seems fine.
2. If I understand correctly I make a folder in the spring folder called LuaCob... or should it be in the mod folder? or script folder?
3. I make an empty lua file with the editor, called main.lua in that folder.
4. I copy pasted trepans code from above in there:
5. PHAIL!!!! the editor complains that [String D:\TASpring\Luacob\main.lua"](14) <eof> expected near `end'
6. I tried to put in <eof> at the bottom, as that is what any sane person would read that it wanted, but still no dice, same message.
Anyway I'm trying this out, but I have no clue how to do it...
1. I realised I need like a LUA compiler or something since .lua files look like garbage in notepad, so I found one "LUAEdit" that works on windsows, seems fine.
2. If I understand correctly I make a folder in the spring folder called LuaCob... or should it be in the mod folder? or script folder?
3. I make an empty lua file with the editor, called main.lua in that folder.
4. I copy pasted trepans code from above in there:
Code: Select all
PlaySound(id, volume, x, y, z)
local file = soundMap[id]
if (not file) then return end
if (not volume) then
Spring.PlaySound(file)
return
end
if ((not x) or (not y) or (not z)) then
Spring.PlaySound(file, volume)
return
end
Spring.PlaySound(file, volume, x, y, z)
return
end
6. I tried to put in <eof> at the bottom, as that is what any sane person would read that it wanted, but still no dice, same message.
Try this (you'll find instructions in the comments):
http://trepan.bzflag.bz/spring/cobsound.lua
It provides:
lua_PlayUnitSound(sndID [, volume])
lua_PlayLocalSound(sndID [, volume])
lua_PlayWorldSound(sndID, x, y, z [, volume])
PlayUnitSound() is the same as PlayWorldSound, except
that the sound's position is grabbed from the calling unit's
position.
NOTE1: You'll need a Units/LuaSound.h header file to
associate sound file names to sound IDs. The same file should be
used to compile the COB scripts, and stored in the archive (which
LuaCob will parse to get the id->name mapping). I've included the
'.luacob print' command in the script to automatically generate
the file data for you (just copy it from infolog.txt)
NOTE2: You'll need to use a recent SVN build, the VFS
DirList call was changed in the past week, and will affect the
'.luacob print' call.
http://trepan.bzflag.bz/spring/cobsound.lua
It provides:
lua_PlayUnitSound(sndID [, volume])
lua_PlayLocalSound(sndID [, volume])
lua_PlayWorldSound(sndID, x, y, z [, volume])
PlayUnitSound() is the same as PlayWorldSound, except
that the sound's position is grabbed from the calling unit's
position.
NOTE1: You'll need a Units/LuaSound.h header file to
associate sound file names to sound IDs. The same file should be
used to compile the COB scripts, and stored in the archive (which
LuaCob will parse to get the id->name mapping). I've included the
'.luacob print' command in the script to automatically generate
the file data for you (just copy it from infolog.txt)
NOTE2: You'll need to use a recent SVN build, the VFS
DirList call was changed in the past week, and will affect the
'.luacob print' call.
Last edited by trepan on 06 Oct 2007, 18:11, edited 1 time in total.
I got it working, good job trepan... this is critical stuff for truely thorough immersion modmaking. And also god job luring me into getting one foot into using LUA... i guess I'll soon be using it more and more... soon spring will lag like supcom.
As a paranthesis I still think the lualess plain vanilla cob play-sound call should be localized, mainly for ease of use... and global sounds from units be handled via LUA in this way. Any use for global sounds is obviously UI related, so it seems to me you would probably be using LUA to begin with when using those. This LUA sound thing obviously is great when for all the control it gives, but I think it's overkill if someone just wants to add step sounds and such. Not to mention doing these LUA calls for every step or two a unit takes is a potential performance hog... but looking at scripts that are (numberofunits)^2 (proximity detection and such) this might not be so bad anyway.
The LuaSounds.h file should be put in the /luacob directory tough, you commented out the one that goes in /units. This is a bit confusing until I looked in the code. Also this is in the mod directory if anyone wondering, not the spring directory. TASpring/mods/yourmodname/luacob.
Also these sounds seem to decrease much faster with distance then weapon/explosion sounds...

As a paranthesis I still think the lualess plain vanilla cob play-sound call should be localized, mainly for ease of use... and global sounds from units be handled via LUA in this way. Any use for global sounds is obviously UI related, so it seems to me you would probably be using LUA to begin with when using those. This LUA sound thing obviously is great when for all the control it gives, but I think it's overkill if someone just wants to add step sounds and such. Not to mention doing these LUA calls for every step or two a unit takes is a potential performance hog... but looking at scripts that are (numberofunits)^2 (proximity detection and such) this might not be so bad anyway.
The LuaSounds.h file should be put in the /luacob directory tough, you commented out the one that goes in /units. This is a bit confusing until I looked in the code. Also this is in the mod directory if anyone wondering, not the spring directory. TASpring/mods/yourmodname/luacob.
Also these sounds seem to decrease much faster with distance then weapon/explosion sounds...
Sorry about the LuaSounds.h location, looks like I simply
forgot to swap the comments after I'd tested the code. The
correct (final) location for the LuaSounds.h file should be in
the Units/ directory, to make it easier to compile scripts (imo).
As for local vs. positional COB sounds, take a look at:
rts/Sim/Units/COB/CobInstance.cpp:869 (should be positional)
I've also left a note on that line concerning the volume handling.
As I've haven't found any BOS scripts that use play-sound, what
kind of values are normally used for the volume?
Using a higher volume setting will probably fix your perceived
fall-off problem when using positional sounds through the Lua
interface.
EDIT: I removed the note from CobInstance.cpp after having
found an example of how play-sound is being used:
play-sound("LIGHTBEAM", PLAYSOUND_PRIORITY_BATTLE)
{ where PLAYSOUND_PRIORITY_BATTLE is 4 }
forgot to swap the comments after I'd tested the code. The
correct (final) location for the LuaSounds.h file should be in
the Units/ directory, to make it easier to compile scripts (imo).
As for local vs. positional COB sounds, take a look at:
rts/Sim/Units/COB/CobInstance.cpp:869 (should be positional)
I've also left a note on that line concerning the volume handling.
As I've haven't found any BOS scripts that use play-sound, what
kind of values are normally used for the volume?
Using a higher volume setting will probably fix your perceived
fall-off problem when using positional sounds through the Lua
interface.
EDIT: I removed the note from CobInstance.cpp after having
found an example of how play-sound is being used:
play-sound("LIGHTBEAM", PLAYSOUND_PRIORITY_BATTLE)
{ where PLAYSOUND_PRIORITY_BATTLE is 4 }
Well check this:
http://www.youtube.com/watch?v=MV8YlXImMr8
The second bang is generated with the lua script like this (no volume specified):
and the first one is the normal death sound. It's the same sound, you can clearly hear their the same volume up close but zooming out just a little the scripted sound pretty much disappears.
http://www.youtube.com/watch?v=MV8YlXImMr8
The second bang is generated with the lua script like this (no volume specified):
Code: Select all
emit-sfx 1026 from body;
call-script lua_PlayUnitSound(119);
I don't know if other players can hear it since using a dev version of spring fucks up the ai... but i certainly hope they can, it would be freaking cool that you can hear what's coming before you see it... like in the 300 when they go:rattle wrote:So the local PlaySound calls mean that only the player to which the unit belongs to will hear the sound and you won't hear it (and more importantly, won't play it) unless it is in hearable range?
"-is that thunder?"
"-no, battle formations"
If you watch the video however, you see the sound falls off very quickly and you can't hear it unless zoomed in.
Ah tested with a load volume, so the volume setting dosn't really make it loader when close by, but increases the distance you can hear it... got confused there, but this is really good.trepan wrote:Zpock: re-read my previous post (3rd paragraph)
Now all we need is to code in the doppler effect and make some nice jet aircraft sounds, hehe.
This is how it was, the sound was played for all players. One of the reasons why almost no one used play-sound.Zpock wrote:but i certainly hope they can, it would be freaking cool that you can hear what's coming before you see it... like in the 300 when they go:
"-is that thunder?"
"-no, battle formations"
It might be actually useful now with the falloff though.
Trepan, I use play-sound for many of the unit-death explosions in NanoBlobs. Also, I should note that the volume controls for play-sound in COB do work... and the sounds are already played positionally. The only problem has been getting the dropoff to work correctly, because the play-sound command in COB only passes a volume parameter, so everything uses the same falloff :-/