Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between SoundEffect and SoundEffectInstance classes

Tags:

audio

xna

Like in question, i don't get what is the difference between using those classes. What are advantages and limitations of each class. I read many tutorials, and still can't decide what should i do.

To make things more clear, i got AudioModule, AudioEmitterComponent and AudioListenerComponent. (Our game's eninge is based on component. When you create and object which is source of some sounds (monster or sth), i add emitter component.. and i got problems then. I can't figure out how to do it best. First i wanted Audio Module to got List, the same for listeners (and also lists for wave and soundbanks). Now i got problem, not in coding itself (at the moment) but with the concept what, where, when, why. Any ideas are valuable :)

like image 539
Skotnik Avatar asked Mar 31 '11 11:03

Skotnik


1 Answers

Think of SoundEffect as being the sound file. You can load these using content manager.

And SoundEffectInstance as a program playing the sound file.

SoundEffect has a Play method. This is just for convenience. What this does internally is create a SoundEffectInstance, play it, and then clean up when it finishes. (Known as "fire and forget".)

When using SoundEffectInstance (that you create with SoundEffect.CreateInstance) you need to manage all of that yourself - the advantage is that you can manipulate the properties of the sound (volume, etc) while the sound is playing. You can also loop the sound, stop or pause part way through, etc.

When you finish with your SoundEffectInstance, don't forget to Dispose() of it!

Another thing you can do with SoundEffectInstance (which is entirely optional) is apply a 3D effect to it. This gives you an easy way to "place" the sound in 3D space. You apply the 3D effect using SoundEffectInstance.Apply3D (giving it a listener and an emitter that provide information about the camera and the sound source). The exact details of this are best left to the "Applying a 3D Positional Effect to a Sound" article on MSDN.

like image 165
Andrew Russell Avatar answered Oct 22 '22 18:10

Andrew Russell