using System.Collections; using System.Collections.Generic; using UnityEngine; using Sirenix.OdinInspector; using UnityEngine.SceneManagement; using KairoEngine.Core; namespace KairoEngine.SoundtrackSystem { public class SoundtrackManager : MonoBehaviour { #region Singleton private static SoundtrackManager soundtrackManager; public static SoundtrackManager instance { get { if(!soundtrackManager) { soundtrackManager = FindObjectOfType (typeof(SoundtrackManager)) as SoundtrackManager; if(!soundtrackManager) { //Debug.LogError("There need to one active SountrackManager script on the scene."); return null; } } return soundtrackManager; } } #endregion public AudioSource audioSource1; public AudioSource audioSource2; public Soundtrack currentTrack; public Playlist playlist; public int currentTrackIndex; public bool playOnStart = true; public bool showDebug = false; private int currentAudioSourceIndex = 1; private bool isPlaying = false; private bool playlistEnded = false; void Start() { if(playOnStart) { if(playlist != null) StartPlaylist(playlist, currentTrackIndex); else if(currentTrack != null) PlayTrack(currentTrack); } } void FixedUpdate() { if(!isPlaying) return; AudioSource currentAudioSource = GetAudioSource(currentAudioSourceIndex); if (!currentAudioSource.isPlaying && !playlistEnded) { // Song endend in current audio source if(playlist == null) return; currentTrackIndex += 1; if(playlist.soundtracks.Count <= currentTrackIndex) { // Reached the end of the playlist playlistEnded = true; if(showDebug) Debug.Log($"Reached the end of the playlist \'{playlist.title}\'"); } else { // Play next song PlayTrack(playlist.soundtracks[currentTrackIndex]); } } } public static void StartPlaylist(Playlist newPlaylist, int index = 0) { if(instance == null) return; if(newPlaylist == null) { Debug.LogWarning("Missing playlist... music wont play"); return; } if(instance.showDebug) Debug.Log($"Starting playlist \'{newPlaylist.title}\'"); instance.playlist = newPlaylist; instance.currentTrackIndex = index; instance.playlistEnded = false; PlayTrack(instance.playlist.soundtracks[instance.currentTrackIndex]); } public static void PlayTrack(Soundtrack soundtrack) { if(instance == null) return; if(instance.showDebug) Debug.Log($"{(soundtrack.fadeIn ? "Fading in" : "Playing")} song \'{soundtrack.name}\'"); instance.isPlaying = true; instance.currentTrack = soundtrack; instance.SwitchAudioSource(); instance.Play(soundtrack, instance.currentAudioSourceIndex); if(soundtrack.fadeIn) instance.StartCoroutine(SoundtrackManager.StartFade(instance.currentAudioSourceIndex, soundtrack.fadeTime, 1f, false)); else instance.audioSource1.volume = 1f; } public static void FadeOutSoundtrack(float time = 1f) { if(instance == null) return; if(instance.showDebug) Debug.Log($"Fading out song \'{instance.playlist.soundtracks[instance.currentTrackIndex].name}\'"); instance.StartCoroutine(SoundtrackManager.StartFade(instance.currentAudioSourceIndex, time, 0f, true)); } public static IEnumerator StartFade(int audioSourceIndex, float duration, float targetVolume, bool stop = false) { if(instance == null) yield break; AudioSource audioSource = instance.GetAudioSource(audioSourceIndex); if(stop == false) audioSource.Play(); float currentTime = 0; float start = audioSource.volume; while (currentTime < duration) { currentTime += Time.deltaTime; audioSource.volume = Mathf.Lerp(start, targetVolume, currentTime / duration); yield return null; } if(stop == true) { audioSource.Stop(); audioSource.clip = null; } if(instance.showDebug) Debug.Log($"Song \'{instance.currentTrack.name}\' ended"); yield break; } private void Play(Soundtrack soundtrack, int index) { if(index == 1) { audioSource1.clip = soundtrack.audioClip; audioSource1.Play(); } else if(index == 2) { audioSource2.clip = soundtrack.audioClip; audioSource2.Play(); } } private AudioSource GetAudioSource(int index) { if(index == 1) return audioSource1; else return audioSource2; } private void SwitchAudioSource() { if(currentAudioSourceIndex == 1) currentAudioSourceIndex = 2; else if(currentAudioSourceIndex == 2) currentAudioSourceIndex = 1; } } }