Unigine.AmbientSource Class
This class is used to create a non-directional ambient background sound.
Creating an Ambient Sound Source#
To create an ambient sound source, create an instance of the AmbientSource class and specify all required settings:
// create a player so that an ambient sound source is played
PlayerSpectator player = new PlayerSpectator();
player.Position = new Vec3(0.0f, -3.401f, 1.5f);
player.ViewDirection = new vec3(0.0f, 1.0f, -0.4f);
Game.Player = player;
// create the ambient sound source
AmbientSource sound = new AmbientSource("sound.mp3");
// set necessary sound settings
sound.Gain = 0.5f;
sound.Pitch = 1.0f;
sound.Loop = 1;
sound.Play();
Updating an Existing Ambient Sound Source#
To update the settings of the ambient sound source, you can simply call the corresponding methods:
// change the sample file of the playing sound source
if ((sound.IsPlaying) && (Input.IsKeyDown(Input.KEY.C)))
{
// increase the pitch
sound.Pitch = 2.0f;
// 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, or the sample name), 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#
- 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:
- ambient_static_00
- ambient_static_01
- ambient_stream_00
- ambient_stream_01
AmbientSource Class
Properties
bool IsStopped#
bool IsPlaying#
float Time#
int SourceMask#
string SampleName#
if (sound.IsPlaying)
{
// stop the playing sound
sound.Stop();
// force updating of the sound thread
Sound.RenderWorld(1);
// set a new sample file to be played
sound.SampleName = "stream_stereo_01.oga";
// play the sound
sound.Play();
}
float Pitch#
int Loop#
float Length#
float Gain#
Members
AmbientSource ( string name, int stream = 0 ) #
Constructor. Creates a new ambient sound source using a given sound file.Arguments
- string name - Path to the sound 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.