This page has been translated automatically.
Video Tutorials
Interface
Essentials
Advanced
How To
UnigineEditor
Interface Overview
Assets Workflow
Settings and Preferences
Working With Projects
Adjusting Node Parameters
Setting Up Materials
Setting Up Properties
Lighting
Landscape Tool
Sandworm
Using Editor Tools for Specific Tasks
Extending Editor Functionality
Built-in Node Types
Nodes
Objects
Effects
Decals
Light Sources
Geodetics
World Nodes
Sound Objects
Pathfinding Objects
Players
Programming
Fundamentals
Setting Up Development Environment
Usage Examples
C++
C#
UnigineScript
UUSL (Unified UNIGINE Shader Language)
Plugins
File Formats
Rebuilding the Engine Tools
GUI
Double Precision Coordinates
API
Containers
Common Functionality
Controls-Related Classes
Engine-Related Classes
Filesystem Functionality
GUI-Related Classes
Math Functionality
Node-Related Classes
Objects-Related Classes
Networking Functionality
Pathfinding-Related Classes
Physics-Related Classes
Plugins-Related Classes
IG Plugin
CIGIConnector Plugin
Rendering-Related Classes
Content Creation
Content Optimization
Materials
Material Nodes Library
Miscellaneous
Input
Math
Matrix
Textures
Art Samples
Tutorials
Warning! This version of documentation is OUTDATED, as it describes an older SDK version! Please switch to the documentation for the latest SDK version.
Warning! This version of documentation describes an old SDK version which is no longer supported! Please upgrade to the latest SDK version.

AmbientSource Class

Warning
The scope of applications for UnigineScript is limited to implementing materials-related logic (material expressions, scriptable materials, brush materials). Do not use UnigineScript as a language for application logic, please consider C#/C++ instead, as these APIs are the preferred ones. Availability of new Engine features in UnigineScript (beyond its scope of applications) is not guaranteed, as the current level of support assumes only fixing critical issues.

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:

Notice
For an ambient source to be played, a player is always required. In case an ambient source needs to be played when neither a world, nor the editor are loaded, a player, as well as the sound source (see code below), should be created in the system script unigine.usc; otherwise, no sound will be heard.
Source code (UnigineScript)
// create a player so that an ambient sound source is played
PlayerSpectator player = new PlayerSpectator();
player.setPosition(Vec3(0.0f, -3.401f, 1.5f));
player.setViewDirection(vec3(0.0f, 1.0f, -0.4f));
engine.game.setPlayer(player);

// create the ambient sound source
AmbientSource sound = new AmbientSource("sound.mp3");
// set necessary sound settings
sound.setGain(0.5f);
sound.setPitch(1.0f);
sound.setLoop(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:

Source code (UnigineScript)
// change the sample file of the playing sound source
if ((sound.isPlaying()) && (engine.app.clearKeyState('c')))
{
	// increase the pitch
	sound.setPitch(2.0f);
	// reduce the gain
	sound.setGain(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:

Source code (UnigineScript)
// check if the sound sample is playing
if (sound.isPlaying())
{
	// stop playing the sample
	sound.stop();
	// force updating of the sound thread
	engine.sound.renderWorld(1);
	// update time
	sound.setTime(0.0f);
	// play the sample 
	sound.play();
}

See Also#

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

Members


static 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.

void setGain ( float gain ) #

Sets volume of the sound.

Arguments

  • float gain - Volume. 0 means muted, 1 means maximum volume.

float getGain ( ) #

Returns volume of the sound.

Return value

Volume. 0 means muted, 1 means maximum volume.

float getLength ( ) #

Returns the total length of the sound sample.

Return value

Length of the sample in seconds.

void setLoop ( int loop ) #

Sets a value indicating if the sample should be looped.

Arguments

  • int loop - Positive number to loop the sample, 0 to play it only once.

int getLoop ( ) #

Returns a value indicating if the sample is looped.

Return value

Positive number if the sample is looped; otherwise, 0.

void setPitch ( float pitch ) #

Sets a sound pitch.

Arguments

  • float pitch - Factor, by which the current pitch will be multiplied.

float getPitch ( ) #

Returns a sound pitch.

Return value

Factor, by which the current pitch will be multiplied.

int isPlaying ( ) #

Returns a value indicating if the sample is being played.

Return value

1 if the sample is being played; otherwise, 0.

void setSampleName ( string name ) #

Sets a new sound file for the ambient sound.
Notice
To change the sound file of the currently played ambient sound, first, you should stop the playback, set the sample name, and then resume the playback.
Source code (UnigineScript)
if (sound.isPlaying())
{
	// stop the playing sound
	sound.stop();
	// force updating of the sound thread
	engine.sound.renderWorld(1);
	// set a new sample file to be played
	sound.setSampleName("stream_stereo_01.oga");
	// play the sound
	sound.play();
}

Arguments

  • string name - Path to the sound file.

string getSampleName ( ) #

Returns the name of the sound file.

Return value

Path to the sound file.

void setSourceMask ( int mask ) #

Updates a bit mask that determines to what sound channels the source belongs to. For a sound source to be heard, its mask should match with the player's sound mask in at least one bit.

Arguments

  • int mask - Integer, each bit of which specifies a sound channel.

int getSourceMask ( ) #

Returns a bit mask that determines to what sound channels the source belongs to. For a sound source to be heard, its mask should match at least with the player's sound mask in at least one bit.

Return value

Integer, each bit of which specifies a sound channel.

int isStopped ( ) #

Returns a value indicating if playback is stopped.

Return value

1 if the sample is stopped; otherwise, 0.

void setTime ( float time ) #

Sets time, from which the sample should be played.
Notice
This function is ineffective if the sample is already playing. At first it is necessary to stop the playback, set the time, and then resume the playback.

Arguments

  • float time - Time in seconds.

float getTime ( ) #

Returns the current time, at which the sample is being played.

Return value

Time in seconds.

void play ( ) #

Starts playing the sample.

void stop ( ) #

Stops playback. This function saves the playback position so that playing of the file can be resumed from the same point.
Notice
The playback won't stop immediately, as the sound thread is updated at 30 FPS. 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.
Last update: 2021-12-13
Build: ()