Revision Difference
Creating_Sounds#548335
<cat>Audio.Intro</cat>
<title>Creating Sounds</title>
# Creating a sound asset type
⤶
Following a common file structure, we will create a `.sound` file called `atm_card.insert` inside your addon under `sounds/ambient/atm`. The original sound file (`.wav` or `.mp3`) will be called `atm_card.insert.wav` inside the same folder. A single `.sound` asset can contain multiple different sounds which will be picked from randomly on every play.
⤶
For looping sounds you will have to either place loop markers inside your sound file or compile the sound as a looping sound beforehand. To do this, you left-click once the sound in the assets browser and select the loop checkbox. This will only work with `.wav` and `.mp3` files.
⤶
To create the actual `.sound` file, you can right click on the sound file within the assets browser and select `Create Sound Definition`. You can also do this without having a sound file selected in which case a blank `.sound` file will be created. ⤶
⤶
Inside the inspector you can now select your sound file and set values such as Volume, Volume Random, Pitch, Pitch Random, and Decibels/audible distance. If your sound is supposed to be a screen sound, you will also need to select the `UI` option.
⤶
<upload src="a768a/8da64894a876055.png" size="47365" name="sbox-dev_848466LML6.png" />⤶
⤶
Saving this file via <key>CTRL</key> + <key>S</key> or clicking on Full Recompile will result on a `.sound_c` asset for the metadata and a `.vsnd_c` file for the actual sound file, if you haven't compiled your sound already in a previous step.
<upload src="a768a/8da648a48db7424.png" size="17821" name="sbox-dev_y60q6R9n7y.png" />
<upload src="a768a/8da64897386c742.png" size="9996" name="explorer_4sGDM9lJ65.png" />⤶
⤶
If you want to create a sound, first add a **.wav** or **.mp3** into your game somewhere - this should be typically be the **sounds** folder.
⤶
Right click on your sound and click **Create Sound Event**.
⤶
<upload src="aa125/8dad92d7ce23899.png" size="10888" name="image.png" />⤶
⤶
Name your sound event in the pop-up dialog. The new asset will then be selected automatically for you and appear in the inspector.
⤶
Here you can adjust values like **volume**, **pitch**, and **decibels**. If your sound is supposed to be a screen sound, you will also need to select the **UI** option.⤶
⤶
From here, you can also [upload the sound to asset.party](UploadingAssets).
<upload src="aa125/8dad92f890b4dd9.png" size="26534" name="image.png" />
⤶
Saving this file via <key>CTRL</key> + <key>S</key> or clicking on Full Recompile will compile both the source sound and the sound event.⤶
⤶
<upload src="aa125/8dad92f97e60b07.png" size="11294" name="image.png" />⤶
<upload src="aa125/8dad92dde4082e1.png" size="3896" name="image.png" />⤶
⤶
## Looping Sounds⤶
⤶
For looping sounds you will have to either:⤶
⤶
- Place loop markers inside your sound file⤶
- Left-click the source sound in the assets browser and select the loop checkbox in the inspector. This will only work with **.wav** and **.mp3** files.⤶
⤶
<upload src="aa125/8dad92dc9a457f6.png" size="9502" name="image.png" />⤶
# Playing the Sound from Your Code
To play the sound in-game, you should use one of the static methods from the `Sound` class, using the name of the `.sound` asset type without the extension:
To play the sound in-game, you should use one of the static methods from the [Sound](https://asset.party/api/Sandbox.Sound) class, using the name of the **.sound** asset type without the extension:
```cs
public override void Spawn()
{
// Plays the sound from an entity's position
Sound.FromEntity("atm_card.insert", this);
// Plays the sound from a position in the world
Sound.FromWorld("atm_card.insert", new Vector3(0, 0, 0));
// Plays the sound from a position on your screen, useful for UI interactions
// Make sure your .sound file has "2D" enabled
Sound.FromScreen("atm_card.insert");
}
```
<note>Sounds can be [predicted](Prediction) - if you're playing a sound in a predicted method such as Simulate you should make sure your logic is predictable, if it isn't you should appropriately wrap it with Prediction.Off otherwise you will hear nothing.</note>