private IEnumerator FadeInOut() { var fadeOutStartTime = _audio.clip.length - (fadeOutTime * _audio.pitch); yield return(StartCoroutine(CoroutineHelper.WaitForRealSeconds(MasterAudio.INNER_LOOP_CHECK_INTERVAL))); // wait for clip to start playing var stepVolumeUp = fadeMaxVolume / fadeInTime * MasterAudio.INNER_LOOP_CHECK_INTERVAL; curFadeMode = FadeMode.FadeInOut; // wait to set this so it stops the previous one if it's still going. if (fadeInTime > 0f) { while (_audio.isPlaying && curFadeMode == FadeMode.FadeInOut && _audio.time < fadeInTime) { _audio.volume += stepVolumeUp; yield return(StartCoroutine(CoroutineHelper.WaitForRealSeconds(MasterAudio.INNER_LOOP_CHECK_INTERVAL))); } } if (curFadeMode != FadeMode.FadeInOut) { yield break; // in case another fade cancelled this one } _audio.volume = fadeMaxVolume; // just in case it didn't align exactly if (fadeOutTime == 0f || _audio.loop) { yield break; // nothing more to do! } // wait for fade out time. while (_audio.isPlaying && curFadeMode == FadeMode.FadeInOut && _audio.time < fadeOutStartTime) { yield return(StartCoroutine(CoroutineHelper.WaitForRealSeconds(MasterAudio.INNER_LOOP_CHECK_INTERVAL))); } if (curFadeMode != FadeMode.FadeInOut) { yield break; // in case another fade cancelled this one } var stepVolumeDown = fadeMaxVolume / fadeOutTime * MasterAudio.INNER_LOOP_CHECK_INTERVAL; while (_audio.isPlaying && curFadeMode == FadeMode.FadeInOut && _audio.volume > 0) { _audio.volume -= stepVolumeDown; yield return(StartCoroutine(CoroutineHelper.WaitForRealSeconds(MasterAudio.INNER_LOOP_CHECK_INTERVAL))); } audio.volume = 0f; Stop(); if (curFadeMode != FadeMode.FadeInOut) { yield break; // in case another fade cancelled this one } curFadeMode = FadeMode.None; }
private IEnumerator PlaySongWithDelay() { var randomTime = Random.Range(minTimeToWait, maxTimeToWait); yield return(StartCoroutine(CoroutineHelper.WaitForRealSeconds(randomTime))); controller.PlayNextSong(); }
private IEnumerator FollowSoundMaker() { UpdateAudioLocationAndPriority(false, attachToSource); while (curDetectEndMode == DetectEndMode.DetectEnd) { var timeToWait = MasterAudio.INNER_LOOP_CHECK_INTERVAL; yield return(StartCoroutine(CoroutineHelper.WaitForRealSeconds(timeToWait))); UpdateAudioLocationAndPriority(true, attachToSource); } }
private IEnumerator TryPlayStartSound(AudioEventGroup grp, AudioEvent aEvent) { for (var i = 0; i < 3; i++) { yield return(StartCoroutine(CoroutineHelper.WaitForRealSeconds(MasterAudio.INNER_LOOP_CHECK_INTERVAL))); var result = PerformSingleAction(grp, aEvent, EventType.OnStart, false); if (result != null && result.SoundPlayed) { break; } } }
private IEnumerator FadeOverTimeToVolume(float targetVolume, float fadeTime) { if (fadeTime <= MasterAudio.INNER_LOOP_CHECK_INTERVAL) { _audio.volume = targetVolume; // time really short, just do it at once. if (_audio.volume <= 0f) { Stop(); } curFadeMode = FadeMode.None; yield break; } yield return(StartCoroutine(CoroutineHelper.WaitForRealSeconds(MasterAudio.INNER_LOOP_CHECK_INTERVAL))); // wait for clip to start playing. var volStep = (targetVolume - _audio.volume) / (fadeTime / MasterAudio.INNER_LOOP_CHECK_INTERVAL); float newVol; while (_audio.volume != targetVolume && curFadeMode == FadeMode.GradualFade) { newVol = _audio.volume + volStep; if (volStep > 0f) { newVol = Math.Min(newVol, targetVolume); } else { newVol = Math.Max(newVol, targetVolume); } _audio.volume = newVol; yield return(StartCoroutine(CoroutineHelper.WaitForRealSeconds(MasterAudio.INNER_LOOP_CHECK_INTERVAL))); // wait for clip to start playing. } if (_audio.volume <= 0f) { Stop(); } if (curFadeMode != FadeMode.GradualFade) { yield break; // in case another fade cancelled this one } curFadeMode = FadeMode.None; }
IEnumerator CoUpdate() { while (true) { yield return(StartCoroutine(CoroutineHelper.WaitForRealSeconds(MasterAudio.INNER_LOOP_CHECK_INTERVAL))); // works with zero timescale also. //yield return MasterAudio.loopDelay; // Stops working with zero timescale. // gradual fade code if (curFadeMode != FadeMode.GradualFade) { continue; } if (activeAudio == null) { continue; // paused or error in setup } var newVolume = playlistVolume + slowFadeVolStep; if (slowFadeVolStep > 0f) { newVolume = Math.Min(newVolume, slowFadeTargetVolume); } else { newVolume = Math.Max(newVolume, slowFadeTargetVolume); } playlistVolume = newVolume; UpdateMasterVolume(); if (newVolume == slowFadeTargetVolume) { if (fadeCompleteCallback != null) { fadeCompleteCallback(); fadeCompleteCallback = null; } curFadeMode = FadeMode.None; } } }
private IEnumerator FadeOutEarly(float fadeTime) { curFadeMode = FadeMode.FadeOutEarly; // cancel the FadeInOut loop, if it's going. var stepVolumeDown = _audio.volume / fadeTime * MasterAudio.INNER_LOOP_CHECK_INTERVAL; while (_audio.isPlaying && curFadeMode == FadeMode.FadeOutEarly && _audio.volume > 0) { _audio.volume -= stepVolumeDown; yield return(StartCoroutine(CoroutineHelper.WaitForRealSeconds(MasterAudio.INNER_LOOP_CHECK_INTERVAL))); } _audio.volume = 0f; Stop(); if (curFadeMode != FadeMode.FadeOutEarly) { yield break; // in case another fade cancelled this one } curFadeMode = FadeMode.None; }
private IEnumerator DetectSoundFinished(float delaySound) { if (useIntroSilence && introSilenceMax > 0f) { var rndSilence = UnityEngine.Random.Range(introSilenceMin, introSilenceMax); isWaitingForDelay = true; yield return(StartCoroutine(CoroutineHelper.WaitForRealSeconds(rndSilence))); isWaitingForDelay = false; } if (delaySound > 0f) { isWaitingForDelay = true; yield return(StartCoroutine(CoroutineHelper.WaitForRealSeconds(delaySound))); isWaitingForDelay = false; } if (curDetectEndMode != DetectEndMode.DetectEnd) { yield break; } _audio.Play(); lastTimePlayed = Time.time; // sound play worked! Duck music if a ducking sound. MasterAudio.DuckSoundGroup(ParentGroup.name, _audio); var clipLength = Math.Abs(_audio.clip.length / _audio.pitch); yield return(StartCoroutine(CoroutineHelper.WaitForRealSeconds(clipLength))); // wait for clip to play if (HasActiveFXFilter && fxTailTime > 0f) { yield return(StartCoroutine(CoroutineHelper.WaitForRealSeconds(fxTailTime))); } var playSnd = playSndParams; if (curDetectEndMode != DetectEndMode.DetectEnd) { yield break; } if (!_audio.loop || (playSndParams != null && playSndParams.isChainLoop)) { Stop(); } if (playSnd != null && playSnd.isChainLoop) { // check if loop count is over. if (ParentGroup.chainLoopMode == MasterAudioGroup.ChainedLoopLoopMode.NumberOfLoops && ParentGroup.ChainLoopCount >= ParentGroup.chainLoopNumLoops) { // done looping yield break; } var rndDelay = playSnd.delaySoundTime; if (ParentGroup.chainLoopDelayMin > 0f || ParentGroup.chainLoopDelayMax > 0f) { rndDelay = UnityEngine.Random.Range(ParentGroup.chainLoopDelayMin, ParentGroup.chainLoopDelayMax); } // cannot use "AndForget" methods! Chain loop needs to check the status. if (playSnd.attachToSource || playSnd.sourceTrans != null) { if (playSnd.attachToSource) { MasterAudio.PlaySound3DFollowTransform(playSnd.soundType, playSnd.sourceTrans, playSnd.volumePercentage, playSnd.pitch, rndDelay, null, true); } else { MasterAudio.PlaySound3DAtTransform(playSnd.soundType, playSnd.sourceTrans, playSnd.volumePercentage, playSnd.pitch, rndDelay, null, true); } } else { MasterAudio.PlaySound(playSnd.soundType, playSnd.volumePercentage, playSnd.pitch, rndDelay, null, true); } } }