123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433 |
- using UnityEngine;
- using UnityEngine.Audio;
- using System.Collections;
- namespace OVR
- {
- /*
- -----------------------
-
- SoundEmitter()
-
- -----------------------
- */
- public class SoundEmitter : MonoBehaviour {
- public enum FadeState {
- Null,
- FadingIn,
- FadingOut,
- Ducking,
- }
- // OPTIMIZE
- public float volume { get { return audioSource.volume; } set { audioSource.volume = value; } }
- public float pitch { get { return audioSource.pitch; } set { audioSource.pitch = value; } }
- public AudioClip clip { get { return audioSource.clip; } set { audioSource.clip = value; } }
- public float time { get { return audioSource.time; } set { audioSource.time = value; } }
- public float length { get { return ( audioSource.clip != null ) ? audioSource.clip.length : 0.0f; } }
- public bool loop { get { return audioSource.loop; } set { audioSource.loop = value; } }
- public bool mute { get { return audioSource.mute; } set { audioSource.mute = value; } }
- public AudioVelocityUpdateMode velocityUpdateMode { get { return audioSource.velocityUpdateMode; } set { audioSource.velocityUpdateMode = value; } }
- public bool isPlaying { get { return audioSource.isPlaying; } }
- public EmitterChannel channel = EmitterChannel.Reserved;
- public bool disableSpatialization = false;
- private FadeState state = FadeState.Null;
- [System.NonSerialized]
- [HideInInspector]
- public AudioSource audioSource = null;
- [System.NonSerialized]
- [HideInInspector]
- public SoundPriority priority = SoundPriority.Default;
- [System.NonSerialized]
- [HideInInspector]
- public ONSPAudioSource osp = null;
- [System.NonSerialized]
- [HideInInspector]
- public float endPlayTime = 0.0f;
- private Transform lastParentTransform = null;
- [System.NonSerialized]
- [HideInInspector]
- public float defaultVolume = 1.0f;
- [System.NonSerialized]
- [HideInInspector]
- public Transform defaultParent = null;
- [System.NonSerialized]
- [HideInInspector]
- public int originalIdx = -1;
- [System.NonSerialized]
- [HideInInspector]
- public System.Action onFinished = null;
- [System.NonSerialized]
- [HideInInspector]
- public System.Action<object> onFinishedObject = null;
- [System.NonSerialized]
- [HideInInspector]
- public object onFinishedParam;
- [System.NonSerialized]
- [HideInInspector]
- public SoundGroup playingSoundGroup = null;
- /*
- -----------------------
- Awake()
- -----------------------
- */
- void Awake() {
- // unity defaults to 'playOnAwake = true'
- audioSource = GetComponent<AudioSource>();
- if ( audioSource == null ) {
- audioSource = gameObject.AddComponent<AudioSource>();
- }
- // is the spatialized audio enabled?
- if ( AudioManager.enableSpatialization && !disableSpatialization ) {
- osp = GetComponent<ONSPAudioSource>();
- if ( osp == null ) {
- osp = gameObject.AddComponent<ONSPAudioSource>();
- }
- }
- audioSource.playOnAwake = false;
- audioSource.Stop();
- }
- /*
- -----------------------
- SetPlayingSoundGroup()
- -----------------------
- */
- public void SetPlayingSoundGroup( SoundGroup soundGroup ) {
- playingSoundGroup = soundGroup;
- if ( soundGroup != null ) {
- soundGroup.IncrementPlayCount();
- }
- }
- /*
- -----------------------
- SetOnFinished()
- -----------------------
- */
- public void SetOnFinished( System.Action onFinished ) {
- this.onFinished = onFinished;
- }
- /*
- -----------------------
- SetOnFinished()
- -----------------------
- */
- public void SetOnFinished( System.Action<object> onFinished, object obj ) {
- onFinishedObject = onFinished;
- onFinishedParam = obj;
- }
- /*
- -----------------------
- SetChannel()
- -----------------------
- */
- public void SetChannel( int _channel ) {
- channel = (EmitterChannel)_channel;
- }
- /*
- -----------------------
- SetDefaultParent()
- -----------------------
- */
- public void SetDefaultParent( Transform parent ) {
- defaultParent = parent;
- }
- /*
- -----------------------
- SetAudioMixer()
- -----------------------
- */
- public void SetAudioMixer( AudioMixerGroup _mixer ) {
- if ( audioSource != null ) {
- audioSource.outputAudioMixerGroup = _mixer;
- }
- }
- /*
- -----------------------
- IsPlaying()
- -----------------------
- */
- public bool IsPlaying() {
- if ( loop && audioSource.isPlaying ) {
- return true;
- }
- return endPlayTime > Time.time;
- }
- /*
- -----------------------
- Play()
- -----------------------
- */
- public void Play() {
- // overrides everything
- state = FadeState.Null;
- endPlayTime = Time.time + length;
- StopAllCoroutines();
- audioSource.Play();
- }
- /*
- -----------------------
- Pause()
- -----------------------
- */
- public void Pause() {
- // overrides everything
- state = FadeState.Null;
- StopAllCoroutines();
- audioSource.Pause();
- }
-
- /*
- -----------------------
- Stop()
- -----------------------
- */
- public void Stop() {
- // overrides everything
- state = FadeState.Null;
- StopAllCoroutines();
- if ( audioSource != null ) {
- audioSource.Stop();
- }
- if ( onFinished != null ) {
- onFinished();
- onFinished = null;
- }
- if ( onFinishedObject != null ) {
- onFinishedObject( onFinishedParam );
- onFinishedObject = null;
- }
- if ( playingSoundGroup != null ) {
- playingSoundGroup.DecrementPlayCount();
- playingSoundGroup = null;
- }
- }
- /*
- -----------------------
- GetSampleTime()
- -----------------------
- */
- int GetSampleTime() {
- return audioSource.clip.samples - audioSource.timeSamples;
- }
- /*
- -----------------------
- ParentTo()
- -----------------------
- */
- public void ParentTo( Transform parent ) {
- if ( lastParentTransform != null ) {
- Debug.LogError( "[SoundEmitter] You must detach the sound emitter before parenting to another object!" );
- return;
- }
- lastParentTransform = transform.parent;
- transform.parent = parent;
- }
- /*
- -----------------------
- DetachFromParent()
- -----------------------
- */
- public void DetachFromParent() {
- if ( lastParentTransform == null ) {
- transform.parent = defaultParent;
- return;
- }
- transform.parent = lastParentTransform;
- lastParentTransform = null;
- }
- /*
- -----------------------
- ResetParent()
- -----------------------
- */
- public void ResetParent( Transform parent ) {
- transform.parent = parent;
- lastParentTransform = null;
- }
- /*
- -----------------------
- SyncTo()
- -----------------------
- */
- public void SyncTo( SoundEmitter other, float fadeTime, float toVolume ) {
- StartCoroutine( DelayedSyncTo( other, fadeTime, toVolume ) );
- }
- /*
- -----------------------
- DelayedSyncTo()
- have to wait until the end of frame to do proper sync'ing
- -----------------------
- */
- IEnumerator DelayedSyncTo( SoundEmitter other, float fadeTime, float toVolume ) {
- yield return new WaitForEndOfFrame();
- //audio.timeSamples = other.GetSampleTime();
- //audio.time = Mathf.Min( Mathf.Max( 0.0f, other.time - other.length ), other.time );
- audioSource.time = other.time;
- audioSource.Play();
- FadeTo( fadeTime, toVolume );
- }
- /*
- -----------------------
- FadeTo()
- -----------------------
- */
- public void FadeTo( float fadeTime, float toVolume ) {
- //Log.Print( ">>> FADE TO: " + channel );
-
- // don't override a fade out
- if ( state == FadeState.FadingOut ) {
- //Log.Print( " ....ABORTED" );
- return;
- }
- state = FadeState.Ducking;
- StopAllCoroutines();
- StartCoroutine( FadeSoundChannelTo( fadeTime, toVolume ) );
- }
-
- /*
- -----------------------
- FadeIn()
- -----------------------
- */
- public void FadeIn( float fadeTime, float defaultVolume ) {
-
- //Log.Print( ">>> FADE IN: " + channel );
- audioSource.volume = 0.0f;
- state = FadeState.FadingIn;
- StopAllCoroutines();
- StartCoroutine( FadeSoundChannel( 0.0f, fadeTime, Fade.In, defaultVolume ) );
- }
- /*
- -----------------------
- FadeIn()
- -----------------------
- */
- public void FadeIn( float fadeTime ) {
- //Log.Print( ">>> FADE IN: " + channel );
- audioSource.volume = 0.0f;
- state = FadeState.FadingIn;
- StopAllCoroutines();
- StartCoroutine( FadeSoundChannel( 0.0f, fadeTime, Fade.In, defaultVolume ) );
- }
- /*
- -----------------------
- FadeOut()
- -----------------------
- */
- public void FadeOut( float fadeTime ) {
- //Log.Print( ">>> FADE OUT: " + channel );
- if ( !audioSource.isPlaying ) {
- //Log.Print( " ... SKIPPING" );
- return;
- }
- state = FadeState.FadingOut;
- StopAllCoroutines();
- StartCoroutine( FadeSoundChannel( 0.0f, fadeTime, Fade.Out, audioSource.volume ) );
- }
-
- /*
- -----------------------
- FadeOutDelayed()
- -----------------------
- */
- public void FadeOutDelayed( float delayedSecs, float fadeTime ) {
- //Log.Print( ">>> FADE OUT DELAYED: " + channel );
- if ( !audioSource.isPlaying ) {
- //Log.Print( " ... SKIPPING" );
- return;
- }
- state = FadeState.FadingOut;
- StopAllCoroutines();
- StartCoroutine( FadeSoundChannel( delayedSecs, fadeTime, Fade.Out, audioSource.volume ) );
- }
-
- /*
- -----------------------
- FadeSoundChannelTo()
- -----------------------
- */
- IEnumerator FadeSoundChannelTo( float fadeTime, float toVolume ) {
- float start = audioSource.volume;
- float end = toVolume;
- float startTime = Time.realtimeSinceStartup;
- float elapsedTime = 0.0f;
- while ( elapsedTime < fadeTime ) {
- elapsedTime = Time.realtimeSinceStartup - startTime;
- float t = elapsedTime / fadeTime;
- audioSource.volume = Mathf.Lerp( start, end, t );
- yield return 0;
- }
- state = FadeState.Null;
- }
-
- /*
- -----------------------
- FadeSoundChannel()
- -----------------------
- */
- IEnumerator FadeSoundChannel( float delaySecs, float fadeTime, Fade fadeType, float defaultVolume ) {
- if ( delaySecs > 0.0f ) {
- yield return new WaitForSeconds( delaySecs );
- }
- float start = ( fadeType == Fade.In ) ? 0.0f : defaultVolume;
- float end = ( fadeType == Fade.In ) ? defaultVolume : 0.0f;
- bool restartPlay = false;
-
- if ( fadeType == Fade.In ) {
- if ( Time.time == 0.0f ) {
- restartPlay = true;
- }
- audioSource.volume = 0.0f;
- audioSource.Play();
- }
- float startTime = Time.realtimeSinceStartup;
- float elapsedTime = 0.0f;
- while ( elapsedTime < fadeTime ) {
- elapsedTime = Time.realtimeSinceStartup - startTime;
- float t = elapsedTime / fadeTime;
- audioSource.volume = Mathf.Lerp( start, end, t );
- yield return 0;
- if ( restartPlay && ( Time.time > 0.0f ) ) {
- audioSource.Play();
- restartPlay = false;
- }
- if ( !audioSource.isPlaying ) {
- break;
- }
- }
- if ( fadeType == Fade.Out ) {
- Stop();
- }
- state = FadeState.Null;
- }
- }
- } // namespace OVR
|