Exemple #1
0
 private void HandlePause()
 {
     if (pauseSpeed == 0f)
     {
         return;
     }
     pauseVol = Mathf.Clamp01(pauseVol + pauseSpeed * Time.unscaledDeltaTime);
     if (pauseVol == 0f)
     {
         ReleaseAudioSource(ref inactiveSource, waitToFinish: false);
         if (inactiveClip != null)
         {
             OnClipDeactivated(inactiveClip);
         }
         inactiveClip = null;
         if (activeSource != null && activeSource.isPlaying)
         {
             activeSource.Pause();
             isSourcePaused = true;
         }
         pauseSpeed = 0f;
     }
     else if (pauseVol == 1f)
     {
         pauseSpeed = 0f;
     }
 }
Exemple #2
0
        private void HandleFadeOut()
        {
            fadeOutTime += Time.unscaledDeltaTime;
            if (inactiveSource == null)
            {
                return;
            }
            float num = (fadeOutDuration != 0f) ? (fadeOutTime / fadeOutDuration) : 1f;

            if (num >= 1f)
            {
                fadeInDuration = 0f;
                num            = 1f;
                ReleaseAudioSource(ref inactiveSource, waitToFinish: false);
                if (inactiveClip != null)
                {
                    OnClipDeactivated(inactiveClip);
                }
                inactiveClip = null;
            }
            else
            {
                inactiveSource.volume = Mathf.Sqrt(1f - num) * inactiveVolume * baseVolume * rtVolume * Mathf.Sqrt(pauseVol) * mute;
                inactiveSource.pitch  = inactivePitch * basePitch * rtPitch;
            }
        }
Exemple #3
0
 public void FadeOut(float duration)
 {
     if (activeClip != null)
     {
         if (fadeQueued)
         {
             if (activeClip != null)
             {
                 OnClipDeactivated(activeClip);
             }
             fadeQueued = false;
             activeClip = null;
         }
         else
         {
             if (inactiveClip != null)
             {
                 ReleaseAudioSource(ref inactiveSource, waitToFinish: false);
             }
             if (inactiveClip != null)
             {
                 OnClipDeactivated(inactiveClip);
             }
             inactiveSource = activeSource;
             inactiveClip   = activeClip;
             inactivePitch  = activePitch;
             inactiveVolume = activeVolume;
             activeClip     = null;
             activeSource   = null;
             if (fadeInDuration > 0f)
             {
                 inactiveVolume *= fadeInTime / fadeInDuration;
             }
             fadeOutTime     = 0f;
             fadeOutDuration = duration;
         }
     }
     if (inactiveClip != null)
     {
         if (duration == 0f)
         {
             ReleaseAudioSource(ref inactiveSource, waitToFinish: false);
             OnClipDeactivated(inactiveClip);
             inactiveClip = null;
         }
         else if (fadeOutDuration - fadeOutTime > duration)
         {
             inactiveVolume *= 1f - fadeOutTime / duration;
             fadeOutTime     = 0f;
             fadeOutDuration = duration;
         }
     }
     fadeInDelay    = 0f;
     fadeInDuration = 0f;
     fadeInTime     = 0f;
 }
Exemple #4
0
 public void PlayOneShot(Vector3 pos, float volume = 1f, float pitch = 1f, float delay = 0f)
 {
     if (soundSample != null)
     {
         SoundLibrary.SerializedClip clip = soundSample.GetClip(null, currentChoice, SoundLibrary.SampleContainerChildType.None);
         if (clip != null)
         {
             PlayOneShot(clip, pos, volume, pitch, delay);
         }
     }
 }
Exemple #5
0
 protected virtual void OnSampleLoaded()
 {
     if (isPlaying)
     {
         SoundLibrary.SerializedClip clip = soundSample.GetClip(currentClip, currentChoice, SoundLibrary.SampleContainerChildType.Loop);
         if (clip != null)
         {
             CrossfadeSound(clip, loop: true, 0f, 0.1f);
         }
     }
     else if (startOnLoad)
     {
         Play(forceLoop: true);
     }
 }
Exemple #6
0
 public void Stop()
 {
     startOnLoad = false;
     if (soundSample != null)
     {
         SoundLibrary.SerializedClip clip = soundSample.GetClip(currentClip, currentChoice, SoundLibrary.SampleContainerChildType.Stop);
         if (clip != null)
         {
             CrossfadeSound(clip, loop: false, 0f, soundSample.crossFade);
         }
         else
         {
             FadeOut(soundSample.crossFade);
         }
     }
 }
Exemple #7
0
 public void Switch(char choice, bool crossfade = true)
 {
     if (currentChoice == choice)
     {
         return;
     }
     currentChoice = choice;
     if (soundSample != null && isPlaying && crossfade)
     {
         SoundLibrary.SerializedClip serializedClip = currentClip = soundSample.GetClip(currentClip, currentChoice, SoundLibrary.SampleContainerChildType.Loop);
         if (serializedClip != null)
         {
             CrossfadeSound(serializedClip, loop: true, 0f, soundSample.crossFade);
         }
     }
 }
Exemple #8
0
 public void FadeIn(SoundLibrary.SerializedClip clip, bool loop, float after, float duration)
 {
     if (activeClip != null)
     {
         OnClipDeactivated(activeClip);
     }
     fadeQueued = true;
     activeClip = clip;
     OnClipActivating(activeClip);
     activeVolume   = clip.volume * soundSample.volume;
     activePitch    = clip.pitch * soundSample.pitch;
     activeLoop     = loop;
     fadeInDelay    = after;
     fadeInDuration = duration;
     fadeInTime     = 0f;
     HandleFadeIn();
 }
Exemple #9
0
        public void PlayOneShot(SoundLibrary.SerializedClip clip, Vector3 pos, float volume = 1f, float pitch = 1f, float delay = 0f)
        {
            AudioSource source = AcquireAudioSource(pos);

            source.clip   = clip.clip;
            source.volume = volume * clip.volume * soundSample.volume * baseVolume * rtVolume * pauseVol * mute;
            source.pitch  = pitch * clip.pitch * soundSample.pitch * basePitch * rtPitch;
            source.loop   = false;
            if (delay == 0f)
            {
                source.Play();
            }
            else
            {
                source.PlayDelayed(delay);
            }
            ReleaseAudioSource(ref source, waitToFinish: true);
        }
Exemple #10
0
 public void Play(bool forceLoop = false)
 {
     startOnLoad = true;
     if (soundSample == null || !soundSample.loaded)
     {
         return;
     }
     SoundLibrary.SerializedClip serializedClip = currentClip = soundSample.GetClip(null, currentChoice, SoundLibrary.SampleContainerChildType.Start);
     if (currentClip != null)
     {
         PlaySound(currentClip, forceLoop && !soundSample.isLoop);
     }
     if (forceLoop || soundSample.isLoop)
     {
         serializedClip = soundSample.GetClip(currentClip, currentChoice, SoundLibrary.SampleContainerChildType.Loop);
         if (serializedClip != null)
         {
             CrossfadeSound(serializedClip, loop: true, 0f, soundSample.crossFade);
         }
     }
 }
Exemple #11
0
 public virtual void OnClipActivating(SoundLibrary.SerializedClip clip)
 {
 }
Exemple #12
0
 public virtual void OnClipDeactivated(SoundLibrary.SerializedClip clip)
 {
 }
Exemple #13
0
 public void CrossfadeSound(SoundLibrary.SerializedClip clip, bool loop, float after, float duration, float outDuration, float inDuration)
 {
     FadeOut(outDuration);
     FadeIn(clip, loop, duration - inDuration, inDuration);
 }
Exemple #14
0
 public void PlaySound(SoundLibrary.SerializedClip clip, bool loop)
 {
     FadeIn(clip, loop, 0f, 0f);
 }