# Audio Manager

It's ready for one shot sound Effects, Music theme switcher.

## How to use

add **`using Game.Audio;`**

In **Assets\_Main\Prefabs\Common\Audio** you can find SoundPool and SoundNoPool prefabs.\
In \_Loader scene by default 5 SoundPool object will be instantiated in PoolSystem for future usage.

Select AudioClip files in project and right-click -> Create -> Sound Audio Clip Selected. It will create sound configs. See image below:

<figure><img src="https://1613353771-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fygjewtla1W7kzTTIpGWq%2Fuploads%2FhrUSK34nPHeSfMerUDT3%2Fimage.png?alt=media&#x26;token=61201976-6bef-4393-beae-ced16f08d42a" alt=""><figcaption><p>Example how to create SoundAudioClip form AudioClip assets</p></figcaption></figure>

And result:

<figure><img src="https://1613353771-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fygjewtla1W7kzTTIpGWq%2Fuploads%2FZ49qpmVWOdRhIczcQP6L%2Fimage.png?alt=media&#x26;token=a31e16f3-f0b2-442a-bc90-28a686685914" alt=""><figcaption><p>Created SoundAudioClips result</p></figcaption></figure>

You should name your audio file with end string like "\_*sfx"* (for sound effects) *or "\_music"* or *"\_m"* (for music). So configs will be automaticaly linked to correspond mixer group. If there is no end strings found then config will be in Sounds (SFX) mixer group.

Also you can create empty sound audio clip scriptable object by create asset menu: Create -> Game -> Audio -> Sound Audio Clip.

#### Prepare your SoundAudioClip in inspector

You need to set bool for 2D sound. If it's false -> it will be 3D.\
AudioClip -> what sound to play.\
MixerGroup -> on what channel we should play AudioClip (by default in Mixer we have Music and Sound groups)\
Volume -> if you sound too loud, then change value less then 1.

<figure><img src="https://1613353771-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fygjewtla1W7kzTTIpGWq%2Fuploads%2FTNiphJVSdnpxxHB2rdc8%2Fimage.png?alt=media&#x26;token=1cd7d067-d901-4c00-9e9f-d31c32820494" alt=""><figcaption><p>How SoundAudioClip looks in inspector</p></figcaption></figure>

**Using sounds**

Reference Sound Audio Clip scriptable object in your controller scripts. See below is example:

```csharp
// Example how to call sound play
using Game.Audio;

public class Weapon : Monobehaviour {
    // used to spawn shoot obj, we can use it for 3d sound too;
    [SerializedField] private Transform _shootPoint;

    // declare sound clip info, note that this is Scriptable object,
    // you can create instance of it by Create/Game/Audio/Sound Audio Clip;
    [SerializedField] private SoundAudioClip _shootSound;
    
    public void Shoot() {
        // some logic
        
        // if 2D sound                                 // here is pitch random
        AudioManager.Instance.PlaySingleShot(_shootSound, Random.Range(0.9f, 1.1f));
        // if you want callback on sound end
        AudioManager.Instance.PlaySingleShot(_shootSound,
                                             onPlayed: () => print("soundEnd"),
                                             Random.Range(0.9f, 1.1f));
        
        
        
        
        // if 3D sound (no need to call this for 2D, it's empty overhead)
        AudioManager.Instance.PlaySingleShotAt(_shootSound, 
                                               _shootPoint.position,
                                               Random.Range(0.9f, 1.1f));
        // with on sound end callback
        AudioManager.Instance.PlaySingleShotAt(_shootSound, 
                                               _shootPoint.position,
                                               onPlayed: () => print("soundEnd"),
                                               Random.Range(0.9f, 1.1f));
    }
}
```

You can also use sounds not from prefab using SoundNoPool prefab.\
**Replace it in scene or other prefab** and use **PlaySingleShot(Sound soundObj)** method in AudioManager. **You need to reference SoundNoPool** obj from scene in your script where you want to call AudioManager. If you use this method (sound will not overlap on itself if you try to play it again before old one finishes)\
\
but the **easiest way** is like script example above (also sounds will overlap each other with pool system in use, so sounds will not cut if you try to use the same sound again).

### Other scripts

#### RepeatSound2D

You can use RepeatSound2D as field in controller scripts. Use it's Play() and Stop() methods. Usefull for sounds that needs to continiously play while player touches screen or similar cases.

#### CompoundSound

You can use CompoundSound as field in controller scripts. It contains several sounds that will be played on Play() call and you can stop by Stop(). Usefull if you want to play multiple sounds with delay and random pitch for each.

More examples in project in **Assets\\\_Examples\Audio System\\**
