Quick know-how > Adding sounds to your game

Starting from the version 0.0.47, Cubzh has implemented a sound system to help you improve the ambience of your games. You can now pick from a library of built-in musics and sound effects to bring your worlds to life!

In a nutshell, the sound system works as follows:

- First, you have to place a listener somewhere in your scene: this is basically a microphone-like object that captures the sounds from all the sources around it and outputs the result to your speakers. It is unique and readily available using the AudioListener variable.
- Then, to add a new sound source in the game, you have to create a new instance of the AudioSource class, assign it a reference to a sound file (among the list of built-in ones, see below) and parent it somewhere in the world's hierarchy.

All this can be done via a code snippet like this one:

Client.OnStart = function()
    -- add the AudioListener to the player's head
    -- to "glue the microphone" to this object
    Player.Head:AddChild(AudioListener)

    -- create a new source
    as = AudioSource()
    as.Sound = "death_scream_guy_1"
    as:SetParent(Player.Head)
end

You can create as many sources as you want, keeping in mind that one source can only play one sound at a time. And since both the AudioListener and AudioSource classes inherit from the Object base, you can set their transform and parent-dependencies like you're used to :)

Once initialized like this, your AudioSource instance can be turned on by calling its Play method, and off by calling its Stop method:

gameOver = function()
    as:Play()

    Timer(2, false, function()
        as:Stop()
    end)
end

The whole trick is then to properly choose your sounds and musics, and to tweak their parameters to better integrate them to the context! Let's see some additional tricks!

Changing the volume of your AudioSource

The most commonly used option is the Volume. This value goes from 0 (full silence) to 1 (full volume) and makes it easy to level the various sources according to one another.

as = AudioSource()
as.Sound = "gun_shot_1"
as:SetParent(gun)

as.Volume = 0.3

Taking an excerpt of the sound

Our most creative contributors have already managed to go above and beyond and make their own sounds by playing around with the existing ones and chopping them up in clever ways! If you too want to try and remix the raw material, have some fun with the StartAt and StopAt properties of your AudioSource (both expressed in milliseconds):

as = AudioSource()
as.Sound = "waterdrop_1"
as:SetParent(gun)

as.StartAt = 200
as.StopAt = 1000

Playing around with the pitch

To expand the audio library, you can also tweak the pitch of the sound file, i.e. how "high" or "low" it is perceived. This property works like this:

- 1.0 keeps the normal frequency, meaning the sound is as-is
- 0.5 halves the frequency, so the audio will sound lower
- 2.0 doubles the frequency, so the audio will sound higher

The pitch cannot be 0, but it can technically go up to +infinity. To change it, just set the Pitch option of the source:

as = AudioSource()
as.Sound = "waterdrop_1"
as:SetParent(gun)

as.Pitch = 0.5 -- lower the sound

Tweaking or disabling spatialization

Another interesting features of the sounds in Cubzh is that, by default, they are spatialized. This means that you won't hear your AudioSource the same depending on where the listener is. For example, the sound will be louder when the listener is closer to it, and you will get a left/right pan effect as the angle between the source and the listener changes. This is a great way to make really believable positional sound source in your world in a flash!

But of course, sometimes, you might want one of your AudioSource instances to be just some ambient sound, or a background music. In that case, the sound should not be spatialized, because it is independent of the position of the listener. This is easy to setup: just turn the Spatialized property off like this:

as = AudioSource()
as.Sound = "gun_shot_1"
as:SetParent(gun)

as.Spatialized = False

If you want to keep the spatialization but customize it in more details, you can force its pan to be more in the left or the right ear using the Pan setting.

The Pan value goes from -1 (left ear only) to 1 (right ear only), with a default of 0 (equal in both ears):

as = AudioSource()
as.Sound = "gun_shot_1"
as:SetParent(gun)

as.Pan = -0.5 -- "move the sound" slightly on the left

List of available sounds

Here is a list of all the sounds currently available in Cubzh (they should be pretty self-explanatory, but don't hesitate to try out some "surprising" effects to give a unique style to your world!):

440hz
automaticdoor_1
big_explosion_1
big_explosion_2
big_explosion_3
birdstweeting_1
bookpageturning_1
broken_glass_1
broken_glass_2
broken_glass_3
broken_glass_4
broken_glass_5
button_1
button_2
button_3
button_4
button_5
buttonnegative_1
buttonnegative_2
buttonnegative_3
buttonpositive_1
buttonpositive_2
buttonpositive_3
carengine_1
carengine_2
carengine_3
carhonk_1
carstarting_1
catmeow_1
catmeow_2
catmeow_3
coin_1
cow_1
cow_2
cow_3
crowd_1
crowd_2
crowdapplause_1
crowdbooing_1
crowdlaughing_1
death_scream_guy_1
death_scream_guy_2
death_scream_guy_3
death_scream_guy_4
death_scream_guy_5
death_scream_lady_1
death_scream_lady_2
death_scream_lady_3
death_scream_lady_4
death_scream_lady_5
deathscream_1
deathscream_2
deathscream_3
dogbark_1
dogbark_2
dogbark_3
doorbell_1
doorbell_2
doorclose_1
doorclose_2
doorclose_3
dooropen_1
dooropen_2
dooropen_3
drinking_1
drinking_2
drinking_3
drumkick_1
drumsnare_1
drumtom_1
eating_1
eating_2
eating_3
eating_4
eating_5
fire_1
fire_2
fire_3
firecamp_1
fireworks_fireworks_child_1
fireworks_fireworks_child_2
fireworks_fireworks_child_3
fryingpane_1
ground_digging_1
ground_digging_2
ground_digging_3
gun_reload_1
gun_reload_2
gun_reload_3
gun_shot_1
gun_shot_2
gunshot_1
gunshot_2
gunshot_3
gunshot_4
gunshot_5
hitmarker_1
hitmarker_2
hitmarker_3
hitmarker_4
hitmarker_5
hurt_scream_female_1
hurt_scream_female_2
hurt_scream_female_3
hurt_scream_female_4
hurt_scream_female_5
hurt_scream_male_1
hurt_scream_male_2
hurt_scream_male_3
hurt_scream_male_4
hurt_scream_male_5
hurtscream_1
hurtscream_2
hurtscream_3
keydown_1
keydown_2
keydown_3
keydown_4
keydown_5
keydown_6
keydown_7
laser_gun_shot_1
metal_clanging_1
metal_clanging_2
metal_clanging_3
metal_clanging_4
metal_clanging_5
metal_clanging_6
metal_clanging_7
metal_clanging_8
modal_1
modal_2
modal_3
owl_1
owl_2
owl_3
pencilwriting_1
pencilwriting_2
pencilwriting_3
phonecall_1
phonecall_2
phonecall_3
piano_attack
piano_sustain
predatorgrowl_1
predatorgrowl_2
predatorgrowl_3
punch_1
punch_2
rain_1
rain_2
rain_3
river_1
rocketexhaust_1
small_explosion_1
small_explosion_2
small_explosion_3
sword_impact_1
sword_impact_10
sword_impact_11
sword_impact_12
sword_impact_2
sword_impact_3
sword_impact_4
sword_impact_5
sword_impact_6
sword_impact_7
sword_impact_8
sword_impact_9
thunder_1
thunder_2
thunder_3
twang_1
twang_2
twang_3
twang_4
underwaterbubbles_1
underwaterbubbles_amb_1
victory_1
walk_concrete_1
walk_concrete_2
walk_concrete_3
walk_concrete_4
walk_concrete_5
walk_grass_1
walk_grass_2
walk_grass_3
walk_grass_4
walk_grass_5
walk_gravel_1
walk_gravel_2
walk_gravel_3
walk_gravel_4
walk_gravel_5
walk_sand_1
walk_sand_2
walk_sand_3
walk_sand_4
walk_sand_5
walk_snow_1
walk_snow_2
walk_snow_3
walk_snow_4
walk_snow_5
walk_wood_1
walk_wood_2
walk_wood_3
walk_wood_4
walk_wood_5
walksnow_1
walksnow_2
walksnow_3
walksnow_4
water_impact_1
water_impact_2
water_impact_3
waterdrop_1
waterdrop_2
waterdrop_3
waterdrop_4
whooshes_medium_1
whooshes_medium_2
whooshes_medium_3
whooshes_small_1
whooshes_small_2
whooshes_small_3
whooshes_small_4
wind_wind_child_1
wood_impact_1
wood_impact_2
wood_impact_3
wood_impact_4
wood_impact_5
zombiegrowl_1
zombiegrowl_2
zombiegrowl_3