# Audio

The **Audio** system provides a centralized way to manage music, sound effects (SFX), and UI sounds.

## Components

### [AudioManager](https://github.com/MeshMap/com.meshmap.sdk.bb/blob/main/Runtime/Audio/AudioManager.cs)

* Singleton manager for playing Music, SFX, UI, and Other `AudioClips`.
* Supports:
  * Mixer group routing (`MusicGroup`, `SFXGroup`, `UIGroup`, `OtherGroup`).
  * Fade-in/out for Music.
  * Saving/restoring music playback positions.
  * Randomized pitch for SFX/UI.
  * Override previous Other audio clip.
* Requires an `AudioRegistry`.asset in a `Resources` folder in your project.

### [AudioRegistry](https://github.com/MeshMap/com.meshmap.sdk.bb/blob/main/Runtime/Audio/AudioRegistry.cs)

* `ScriptableObject` that maps `string` keys to `AudioClips`.
* Create one `AudioRegistry` in your Project via `Create > MeshMapLabs > Building Blocks > Audio > AudioRegistry`. Place it in a `Resources` folder to be loaded via `AudioRegistryCache.Instance`.
* Change load path dynamically with `AudioRegistryCache.SetResourcePath()`.

### [AudioSettings](https://github.com/MeshMap/com.meshmap.sdk.bb/blob/main/Runtime/Audio/AudioSettings.cs)

* UI slider bindings for mixer volume control.
* Saves user preferences to `PlayerPrefs`.
* Mixer parameters: `MusicVolume`, `SFXVolume`, `UIVolume`.

### [PlaySound](https://github.com/MeshMap/com.meshmap.sdk.bb/blob/main/Runtime/Audio/PlaySound.cs)

* Simple `MonoBehaviour` to play a clip on an `AudioSource`.
* Optional pitch randomization.
* Can trigger on `OnEnable` or manually.

## Example Workflow

1. Add the `AudioManagerWithSettings`.prefab to your scene.
2. Create an `AudioRegistry`.asset in a `Resources` folder.
3. Add unique `string` keys and `AudioClips` to the `AudioRegistry`.
4. Add three `Sliders` to your game UI and assign them in `AudioSettings`.
5. Add a `SliderValueText` component to each `Slider` and assign a `TextMeshProUGUI` to each.
6. Play the `AudioClips` from anywhere in your project by referencing `AudioManager.Instance`.

## Example Code

```csharp
// Play UI sound
AudioManager.Instance.PlayUI("UI.ButtonClick", randomizePitch: true);

// Play looping background music
AudioManager.Instance.PlayMusic("Music.MainTheme", loop: true, fadeIn: true);

// Adjust music volume via UI slider
public void OnMusicVolumeChanged(float value)
{
    AudioManager.Instance.SetMusicVolume(value);
}
```

## Prefabs

* **AudioManager** — Ready-to-use manager `GameObject` with mixer groups and `AudioSource`s preconfigured. Make sure there is an [`AudioRegistry`](#audioregistry-and-audioregistrycache) in your Project.
* **AudioManagerWithSettings** — Variant with `AudioSettings` UI bindings. Requires a UI with volume sliders to be setup.
