| 1 | using UnityEngine; |
| 2 | using UnityEngine.Audio; |
| 3 | using System.Collections; |
| 4 | |
| 5 | namespace OVR |
| 6 | { |
| 7 | |
| 8 | /* |
| 9 | ----------------------- |
| 10 | |
| 11 | SoundEmitter() |
| 12 | |
| 13 | ----------------------- |
| 14 | */ |
| 15 | public class SoundEmitter : MonoBehaviour { |
| 16 | public enum FadeState { |
| 17 | Null, |
| 18 | FadingIn, |
| 19 | FadingOut, |
| 20 | Ducking, |
| 21 | } |
| 22 | |
| 23 | // OPTIMIZE |
| 24 | |
| 25 | public float volume { get { return audioSource.volume; } set { audioSource.volume = value; } } |
| 26 | public float pitch { get { return audioSource.pitch; } set { audioSource.pitch = value; } } |
| 27 | public AudioClip clip { get { return audioSource.clip; } set { audioSource.clip = value; } } |
| 28 | public float time { get { return audioSource.time; } set { audioSource.time = value; } } |
| 29 | public float length { get { return ( audioSource.clip != null ) ? audioSource.clip.length : 0.0f; } } |
| 30 | public bool loop { get { return audioSource.loop; } set { audioSource.loop = value; } } |
| 31 | public bool mute { get { return audioSource.mute; } set { audioSource.mute = value; } } |
| 32 | public AudioVelocityUpdateMode velocityUpdateMode { get { return audioSource.velocityUpdateMode; } set { audioSource.velocityUpdateMode = value; } } |
| 33 | public bool isPlaying { get { return audioSource.isPlaying; } } |
| 34 | |
| 35 | public EmitterChannel channel = EmitterChannel.Reserved; |
| 36 | public bool disableSpatialization = false; |
| 37 | private FadeState state = FadeState.Null; |
| 38 | [System.NonSerialized] |
| 39 | [HideInInspector] |
| 40 | public AudioSource audioSource = null; |
| 41 | [System.NonSerialized] |
| 42 | [HideInInspector] |
| 43 | public SoundPriority priority = SoundPriority.Default; |
| 44 | [System.NonSerialized] |
| 45 | [HideInInspector] |
| 46 | public ONSPAudioSource osp = null; |
| 47 | [System.NonSerialized] |
| 48 | [HideInInspector] |
| 49 | public float endPlayTime = 0.0f; |
| 50 | private Transform lastParentTransform = null; |
| 51 | [System.NonSerialized] |
| 52 | [HideInInspector] |
| 53 | public float defaultVolume = 1.0f; |
| 54 | [System.NonSerialized] |
| 55 | [HideInInspector] |
| 56 | public Transform defaultParent = null; |
| 57 | [System.NonSerialized] |
| 58 | [HideInInspector] |
| 59 | public int originalIdx = -1; |
| 60 | [System.NonSerialized] |
| 61 | [HideInInspector] |
| 62 | public System.Action onFinished = null; |
| 63 | [System.NonSerialized] |
| 64 | [HideInInspector] |
| 65 | public System.Action<object> onFinishedObject = null; |
| 66 | [System.NonSerialized] |
| 67 | [HideInInspector] |
| 68 | public object onFinishedParam; |
| 69 | [System.NonSerialized] |
| 70 | [HideInInspector] |
| 71 | public SoundGroup playingSoundGroup = null; |
| 72 | |
| 73 | /* |
| 74 | ----------------------- |
| 75 | Awake() |
| 76 | ----------------------- |
| 77 | */ |
| 78 | void Awake() { |
| 79 | // unity defaults to 'playOnAwake = true' |
| 80 | audioSource = GetComponent<AudioSource>(); |
| 81 | if ( audioSource == null ) { |
| 82 | audioSource = gameObject.AddComponent<AudioSource>(); |
| 83 | } |
| 84 | // is the spatialized audio enabled? |
| 85 | if ( AudioManager.enableSpatialization && !disableSpatialization ) { |
| 86 | osp = GetComponent<ONSPAudioSource>(); |
| 87 | if ( osp == null ) { |
| 88 | osp = gameObject.AddComponent<ONSPAudioSource>(); |
| 89 | } |
| 90 | } |
| 91 | audioSource.playOnAwake = false; |
| 92 | audioSource.Stop(); |
| 93 | } |
| 94 | |
| 95 | /* |
| 96 | ----------------------- |
| 97 | SetPlayingSoundGroup() |
| 98 | ----------------------- |
| 99 | */ |
| 100 | public void SetPlayingSoundGroup( SoundGroup soundGroup ) { |
| 101 | playingSoundGroup = soundGroup; |
| 102 | if ( soundGroup != null ) { |
| 103 | soundGroup.IncrementPlayCount(); |
| 104 | } |
| 105 | } |
| 106 | |
| 107 | /* |
| 108 | ----------------------- |
| 109 | SetOnFinished() |
| 110 | ----------------------- |
| 111 | */ |
| 112 | public void SetOnFinished( System.Action onFinished ) { |
| 113 | this.onFinished = onFinished; |
| 114 | } |
| 115 | |
| 116 | /* |
| 117 | ----------------------- |
| 118 | SetOnFinished() |
| 119 | ----------------------- |
| 120 | */ |
| 121 | public void SetOnFinished( System.Action<object> onFinished, object obj ) { |
| 122 | onFinishedObject = onFinished; |
| 123 | onFinishedParam = obj; |
| 124 | } |
| 125 | |
| 126 | /* |
| 127 | ----------------------- |
| 128 | SetChannel() |
| 129 | ----------------------- |
| 130 | */ |
| 131 | public void SetChannel( int _channel ) { |
| 132 | channel = (EmitterChannel)_channel; |
| 133 | } |
| 134 | |
| 135 | /* |
| 136 | ----------------------- |
| 137 | SetDefaultParent() |
| 138 | ----------------------- |
| 139 | */ |
| 140 | public void SetDefaultParent( Transform parent ) { |
| 141 | defaultParent = parent; |
| 142 | } |
| 143 | |
| 144 | /* |
| 145 | ----------------------- |
| 146 | SetAudioMixer() |
| 147 | ----------------------- |
| 148 | */ |
| 149 | public void SetAudioMixer( AudioMixerGroup _mixer ) { |
| 150 | if ( audioSource != null ) { |
| 151 | audioSource.outputAudioMixerGroup = _mixer; |
| 152 | } |
| 153 | } |
| 154 | |
| 155 | /* |
| 156 | ----------------------- |
| 157 | IsPlaying() |
| 158 | ----------------------- |
| 159 | */ |
| 160 | public bool IsPlaying() { |
| 161 | if ( loop && audioSource.isPlaying ) { |
| 162 | return true; |
| 163 | } |
| 164 | return endPlayTime > Time.time; |
| 165 | } |
| 166 | |
| 167 | /* |
| 168 | ----------------------- |
| 169 | Play() |
| 170 | ----------------------- |
| 171 | */ |
| 172 | public void Play() { |
| 173 | // overrides everything |
| 174 | state = FadeState.Null; |
| 175 | endPlayTime = Time.time + length; |
| 176 | StopAllCoroutines(); |
| 177 | audioSource.Play(); |
| 178 | } |
| 179 | |
| 180 | /* |
| 181 | ----------------------- |
| 182 | Pause() |
| 183 | ----------------------- |
| 184 | */ |
| 185 | public void Pause() { |
| 186 | // overrides everything |
| 187 | state = FadeState.Null; |
| 188 | StopAllCoroutines(); |
| 189 | audioSource.Pause(); |
| 190 | } |
| 191 | |
| 192 | /* |
| 193 | ----------------------- |
| 194 | Stop() |
| 195 | ----------------------- |
| 196 | */ |
| 197 | public void Stop() { |
| 198 | // overrides everything |
| 199 | state = FadeState.Null; |
| 200 | StopAllCoroutines(); |
| 201 | if ( audioSource != null ) { |
| 202 | audioSource.Stop(); |
| 203 | } |
| 204 | if ( onFinished != null ) { |
| 205 | onFinished(); |
| 206 | onFinished = null; |
| 207 | } |
| 208 | if ( onFinishedObject != null ) { |
| 209 | onFinishedObject( onFinishedParam ); |
| 210 | onFinishedObject = null; |
| 211 | } |
| 212 | if ( playingSoundGroup != null ) { |
| 213 | playingSoundGroup.DecrementPlayCount(); |
| 214 | playingSoundGroup = null; |
| 215 | } |
| 216 | } |
| 217 | |
| 218 | /* |
| 219 | ----------------------- |
| 220 | GetSampleTime() |
| 221 | ----------------------- |
| 222 | */ |
| 223 | int GetSampleTime() { |
| 224 | return audioSource.clip.samples - audioSource.timeSamples; |
| 225 | } |
| 226 | |
| 227 | /* |
| 228 | ----------------------- |
| 229 | ParentTo() |
| 230 | ----------------------- |
| 231 | */ |
| 232 | public void ParentTo( Transform parent ) { |
| 233 | if ( lastParentTransform != null ) { |
| 234 | Debug.LogError( "[SoundEmitter] You must detach the sound emitter before parenting to another object!" ); |
| 235 | return; |
| 236 | } |
| 237 | lastParentTransform = transform.parent; |
| 238 | transform.parent = parent; |
| 239 | } |
| 240 | |
| 241 | /* |
| 242 | ----------------------- |
| 243 | DetachFromParent() |
| 244 | ----------------------- |
| 245 | */ |
| 246 | public void DetachFromParent() { |
| 247 | if ( lastParentTransform == null ) { |
| 248 | transform.parent = defaultParent; |
| 249 | return; |
| 250 | } |
| 251 | transform.parent = lastParentTransform; |
| 252 | lastParentTransform = null; |
| 253 | } |
| 254 | |
| 255 | /* |
| 256 | ----------------------- |
| 257 | ResetParent() |
| 258 | ----------------------- |
| 259 | */ |
| 260 | public void ResetParent( Transform parent ) { |
| 261 | transform.parent = parent; |
| 262 | lastParentTransform = null; |
| 263 | } |
| 264 | |
| 265 | /* |
| 266 | ----------------------- |
| 267 | SyncTo() |
| 268 | ----------------------- |
| 269 | */ |
| 270 | public void SyncTo( SoundEmitter other, float fadeTime, float toVolume ) { |
| 271 | StartCoroutine( DelayedSyncTo( other, fadeTime, toVolume ) ); |
| 272 | } |
| 273 | |
| 274 | /* |
| 275 | ----------------------- |
| 276 | DelayedSyncTo() |
| 277 | have to wait until the end of frame to do proper sync'ing |
| 278 | ----------------------- |
| 279 | */ |
| 280 | IEnumerator DelayedSyncTo( SoundEmitter other, float fadeTime, float toVolume ) { |
| 281 | yield return new WaitForEndOfFrame(); |
| 282 | //audio.timeSamples = other.GetSampleTime(); |
| 283 | //audio.time = Mathf.Min( Mathf.Max( 0.0f, other.time - other.length ), other.time ); |
| 284 | audioSource.time = other.time; |
| 285 | audioSource.Play(); |
| 286 | FadeTo( fadeTime, toVolume ); |
| 287 | } |
| 288 | |
| 289 | /* |
| 290 | ----------------------- |
| 291 | FadeTo() |
| 292 | ----------------------- |
| 293 | */ |
| 294 | public void FadeTo( float fadeTime, float toVolume ) { |
| 295 | //Log.Print( ">>> FADE TO: " + channel ); |
| 296 | |
| 297 | |
| 298 | // don't override a fade out |
| 299 | if ( state == FadeState.FadingOut ) { |
| 300 | //Log.Print( " ....ABORTED" ); |
| 301 | return; |
| 302 | } |
| 303 | state = FadeState.Ducking; |
| 304 | StopAllCoroutines(); |
| 305 | StartCoroutine( FadeSoundChannelTo( fadeTime, toVolume ) ); |
| 306 | } |
| 307 | |
| 308 | /* |
| 309 | ----------------------- |
| 310 | FadeIn() |
| 311 | ----------------------- |
| 312 | */ |
| 313 | public void FadeIn( float fadeTime, float defaultVolume ) { |
| 314 | |
| 315 | //Log.Print( ">>> FADE IN: " + channel ); |
| 316 | audioSource.volume = 0.0f; |
| 317 | state = FadeState.FadingIn; |
| 318 | StopAllCoroutines(); |
| 319 | StartCoroutine( FadeSoundChannel( 0.0f, fadeTime, Fade.In, defaultVolume ) ); |
| 320 | } |
| 321 | |
| 322 | /* |
| 323 | ----------------------- |
| 324 | FadeIn() |
| 325 | ----------------------- |
| 326 | */ |
| 327 | public void FadeIn( float fadeTime ) { |
| 328 | |
| 329 | //Log.Print( ">>> FADE IN: " + channel ); |
| 330 | audioSource.volume = 0.0f; |
| 331 | state = FadeState.FadingIn; |
| 332 | StopAllCoroutines(); |
| 333 | StartCoroutine( FadeSoundChannel( 0.0f, fadeTime, Fade.In, defaultVolume ) ); |
| 334 | } |
| 335 | |
| 336 | /* |
| 337 | ----------------------- |
| 338 | FadeOut() |
| 339 | ----------------------- |
| 340 | */ |
| 341 | public void FadeOut( float fadeTime ) { |
| 342 | //Log.Print( ">>> FADE OUT: " + channel ); |
| 343 | if ( !audioSource.isPlaying ) { |
| 344 | //Log.Print( " ... SKIPPING" ); |
| 345 | return; |
| 346 | } |
| 347 | state = FadeState.FadingOut; |
| 348 | StopAllCoroutines(); |
| 349 | StartCoroutine( FadeSoundChannel( 0.0f, fadeTime, Fade.Out, audioSource.volume ) ); |
| 350 | } |
| 351 | |
| 352 | /* |
| 353 | ----------------------- |
| 354 | FadeOutDelayed() |
| 355 | ----------------------- |
| 356 | */ |
| 357 | public void FadeOutDelayed( float delayedSecs, float fadeTime ) { |
| 358 | //Log.Print( ">>> FADE OUT DELAYED: " + channel ); |
| 359 | if ( !audioSource.isPlaying ) { |
| 360 | //Log.Print( " ... SKIPPING" ); |
| 361 | return; |
| 362 | } |
| 363 | state = FadeState.FadingOut; |
| 364 | StopAllCoroutines(); |
| 365 | StartCoroutine( FadeSoundChannel( delayedSecs, fadeTime, Fade.Out, audioSource.volume ) ); |
| 366 | } |
| 367 | |
| 368 | /* |
| 369 | ----------------------- |
| 370 | FadeSoundChannelTo() |
| 371 | ----------------------- |
| 372 | */ |
| 373 | IEnumerator FadeSoundChannelTo( float fadeTime, float toVolume ) { |
| 374 | float start = audioSource.volume; |
| 375 | float end = toVolume; |
| 376 | float startTime = Time.realtimeSinceStartup; |
| 377 | float elapsedTime = 0.0f; |
| 378 | |
| 379 | while ( elapsedTime < fadeTime ) { |
| 380 | elapsedTime = Time.realtimeSinceStartup - startTime; |
| 381 | float t = elapsedTime / fadeTime; |
| 382 | audioSource.volume = Mathf.Lerp( start, end, t ); |
| 383 | yield return 0; |
| 384 | } |
| 385 | state = FadeState.Null; |
| 386 | } |
| 387 | |
| 388 | /* |
| 389 | ----------------------- |
| 390 | FadeSoundChannel() |
| 391 | ----------------------- |
| 392 | */ |
| 393 | IEnumerator FadeSoundChannel( float delaySecs, float fadeTime, Fade fadeType, float defaultVolume ) { |
| 394 | if ( delaySecs > 0.0f ) { |
| 395 | yield return new WaitForSeconds( delaySecs ); |
| 396 | } |
| 397 | float start = ( fadeType == Fade.In ) ? 0.0f : defaultVolume; |
| 398 | float end = ( fadeType == Fade.In ) ? defaultVolume : 0.0f; |
| 399 | bool restartPlay = false; |
| 400 | |
| 401 | if ( fadeType == Fade.In ) { |
| 402 | if ( Time.time == 0.0f ) { |
| 403 | restartPlay = true; |
| 404 | } |
| 405 | audioSource.volume = 0.0f; |
| 406 | audioSource.Play(); |
| 407 | } |
| 408 | |
| 409 | float startTime = Time.realtimeSinceStartup; |
| 410 | float elapsedTime = 0.0f; |
| 411 | |
| 412 | while ( elapsedTime < fadeTime ) { |
| 413 | elapsedTime = Time.realtimeSinceStartup - startTime; |
| 414 | float t = elapsedTime / fadeTime; |
| 415 | audioSource.volume = Mathf.Lerp( start, end, t ); |
| 416 | yield return 0; |
| 417 | if ( restartPlay && ( Time.time > 0.0f ) ) { |
| 418 | audioSource.Play(); |
| 419 | restartPlay = false; |
| 420 | } |
| 421 | if ( !audioSource.isPlaying ) { |
| 422 | break; |
| 423 | } |
| 424 | } |
| 425 | |
| 426 | if ( fadeType == Fade.Out ) { |
| 427 | Stop(); |
| 428 | } |
| 429 | state = FadeState.Null; |
| 430 | } |
| 431 | } |
| 432 | |
| 433 | } // namespace OVR |