make playsound volume distance dependent

make playsound volume distance dependent

Requests for features in the spring code.

Moderator: Moderators

Post Reply
User avatar
Zpock
Posts: 1218
Joined: 16 Sep 2004, 23:20

make playsound volume distance dependent

Post by Zpock »

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.
User avatar
Peet
Malcontent
Posts: 4384
Joined: 27 Feb 2006, 22:04

Post by Peet »

This should be an option IMO, as it's usable as a global notification too.
trepan
Former Engine Dev
Posts: 1200
Joined: 17 Nov 2005, 00:52

Post by trepan »

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

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
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().
User avatar
Zpock
Posts: 1218
Joined: 16 Sep 2004, 23:20

Post by Zpock »

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:

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
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.
trepan
Former Engine Dev
Posts: 1200
Joined: 17 Nov 2005, 00:52

Post by trepan »

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.
Last edited by trepan on 06 Oct 2007, 18:11, edited 1 time in total.
trepan
Former Engine Dev
Posts: 1200
Joined: 17 Nov 2005, 00:52

Post by trepan »

Hm, might want to try a PlayGroundSound() as well, so that
packed unit coordinates can be used. Could also write a call
that uses packed XZ, and a separate Y.
User avatar
Zpock
Posts: 1218
Joined: 16 Sep 2004, 23:20

Post by Zpock »

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. :lol:

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...
trepan
Former Engine Dev
Posts: 1200
Joined: 17 Nov 2005, 00:52

Post by trepan »

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 }
User avatar
Zpock
Posts: 1218
Joined: 16 Sep 2004, 23:20

Post by Zpock »

Well check this:

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);
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.
User avatar
rattle
Damned Developer
Posts: 8278
Joined: 01 Jun 2006, 13:15

Post by rattle »

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?
User avatar
Zpock
Posts: 1218
Joined: 16 Sep 2004, 23:20

Post by Zpock »

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?
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:

"-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.
trepan
Former Engine Dev
Posts: 1200
Joined: 17 Nov 2005, 00:52

Post by trepan »

Zpock: re-read my previous post (3rd paragraph)
User avatar
Zpock
Posts: 1218
Joined: 16 Sep 2004, 23:20

Post by Zpock »

trepan wrote:Zpock: re-read my previous post (3rd paragraph)
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.

Now all we need is to code in the doppler effect and make some nice jet aircraft sounds, hehe.
User avatar
rattle
Damned Developer
Posts: 8278
Joined: 01 Jun 2006, 13:15

Post by rattle »

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"
This is how it was, the sound was played for all players. One of the reasons why almost no one used play-sound.

It might be actually useful now with the falloff though.
User avatar
Snipawolf
Posts: 4357
Joined: 12 Dec 2005, 01:49

Post by Snipawolf »

So, would this lag if we had 1500 or so units, all using scripts like these? I wanna do this for infantry running, tanks rolling, etc. but I would like to know if it will be too much...
User avatar
Argh
Posts: 10920
Joined: 21 Feb 2005, 03:38

Post by Argh »

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 :-/
Post Reply

Return to “Feature Requests”