private IEnumerator CoFade(MusicSlot slot, float endVolume, float duration, bool realTime) { float?sv = AudioPlayer.Instance.GetMusicVolume(slot); if (!sv.HasValue) { yield break; } float startVolume = sv.Value; float elapsed = 0; while (elapsed < duration) { elapsed += realTime ? Time.unscaledDeltaTime : Time.deltaTime; float ratio = elapsed / duration; float volume = Mathf.Lerp(startVolume, endVolume, ratio); AudioPlayer.Instance.SetMusicVolume(volume, slot); yield return(null); } yield return(null); AudioPlayer.Instance.SetMusicVolume(endVolume, slot); ActiveFades[slot].Coroutine = null; }
/// <summary> /// Sets the music in a slot to the specified audio. /// </summary> /// <param name="clip">The music clip to play</param> /// <param name="name">The name of the music clip</param> /// <param name="slot">The slot to play music in</param> /// <param name="volume">The volume to play the music at (will be multiplied with global music volume)</param> /// <param name="loop">Whether to loop the music</param> /// <param name="retain">Whether to retain the music across scene loads</param> /// <remarks> /// <para>No longer effects playback state</para> /// </remarks> public void SetMusic(AudioClip clip, string name, MusicSlot slot, float volume, bool loop, bool retain) { bool wasPlaying = false; if (CurrentMusics.ContainsKey(slot)) { wasPlaying = CurrentMusics[slot].Playing; } //release old clip from user music slot if (slot == MusicSlot.User && CurrentMusics.ContainsKey(slot) && CurrentMusics[slot].Clip != null && CurrentMusics[slot].Clip != clip) { MusicPlayers[MusicSlot.User].Stop(); var oldClip = MusicPlayers[MusicSlot.User].clip; MusicPlayers[MusicSlot.User].clip = null; CurrentUserMusicComponent.ReportClipReleased(oldClip); } var musicInfo = new MusicInfo() { Clip = clip, Name = name, Volume = volume, Loop = loop, Retain = retain, Playing = wasPlaying, Time = 0 }; CurrentMusics[slot] = musicInfo; HandleMusicChanged(); }
public static void ClearFade(MusicSlot slot) { if (MusicFaderScript != null) { MusicFaderScript.AbortFade(slot); } }
/// <summary> /// Sets the looping state of a music slot /// </summary> public void SetMusicLooping(bool looping, MusicSlot slot) { if (CurrentMusics.ContainsKey(slot)) { CurrentMusics[slot].Loop = looping; HandleMusicChanged(); } }
/// <summary> /// Sets the volume of a music slot /// </summary> public void SetMusicVolume(float volume, MusicSlot slot) { if (CurrentMusics.ContainsKey(slot)) { CurrentMusics[slot].Volume = volume; HandleMusicChanged(); } }
/// <summary> /// Gets the name of the current music in the slot, if it exists /// </summary> public string GetMusicName(MusicSlot slot) { if (CurrentMusics.ContainsKey(slot)) { return(CurrentMusics[slot].Name); } return(null); }
/// <summary> /// Gets the volume of the current music in the slot, if it exists /// </summary> public float?GetMusicVolume(MusicSlot slot) { if (CurrentMusics.ContainsKey(slot)) { return(CurrentMusics[slot].Volume); } return(null); }
//TODO more music options like "play, or continue playing if already playing" //things like fades and overrides would also be nice /// <summary> /// Sets the music in a slot to the specified audio. /// </summary> /// <param name="sound">The music file to play</param> /// <param name="slot">The slot to play music in</param> /// <param name="volume">The volume to play the music at (will be multiplied with global music volume)</param> /// <param name="loop">Whether to loop the music</param> /// <param name="retain">Whether to retain the music across scene loads</param> /// <remarks> /// <para>No longer effects playback state</para> /// </remarks> public void SetMusic(string sound, MusicSlot slot, float volume, bool loop, bool retain) { var clip = Module.GetSound(sound, SoundType.Music); if (clip == null) { Debug.LogError("Can't find music " + sound); return; } SetMusic(clip, sound, slot, volume, loop, retain); }
/// <summary> /// Sets the volume of a music slot directly, bypassing HandleMusicChanged /// </summary> public void SetMusicVolumeDirect(float volume, MusicSlot slot) { if (CurrentMusics.ContainsKey(slot)) { CurrentMusics[slot].Volume = volume; } if (MusicPlayers.ContainsKey(slot)) { MusicPlayers[slot].volume = Mathf.Clamp(volume * ConfigState.Instance.MusicVolume, 0, 1); } }
/// <summary> /// Stops the currently set music in a slot /// </summary> public void StopMusic(MusicSlot slot) { if (CurrentMusics.ContainsKey(slot)) { CurrentMusics[slot].Playing = false; HandleMusicChanged(); } else { Debug.LogWarning($"Tried to stop music in slot {slot.ToString()} but none exists!"); } }
public void StartFade(MusicSlot slot, float volume, float duration, bool realTime, bool persist) { AbortFade(slot); Coroutine fadeCoroutine = StartCoroutine(CoFade(slot, volume, duration, realTime)); ActiveFades[slot] = new FadeInfo() { Coroutine = fadeCoroutine, Persist = persist }; }
/// <summary> /// Checks if a music slot has something in it /// </summary> public bool MusicHasClip(MusicSlot slot) { if (CurrentMusics.ContainsKey(slot)) { if (slot == MusicSlot.User || CurrentMusics[slot].Playing) { return(true); } } return(false); }
public void AbortFade(MusicSlot slot) { if (ActiveFades.TryGetValue(slot, out var fade)) { if (fade.Coroutine != null) { StopCoroutine(fade.Coroutine); } ActiveFades.Remove(slot); } }
/// <summary> /// Seeks the currently set music in a slot /// </summary> public void SeekMusic(MusicSlot slot, float time) { if (CurrentMusics.ContainsKey(slot)) { CurrentMusics[slot].Time = time; HandleMusicChanged(); } else { Debug.LogWarning($"Tried to seek music in slot {slot.ToString()} but none exists!"); } }
/// <summary> /// Plays the currently set music in a slot /// </summary> public void StartMusic(MusicSlot slot, bool restart) { if (CurrentMusics.ContainsKey(slot)) { CurrentMusics[slot].Playing = true; if (restart) { CurrentMusics[slot].Time = 0; } HandleMusicChanged(); } else { Debug.LogWarning($"Tried to start music in slot {slot.ToString()} but none exists!"); } }
/// <summary> /// Sets the music in a slot to the specified audio, keeping everything else the same /// </summary> /// <remarks>Intended to be used with UserAudioComponent</remarks> public void SetMusicClip(AudioClip clip, MusicSlot slot) { if (CurrentMusics.ContainsKey(slot)) { //release old clip from user music slot if (slot == MusicSlot.User && CurrentMusics.ContainsKey(slot) && CurrentMusics[slot].Clip != null && CurrentMusics[slot].Clip != clip) { MusicPlayers[MusicSlot.User].Stop(); var oldClip = MusicPlayers[MusicSlot.User].clip; MusicPlayers[MusicSlot.User].clip = null; CurrentUserMusicComponent.ReportClipReleased(oldClip); } CurrentMusics[slot].Clip = clip; HandleMusicChanged(); } else { SetMusic(clip, slot, 1f, true, false); } }
public static void FadeIn(MusicSlot slot, float volume, float duration, bool realTime = false, bool persist = false) { AudioPlayer.Instance.SetMusicVolume(0, slot); FadeTo(slot, volume, duration, realTime, persist); }
public static void FadeTo(MusicSlot slot, float volume, float duration, bool realTime = false, bool persist = false) { InitFaderObjectIfNotExists(); MusicFaderScript.StartFade(slot, volume, duration, realTime, persist); }
/// <summary> /// Sets the music in a slot to the specified audio. /// </summary> /// <param name="clip">The music clip to play</param> /// <param name="slot">The slot to play music in</param> /// <param name="volume">The volume to play the music at (will be multiplied with global music volume)</param> /// <param name="loop">Whether to loop the music</param> /// <param name="retain">Whether to retain the music across scene loads</param> /// <remarks> /// <para>No longer effects playback state</para> /// </remarks> public void SetMusic(AudioClip clip, MusicSlot slot, float volume, bool loop, bool retain) => SetMusic(clip, clip.Ref()?.name, slot, volume, loop, retain);
/// <summary> /// Plays the currently set music in a slot /// </summary> public void StartMusic(MusicSlot slot) => StartMusic(slot, true);
/// <summary> /// Checks if a slot is currently playing /// </summary> public bool IsMusicPlaying(MusicSlot slot) { var highestPlayingMusic = GetHighestPlayingMusicSlot(); return(highestPlayingMusic != null && highestPlayingMusic.Value == slot); }
/// <summary> /// Sets the music in a slot to the specified audio. /// </summary> /// <param name="clip">The music clip to play</param> /// <param name="slot">The slot to play music in</param> /// <param name="volume">The volume to play the music at (will be multiplied with global music volume)</param> /// <param name="loop">Whether to loop the music</param> /// <param name="retain">Whether to retain the music across scene loads</param> /// <remarks> /// <para>No longer effects playback state</para> /// </remarks> public void SetMusic(AudioClip clip, MusicSlot slot, float volume, bool loop, bool retain) => SetMusic(clip, null, slot, volume, loop, retain);
/// <summary> /// Plays music into a specified slot. /// </summary> /// <param name="clip">The music clip to play</param> /// <param name="slot">The slot to play music in</param> /// <param name="volume">The volume to play the music at (will be multiplied with global music volume)</param> /// <param name="loop">Whether to loop the music</param> /// <param name="retain">Whether to retain the music across scene loads</param> /// <remarks> /// <para>Equivalent to calling SetMusic followed by StartMusic</para> /// </remarks> public void PlayMusic(AudioClip clip, MusicSlot slot, float volume, bool loop, bool retain) { SetMusic(clip, slot, volume, loop, retain); StartMusic(slot); }
public static void FadeOut(MusicSlot slot, float duration, bool realTime = false, bool persist = false) { FadeTo(slot, 0, duration, realTime, persist); }
/// <summary> /// Checks if a slot has music and is set to play /// </summary> public bool IsMusicSetToPlay(MusicSlot slot) { return(MusicHasClip(slot) && CurrentMusics[slot].Playing); }
//TODO more music options like "play, or continue playing if already playing" //things like fades and overrides would also be nice /// <summary> /// Plays music into a specified slot. /// </summary> /// <param name="sound">The music file to play</param> /// <param name="slot">The slot to play music in</param> /// <param name="volume">The volume to play the music at (will be multiplied with global music volume)</param> /// <param name="loop">Whether to loop the music</param> /// <param name="retain">Whether to retain the music across scene loads</param> /// <remarks> /// <para>Equivalent to calling SetMusic followed by StartMusic</para> /// </remarks> public void PlayMusic(string sound, MusicSlot slot, float volume, bool loop, bool retain) { SetMusic(sound, slot, volume, loop, retain); StartMusic(slot); }
/// <summary> /// Stops and clears the currently set music in a slot /// </summary> public void ClearMusic(MusicSlot slot) { CurrentMusics.Remove(slot); HandleMusicChanged(); }