This repository has been archived on 2024-11-20. You can view files and clone it, but cannot push or open issues or pull requests.
colourmeok/ColourMeOKGame/Assets/Scripts/AudioManager.cs
2024-11-15 07:54:43 +08:00

163 lines
No EOL
5.1 KiB
C#

/*
* author: mark joshwel
* date: 29/5/2024
* description: audio manager for handling audio in the game
*/
using System;
using UnityEngine;
/// <summary>
/// singleton class for handling audio in the game
/// </summary>
public class AudioManager : MonoBehaviour
{
/// <summary>
/// enum for available audio channels in the game
/// </summary>
public enum AudioChannel
{
Music,
SoundEffects
}
/// <summary>
/// singleton pattern: define instance field for accessing the singleton elsewhere
/// </summary>
public static AudioManager Instance;
/// <summary>
/// music audio source
/// </summary>
[SerializeField] private AudioSource musicSource;
/// <summary>
/// sound effects (sfx) audio source
/// </summary>
[SerializeField] private AudioSource sfxSource;
/// <summary>
/// music source default volume
/// </summary>
[SerializeField] private float musicSourceDefaultVolume = 0.6f;
/// <summary>
/// sound effects (sfx) source default volume
/// </summary>
[SerializeField] private float sfxSourceDefaultVolume = 0.6f;
/// <summary>
/// enforces singleton behaviour; sets doesn't destroy on load and checks for multiple instances
/// </summary>
private void Awake()
{
// check if instance hasn't been set yet
if (Instance == null)
{
Debug.Log("awake as singleton instance, setting self as the forever-alive instance");
Instance = this;
DontDestroyOnLoad(gameObject);
}
// check if instance is already set and it's not this instance
else if (Instance != null && Instance != this)
{
Debug.Log("awake as non-singleton instance, destroying self");
Destroy(gameObject);
}
}
/// <summary>
/// function to set default volumes for the audio sources
/// </summary>
public void Start()
{
// set the default volume for the music source
musicSource.volume = musicSourceDefaultVolume;
// set the default volume for the sfx source
sfxSource.volume = sfxSourceDefaultVolume;
}
/// <summary>
/// plays the audio clip once on a given channel
/// </summary>
/// <param name="clip">the audio clip to play</param>
/// <param name="channel">the audio channel to play the clip on</param>
public void PlayClipOnChannel(AudioClip clip, AudioChannel channel)
{
switch (channel)
{
case AudioChannel.Music:
musicSource.PlayOneShot(clip);
break;
case AudioChannel.SoundEffects:
sfxSource.PlayOneShot(clip);
break;
default:
Debug.LogError($"invalid channel '{channel}'");
break;
}
}
/// <summary>
/// gets the volume of a given audio channel
/// </summary>
/// <param name="channel">the AudioManager.AudioChannel to get the volume of</param>
/// <returns>volume float value of the channel, from 0.0f-1.0f</returns>
public float GetChannelVolume(AudioChannel channel)
{
switch (channel)
{
case AudioChannel.Music:
return musicSource.volume;
case AudioChannel.SoundEffects:
return sfxSource.volume;
default:
Debug.LogError($"invalid channel '{channel}'");
return 0f;
}
}
/// <summary>
/// sets the pure volume value of a given audio channel
/// </summary>
/// <param name="volume">volume float value for the channel, from 0.0f-1.0f</param>
/// <param name="channel">the AudioManager.AudioChannel to set the volume of</param>
public void SetChannelVolumeReal(float volume, AudioChannel channel)
{
switch (channel)
{
case AudioChannel.Music:
musicSource.volume = Math.Min(volume, 1.0f);
break;
case AudioChannel.SoundEffects:
sfxSource.volume = Math.Min(volume, 1.0f);
break;
default:
Debug.LogError($"invalid channel '{channel}'");
break;
}
}
/// <summary>
/// sets the volume value of a given audio channel based on a logarithmic scale
/// for human perception (e.g. 0.5f is half volume, 0.1f is very quiet)
/// </summary>
/// <param name="volume">volume float value for the channel, from 0.0f-1.0f</param>
/// <param name="channel">the AudioManager.AudioChannel to set the volume of</param>
public void SetChannelVolumeLog(float volume, AudioChannel channel)
{
switch (channel)
{
case AudioChannel.Music:
musicSource.volume = Mathf.Log10(Mathf.Max(volume, 0.0001f)) * 20;
break;
case AudioChannel.SoundEffects:
sfxSource.volume = Mathf.Log10(Mathf.Max(volume, 0.0001f)) * 20;
break;
default:
Debug.LogError($"invalid channel '{channel}'");
break;
}
}
}