Skip to main content

Audio

Resources

As briefly mentioned in the earlier section audio resources can be loaded using File.LoadAudio, the file must be a wav file in PCM format. The result returned is a unique identifier into the audio bank which will be used with other primitives.

-- Loads an audio file and returns the audio bank id.
int File.LoadAudio(string path, int? level)

The file details can be queried using the function Audio.Query by its identifier, which will return information about the file details as well as its duration.

table AudioDetails {
uint numChannels -- Number of channels in the file
uint numSamples -- Number of samples in the file
uint bitsPerSample -- Bits per sample instance
uint samplesPerSec -- Samples per second.
float duration -- Duration of the audio track in milliseconds
}
-- Given a audio bank identifier, returns the details of the wave file.
AudioDetails Audio.Query(int id)

Playback

Audio can be played using the API Audio.Play with an optional configuration specifying details about the playback. This function returns a unique playback identifier which identifies each individual playback request.

table PlaybackConfig {
float? startTime -- Timestamp to starts the audio track at (ms), defaults to 0.
float? duration -- Duration to play the audio for (ms), defaults to maximum.
float? playRate -- Speed of the playback, defaults to 1.
float? volume -- Volume of the playback from 0 to 1, defaults to 1.
float? leftVolume --
float? rightVolume --
uint? loopCount -- Number of loops to play, defaults to 1.
}
-- Given an audio bank identifier and optionally the audio player configuration, starts playing it, returns the unique playback identifier.
int Audio.Play(int id, PlaybackConfig? cfg)

An ongoing playback can be halted by calling Audio.Stop with its playback identifier and the state of the playback can be queried using Audio.IsPlaying.

-- Stops an audio playback given its identifier.
void Audio.Stop(int uid)

-- Stops all audio playback.
void Audio.Halt()

-- Returns true if the given audio playback is on going.
bool Audio.IsPlaying(int uid)