We are able to set the volume and pitch of our Audio objects as they are playing by calling the SetVolume and SetPitch actions. These actions both take number parameters that represent the percent change in the pitch. For example, a number of 0.5 passed to SetVolume will set the new volume at 50% of the maximum volume. A number of 1.2 passed to SetPitch will raise the pitch 20% above the normal pitch. Examples of these actions follow:
use Libraries.Sound.Audio
Audio audio
audio:Load("Sounds/Clang.ogg")
// this line sets the volume of our Audio object at 50% its maximum volume.
audio:SetVolume(0.5)
// this line sets the pitch of our Audio object at 120% the normal pitch
audio:SetPitch(1.2)
audio:PlayUntilDone()
If we want to know what the current settings of the volume and pitch of our Audio objects are, we can use the GetVolume and GetPitch actions, respectively. These actions return a number that represents the value of the volume or pitch and can be used with their corresponding Set actions to change the values of the volume and/or pitch over time. An example of this follows:
use Libraries.Sound.Audio
Audio audio
audio:Load("Sounds/song.ogg")
audio:Play()
// this loop keeps our sound playing until it is done
repeat while audio:IsPlaying()
// this conditional checks if the current volume is greater than 0. If it is, it decreases the volume
if audio:GetVolume() > 0.0
// this line calculates the new volume by getting the current volume and subtracting 0.00001% (one hundred-thousandth of a percent)
number newVolume = audio:GetVolume() - 0.0000001
// this line sets the volumen to newVolume, which will have the effect of fading the sound out quickly.
audio:SetVolume(newVolume)
end
end
Call the SetVolume and SetPitch actions to set the volume and pitch of our Audio objects.
When using these actions, we should be aware of a few things.
In the next tutorial, we will discuss Changing Where the Sound is Played: Balance, Fade and Rotation, which describes how to play audio files Quorum.