The audio system also has the ability to alter the minimum and maximum states of audio volume, as well as fading the audio when needed.
The volume of the audio can be set with values ranging from 0 to 1.0. This setting can be adjusted at any time before or during the extended sound playback.
The audio.setVolume
function sets the volume.
The syntax is audio.setVolume( volume [, [options] ] ) --upon success, should return true
.
The parameters are as follows:
volume
: This lets you set the volume level you want to apply. Valid numbers range from 0.0 to 1.0, where 1.0 is the maximum volume value. The default volume is based on your device ringer volume and will vary.options
: This is a table that supports the channel number you want to set the volume on. You can set the volume on any channel between 1 to 32. Specify 0 to apply the volume to all the channels. Omitting this parameter entirely sets the master volume, which is different from the channel volume.For example:
audio.setVolume( 0.75 ) -- set master volume
audio.setVolume( 0.5, { channel=2 } ) -- set volume on channel scaled to the volume on the master channel
The audio.setMinVolume()
function clamps the minimum volume to the set value. Any volumes that go below the minimum volume will be played at the minimum volume level.
The syntax is audio.setMinVolume( volume, options )
.
The parameters are as follows:
volume
: This lets you set the new minimum volume level you want to apply. Valid numbers range from 0.0 to 1.0, where 1.0 is the maximum volume value.options
: This is a table that supports a single key channel number you want to set the minimum volume on. 1 to the minimum number of channels are valid channels. Specify 0 to apply the minimum volume to all the channels.The example is mentioned as follows:
audio.setMinVolume( 0.10, { channel=1 } ) -- set the min volume on channel 1
The audio.setMaxVolume()
function clamps the maximum volume to the set value. Any volumes that exceed the maximum volume will be played at the maximum volume level.
The syntax is audio.setMaxVolume( volume, options )
.
The are parameters are as follows:
volume
: This lets you set the new maximum volume level you want to apply. Valid numbers range from 0.0 to 1.0, where 1.0 is the maximum value.options
: This is a table that supports a single key channel number you want to set the maximum volume on. 1 to the maximum number of channels are valid channels. Specify 0 to apply the maximum volume to all the channels.The example is mentioned as follows:
audio.setMaxVolume( 0.9, { channel=1 } ) -- set the max volume on channel 1
The audio.getVolume()
function gets the volume either for a specific channel or the master volume.
The syntax is audio.getVolume( { channel=c } )
.
Some example are mentioned as follows:
masterVolume = audio.getVolume() -- get the master volume
channel1Volume = audio.getVolume( { channel=1 } ) -- get the volume on channel 1
The audio.getMinVolume()
function gets the minimum volume for a specific channel.
The syntax is audio.getMinVolume( { channel=c } )
.
The parameter is as follows:
The example is mentioned as follows:
channel1MinVolume = audio.getMinVolume( { channel=1 } ) -- get the min volume on channel 1
The audio.getMaxVolume()
function gets the maximum volume for a specific channel.
The syntax is audio.getMaxVolume( { channel=c } )
.
channel
: This sets the channel number you want to get the maximum volume on. There can be a maximum number of 32 channels that are valid. Specifying 0 will return the average volume across all channels.The example is mentioned as follows:
channel1MaxVolume = audio.getMaxVolume( { channel=1 } ) -- get the max volume on channel 1
You can fade in the volume at the time any audio starts playing, but there are other ways to control it as well.
The audio.fade()
function fades a playing sound in a specified amount to a specified volume. The audio will continue playing after the fade completes.
The syntax is audio.fade( [ { [channel=c] [, time=t] [, volume=v ] } ] )
.
The parameters are as follows:
channel
: This sets the channel number you want to fade on. 1 to the maximum number of channels are valid channels. Specify 0 to apply fade to all the channels.time
: This sets the amount of time from now that you want the audio to fade out and stop. Omitting this parameter invokes a default fade time, which is 1,000 milliseconds.volume
: This sets the target volume you want to change the fade to. Valid numbers are 0.0 to 1.0, where 1.0 is the maximum volume. If this parameter is omitted, the default value is 0.0.See the following example:
audio.fade({ channel=1, time=3000, volume=0.5 } )
The audio.fadeOut()
function stops playing the sound in a specified amount of time and fades to the minimum volume. The audio will stop at the end of the time and the channel will be freed.
The syntax is audio.fadeOut( [ { [channel=c] [, time=t] } ] )
.
The parameters are as follows:
channel
: This sets the channel number you want to fade out on. 1 to the maximum number of channels are valid channels. Specify 0 to apply fade out to all the channels.time
: This sets the amount of time from now that you want the audio to fade out over and stop. Omitting this parameter invokes a default fade out time, which is 1,000 milliseconds.The example is mentioned as follows:
audio.fadeOut({ channel=1, time=5000 } )