123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418 |
- using UnityEngine;
- using System.Collections;
- using System.Collections.Generic;
- using System;
- [Serializable]
- public class SoundConfig
- {
- public SoundEvent soundEvent;
- public AudioClip clip;
- public float volume = 1;
- public float pitch = 1;
- }
- public class SoundManager : MonoBehaviour
- {
- #region Unity Singleton
- private static SoundManager _instance;
- public static SoundManager Instance
- {
- get
- {
- if (_instance == null)
- {
- _instance = FindObjectOfType(typeof(SoundManager)) as SoundManager;
- if (_instance == null)
- {
- AVDebug.LogError(string.Format("No gameObject with {0} component exists. Make sure to create a gameObject with {0} component", new System.Diagnostics.StackFrame().GetMethod().DeclaringType));
- }
- }
- return _instance;
- }
- }
- void Awake()
- {
- if (_instance != null && _instance != this)
- {
- AVDebug.LogWarning(string.Format("{0} Instance already exists on another gameObject. Destroying this gameObject {1}", this.GetType().Name, gameObject.name));
- Destroy(gameObject);
- return;
- }
- _instance = this;
- DontDestroyOnLoad(gameObject);
- audioSources = new AudioSource[soundConfigs.Length];
- sfxVolume = PlayerPrefs.GetFloat("sfxVolume", 1);
- Debug.Log(PlayerPrefs.GetFloat("musicVolume"));
- musicVolume = PlayerPrefs.GetFloat("musicVolume", GameConstants.MUSIC_VOLUME);
- }
- #endregion
- #region soundRecording
- enum SoundActionType
- {
- Play,
- Stop,
- Loop,
- StopLoop
- }
- struct SoundAction
- {
- public SoundEvent soundEvent;
- public SoundActionType actionType;
- public float delay;
- }
-
- List<SoundAction> soundActions = new List<SoundAction>(10000);
- bool isRecording;
- float lastRecordedTime;
-
- public bool IsRecording
- {
- get
- {
- return isRecording;
- }
- set
- {
- isRecording = value;
- if (isRecording)
- {
- StartRecording();
- }
- else
- {
- StopRecording();
- }
- }
- }
-
- void StartRecording()
- {
- isRecording = true;
- lastRecordedTime = Time.time;
- soundActions.Clear();
- }
-
- void StopRecording()
- {
- isRecording = false;
- }
-
- void RecordSoundAction(SoundEvent soundEvent, SoundActionType actionType)
- {
- SoundAction action;
- action.soundEvent = soundEvent;
- action.actionType = actionType;
- action.delay = Time.time - lastRecordedTime;
- lastRecordedTime = Time.time;
- soundActions.Add(action);
- }
-
- int currentPlaybackSound;
- public void PlayBack()
- {
- isRecording = false;
- currentPlaybackSound = 0;
- PlaySounds();
- }
-
- void PlaySounds()
- {
- float delay = 0;
- foreach (SoundAction soundAction in soundActions)
- {
- delay += soundAction.delay;
- Invoke("PlayNextSound", delay);
- }
- }
-
- void PlayNextSound()
- {
- SoundAction soundAction = soundActions[currentPlaybackSound];
- ++currentPlaybackSound;
- switch (soundAction.actionType)
- {
- case SoundActionType.Play:
- Play(soundAction.soundEvent);
- break;
- case SoundActionType.Stop:
- Stop(soundAction.soundEvent);
- break;
- case SoundActionType.Loop:
- Loop(soundAction.soundEvent);
- break;
- case SoundActionType.StopLoop:
- StopLoop(soundAction.soundEvent);
- break;
- }
- }
-
- public void DumpToPrefs()
- {
- string s = soundActions.Count + "\n";
- foreach (SoundAction soundAction in soundActions)
- {
- s += soundAction.delay + " " + (int)soundAction.actionType + " " + (int)soundAction.soundEvent + "\n";
- }
- PlayerPrefs.SetString("audioRecording", s);
- PlayerPrefs.Save();
- }
-
- public void DumpFromPrefs()
- {
- Debug.Log(PlayerPrefs.GetString("audioRecording"));
- }
-
- public void LoadFromText()
- {
- TextAsset txt = Resources.Load("audioRecording", typeof(TextAsset)) as TextAsset;
- ByteReader reader = new ByteReader(txt);
- int numOfActions = int.Parse(reader.ReadLine());
- soundActions.Clear();
- for (int i = 0; i < numOfActions; i++)
- {
- string line = reader.ReadLine();
- string[] tokens = line.Split(' ');
- SoundAction action;
- action.delay = float.Parse(tokens[0]);
- action.actionType = (SoundActionType)int.Parse(tokens[1]);
- action.soundEvent = (SoundEvent)int.Parse(tokens[2]);
- soundActions.Add(action);
- }
- PlayBack();
- }
- #endregion
-
- public SoundConfig[] soundConfigs;
- float sfxVolume = 1;
- float musicVolume = GameConstants.MUSIC_VOLUME;
- AudioSource[] audioSources;
- public static AudioSource SourceCelebSperm;
- #region Static helpers
- public static void PlayRandom(SoundEvent[] sounds)
- {
- AVDebug.Assert(sounds.Length != 0, "The passed sounds should not be empty");
- SoundEvent sound = sounds[UnityEngine.Random.Range(0, sounds.Length)];
- Play(sound);
- }
- public static void PlayRandomPitch(SoundEvent sound, float maxNegativePitch, float maxPositivePitch)
- {
- float randomPitch = 1 + UnityEngine.Random.Range(maxNegativePitch, maxPositivePitch);
- PlayWithPitch(sound, randomPitch);
- }
- public static void PlayWithPitch(SoundEvent soundEvent, float pitch)
- {
- //Debug.Log("PlayWithPitch");
- _instance._PlayWithPitch(soundEvent, pitch);
- }
- public static void PlayWithPitch(SoundEvent soundEvent, float pitch, float delay)
- {
- //Debug.Log("PlayWithPitch");
- _instance._PlayWithPitch(soundEvent, pitch, delay);
- }
- public static void Play(SoundEvent soundEvent)
- {
- _instance._Play(soundEvent);
- }
-
- public static void Play(SoundEvent soundEvent, float delay)
- {
- _instance._Play(soundEvent, delay);
- }
-
- public static float ClipLength(SoundEvent soundEvent)
- {
- return _instance._ClipLength(soundEvent);
- }
-
- public static void Stop(SoundEvent soundEvent)
- {
- _instance._Stop(soundEvent);
- }
- public static void Loop(SoundEvent soundEvent)
- {
- _instance._Loop(soundEvent);
- }
-
- public static void StopLoop(SoundEvent soundEvent)
- {
- _instance._StopLoop(soundEvent);
- }
- #endregion
-
-
- void _Play(SoundEvent soundEvent)
- {
- if(musicVolume >0)
- {
- SoundConfig soundConfig = soundConfigs[(int)soundEvent];
- AudioSource audioSource = GetAudioSourceFor(soundEvent);
- SourceCelebSperm = audioSource;
- audioSource.pitch = soundConfig.pitch;
- audioSource.clip = soundConfig.clip;
- audioSource.Play();
- //Debug.Log(audioSource);
- if (isRecording)
- {
- RecordSoundAction(soundEvent, SoundActionType.Play);
- }
- }
-
- }
-
- void _Play(SoundEvent soundEvent, float delay)
- {
- StartCoroutine(DelayedPlayCoroutine(soundEvent, delay));
- }
- IEnumerator DelayedPlayCoroutine(SoundEvent soundEvent, float delay)
- {
- yield return new WaitForSeconds(delay);
- if (!MenuManager._instance.ExitToMainMenu)
- {
- _Play(soundEvent);
- }
- }
- void _PlayWithPitch(SoundEvent soundEvent, float pitch, float delay)
- {
- StartCoroutine(DelayedPlayWithPitchCoroutine(soundEvent, pitch, delay));
- }
- IEnumerator DelayedPlayWithPitchCoroutine(SoundEvent soundEvent, float pitch, float delay)
- {
- yield return new WaitForSeconds(delay);
- _PlayWithPitch(soundEvent, pitch);
- }
-
- void _PlayWithPitch(SoundEvent soundEvent, float pitch)
- {
- SoundConfig soundConfig = soundConfigs[(int)soundEvent];
- AudioSource audioSource = GetAudioSourceFor(soundEvent);
- audioSource.pitch = pitch;
- audioSource.PlayOneShot(soundConfig.clip, soundConfig.volume * sfxVolume);
- if (isRecording)
- {
- RecordSoundAction(soundEvent, SoundActionType.Play);
- }
- }
-
- public float _ClipLength(SoundEvent soundEvent)
- {
- SoundConfig soundConfig = soundConfigs[(int)soundEvent];
- return soundConfig.clip.length;
- }
-
- void _Stop(SoundEvent soundEvent)
- {
- audioSources[(int)soundEvent].Stop();
- if (isRecording)
- {
- RecordSoundAction(soundEvent, SoundActionType.Stop);
- }
- }
-
- void _Loop(SoundEvent soundEvent)
- {
- SoundConfig soundConfig = soundConfigs[(int)soundEvent];
- AudioSource audioSource = GetAudioSourceFor(soundEvent);
-
- audioSource.clip = soundConfig.clip;
- /*if (audioSource.clip.name == "loop_PointCounter")
- {
- MenuManager._instance.audioLoop = audioSource;
- }*/
- audioSource.loop = true;
- audioSource.volume = soundConfig.volume * sfxVolume;
- audioSource.pitch = soundConfig.pitch;
- audioSource.Play();
- if (isRecording)
- {
- RecordSoundAction(soundEvent, SoundActionType.Loop);
- }
- }
-
- void _StopLoop(SoundEvent soundEvent)
- {
- AudioSource audioSource = GetAudioSourceFor(soundEvent);
- audioSource.loop = false;
- audioSource.Stop();
- if (isRecording)
- {
- RecordSoundAction(soundEvent, SoundActionType.StopLoop);
- }
- }
-
- public AudioSource GetAudioSourceFor(SoundEvent soundEvent)
- {
- int i = (int)soundEvent;
- if (audioSources[i] == null)
- {
- audioSources[i] = gameObject.AddComponent<AudioSource>();
- }
- return audioSources[i];
- }
-
- public float SfxVolume
- {
- get
- {
- return sfxVolume;
- }
- set
- {
- SetSfxVolume(value);
- }
- }
-
- public float MusicVolume
- {
- get
- {
- return musicVolume;
- }
- set
- {
- SetMusicVolume(value);
- }
- }
-
- public void SetSfxVolume(float value)
- {
- sfxVolume = value;
- PlayerPrefs.SetFloat("sfxVolume", sfxVolume);
- PlayerPrefs.Save();
- //scale to new volume
- foreach (AudioSource s in audioSources)
- {
- if (s != null)
- {
- s.volume = sfxVolume;
- }
- }
- }
-
- public void SetMusicVolume(float value)
- {
- musicVolume = value;
- PlayerPrefs.SetFloat("musicVolume", musicVolume);
- PlayerPrefs.Save();
- MusicManager.Instance.SetMusicVolume(value);
- }
-
- }
|