Game Development Community

Quick guide to sound and audio

by Ben Sizer · in Torque 3D Professional · 07/12/2011 (10:59 am) · 11 replies

There's a lot of reference detail in the docs, which is great, but is there something more simple and straightforward to get me started? I have 3 basic use cases:

- immediately trigger a one-shot sound centred on a given position in the world
- associate a looping sound with a certain position or area in the world
- play a piece of background music

The docs mention SFXPlay and SFXPlayOnce but when I search through the demo project there's hardly anything that calls these, and the code that does call them is not commented or very self-explanatory.

Is there a guide or very simple tutorial somewhere?


#1
07/12/2011 (11:20 am)
Only very rough and quick guidelines but maybe it helps...

Quote:- immediately trigger a one-shot sound centred on a given position in the world

// Play a one-shot sound effect at position (10,10,10).
sfxPlayOnce( AudioDefault3D, "art/sound/mySound", "10 10 10" );

// Alternatively, set up your sound data in advance somewhere (where it gets loaded on startup)...
singleton SFXProfile( MySound )
{
   fileName = "art/sound/mySound";
   description = AudioDefault3D;
};

//... and then play it.
sfxPlayOnce( MySound, "10 10 10" );

Another alternative is to use an SFXEmitter in the world if only one such sound can be audible at any one time.

Quote:- associate a looping sound with a certain position or area in the world

// 1. Set up your audio file:
singleton SFXProfile( MyAmbientAudio )
{
   fileName = "art/sound/myAmbientSound";
   
   // This description is for a streaming sound playing in the music channel.
   // For reasonably short sounds, set up a non-streaming sound playing in that channel.
   description = AudioMusicLoop2D;
};

// 2. Set up your ambience:
singleton SFXAmbience( MyAmbience )
{
   soundTrack = MyAmbientAudio;
};

// 3. Create an SFXSpace in your level and assign "MyAmbience" to its "soundAmbience" property.  You can create an SFXSpace from the library tab or by converting a ConvexShape to it (right-click it in the scene tree).

Quote:- play a piece of background music

// 1. Set up the music track.
singleton SFXProfile( MyMusic )
{
   fileName = "art/sound/myMusic";
   description = AudioMusicLoop2D;
};

// 2. Set up an ambience.
singleton SFXAmbience( MyBackgroundMusic )
{
   soundTrack = MyMusic;
};

// 3. Assign "MyBackgroundMusic" to the "soundAmbience" property of the LevelInfo object in your level.
#3
07/12/2011 (11:40 am)
That's great Rene! Thank you very much for those examples. Thanks also to you Steve for those extra links.

Just one follow-up question though, if that's ok: Rene, you say "Create an SFXSpace in your level and assign "MyAmbience" to its "soundAmbience" property." - is it possible to create an SFXSpace from script? I'm trying to do everything from script if possible so that it can be dynamic at run-time.
#4
07/12/2011 (11:45 am)
Just create an SFXSpace once in the editor, save your mission, then look at the mission file. You can easily copy out the SFXSpace block from there and put it in a script instead, just remove it from your mission file once you've got a copy of the code block.

You can pretty much create anything on-the-fly in script that you can put in a mission file, at least in my experience. We have tons of stuff loading in script being added to the mission as it runs, sometimes our mission file in fact is just the sun and sky and some spawn points, everything else is called in sub-scripts.
#5
07/12/2011 (2:05 pm)
Where is the SFXSpace in the editor? I couldn't find it anywhere. Is this where I should have pointed out earlier that I'm still on 1.0.1? :) (In the process of merging code up to 1.1.)
#6
07/12/2011 (2:18 pm)
Ah, yeah, SFXSpace is new in 1.1. In 1.0.1 you can use Zone, though that comes with implications for visibility and that stuff is quite broken in 1.0.1.
#7
07/12/2011 (2:26 pm)
Ok, no problem, my fault for not mentioning it earlier - though all this will still be helpful once we upgrade. I'll see how far I can get with Zones. Thanks again!

Oh, sorry, one more thing: what is meant by this: "Another alternative is to use an SFXEmitter in the world if only one such sound can be audible at any one time." Where does this 'only one' limit come from? Can't I just place multiple SFXEmitters?
#8
07/12/2011 (3:45 pm)
It's an 'if.' If for some reason you want to limit a particular sound to a single instance in a level....

Also - the weapon scripts include 3D sound emitter code that you can use as an example as well as all of the great info already presented.
#9
07/12/2011 (5:28 pm)
With the weapon scripts, I couldn't see any real sound code in there but they seem to delegate to play3D on the GameConnection object - is this basically a way of calling sfxPlayOnce on clients that are near enough to hear?
#10
07/13/2011 (6:25 am)
Cuts down on wasted bandwidth and processor cycles overall I'm guessing. For frogs by a pond, perhaps have a trigger that starts your sound for each client as they enter and stops it as they exit? The beautiful thing is, it still works in 'single player.'
#11
07/20/2011 (8:37 am)
We've been testing the SFXEmitters for ambient sounds by placing them in the editor and have had a couple of problems, which maybe someone might be able to help with.

- Firstly, the maxDistance property doesn't seem to always work - sometimes you can increase that to a large value but you still have to get close before you can hear it, but the linear fall-off still seems to work ok. It's as if the maxDistance has been clamped to some value - is there a hidden setting somewhere? If it's any help, it doesn't appear to change size in the editor either (if the pink sphere is anything to go by).

- Secondly, even with "Is 3D sound" unchecked, the sound still seems to be limited to a certain radius. How do we achieve something like global ambience or background music with an SFXEmitter? Is there a way to turn off the fall-off entirely?

(Also, changing some of the distance values in the envAudioProfiles.cs sometimes seems to make Torque crash on startup.)