123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787 |
- using UnityEngine;
- using UnityEngine.Audio;
- namespace OVR
- {
- //-------------------------------------------------------------------------
- // Types
- //-------------------------------------------------------------------------
- public enum EmitterChannel {
- None = -1,
- Reserved = 0, // plays on the single reserved emitter
- Any // queues to the next available emitter
- }
- [System.Serializable]
- public class MixerSnapshot {
- public AudioMixerSnapshot snapshot = null;
- public float transitionTime = 0.25f;
- }
- /*
- -----------------------
-
- GameManager Sound Routines
- -----------------------
- */
- public partial class AudioManager : MonoBehaviour {
- public enum Fade {
- In,
- Out
- }
- private float audioMaxFallOffDistanceSqr = 25.0f * 25.0f; // past this distance, sounds are ignored for the local player
- private SoundEmitter[] soundEmitters = null; // pool of sound emitters to play sounds through
- private FastList<SoundEmitter> playingEmitters = new FastList<SoundEmitter>();
- private FastList<SoundEmitter> nextFreeEmitters = new FastList<SoundEmitter>();
- private MixerSnapshot currentSnapshot = null;
- static private GameObject soundEmitterParent = null; // parent object for the sound emitters
- static private Transform staticListenerPosition = null; // play position for regular 2D sounds
- static private bool showPlayingEmitterCount = false;
- static private bool forceShowEmitterCount = false;
- static private bool soundEnabled = true;
- static public bool SoundEnabled { get { return soundEnabled; } }
- static readonly AnimationCurve defaultReverbZoneMix = new AnimationCurve( new Keyframe[2] { new Keyframe( 0f, 1.0f ), new Keyframe( 1f, 1f ) } );
- /*
- -----------------------
- InitializeSoundSystem()
- initialize persistent sound emitter objects that live across scene loads
- -----------------------
- */
- void InitializeSoundSystem() {
- int bufferLength = 960;
- int numBuffers = 4;
- AudioSettings.GetDSPBufferSize( out bufferLength, out numBuffers );
- if ( Application.isPlaying ) {
- Debug.Log( "[AudioManager] Audio Sample Rate: " + AudioSettings.outputSampleRate );
- Debug.Log( "[AudioManager] Audio Buffer Length: " + bufferLength + " Size: " + numBuffers );
- }
- // find the audio listener for playing regular 2D sounds
- AudioListener audioListenerObject = GameObject.FindObjectOfType<AudioListener>() as AudioListener;
- if ( audioListenerObject == null ) {
- Debug.LogError( "[AudioManager] Missing AudioListener object! Add one to the scene." );
- } else {
- staticListenerPosition = audioListenerObject.transform;
- }
- // we allocate maxSoundEmitters + reserved channels
- soundEmitters = new SoundEmitter[maxSoundEmitters+(int)EmitterChannel.Any];
-
- // see if the sound emitters have already been created, if so, nuke it, it shouldn't exist in the scene upon load
- soundEmitterParent = GameObject.Find( "__SoundEmitters__" );
- if ( soundEmitterParent != null ) {
- // delete any sound emitters hanging around
- Destroy( soundEmitterParent );
- }
- // create them all
- soundEmitterParent = new GameObject( "__SoundEmitters__" );
- for ( int i = 0; i < maxSoundEmitters + (int)EmitterChannel.Any; i++ ) {
- GameObject emitterObject = new GameObject( "SoundEmitter_" + i );
- emitterObject.transform.parent = soundEmitterParent.transform;
- emitterObject.transform.position = Vector3.zero;
- // don't ever save this to the scene
- emitterObject.hideFlags = HideFlags.DontSaveInEditor;
- // add the sound emitter components
- soundEmitters[i] = emitterObject.AddComponent<SoundEmitter>();
- soundEmitters[i].SetDefaultParent( soundEmitterParent.transform );
- soundEmitters[i].SetChannel( i );
- soundEmitters[i].Stop();
- // save off the original index
- soundEmitters[i].originalIdx = i;
- }
- // reset the free emitter lists
- ResetFreeEmitters();
- soundEmitterParent.hideFlags = HideFlags.DontSaveInEditor;
-
- audioMaxFallOffDistanceSqr = audioMaxFallOffDistance * audioMaxFallOffDistance;
- }
- /*
- -----------------------
- UpdateFreeEmitters()
- -----------------------
- */
- void UpdateFreeEmitters() {
- if ( verboseLogging ) {
- if ( Input.GetKeyDown( KeyCode.A ) ) {
- forceShowEmitterCount = !forceShowEmitterCount;
- }
- if ( forceShowEmitterCount ) {
- showPlayingEmitterCount = true;
- }
- }
- // display playing emitter count when the sound system is overwhelmed
- int total = 0, veryLow = 0, low = 0, def = 0, high = 0, veryHigh = 0;
- // find emitters that are done playing and add them to the nextFreeEmitters list
- for ( int i = 0; i < playingEmitters.size; ) {
- if ( playingEmitters[i] == null ) {
- Debug.LogError( "[AudioManager] ERROR: playingEmitters list had a null emitter! Something nuked a sound emitter!!!" );
- playingEmitters.RemoveAtFast( i );
- return;
- }
- if ( !playingEmitters[i].IsPlaying() ) {
- // add to the free list and remove from the playing list
- if ( verboseLogging ) {
- if ( nextFreeEmitters.Contains( playingEmitters[i] ) ) {
- Debug.LogError( "[AudioManager] ERROR: playing sound emitter already in the free emitters list!" );
- }
- }
- playingEmitters[i].Stop();
- nextFreeEmitters.Add( playingEmitters[i] );
- playingEmitters.RemoveAtFast( i );
- continue;
- }
- // debugging/profiling
- if ( verboseLogging && showPlayingEmitterCount ) {
- total++;
- switch ( playingEmitters[i].priority ) {
- case SoundPriority.VeryLow: veryLow++; break;
- case SoundPriority.Low: low++; break;
- case SoundPriority.Default: def++; break;
- case SoundPriority.High: high++; break;
- case SoundPriority.VeryHigh: veryHigh++; break;
- }
- }
- i++;
- }
- if ( verboseLogging && showPlayingEmitterCount ) {
- Debug.LogWarning( string.Format( "[AudioManager] Playing sounds: Total {0} | VeryLow {1} | Low {2} | Default {3} | High {4} | VeryHigh {5} | Free {6}", Fmt( total ), Fmt( veryLow ), Fmt( low ), Fmt( def ), Fmt( high ), Fmt( veryHigh ), FmtFree( nextFreeEmitters.Count ) ) );
- showPlayingEmitterCount = false;
- }
- }
- /*
- -----------------------
- Fmt()
- -----------------------
- */
- string Fmt( int count ) {
- float t = count / (float)theAudioManager.maxSoundEmitters;
- if ( t < 0.5f ) {
- return "<color=green>" + count.ToString() + "</color>";
- } else if ( t < 0.7 ) {
- return "<color=yellow>" + count.ToString() + "</color>";
- } else {
- return "<color=red>" + count.ToString() + "</color>";
- }
- }
- /*
- -----------------------
- FmtFree()
- -----------------------
- */
- string FmtFree( int count ) {
- float t = count / (float)theAudioManager.maxSoundEmitters;
- if ( t < 0.2f ) {
- return "<color=red>" + count.ToString() + "</color>";
- } else if ( t < 0.3 ) {
- return "<color=yellow>" + count.ToString() + "</color>";
- } else {
- return "<color=green>" + count.ToString() + "</color>";
- }
- }
- /*
- -----------------------
- OnPreSceneLoad()
- -----------------------
- */
- void OnPreSceneLoad() {
- // move any attached sounds back to the sound emitters parent before changing levels so they don't get destroyed
- Debug.Log( "[AudioManager] OnPreSceneLoad cleanup" );
- for ( int i = 0; i < soundEmitters.Length; i++ ) {
- soundEmitters[i].Stop();
- soundEmitters[i].ResetParent( soundEmitterParent.transform );
- }
- // reset our emitter lists
- ResetFreeEmitters();
- }
- /*
- -----------------------
- ResetFreeEmitters()
- -----------------------
- */
- void ResetFreeEmitters() {
- nextFreeEmitters.Clear();
- playingEmitters.Clear();
- for ( int i = (int)EmitterChannel.Any; i < soundEmitters.Length; i++ ) {
- nextFreeEmitters.Add( soundEmitters[i] );
- }
- }
- /*
- -----------------------
- FadeOutSoundChannel()
- utility function to fade out a playing sound channel
- -----------------------
- */
- static public void FadeOutSoundChannel( int channel, float delaySecs, float fadeTime ) {
- theAudioManager.soundEmitters[channel].FadeOutDelayed( delaySecs, fadeTime );
- }
- /*
- -----------------------
- StopSound()
- -----------------------
- */
- static public bool StopSound( int idx, bool fadeOut = true, bool stopReserved = false ) {
- if ( !stopReserved && ( idx == (int)EmitterChannel.Reserved ) ) {
- return false;
- }
- if ( !fadeOut ) {
- theAudioManager.soundEmitters[idx].Stop();
- }
- else {
- theAudioManager.soundEmitters[idx].FadeOut( theAudioManager.soundFxFadeSecs );
- }
- return true;
- }
- /*
- -----------------------
- FadeInSound()
- -----------------------
- */
- public static void FadeInSound( int idx, float fadeTime, float volume ) {
- theAudioManager.soundEmitters[idx].FadeIn( fadeTime, volume );
- }
- /*
- -----------------------
- FadeInSound()
- -----------------------
- */
- public static void FadeInSound( int idx, float fadeTime ) {
- theAudioManager.soundEmitters[idx].FadeIn( fadeTime );
- }
- /*
- -----------------------
- FadeOutSound()
- -----------------------
- */
- public static void FadeOutSound( int idx, float fadeTime ) {
- theAudioManager.soundEmitters[idx].FadeOut( fadeTime );
- }
- /*
- -----------------------
- StopAllSounds()
- -----------------------
- */
- public static void StopAllSounds( bool fadeOut, bool stopReserved = false ) {
- for ( int i = 0; i < theAudioManager.soundEmitters.Length; i++ ) {
- StopSound( i, fadeOut, stopReserved );
- }
- }
-
- /*
- -----------------------
- MuteAllSounds()
- -----------------------
- */
- public void MuteAllSounds( bool mute, bool muteReserved = false ) {
- for ( int i = 0; i < soundEmitters.Length; i++ ) {
- if ( !muteReserved && ( i == (int)EmitterChannel.Reserved ) ) {
- continue;
- }
- soundEmitters[i].audioSource.mute = true;
- }
- }
- /*
- -----------------------
- UnMuteAllSounds()
- -----------------------
- */
- public void UnMuteAllSounds( bool unmute, bool unmuteReserved = false ) {
- for ( int i = 0; i < soundEmitters.Length; i++ ) {
- if ( !unmuteReserved && ( i == (int)EmitterChannel.Reserved ) ) {
- continue;
- }
- if ( soundEmitters[i].audioSource.isPlaying ) {
- soundEmitters[i].audioSource.mute = false;
- }
- }
- }
- /*
- -----------------------
- GetEmitterEndTime()
- -----------------------
- */
- static public float GetEmitterEndTime( int idx ) {
- return theAudioManager.soundEmitters[idx].endPlayTime;
- }
- /*
- -----------------------
- SetEmitterTime()
- -----------------------
- */
- static public float SetEmitterTime( int idx, float time ) {
- return theAudioManager.soundEmitters[idx].time = time;
- }
- /*
- -----------------------
- PlaySound()
- -----------------------
- */
- static public int PlaySound( AudioClip clip, float volume, EmitterChannel src = EmitterChannel.Any, float delay = 0.0f, float pitchVariance = 1.0f, bool loop = false ) {
- if ( !SoundEnabled ) {
- return -1;
- }
- return PlaySoundAt( ( staticListenerPosition != null ) ? staticListenerPosition.position : Vector3.zero, clip, volume, src, delay, pitchVariance, loop );
- }
- /*
- -----------------------
- FindFreeEmitter()
- -----------------------
- */
- static private int FindFreeEmitter( EmitterChannel src, SoundPriority priority ) {
- // default to the reserved emitter
- SoundEmitter next = theAudioManager.soundEmitters[0];
- if ( src == EmitterChannel.Any ) {
- // pull from the free emitter list if possible
- if ( theAudioManager.nextFreeEmitters.size > 0 ) {
- // return the first in the list
- next = theAudioManager.nextFreeEmitters[0];
- // remove it from the free list
- theAudioManager.nextFreeEmitters.RemoveAtFast( 0 );
- } else {
- // no free emitters available so pull from the lowest priority sound
- if ( priority == SoundPriority.VeryLow ) {
- // skip low priority sounds
- return -1;
- } else {
- // find a playing emitter that has a lower priority than what we're requesting
- // TODO - we could first search for Very Low, then Low, etc ... TBD if it's worth the effort
- next = theAudioManager.playingEmitters.Find( item => item != null && item.priority < priority );
- if ( next == null ) {
- // last chance to find a free emitter
- if ( priority < SoundPriority.Default ) {
- // skip sounds less than the default priority
- if ( theAudioManager.verboseLogging ) {
- Debug.LogWarning( "[AudioManager] skipping sound " + priority );
- }
- return -1;
- } else {
- // grab a default priority emitter so that we don't cannabalize a high priority sound
- next = theAudioManager.playingEmitters.Find( item => item != null && item.priority <= SoundPriority.Default ); ;
- }
- }
- if ( next != null ) {
- if ( theAudioManager.verboseLogging ) {
- Debug.LogWarning( "[AudioManager] cannabalizing " + next.originalIdx + " Time: " + Time.time );
- }
- // remove it from the playing list
- next.Stop();
- theAudioManager.playingEmitters.RemoveFast( next );
- }
- }
- }
- }
- if ( next == null ) {
- Debug.LogError( "[AudioManager] ERROR - absolutely couldn't find a free emitter! Priority = " + priority + " TOO MANY PlaySound* calls!" );
- showPlayingEmitterCount = true;
- return -1;
- }
- return next.originalIdx;
- }
- /*
- -----------------------
- PlaySound()
- -----------------------
- */
- static public int PlaySound( SoundFX soundFX, EmitterChannel src = EmitterChannel.Any, float delay = 0.0f ) {
- if ( !SoundEnabled ) {
- return -1;
- }
- return PlaySoundAt( ( staticListenerPosition != null ) ? staticListenerPosition.position : Vector3.zero, soundFX, src, delay );
- }
- /*
- -----------------------
- PlaySoundAt()
- -----------------------
- */
- static public int PlaySoundAt( Vector3 position, SoundFX soundFX, EmitterChannel src = EmitterChannel.Any, float delay = 0.0f, float volumeOverride = 1.0f, float pitchMultiplier = 1.0f ) {
- if ( !SoundEnabled ) {
- return -1;
- }
- AudioClip clip = soundFX.GetClip();
- if ( clip == null ) {
- return -1;
- }
- // check the distance from the local player and ignore sounds out of range
- if ( staticListenerPosition != null ) {
- float distFromListener = ( staticListenerPosition.position - position ).sqrMagnitude;
- if ( distFromListener > theAudioManager.audioMaxFallOffDistanceSqr ) {
- return -1;
- }
- if ( distFromListener > soundFX.MaxFalloffDistSquared ) {
- return -1;
- }
- }
- // check max playing sounds
- if ( soundFX.ReachedGroupPlayLimit() ) {
- if ( theAudioManager.verboseLogging ) {
- Debug.Log( "[AudioManager] PlaySoundAt() with " + soundFX.name + " skipped due to group play limit" );
- }
- return -1;
- }
- int idx = FindFreeEmitter( src, soundFX.priority );
- if ( idx == -1 ) {
- // no free emitters - should only happen on very low priority sounds
- return -1;
- }
- SoundEmitter emitter = theAudioManager.soundEmitters[idx];
- // make sure to detach the emitter from a previous parent
- emitter.ResetParent( soundEmitterParent.transform );
- emitter.gameObject.SetActive( true );
- // set up the sound emitter
- AudioSource audioSource = emitter.audioSource;
- ONSPAudioSource osp = emitter.osp;
- audioSource.enabled = true;
- audioSource.volume = Mathf.Clamp01( Mathf.Clamp01( theAudioManager.volumeSoundFX * soundFX.volume ) * volumeOverride * soundFX.GroupVolumeOverride );
- audioSource.pitch = soundFX.GetPitch() * pitchMultiplier;
- audioSource.time = 0.0f;
- audioSource.spatialBlend = 1.0f;
- audioSource.rolloffMode = soundFX.falloffCurve;
- if ( soundFX.falloffCurve == AudioRolloffMode.Custom ) {
- audioSource.SetCustomCurve( AudioSourceCurveType.CustomRolloff, soundFX.volumeFalloffCurve );
- }
- audioSource.SetCustomCurve( AudioSourceCurveType.ReverbZoneMix, soundFX.reverbZoneMix );
- audioSource.dopplerLevel = 0;
- audioSource.clip = clip;
- audioSource.spread = soundFX.spread;
- audioSource.loop = soundFX.looping;
- audioSource.mute = false;
- audioSource.minDistance = soundFX.falloffDistance.x;
- audioSource.maxDistance = soundFX.falloffDistance.y;
- audioSource.outputAudioMixerGroup = soundFX.GetMixerGroup( AudioManager.EmitterGroup );
- // set the play time so we can check when sounds are done
- emitter.endPlayTime = Time.time + clip.length + delay;
- // cache the default volume for fading
- emitter.defaultVolume = audioSource.volume;
- // sound priority
- emitter.priority = soundFX.priority;
- // reset this
- emitter.onFinished = null;
- // update the sound group limits
- emitter.SetPlayingSoundGroup( soundFX.Group );
- // add to the playing list
- if ( src == EmitterChannel.Any ) {
- theAudioManager.playingEmitters.AddUnique( emitter );
- }
- // OSP properties
- if ( osp != null ) {
- osp.EnableSpatialization = soundFX.ospProps.enableSpatialization;
- osp.EnableRfl = theAudioManager.enableSpatializedFastOverride || soundFX.ospProps.useFastOverride ? true : false;
- osp.Gain = soundFX.ospProps.gain;
- osp.UseInvSqr = soundFX.ospProps.enableInvSquare;
- osp.Near = soundFX.ospProps.invSquareFalloff.x;
- osp.Far = soundFX.ospProps.invSquareFalloff.y;
- audioSource.spatialBlend = (soundFX.ospProps.enableSpatialization) ? 1.0f : 0.8f;
-
- // make sure to set the properties in the audio source before playing
- osp.SetParameters(ref audioSource);
- }
- audioSource.transform.position = position;
- if ( theAudioManager.verboseLogging ) {
- Debug.Log( "[AudioManager] PlaySoundAt() channel = " + idx + " soundFX = " + soundFX.name + " volume = " + emitter.volume + " Delay = " + delay + " time = " + Time.time + "\n" );
- }
-
- // play the sound
- if ( delay > 0f ) {
- audioSource.PlayDelayed( delay );
- } else {
- audioSource.Play();
- }
-
- return idx;
- }
- /*
- -----------------------
- PlayRandomSoundAt()
- -----------------------
- */
- static public int PlayRandomSoundAt( Vector3 position, AudioClip[] clips, float volume, EmitterChannel src = EmitterChannel.Any, float delay = 0.0f, float pitch = 1.0f, bool loop = false ) {
- if ( ( clips == null ) || ( clips.Length == 0 ) ) {
- return -1;
- }
- int idx = Random.Range( 0, clips.Length );
- return PlaySoundAt( position, clips[idx], volume, src, delay, pitch, loop );
- }
-
- /*
- -----------------------
- PlaySoundAt()
- -----------------------
- */
- static public int PlaySoundAt( Vector3 position, AudioClip clip, float volume = 1.0f, EmitterChannel src = EmitterChannel.Any, float delay = 0.0f, float pitch = 1.0f, bool loop = false ) {
- if ( !SoundEnabled ) {
- return -1;
- }
-
- if ( clip == null ) {
- return -1;
- }
- // check the distance from the local player and ignore sounds out of range
- if ( staticListenerPosition != null ) {
- if ( ( staticListenerPosition.position - position ).sqrMagnitude > theAudioManager.audioMaxFallOffDistanceSqr ) {
- // no chance of being heard
- return -1;
- }
- }
- int idx = FindFreeEmitter( src, 0 );
- if ( idx == -1 ) {
- // no free emitters - should only happen on very low priority sounds
- return -1;
- }
- SoundEmitter emitter = theAudioManager.soundEmitters[idx];
- // make sure to detach the emitter from a previous parent
- emitter.ResetParent( soundEmitterParent.transform );
- emitter.gameObject.SetActive( true );
- // set up the sound emitter
- AudioSource audioSource = emitter.audioSource;
- ONSPAudioSource osp = emitter.osp;
- audioSource.enabled = true;
- audioSource.volume = Mathf.Clamp01( theAudioManager.volumeSoundFX * volume );
- audioSource.pitch = pitch;
- audioSource.spatialBlend = 0.8f;
- audioSource.rolloffMode = AudioRolloffMode.Linear;
- audioSource.SetCustomCurve( AudioSourceCurveType.ReverbZoneMix, defaultReverbZoneMix );
- audioSource.dopplerLevel = 0.0f;
- audioSource.clip = clip;
- audioSource.spread = 0.0f;
- audioSource.loop = loop;
- audioSource.mute = false;
- audioSource.minDistance = theAudioManager.audioMinFallOffDistance;
- audioSource.maxDistance = theAudioManager.audioMaxFallOffDistance;
- audioSource.outputAudioMixerGroup = AudioManager.EmitterGroup;
- // set the play time so we can check when sounds are done
- emitter.endPlayTime = Time.time + clip.length + delay;
- // cache the default volume for fading
- emitter.defaultVolume = audioSource.volume;
- // default priority
- emitter.priority = 0;
- // reset this
- emitter.onFinished = null;
- // update the sound group limits
- emitter.SetPlayingSoundGroup( null );
- // add to the playing list
- if ( src == EmitterChannel.Any ) {
- theAudioManager.playingEmitters.AddUnique( emitter );
- }
- // disable spatialization (by default for regular AudioClips)
- if ( osp != null ) {
- osp.EnableSpatialization = false;
- }
- audioSource.transform.position = position;
- if ( theAudioManager.verboseLogging ) {
- Debug.Log( "[AudioManager] PlaySoundAt() channel = " + idx + " clip = " + clip.name + " volume = " + emitter.volume + " Delay = " + delay + " time = " + Time.time + "\n" );
- }
- // play the sound
- if ( delay > 0f ) {
- audioSource.PlayDelayed( delay );
- } else {
- audioSource.Play();
- }
-
- return idx;
- }
- /*
- -----------------------
- SetOnFinished()
- -----------------------
- */
- public static void SetOnFinished( int emitterIdx, System.Action onFinished ) {
- if ( emitterIdx >= 0 && emitterIdx < theAudioManager.maxSoundEmitters ) {
- theAudioManager.soundEmitters[emitterIdx].SetOnFinished( onFinished );
- }
- }
- /*
- -----------------------
- SetOnFinished()
- -----------------------
- */
- public static void SetOnFinished( int emitterIdx, System.Action<object> onFinished, object obj ) {
- if ( emitterIdx >= 0 && emitterIdx < theAudioManager.maxSoundEmitters ) {
- theAudioManager.soundEmitters[emitterIdx].SetOnFinished( onFinished, obj );
- }
- }
- /*
- -----------------------
- AttachSoundToParent()
- -----------------------
- */
- public static void AttachSoundToParent( int idx, Transform parent ) {
- if ( theAudioManager.verboseLogging ) {
- string parentName = parent.name;
- if ( parent.parent != null ) {
- parentName = parent.parent.name + "/" + parentName;
- }
- Debug.Log( "[AudioManager] ATTACHING INDEX " + idx + " to " + parentName );
- }
- theAudioManager.soundEmitters[idx].ParentTo( parent );
- }
- /*
- -----------------------
- DetachSoundFromParent()
- -----------------------
- */
- public static void DetachSoundFromParent( int idx ) {
- if ( theAudioManager.verboseLogging ) {
- Debug.Log( "[AudioManager] DETACHING INDEX " + idx );
- }
- theAudioManager.soundEmitters[idx].DetachFromParent();
- }
- /*
- -----------------------
- DetachSoundsFromParent()
- -----------------------
- */
- public static void DetachSoundsFromParent( SoundEmitter[] emitters, bool stopSounds = true ) {
- if ( emitters == null ) {
- return;
- }
- foreach ( SoundEmitter emitter in emitters ) {
- if ( emitter.defaultParent != null ) {
- if ( stopSounds ) {
- emitter.Stop();
- }
- emitter.DetachFromParent();
- // make sure it's active
- emitter.gameObject.SetActive( true );
- } else {
- if ( stopSounds ) {
- emitter.Stop();
- }
- }
- }
- }
- /*
- -----------------------
- SetEmitterMixerGroup()
- -----------------------
- */
- public static void SetEmitterMixerGroup( int idx, AudioMixerGroup mixerGroup ) {
- if ( ( theAudioManager != null ) && ( idx > -1 ) ) {
- theAudioManager.soundEmitters[idx].SetAudioMixer( mixerGroup );
- }
- }
- /*
- -----------------------
- GetActiveSnapshot()
- -----------------------
- */
- public static MixerSnapshot GetActiveSnapshot() {
- return ( theAudioManager != null ) ? theAudioManager.currentSnapshot : null;
- }
- /*
- -----------------------
- SetCurrentSnapshot()
- -----------------------
- */
- public static void SetCurrentSnapshot( MixerSnapshot mixerSnapshot ) {
- #if UNITY_EDITOR
- if ( mixerSnapshot == null || mixerSnapshot.snapshot == null ) {
- Debug.LogError( "[AudioManager] ERROR setting empty mixer snapshot!" );
- } else {
- Debug.Log( "[AudioManager] Setting audio mixer snapshot: " + ( ( mixerSnapshot != null && mixerSnapshot.snapshot != null ) ? mixerSnapshot.snapshot.name : "None" ) + " Time: " + Time.time );
- }
- #endif
- if ( theAudioManager != null ) {
- if ( ( mixerSnapshot != null ) && ( mixerSnapshot.snapshot != null ) ) {
- mixerSnapshot.snapshot.TransitionTo( mixerSnapshot.transitionTime );
- } else {
- mixerSnapshot = null;
- }
- theAudioManager.currentSnapshot = mixerSnapshot;
- }
- }
- /*
- -----------------------
- BlendWithCurrentSnapshot()
- -----------------------
- */
- public static void BlendWithCurrentSnapshot( MixerSnapshot blendSnapshot, float weight, float blendTime = 0.0f ) {
- if ( theAudioManager != null ) {
- if ( theAudioManager.audioMixer == null ) {
- Debug.LogWarning( "[AudioManager] can't call BlendWithCurrentSnapshot if the audio mixer is not set!" );
- return;
- }
- if ( blendTime == 0.0f ) {
- blendTime = Time.deltaTime;
- }
- if ( ( theAudioManager.currentSnapshot != null ) && (theAudioManager.currentSnapshot.snapshot != null ) ) {
- if ( ( blendSnapshot != null ) && ( blendSnapshot.snapshot != null ) ) {
- weight = Mathf.Clamp01( weight );
- if ( weight == 0.0f ) {
- // revert to the default snapshot
- theAudioManager.currentSnapshot.snapshot.TransitionTo( blendTime );
- } else {
- AudioMixerSnapshot[] snapshots = new AudioMixerSnapshot[] { theAudioManager.currentSnapshot.snapshot, blendSnapshot.snapshot };
- float[] weights = new float[] { 1.0f - weight, weight };
- theAudioManager.audioMixer.TransitionToSnapshots( snapshots, weights, blendTime );
- }
- }
- }
- }
- }
- }
- } // namespace OVR
|