void FadeSource(AudioSource source, float endVolume, float fadeSpeed) { if (fadeSpeed != 0) { fadeSpeed = 1.0f / fadeSpeed; } else { fadeSpeed = 1; } foreach (FadeInfo info in fadingChannels) { if (info.source == source) { info.targetVolume = endVolume; info.fadeSpeed = fadeSpeed; return; } } FadeInfo finfo = new FadeInfo(); finfo.source = source; finfo.targetVolume = endVolume; finfo.fadeSpeed = fadeSpeed; finfo.channel = FindChannelFromSource(usedChannels, source); fadingChannels.Add(finfo); }
public IEnumerator Fade(FadeInfo fadeInfo) { if (OnBeforeFadeOut != null) { OnBeforeFadeOut (); } this.fadeInfo = fadeInfo; GameObject eventSystem = GameObject.Find ("EventSystem"); if (eventSystem != null) { eventSystem.SetActive (false); } float time = 0; while (time < fadeInfo.fadeOutTime_secs) { fadeAlpha = time / fadeInfo.fadeOutTime_secs; time += Time.deltaTime; yield return null; } if (OnAfterFadeOut != null) { OnAfterFadeOut (); } if (fadeInfo.useFade) { Application.LoadLevel (fadeInfo.nextScene); yield return null; } eventSystem = GameObject.Find ("EventSystem"); if (eventSystem != null) { eventSystem.SetActive (false); } yield return new WaitForSeconds (fadeInfo.waitTime_secs); if (OnBeforeFadeIn != null) { OnBeforeFadeIn (); } time = 0; while (time < fadeInfo.fadeInTime_secs) { fadeAlpha = 1 - (time / fadeInfo.fadeInTime_secs); time += Time.deltaTime; yield return null; } if (OnAfterFadeIn != null) { OnAfterFadeIn (); } if (eventSystem != null) { eventSystem.SetActive (true); } this.fadeInfo = null; OnBeforeFadeOut = null; OnAfterFadeOut = null; OnBeforeFadeIn = null; OnAfterFadeIn = null; }
FadeInfo FadeChannel(AudioChannel source, float endVolume, float fadeSpeed) { if (fadeSpeed != 0) { fadeSpeed = 1.0f / fadeSpeed; } else { fadeSpeed = 1; } foreach (FadeInfo info in fadingChannels) { if (info.source == source.source) { info.targetVolume = endVolume; info.fadeSpeed = fadeSpeed; return(info); } } FadeInfo finfo = new FadeInfo(); finfo.source = source.source; finfo.targetVolume = endVolume; finfo.fadeSpeed = fadeSpeed; finfo.freeOnCompletion = false; finfo.channel = source; fadingChannels.Add(finfo); return(finfo); }
public void DeregisterExternalSource(AudioSource source) { AudioChannel channelToRemove = null; foreach (AudioChannel channel in externalSources) { if (channel.source == source) { channelToRemove = channel; } } if (channelToRemove != null) { externalSources.Remove(channelToRemove); } FadeInfo infoToRemove = null; foreach (FadeInfo info in fadingChannels) { if (info.source == source) { infoToRemove = info; } } if (infoToRemove != null) { fadingChannels.Remove(infoToRemove); } }
/// <summary> /// 初期化 /// </summary> public void Initialize() { m_fadeInfo = new FadeInfo(); m_fadeInfo.m_fadeObject = GetComponent <Image>(); m_isActive = false; Clear(); }
/// <summary> /// Update function for the manager. Make sure to update it for all the audio features to work properly. /// </summary> public void Update() { if (m_IsPlayingNaratorVo) { m_NaratorVoTimer += Time.deltaTime; if (m_NaratorVoTimer > m_NaratorVoTrigger) { m_IsPlayingNaratorVo = false; OnVoComplete(); if (m_QueuedNaratorVO.Count > 0) { PlayNaratorVO(m_QueuedNaratorVO.Dequeue(), delegate { }); } } } if (m_FadeList.Count > 0) { for (int i = 0; i < m_FadeList.Count; i++) { FadeInfo info = m_FadeList[i]; FadeTrack(ref info); } } if (m_CurrentAutomaticAudioGroup != null) { m_CurrentAutomaticAudioGroup.Update(); } }
public void FadeOutAll() { StopAllCoroutines(); for (int i = 0; i < transform.GetChild(0).childCount; i++) { myFI = new FadeInfo(i, 1, 0); StartCoroutine("Fade", myFI); } }
/// <summary> /// 初期化 /// </summary> private void Awake() { m_fadeInfo = new FadeInfo(); m_fadeInfo.m_fadeObject = GetComponent <Image>(); m_isActive = false; m_canvas.renderMode = RenderMode.ScreenSpaceCamera; m_canvas.worldCamera = UIManager.Instance.UICamera; m_canvas.sortingLayerName = "System"; Clear(); }
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 }; }
public bool ShouldFadeLed(LedStrip ledStrip, int delay, bool ignoreFullBrightness = false) { if (!ledStrip.IsBrightnessGoingUp() || (!ledStrip.IsFadePlanned() && (ledStrip.GetCurrentBrightness() < ledStrip.GetMaxLevelPwm() || ignoreFullBrightness))) { FadeInfo fadePlan = ledStrip.GetFadePlan(); return(fadePlan == null || fadePlan.GetStartOnMillis() > delay + millis()); } return(false); }
public void ScheduleFadeOut(double fadeStartDspTime, double fadeDuration) { if (fadeStartDspTime < AudioSettings.dspTime) { FadeOutAndStop(fadeDuration); #if GAT_DEBUG Debug.Log("Cannot schedule fade earlier than current dspTime! Fading out now."); #endif return; } _scheduledFade = new FadeInfo(1f, 0f, fadeDuration, fadeStartDspTime); }
private GUIStyle GetNodeStyle(BTNode node) { GUIStyle style = null; if (node != null && bt.program != null && SourceDisplay.isPlaying) { style = BTLSyntaxHighlight.statusStyle[node.status]; FadeInfo fadeInfo = null; if (nodeFadeInfos.ContainsKey(node)) { fadeInfo = nodeFadeInfos[node]; } else { fadeInfo = nodeFadeInfos[node] = new FadeInfo(); fadeInfo.done = true; fadeInfo.startTime = float.NegativeInfinity; fadeInfo.status = node.previousStatus; } if (node.previousStatus != Status.Running && node.status == Status.Ready && node.previousStatus != Status.Ready) { if (fadeInfo.status != node.previousStatus && fadeInfo.done) { fadeInfo.status = node.previousStatus; fadeInfo.startTime = Time.realtimeSinceStartup; fadeInfo.done = false; } float t = Mathf.Clamp01((Time.realtimeSinceStartup - fadeInfo.startTime) / 0.3f); if (t < 1.0f && !isPaused) { style = BTLSyntaxHighlight.statusStyle[node.previousStatus]; } else { fadeInfo.done = true; } } else { fadeInfo.done = true; fadeInfo.status = Status.Ready; } } return(style); }
// - Fade /// <summary> /// Fade to a specified value for a specific channel. Can be used to equialise all the available tracks in the channel uniformly. /// </summary> /// <param name="channel"> The type of the channel to fade</param> /// <param name="toValue"> The value for the channel to fade to (from 0.0f to 1.0f)</param> /// <param name="timeToFade"> Time to reach the fade value</param> /// <param name="onFadeComplete"> OnComplete action for when the reached the fade value</param> public void FadeChannel(ChannelType channel, float toValue, float timeToFade, Action onFadeComplete = null) { FadeInfo info = new FadeInfo(); info.FadeTracks = GetTrackList(channel).ToArray(); info.OriginalVolume = new float[info.FadeTracks.Length]; for (int i = 0; i < info.OriginalVolume.Length; i++) { info.OriginalVolume[i] = info.FadeTracks[i].volume; } info.ToValue = toValue; info.TimeToFade = timeToFade; info.OnFadeComplete = onFadeComplete; m_FadeList.Add(info); }
public void SetFadeInfo(FadeInfo info) { if (info.FromGain < 0f) { _fromGain = _lastGain; } else { _fromGain = info.FromGain; } _toGain = info.ToGain; _duration = info.Duration; _startDspTime = info.StartDspTime; _fadeEndDspTime = _startDspTime + _duration; }
private IEnumerator Fade(FadeInfo fI) { bool done = false; while (!done) { myAudioLayers[fI.layer].volume = Mathf.Lerp(myAudioLayers[fI.layer].volume, fI.volume, (fI.speed * Time.deltaTime)); if (Mathf.Abs(myAudioLayers[fI.layer].volume - fI.volume) <= .02f) { myAudioLayers[fI.layer].volume = fI.volume; //print(myAudioLayers[fI.layer].gameObject.name + "is done and at " + fI.volume); yield break; } yield return(null); } }
protected void Update() { for (int i = 0; i < _fadeList.Count; i++) { FadeInfo fade = _fadeList[i]; if (fade == null || fade.audioSource == null) { _fadeList.RemoveAt(i); i--; continue; } else { Keyframe lastFrame = fade.curve.keys[fade.curve.keys.Length - 1]; if (Time.time >= fade.startTime + lastFrame.time) { fade.audioSource.volume = lastFrame.value; fade.onCompleted?.Invoke(); _fadeList.RemoveAt(i); i--; continue; } else { fade.audioSource.volume = fade.curve.Evaluate(Time.time - fade.startTime); } } } for (int i = 0; i < _playList.Count; i++) { PlayInfo play = _playList[i]; if (play == null || play.audioSource == null) { _playList.RemoveAt(i); i--; continue; } if (!play.audioSource.isPlaying) { play.onCompleted?.Invoke(); _playList.RemoveAt(i); i--; continue; } } }
public void StartFade(FadeInfo fadeInfo) { if (IsFading) { return; } LastFadeInfo = fadeInfo; Overlay.SetOverlayColor(fadeInfo.FromColor); Overlay.EnableOverlay(); DOTween.To(() => Overlay.renderer.material.color, value => Overlay.renderer.material.color = value, fadeInfo.ToColor, fadeInfo.Time) .SetEase(fadeInfo.EaseType) .OnComplete(OnITweenFadeComplete); IsFading = true; OnFadeStarted(new GenericEventArgs <FadeInfo>(fadeInfo)); }
// Fade Helpers private void FadeTrack(ref FadeInfo info) { info.Progress += Time.deltaTime / info.TimeToFade; for (int i = 0; i < info.FadeTracks.Length; i++) { float rate = Mathf.Lerp(info.OriginalVolume[i], info.ToValue, info.Progress); info.FadeTracks[i].volume = rate; } if (info.Progress >= 1.0f) { m_FadeList.Remove(info); if (info.OnFadeComplete != null) { info.OnFadeComplete(); } info = null; } }
public void FadeMusic(AudioChannel source, float endVolume, float fadeSpeed, bool keepChanelAlive = false) { if (source == null) { return; } FadeInfo finfo = FadeChannel(source, endVolume, fadeSpeed); if (!keepChanelAlive) { finfo.freeOnCompletion = (endVolume <= Mathf.Epsilon); } else { finfo.freeOnCompletion = false; } finfo.isMusic = true; }
IEnumerator ImageFadeOutC(FadeInfo info) { info.save_time += Time.unscaledDeltaTime; if (info.img != null) { if (info.save_time < info.time) { float rate = (info.save_time / info.time) * info.rate; info.img.color = new Color(info.img.color.r, info.img.color.g, info.img.color.b, info.rate - rate); yield return(null); StartCoroutine("ImageFadeOutC", info); } else { info.img.color = new Color(info.img.color.r, info.img.color.g, info.img.color.b, 0.0f); } } }
public AudioChannel PlayMusic(AudioClip clip, float startVolume, float endVolume, float fadeSpeed, bool loop = true) { if (fadeSpeed != 0) { fadeSpeed = 1.0f / fadeSpeed; } else { fadeSpeed = 1; } AudioChannel channel = GetFreeChannel(); if (channel != null) { usedChannels.Remove(channel); musicChannels.Add(channel); channel.source.gameObject.SetActive(true); channel.source.loop = loop; channel.source.volume = startVolume * MusicLevel; channel.source.clip = clip; channel.source.pitch = 1.0f; channel.source.Play(); channel.volume = endVolume; channel.isMusic = true; FadeInfo info = new FadeInfo(); info.source = channel.source; info.targetVolume = endVolume; info.channel = channel; info.fadeSpeed = fadeSpeed; info.isMusic = true; fadingChannels.Add(info); } return(channel); }
public void FadeIn(int layer) { myFI = new FadeInfo(layer, 3, 1); StartCoroutine("Fade", myFI); }
public Fader(FadeInfo info) { _lastGain = 1f; SetFadeInfo(info); }
protected override bool PlayerWillMixSample(IGATBufferedSample sample, int length, float[] audioBuffer) { int processedSamples; int i; double dspTime = AudioSettings.dspTime; if (sample.IsFirstChunk) { PlayingStatus = Status.Playing; sample.NextIndex = 1; if (_pitch < 0d) { if (StartPosition == 0) { StartPosition = _dataOwner.AudioData.Count - 2; } } else { if (StartPosition == _dataOwner.AudioData.Count - 2) { StartPosition = 0; } } _dataSource.Seek(StartPosition); if (FadesIn && _fadeInDuration > 0d) { _fader.SetFadeInfo(new FadeInfo(0f, 1f, _fadeInDuration)); _shouldFade = true; } } else if (_scheduledFade != null && _shouldFade == false) { if (dspTime + GATInfo.AudioBufferDuration > _scheduledFade.StartDspTime) { _fader.SetFadeInfo(_scheduledFade); _shouldFade = true; _scheduledFade = null; } } if (_pitch == 1d) { processedSamples = _dataSource.GetData(sample.ProcessingBuffer, length, 0, false); } else if (_pitch == -1d) { processedSamples = _dataSource.GetData(sample.ProcessingBuffer, length, 0, true); } else { processedSamples = _dataSource.GetResampledData(sample.ProcessingBuffer, length, 0, _pitch); } if (StopsEarly) { if (dspTime >= _endDspTime) { _shouldStop = true; } } if (processedSamples < length) { if (_loop) { if (onSampleWillLoop != null) { if (onSampleWillLoop(this) == false) { sample.IsLastChunk = true; goto Processing; } } int seekPos; if (_pitch < 0d) { seekPos = StartPosition == 0 ? _dataOwner.AudioData.Count - 2 : StartPosition; } else { seekPos = StartPosition >= _dataOwner.AudioData.Count - 2 ? 0 : StartPosition; } _dataSource.Seek(seekPos); _dataSource.GetResampledData(sample.ProcessingBuffer, length - processedSamples, processedSamples, _pitch); processedSamples = length; } else { sample.IsLastChunk = true; } } else if (_shouldStop) { sample.ProcessingBuffer.FadeOut(0, length); sample.IsLastChunk = true; _shouldStop = false; } Processing: if (_shouldFade) { if (sample.IsFirstChunk) { dspTime += ( double )sample.OffsetInBuffer / GATInfo.OutputSampleRate; } int fadedSamples = _fader.DoFade(sample.ProcessingBuffer, dspTime, processedSamples); if (fadedSamples < processedSamples) //end of fade { if (_fader.ToGain == 0f) //Faded out, don't mix all! { sample.IsLastChunk = true; processedSamples = fadedSamples; } _shouldFade = false; } } for (i = 0; i < _filters.Count; i++) { _filters[i].ProcessChunk(sample.ProcessingBuffer.ParentArray, sample.ProcessingBuffer.MemOffset, processedSamples, false); } if (ReferenceEquals(sample.Track, null) == false) //sample is played in a track, which will handle Panning via it's own processing buffer. Copy data to the tracks buffer: { sample.Track.MixFrom(sample.ProcessingBuffer, 0, sample.OffsetInBuffer, processedSamples, sample.PlayingGain); } else { sample.PanInfo.PanMixProcessingBuffer(sample, processedSamples, audioBuffer, sample.PlayingGain); } if (sample.IsLastChunk) { PlayingStatus = Status.ReadyToPlay; } return(false); }
// Update is called once per frame void Update() { channelsToFree.Clear(); for (int i = 0; i < usedChannels.Count; i++) { AudioChannel channel = usedChannels[i]; if (!channel.source.isPlaying && !channel.paused) { channelsToFree.Add(channel); } } for (int i = 0; i < channelsToFree.Count; i++) { AudioChannel channel = channelsToFree[i]; usedChannels.Remove(channel); freeChannels.Add(channel); channel.source.gameObject.SetActive(false); } // Update any fading channels fadingChannelsToFree.Clear(); for (int i = 0; i < fadingChannels.Count; i++) { FadeInfo info = fadingChannels[i]; float targetLevel = SoundLevel; if (info.isMusic) { targetLevel = MusicLevel; } if (info.source.volume != info.targetVolume * targetLevel) { float delta = 1; if (info.fadeSpeed != 0) { delta = Main.NonStopTime.deltaTime * info.fadeSpeed; } if ((info.targetVolume * targetLevel) - info.source.volume < 0) { delta *= -1; } //DebugUtils.Log(info.source.clip); info.source.volume += delta;// * NonStopTime.deltaTime; info.channel.volume = info.source.volume; if ((delta > 0 && info.source.volume >= (info.targetVolume * targetLevel)) || (delta < 0 && info.source.volume <= (info.targetVolume * targetLevel))) { info.source.volume = info.targetVolume * targetLevel; fadingChannelsToFree.Add(info); } } else { fadingChannelsToFree.Add(info); } } // Remove fading channels that are finished from queue for (int i = 0; i < fadingChannelsToFree.Count; i++) { FadeInfo info = fadingChannelsToFree[i]; if (info.freeOnCompletion) { info.source.volume = 0; AudioChannel channel = FindChannelFromSource(usedChannels, info.source); if (channel != null) { usedChannels.Remove(channel); channel.volume = 0; channel.source.volume = 0; channel.source.gameObject.SetActive(false); freeChannels.Add(channel); } channel = FindChannelFromSource(musicChannels, info.source); if (channel != null) { musicChannels.Remove(channel); channel.volume = 0; channel.source.volume = 0; channel.source.gameObject.SetActive(false); freeChannels.Add(channel); } } fadingChannels.Remove(info); } }
public void StartFade(FadeInfo fadeInfo) { StartCoroutine (Fade (fadeInfo)); }
public void SetFadeInfo(FadeInfo _fadeInInfo, FadeInfo _displayInfo, FadeInfo _fadeOutInfo) { this.fadeInInfo = _fadeInInfo; this.displayInfo = _displayInfo; this.fadeOutInfo = _fadeOutInfo; }
public void FadeOut(int layer) { myFI = new FadeInfo(layer, 1, 0); StartCoroutine("Fade", myFI); }