Skip to content

Instantly share code, notes, and snippets.

@k-harris k-harris/AudioVisualManager.cs Secret
Last active Feb 9, 2019

Embed
What would you like to do?
Audio system from Junior DigiPen project. AudioVisualManager.cs was used by SoundHub.cs, which in turn was called by in-game events. It was written to replace the Wwise implementation.
////////////////////////////////////////////////////////////////////////////////
// Authors: Kaila Harris
// Copyright © 2017 DigiPen (USA) Corp. and its owners. All Rights Reserved.
////////////////////////////////////////////////////////////////////////////////
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
namespace AudioVisualization
{
public class AudioVisualManager : MonoBehaviour
{
// Static instance
static AudioVisualManager _instance;
public static AudioVisualManager GetInstance()
{
if (!_instance)
{
GameObject soundManager = new GameObject("AudioVisualManager");
_instance = soundManager.AddComponent<AudioVisualManager>();
_instance.Initialize();
}
return _instance;
}
const float MaxVolume_BGM = 1f;
const float MaxVolume_SFX = 1f;
static float CurrentVolumeNormalized_BGM = 1f;
static float CurrentVolumeNormalized_SFX = 1f;
static bool isMuted = false;
List<AudioSource> sfxSources;
public AudioSource bgmSource;
//List<Visuals> visuals;
void Initialize()
{
_instance = this;
// add our bgm sound source
bgmSource = gameObject.AddComponent<AudioSource>();
//gameObject.AddComponent<AudioLowPassFilter>();
bgmSource.loop = true;
bgmSource.playOnAwake = false;
bgmSource.volume = GetBGMVolume();
//DontDestroyOnLoad(gameObject);
}
// ==================== Volume Getters =====================
static float GetBGMVolume(float _volumeModifier = 0f)
{
return isMuted ? 0f : (MaxVolume_BGM + _volumeModifier) * CurrentVolumeNormalized_BGM;
}
public static float GetSFXVolume(float _volumeModifier = 0f)
{
return isMuted ? 0f : (MaxVolume_SFX + _volumeModifier) * CurrentVolumeNormalized_SFX;
}
// ====================== BGM Utils ======================
void FadeBGMOut(float _fadeDuration)
{
AudioVisualManager soundMan = GetInstance();
float delay = 0f;
float toVolume = 0f;
if (soundMan.bgmSource.clip == null)
{
Debug.LogError("Error: Could not fade BGM out as BGM AudioSource has no currently playing clip.");
}
StartCoroutine(FadeBGM(toVolume, delay, _fadeDuration));
}
void FadeBGMIn(AudioClip _bgmClip, float _delay, float _fadeDuration)
{
AudioVisualManager soundMan = GetInstance();
soundMan.bgmSource.clip = _bgmClip;
soundMan.bgmSource.Play();
float toVolume = GetBGMVolume();
StartCoroutine(FadeBGM(toVolume, _delay, _fadeDuration));
}
IEnumerator FadeBGM(float _fadeToVolume, float _delay, float _duration)
{
yield return new WaitForSeconds(_delay);
AudioVisualManager soundMan = GetInstance();
float elapsed = 0f;
while (_duration > 0)
{
float t = (elapsed / _duration);
float volume = Mathf.Lerp(0f, _fadeToVolume * CurrentVolumeNormalized_BGM, t); // "CurrentVolumeNormalized_BGM" was "CurrentVolumeModifier_BGM"
soundMan.bgmSource.volume = volume;
elapsed += Time.deltaTime;
yield return 0;
}
}
// ====================== BGM Functions ======================
public static AudioSource PlayBGM(AudioClip _bgmClip, bool _fade, float _fadeDuration, float _volumeMod = 0f)
{
AudioVisualManager soundMan = GetInstance();
if (_fade)
{
if (soundMan.bgmSource.isPlaying)
{
// fade out, then switch and fade in
soundMan.FadeBGMOut(_fadeDuration / 2);
soundMan.FadeBGMIn(_bgmClip, _fadeDuration / 2, _fadeDuration / 2);
}
else
{
// just fade in
float delay = 0f;
soundMan.FadeBGMIn(_bgmClip, delay, _fadeDuration);
}
}
else
{
// play immediately
soundMan.bgmSource.volume = GetBGMVolume(_volumeMod);
soundMan.bgmSource.clip = _bgmClip;
soundMan.bgmSource.Play();
}
return soundMan.bgmSource;
}
public static void StopBGM(bool _fade, float _fadeDuration)
{
AudioVisualManager soundMan = GetInstance();
if (soundMan.bgmSource.isPlaying)
{
// fade out, then switch and fade in
if (_fade)
{
soundMan.FadeBGMOut(_fadeDuration);
}
else
{
soundMan.bgmSource.Stop();
}
}
}
// ======================= SFX Utils ====================================
AudioSource GetSFXSource(GameObject _obj = null, float _spacialBlend = 0f, float _volumeMod = 0f)
{
// set up a new sfx sound source for each new sfx clip
AudioSource sfxSource;
if (_obj == null)
{
sfxSource = gameObject.AddComponent<AudioSource>();
}
else
{
sfxSource = _obj.AddComponent<AudioSource>();
}
sfxSource.loop = false;
sfxSource.playOnAwake = false;
sfxSource.spatialBlend = _spacialBlend;
sfxSource.volume = GetSFXVolume(_volumeMod);
if (sfxSources == null)
{
sfxSources = new List<AudioSource>();
}
sfxSources.Add(sfxSource);
return sfxSource;
}
IEnumerator RemoveSFXSource(AudioSource _sfxSource)
{
yield return new WaitForSeconds(_sfxSource.clip.length);
sfxSources.Remove(_sfxSource);
Destroy(_sfxSource);
}
IEnumerator RemoveSFXSourceFixedLength(AudioSource _sfxSource, float _length)
{
yield return new WaitForSeconds(_length);
sfxSources.Remove(_sfxSource);
Destroy(_sfxSource);
}
// ====================== SFX Functions =================================
public static void PlaySFX(AudioClip _sfxClip, GameObject _object = null, float _spacialBlend = 0f, float _volumeMod = 0f)
{
AudioVisualManager soundMan = GetInstance();
AudioSource source = soundMan.GetSFXSource(_object, _spacialBlend, _volumeMod);
source.volume = GetSFXVolume(_volumeMod);
source.clip = _sfxClip;
source.Play();
soundMan.StartCoroutine(soundMan.RemoveSFXSource(source));
}
public static void PlayMultiSFX(List<AudioClip> _sfxClips, GameObject _object = null, float _spacialBlend = 0f, float _volumeMod = 0f)
{
AudioVisualManager soundMan = GetInstance();
foreach (AudioClip clip in _sfxClips)
{
AudioSource source = soundMan.GetSFXSource(_object, _spacialBlend, _volumeMod);
source.volume = GetSFXVolume(_volumeMod);
source.clip = clip;
source.Play();
soundMan.StartCoroutine(soundMan.RemoveSFXSource(source));
}
}
public static void PlaySFXRandomized(AudioClip _sfxClip, GameObject _object = null, float _spacialBlend = 0f, float _volumeMod = 0f, float _pitchMin = 0.85f, float _pitchMax = 1.2f)
{
AudioVisualManager soundMan = GetInstance();
AudioSource source = soundMan.GetSFXSource(_object, _spacialBlend, _volumeMod);
source.volume = GetSFXVolume(_volumeMod);
source.clip = _sfxClip;
source.pitch = Random.Range(_pitchMin, _pitchMax);
source.Play();
soundMan.StartCoroutine(soundMan.RemoveSFXSource(source));
}
public static void PlaySFXRandomizedFromList(List<AudioClip> _sfxClips, GameObject _object = null, float _spacialBlend = 0f, float _volumeMod = 0f)
{
AudioVisualManager soundMan = GetInstance();
AudioSource source = soundMan.GetSFXSource(_object, _spacialBlend, _volumeMod);
source.volume = GetSFXVolume(_volumeMod);
source.clip = _sfxClips[Random.Range(0, _sfxClips.Capacity - 1)];
source.Play();
soundMan.StartCoroutine(soundMan.RemoveSFXSource(source));
}
public static void PlaySFXFixedDuration(AudioClip _sfxClip, float _duration, GameObject _object = null, float _spacialBlend = 0f, float _volumeMod = 0f, float _volumeMultiplier = 1.0f)
{
AudioVisualManager soundMan = GetInstance();
AudioSource source = soundMan.GetSFXSource(_object, _spacialBlend, _volumeMod);
source.volume = GetSFXVolume(_volumeMod) * _volumeMultiplier;
source.clip = _sfxClip;
source.loop = true;
source.Play();
soundMan.StartCoroutine(soundMan.RemoveSFXSourceFixedLength(source, _duration));
}
public static void StopAllObjectSFX(GameObject _object)
{
AudioVisualManager soundMan = GetInstance();
foreach (AudioSource source in _object.GetComponents<AudioSource>())
{
source.Stop();
soundMan.StartCoroutine(soundMan.RemoveSFXSource(source));
}
}
// ==================== Volume Control Functions ==========================
public static void DisableSoundImmediate()
{
AudioVisualManager soundMan = _instance;
//AudioVisualManager soundMan = GetInstance();
soundMan.StopAllCoroutines();
if (soundMan.sfxSources != null)
{
foreach (AudioSource source in soundMan.sfxSources)
{
source.volume = 0;
}
}
soundMan.bgmSource.volume = 0f;
isMuted = true;
}
public static void EnableSoundImmediate()
{
AudioVisualManager soundMan = _instance;
//AudioVisualManager soundMan = GetInstance();
if (soundMan.sfxSources != null)
{
foreach (AudioSource source in soundMan.sfxSources)
{
source.volume = GetSFXVolume();
}
}
soundMan.bgmSource.volume = GetBGMVolume();
isMuted = false;
}
public static void SetGlobalVolume(float _newVolume)
{
CurrentVolumeNormalized_BGM = _newVolume;
CurrentVolumeNormalized_SFX = _newVolume;
AdjustSoundImmediate();
}
public static void SetSFXVolume(float _newVolume)
{
CurrentVolumeNormalized_SFX = _newVolume;
AdjustSoundImmediate();
}
public static void SetBGMVolume(float _newVolume)
{
CurrentVolumeNormalized_BGM = _newVolume;
AdjustSoundImmediate();
}
public static void AdjustSoundImmediate()
{
AudioVisualManager soundMan = _instance;
//AudioVisualManager soundMan = GetInstance();
if (soundMan.sfxSources != null)
{
foreach (AudioSource source in soundMan.sfxSources)
{
source.volume = GetSFXVolume();
}
}
//Debug.Log("BGMme: " + GetBGMVolume());
soundMan.bgmSource.volume = GetBGMVolume();
//Debug.Log("BGMme is now: " + GetBGMVolume());
}
// ==================== Visualizer Functions ==========================
//when creating/playing an audio source, have a boolean sent into the function
//if the boolean is true, call a function from here to turn on/add a visual script to the object in question
//put visualizer calls here
void OnDestroy()
{
Destroy(_instance);
}
}
}
// player health reaches zero
StartCoroutine(SoundHub.PlayPlayerDeath());
// pause volume, where 0.3f is the new audio level
SoundHub.EnterPauseState(0.3f);
// resume volume, where 1f is default audio level
SoundHub.ExitPauseState();
////////////////////////////////////////////////////////////////////////////////
// Authors: Kaila Harris
// Copyright © 2017 DigiPen (USA) Corp. and its owners. All Rights Reserved.
////////////////////////////////////////////////////////////////////////////////
using UnityEngine;
using AudioVisualization;
using System.Collections.Generic;
using System.Collections;
public class SoundHub : MonoBehaviour
{
// Static instance
public static SoundHub instance;
private void Awake()
{
if (instance == null)
{
instance = this;
instance.Initialize();
}
}
public static SoundHub GetInstance()
{
return instance;
}
float sfxVolume;
float bgmVolume;
float globalVolume;
float globalPauseVolume;
AudioClip logoScreen;
AudioClip ui_select;
AudioClip ui_hover;
public AudioClip bgm;
List<AudioClip> moveDirection;
AudioClip timeStop;
AudioClip timeResume;
AudioClip timeWobble;
AudioClip tickingClock;
AudioClip playerDamage;
AudioClip playerDeath;
AudioClip pickupRift;
AudioClip timeBomb;
AudioClip asteroidExplosion;
static public AudioSource source_bgm { get; set; }
void Initialize()
{
// programatically load audio clips
// eg. sound = Resources.Load<AudioClip>(path);
logoScreen = Resources.Load<AudioClip>("Sound/bgm/Intro_Splashscreen");
ui_select = Resources.Load<AudioClip>("Sound/ui/Menu_Select");
ui_hover = Resources.Load<AudioClip>("Sound/ui/Menu_Hover");
if (bgm == null)
bgm = Resources.Load<AudioClip>("Sound/SpaceTime");
moveDirection = new List<AudioClip>();
moveDirection.Add(Resources.Load<AudioClip>("Sound/sfx/Direction_Change_01"));
moveDirection.Add(Resources.Load<AudioClip>("Sound/sfx/Direction_Change_02"));
moveDirection.Add(Resources.Load<AudioClip>("Sound/sfx/Direction_Change_03"));
moveDirection.Add(Resources.Load<AudioClip>("Sound/sfx/Direction_Change_04"));
moveDirection.Add(Resources.Load<AudioClip>("Sound/sfx/Direction_Change_05"));
timeStop = Resources.Load<AudioClip>("Sound/sfx/TimeSlowsSound");
timeResume = Resources.Load<AudioClip>("Sound/sfx/TimeSpeedsUpSound");
timeWobble = Resources.Load<AudioClip>("Sound/sfx/boopsound");
tickingClock = Resources.Load<AudioClip>("Sound/sfx/persistantClockTick");
playerDamage = Resources.Load<AudioClip>("Sound/sfx/Sinematic - Complex Tech Hits -04");
playerDeath = Resources.Load<AudioClip>("Sound/sfx/EndSoundEffect");
pickupRift = Resources.Load<AudioClip>("Sound/sfx/PickupSound");
timeBomb = Resources.Load<AudioClip>("Sound/sfx/BombSound");
asteroidExplosion = Resources.Load<AudioClip>("Sound/sfx/SmashSound");
source_bgm = AudioVisualManager.PlayBGM(bgm, false, 0, -0.3f);
bgmVolume = source_bgm.volume;
}
public static void PlaySplashScreenAudio()
{
AudioVisualManager.PlaySFX(GetInstance().logoScreen);
}
public static void PlayButtonHover()
{
AudioVisualManager.PlaySFX(GetInstance().ui_hover);
}
public static void PlayButtonClick()
{
AudioVisualManager.PlaySFX(GetInstance().ui_hover);
}
public static void PlayTimeStopSFX(GameObject _obj)
{
AudioVisualManager.StopAllObjectSFX(_obj);
AudioVisualManager.PlaySFX(GetInstance().timeStop, _obj);
AudioVisualManager.PlaySFX(GetInstance().timeWobble, _obj, 0, -0.3f);
}
public static void PlayTimeResumeSFX(GameObject _obj)
{
AudioVisualManager.StopAllObjectSFX(_obj);
AudioVisualManager.PlaySFX(GetInstance().timeResume, _obj);
AudioVisualManager.PlaySFX(GetInstance().timeWobble, _obj, 0, -0.3f);
}
public static void PlayPlayerDamaged()
{
AudioVisualManager.PlaySFX(GetInstance().playerDamage);
}
public static IEnumerator PlayPlayerDeath()
{
AudioVisualManager.SetBGMVolume(0.25f);
AudioVisualManager.PlaySFX(GetInstance().playerDeath);
yield return new WaitForSeconds(2.5f);
yield return null;
}
public static void PlayerMoves(GameObject _obj)
{
AudioVisualManager.PlaySFXRandomized(GetInstance().moveDirection[0], null, 0, +0.3f, 0.8f, 1.3f);
//AudioVisualManager.PlaySFXRandomizedFromList(GetInstance().moveDirection, _obj, 1);
}
public static void PlayEnemyTimeBomb()
{
AudioVisualManager.PlaySFX(GetInstance().timeBomb);
}
public static void PlayAsteroidExplosion()
{
AudioVisualManager.PlaySFX(GetInstance().asteroidExplosion);
}
public static void PlayPickup()
{
AudioVisualManager.PlaySFX(GetInstance().pickupRift);
}
public static void PlayStageChange()
{
AudioVisualManager.PlaySFX(GetInstance().pickupRift, null, 0, -0.7f);
}
// paused volume
public void EnterPauseState(float _newVolume = 0.4f)
{
GetInstance().globalVolume = _newVolume;
if (_newVolume == 0)
AudioVisualManager.DisableSoundImmediate();
else
AudioVisualManager.SetGlobalVolume(GetInstance().globalVolume);
}
// resume volume
public void ExitPauseState(float _newVolume = 1f)
{
GetInstance().globalVolume = _newVolume;
AudioVisualManager.SetGlobalVolume(GetInstance().globalVolume);
}
// mute all sound effects
public void MuteAllSFX(float _newVolume = 0f)
{
GetInstance().sfxVolume = _newVolume;
AudioVisualManager.SetSFXVolume(GetInstance().sfxVolume);
}
// unmute all sound effects
public void UnmuteAllSFX(float _newVolume = 1f)
{
GetInstance().sfxVolume = _newVolume;
AudioVisualManager.SetSFXVolume(GetInstance().sfxVolume);
}
// mute background audio
public void MuteBackgroundAudio(float _newVolume = 0f)
{
GetInstance().bgmVolume = _newVolume;
AudioVisualManager.SetBGMVolume(GetInstance().bgmVolume);
}
// unmute background audio
public void UnmteBackgroundAudio(float _newVolume = 1f)
{
GetInstance().bgmVolume = _newVolume;
AudioVisualManager.SetBGMVolume(GetInstance().bgmVolume);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.