Unigine.SoundSource Class
Inherits from: | Node |
This class is used to create directional sound sources. The source's sound fades out linearly in a specified range (see getMinDistance() and getMaxDistance()). If inner and outer sound cones are defined, they will also have their share in the attenuation factor (see the getConeInnerAngle() and getConeOuterAngle() functions).
Sound sources are automatically handled by the sound manager. When the sound source is within the audible range, it is loaded into the memory (a full sample or its chunk, depending on whether the source is static or streamed). When it gets outside the audible range, the sound file is automatically deleted from the memory. Static sound sources are instanced; streamed ones are not.
If you still want to manage a sound source manually, you can check if it has stopped
playing and after that delete it.
There is also no limitation on the number of sound sources in the world, as only currently audible ones are rendered. In the worst case scenario, when the number of simultaneously heard sound sources exceeds the hardware capabilities, some of them will not be played.
Creating a Sound Source#
To create a sound source, create an instance of the SoundSource class and specify all required settings:
// create a new sound source using the given sound sample file
SoundSource sound = new SoundSource("sound.mp3");
// disable sound muffling when being occluded
sound.Occlusion = 0;
// set the distance at which the sound gets clear
sound.MinDistance = 10.0f;
// set the distance at which the sound becomes out of audible range
sound.MaxDistance = 100.0f;
// set the gain that result in attenuation of 6 dB
sound.Gain = 0.5f;
// loop the sound
sound.Loop = 1;
// start playing the sound sample
sound.Play();
Updating an Existing Sound Source#
To update the sound source settings, you can call the corresponding methods:
// change the sample file of the playing sound source
if ((sound.IsPlaying) && (Input.IsKeyDown(Input.KEY.C)))
{
// specify a new sample file
sound.SampleName = "sound_1.mp3";
// reduce the gain
sound.Gain = 0.2f;
}
As sound has its own thread that updates at 30 FPS, changes won't be applied immediately. However, you can force updating by using the renderWorld() method.
Sound events like play() or stop() aren't updated immediately as well. So, when you need to perform operations that require stopping of the playback (for example, updating the time, from which the sample should be played), you need to force update the sound thread after stopping the playback:
// check if the sound sample is playing
if (sound.IsPlaying)
{
// stop playing the sample
sound.Stop();
// force updating of the sound thread
Sound.RenderWorld(1);
// update time
sound.Time = 0.0f;
// play the sample
sound.Play();
}
See Also
- Playing Sounds on Collisions article to learn how to play sound sources on collisions with physical bodies
- Controlling Sound Sources Globally article to learn how to toggle all sounds at the same time
-
A set of UnigineScript API samples located in the <UnigineSDK>/data/samples/sounds/ folder:
- sound_static_00
- sound_static_01
- sound_static_02
- sound_static_03
- sound_static_04
- sound_stream_00
- Sounds sample in C# Component Samples suite
SoundSource Class
Properties
bool IsStopped#
bool IsPlaying#
float RoomRolloff#
float Adaptation#
int Occlusion#
int OcclusionMask#
float MinDistance#
float MaxDistance#
float ConeOuterGain#
float ConeOuterGainHF#
float ConeOuterAngle#
float ConeInnerAngle#
float AirAbsorption#
float Time#
int ReverbMask#
int SourceMask#
string SampleName#
float Pitch#
bool Stream#
bool RestartOnEnable#
bool PlayOnEnable#
int Loop#
float Length#
float Gain#
Members
SoundSource ( string name, int stream = 0 ) #
Constructor. Creates a new world sound source using a given sound sample file.Arguments
- string name - Path to the sound sample file.
- int stream - Positive value to create a streaming source, 0 to create a static source. If the flag is set, the sample will not be fully loaded into memory. Instead, its successive parts will be read one by one into a memory buffer.