Game Development Community

Simple Sound Question

by Jared Hoberock · in Torque Game Engine · 03/20/2002 (2:15 pm) · 10 replies

How would I get a wav file to play when I enter a function? Do I have to do all the datablock crud like in the rifle and etc. scripts? Is there any way I can just call the file directly?

Thanks, D

#1
03/20/2002 (2:34 pm)
When I try this:

alxPlay("~/data/sound/fx/crunch.wav");

I get this error:

Unable to locate audio profile '~/data/sound/fx/crunch.wav'

And, yes, the file is there.

Thanks, D
#2
03/20/2002 (2:35 pm)
You must have an AudioProfile datablock for the sound (most likely defined in the scripting). Then you call alxPlay, using that datablock and a matrix specifying the sound's position/orientation. Look through the existing code for examples of how alxPlay is used.
#3
03/20/2002 (3:01 pm)
I can't seem to find where alxPlay is called in the scripts. Help, please. Thanks
#4
03/20/2002 (3:16 pm)
Try Desmonds tuts on sound ... pretty much explains every detail you have to know...
holodeck.st.usm.edu/vrcomputing/vrc_t/tutorials/
#5
03/20/2002 (3:39 pm)
alxPlay isn't called in the scripts. That's in the engine code.

For playing sounds from scripts, you need to use ServerPlay2D, ServerPlay3D, or call playAudio on a ShapeBase-derived object.
#6
03/21/2002 (12:54 am)
It IS called in the scripts, e.g. in the /example/common/client/scripts/mission.cs, or at least you CAN call it from a script like that:
function clientCmdMissionStart(%seq)
{
   // The client receives a mission start right before being dropped into the game.
   alxPLay("Mission_Music");  //add this line
}
See above mentioned tutorials...
#7
03/21/2002 (4:28 pm)
Oops! :-)

You're right, looks like a lot of the alx functions are exported to the scripting in engine/audio/audioFunctions.cc.

In particular, the scripted form of alxPlay can take an audiohandle or an AudioProfile datablock identifier as its first argument. If you give it a datablock rather than an audiohandle, obviously it doesn't know where to play the sound (if it's a 3D sound), so it plays it at the world origin unless you follow the datablock identifier with x, y, and z arguments specifying the sound origin.
#8
03/28/2002 (9:43 am)
BTW, something that occurred to me as I was talking about this topic with someone else: an important distinction is that alxPlay is to be used in client-side scripting, while the other methods are for server-side scripting.

AFAICT the following is a summary of the ways you can deal with audio.

On the server, three basic options:

1) In the server scripting, call play2D or play3D on a client (GameConnection) object, specifying the AudioProfile and in the latter case the transform for the sound source too. This will play the sound only for that client.

2) Use ServerPlay2D or ServerPlay3D, similar to the above but sends the sound to all clients.

3) Call playAudio on a ShapeBase-derived object, specifying a slot number and an AudioProfile. This is probably the preferred method for sounds that should be "generated" by an image mounted to a slot of some shape (like a weapon).

On the client, one option:

1)Use one of the many al/alx functions exported to the scripting. Here's a cut-and-paste dump of those functions' help texts to give you some idea:
alGetString(enum) 
alxCreateSource(profile, {x,y,z} | description, filename, {x,y,z}) 
alxSourcef(handle, ALenum, value) 
alxSource3f(handle, ALenum, "x y z" | x, y, z) 
alxSourcei(handle, ALenum, value) 
alxGetSourcef(handle, ALenum) 
alxGetSource3f(handle, ALenum) 
alxGetSourcei(handle, ALenum) 
alxPlay(handle) | alxPlay(profile) | alxPlay(profile, x,y,z) 
alxStop(handle) 
alxStopAll() 
alxIsPlaying(handle) 
alxListener(ALenum, value) 
alListener3f(ALenum, "x y z" | x, y, z) 
alxGetListenerf(Alenum) 
alGetListener3f(Alenum) 
alGetListeneri(Alenum) 
alxGetChannelVolume(channel_id) 
alxGetChannelVolume(channel_id, volume 0.0-1.0)
#9
03/29/2002 (6:02 pm)
Had the occasion to investigate playAudio, so here's what I found about that. (This is just from looking at the code... no testing yet, so caveat hax0r.)

Contrary to what I implied above, the "slot" has nothing to do with the mounting nodes of a shape.

Each shape has associated with it four "sound threads". When you call playAudio, specifying a slot and an AudioProfile, the slot specifies which sound thread of that shape to use, and the AudioProfile is of course the AudioProfile for the sound you want to play.

If the thread you specify had started playing the same sound (AudioProfile) less than half a second ago, then the call to playAudio doesn't do anything.

Otherwise, the thread stops any currently playing sound, and then starts playing the specified sound, using the shape's position/orientation for the emitter position/orientation.

The position/orientation of any playing sounds will be updated as the shape moves around.

So, playAudio is nice because it safeguards a little against overlapping sounds, it automatically determines where to play the sound (assuming you don't mind it emanating from the center of the shape), and it takes care of keeping the sound's position up-to-date. In some cases it won't make any difference whether you use that or ServerPlay3D, but in some other cases, particularly for sounds with some duration attached to moving objects, playAudio might be very nice to have.
#10
07/15/2009 (10:05 pm)

You just add the exec("./AudioProfiles.cs");

and then u run .. it will execute correctly...


exec("./AudioProfiles.cs");
$AudioTestHandleA = alxCreateSource("AudioIntroMusicProfile",expandFilename("data/sound/crowd.wav"));

alxPlay($AudioTestHandleA);