| 1 | using UnityEngine; |
| 2 | using UnityEngine.Audio; |
| 3 | |
| 4 | namespace OVR |
| 5 | { |
| 6 | |
| 7 | public enum SoundFXNext { |
| 8 | Random = 0, |
| 9 | Sequential = 1, |
| 10 | } |
| 11 | |
| 12 | public enum FreqHint { |
| 13 | None = 0, |
| 14 | Wide = 1, |
| 15 | Narrow = 2, |
| 16 | } |
| 17 | |
| 18 | public enum SoundPriority { |
| 19 | VeryLow = -2, |
| 20 | Low = -1, |
| 21 | Default = 0, |
| 22 | High = 1, |
| 23 | VeryHigh = 2, |
| 24 | } |
| 25 | |
| 26 | [System.Serializable] |
| 27 | public class OSPProps { |
| 28 | public OSPProps() { |
| 29 | enableSpatialization = false; |
| 30 | useFastOverride = false; |
| 31 | gain = 0.0f; |
| 32 | enableInvSquare = false; |
| 33 | volumetric = 0.0f; |
| 34 | invSquareFalloff = new Vector2( 1.0f, 25.0f ); |
| 35 | } |
| 36 | |
| 37 | [Tooltip( "Set to true to play the sound FX spatialized with binaural HRTF, default = false")] |
| 38 | public bool enableSpatialization = false; |
| 39 | [Tooltip( "Play the sound FX with reflections, default = false")] |
| 40 | public bool useFastOverride = false; |
| 41 | [Tooltip( "Boost the gain on the spatialized sound FX, default = 0.0")] |
| 42 | [Range( 0.0f, 24.0f )] |
| 43 | public float gain = 0.0f; |
| 44 | [Tooltip("Enable Inverse Square attenuation curve, default = false")] |
| 45 | public bool enableInvSquare = false; |
| 46 | [Tooltip("Change the sound from point source (0.0f) to a spherical volume, default = 0.0")] |
| 47 | [Range(0.0f, 1000.0f)] |
| 48 | public float volumetric = 0.0f; |
| 49 | [Tooltip("Set the near and far falloff value for the OSP attenuation curve, default = 1.0")] |
| 50 | [MinMax ( 1.0f, 25.0f, 0.0f, 250.0f )] |
| 51 | public Vector2 invSquareFalloff = new Vector2( 1.0f, 25.0f ); |
| 52 | } |
| 53 | |
| 54 | /* |
| 55 | ----------------------- |
| 56 | |
| 57 | SoundFX |
| 58 | |
| 59 | ----------------------- |
| 60 | */ |
| 61 | [System.Serializable] |
| 62 | public class SoundFX { |
| 63 | public SoundFX() { |
| 64 | playback = SoundFXNext.Random; |
| 65 | volume = 1.0f; |
| 66 | pitchVariance = Vector2.one; |
| 67 | falloffDistance = new Vector2( 1.0f, 25.0f ); |
| 68 | falloffCurve = AudioRolloffMode.Linear; |
| 69 | volumeFalloffCurve = new AnimationCurve( new Keyframe[2] { new Keyframe( 0f, 1.0f ), new Keyframe( 1f, 1f ) } ); |
| 70 | reverbZoneMix = new AnimationCurve( new Keyframe[2] { new Keyframe( 0f, 1.0f ), new Keyframe( 1f, 1f ) } ); |
| 71 | spread = 0.0f; |
| 72 | pctChanceToPlay = 1.0f; |
| 73 | priority = SoundPriority.Default; |
| 74 | delay = Vector2.zero; |
| 75 | looping = false; |
| 76 | ospProps = new OSPProps(); |
| 77 | } |
| 78 | |
| 79 | [Tooltip( "Each sound FX should have a unique name")] |
| 80 | public string name = string.Empty; |
| 81 | [Tooltip( "Sound diversity playback option when multiple audio clips are defined, default = Random")] |
| 82 | public SoundFXNext playback = SoundFXNext.Random; |
| 83 | [Tooltip( "Default volume for this sound FX, default = 1.0")] |
| 84 | [Range (0.0f, 1.0f)] |
| 85 | public float volume = 1.0f; |
| 86 | [Tooltip( "Random pitch variance each time a sound FX is played, default = 1.0 (none)")] |
| 87 | [MinMax ( 1.0f, 1.0f, 0.0f, 2.0f )] |
| 88 | public Vector2 pitchVariance = Vector2.one; |
| 89 | [Tooltip( "Falloff distance for the sound FX, default = 1m min to 25m max")] |
| 90 | [MinMax ( 1.0f, 25.0f, 0.0f, 250.0f )] |
| 91 | public Vector2 falloffDistance = new Vector2( 1.0f, 25.0f ); |
| 92 | [Tooltip( "Volume falloff curve - sets how the sound FX attenuates over distance, default = Linear")] |
| 93 | public AudioRolloffMode falloffCurve = AudioRolloffMode.Linear; |
| 94 | [Tooltip( "Defines the custom volume falloff curve")] |
| 95 | public AnimationCurve volumeFalloffCurve = new AnimationCurve( new Keyframe[2] { new Keyframe( 0f, 1.0f ), new Keyframe( 1f, 1f ) } ); |
| 96 | [Tooltip( "The amount by which the signal from the AudioSource will be mixed into the global reverb associated with the Reverb Zones | Valid range is 0.0 - 1.1, default = 1.0" )] |
| 97 | public AnimationCurve reverbZoneMix = new AnimationCurve( new Keyframe[2] { new Keyframe( 0f, 1.0f ), new Keyframe( 1f, 1f ) } ); |
| 98 | [Tooltip( "Sets the spread angle (in degrees) of a 3d stereo or multichannel sound in speaker space, default = 0")] |
| 99 | [Range (0.0f, 360.0f)] |
| 100 | public float spread = 0.0f; |
| 101 | [Tooltip( "The percentage chance that this sound FX will play | 0.0 = none, 1.0 = 100%, default = 1.0")] |
| 102 | [Range (0.0f, 1.0f)] |
| 103 | public float pctChanceToPlay = 1.0f; |
| 104 | [Tooltip( "Sets the priority for this sound to play and/or to override a currently playing sound FX, default = Default")] |
| 105 | public SoundPriority priority = SoundPriority.Default; |
| 106 | [Tooltip( "Specifies the default delay when this sound FX is played, default = 0.0 secs")] |
| 107 | [MinMax ( 0.0f, 0.0f, 0.0f, 2.0f )] |
| 108 | public Vector2 delay = Vector2.zero; // this overrides any delay passed into PlaySound() or PlaySoundAt() |
| 109 | [Tooltip( "Set to true for the sound to loop continuously, default = false")] |
| 110 | public bool looping = false; |
| 111 | public OSPProps ospProps = new OSPProps(); |
| 112 | [Tooltip( "List of the audio clips assigned to this sound FX")] |
| 113 | public AudioClip[] soundClips = new AudioClip[1]; |
| 114 | // editor only - unfortunately if we set it not to serialize, we can't query it from the editor |
| 115 | public bool visibilityToggle = false; |
| 116 | // runtime vars |
| 117 | [System.NonSerialized] |
| 118 | private SoundGroup soundGroup = null; |
| 119 | private int lastIdx = -1; |
| 120 | private int playingIdx = -1; |
| 121 | |
| 122 | public int Length { get { return soundClips.Length; } } |
| 123 | public bool IsValid { get { return ( ( soundClips.Length != 0 ) && ( soundClips[0] != null ) ); } } |
| 124 | public SoundGroup Group { get { return soundGroup; } set { soundGroup = value; } } |
| 125 | public float MaxFalloffDistSquared { get { return falloffDistance.y * falloffDistance.y; } } |
| 126 | public float GroupVolumeOverride { get { return ( soundGroup != null ) ? soundGroup.volumeOverride : 1.0f; } } |
| 127 | |
| 128 | /* |
| 129 | ----------------------- |
| 130 | GetClip() |
| 131 | ----------------------- |
| 132 | */ |
| 133 | public AudioClip GetClip() { |
| 134 | if ( soundClips.Length == 0 ) { |
| 135 | return null; |
| 136 | } else if ( soundClips.Length == 1 ) { |
| 137 | return soundClips[0]; |
| 138 | } |
| 139 | if ( playback == SoundFXNext.Random ) { |
| 140 | // random, but don't pick the last one |
| 141 | int idx = Random.Range( 0, soundClips.Length ); |
| 142 | while ( idx == lastIdx ) { |
| 143 | idx = Random.Range( 0, soundClips.Length ); |
| 144 | } |
| 145 | lastIdx = idx; |
| 146 | return soundClips[idx]; |
| 147 | } else { |
| 148 | // sequential |
| 149 | if ( ++lastIdx >= soundClips.Length ) { |
| 150 | lastIdx = 0; |
| 151 | } |
| 152 | return soundClips[lastIdx]; |
| 153 | } |
| 154 | } |
| 155 | |
| 156 | /* |
| 157 | ----------------------- |
| 158 | GetMixerGroup() |
| 159 | ----------------------- |
| 160 | */ |
| 161 | public AudioMixerGroup GetMixerGroup( AudioMixerGroup defaultMixerGroup ) { |
| 162 | if ( soundGroup != null ) { |
| 163 | return ( soundGroup.mixerGroup != null ) ? soundGroup.mixerGroup : defaultMixerGroup; |
| 164 | } |
| 165 | return defaultMixerGroup; |
| 166 | } |
| 167 | |
| 168 | /* |
| 169 | ----------------------- |
| 170 | ReachedGroupPlayLimit() |
| 171 | ----------------------- |
| 172 | */ |
| 173 | public bool ReachedGroupPlayLimit() { |
| 174 | if ( soundGroup != null ) { |
| 175 | return !soundGroup.CanPlaySound(); |
| 176 | } |
| 177 | return false; |
| 178 | } |
| 179 | |
| 180 | /* |
| 181 | ----------------------- |
| 182 | GetClipLength() |
| 183 | ----------------------- |
| 184 | */ |
| 185 | public float GetClipLength( int idx ) { |
| 186 | if ( ( idx == -1 ) || ( soundClips.Length == 0 ) || ( idx >= soundClips.Length ) || ( soundClips[idx] == null ) ) { |
| 187 | return 0.0f; |
| 188 | } else { |
| 189 | return soundClips[idx].length; |
| 190 | } |
| 191 | } |
| 192 | |
| 193 | /* |
| 194 | ----------------------- |
| 195 | GetPitch() |
| 196 | ----------------------- |
| 197 | */ |
| 198 | public float GetPitch() { |
| 199 | return Random.Range( pitchVariance.x, pitchVariance.y ); |
| 200 | } |
| 201 | |
| 202 | /* |
| 203 | ----------------------- |
| 204 | PlaySound() |
| 205 | ----------------------- |
| 206 | */ |
| 207 | public int PlaySound( float delaySecs = 0.0f ) { |
| 208 | playingIdx = -1; |
| 209 | |
| 210 | if ( !IsValid ) { |
| 211 | return playingIdx; |
| 212 | } |
| 213 | |
| 214 | // check the random chance to play here to save the function calls |
| 215 | if ( ( pctChanceToPlay > 0.99f ) || ( Random.value < pctChanceToPlay ) ) { |
| 216 | if ( delay.y > 0.0f ) { |
| 217 | delaySecs = Random.Range( delay.x, delay.y ); |
| 218 | } |
| 219 | playingIdx = AudioManager.PlaySound( this, EmitterChannel.Any, delaySecs ); |
| 220 | } |
| 221 | |
| 222 | return playingIdx; |
| 223 | } |
| 224 | |
| 225 | /* |
| 226 | ----------------------- |
| 227 | PlaySoundAt() |
| 228 | ----------------------- |
| 229 | */ |
| 230 | public int PlaySoundAt( Vector3 pos, float delaySecs = 0.0f, float volumeOverride = 1.0f, float pitchMultiplier = 1.0f ) { |
| 231 | playingIdx = -1; |
| 232 | |
| 233 | if ( !IsValid ) { |
| 234 | return playingIdx; |
| 235 | } |
| 236 | |
| 237 | // check the random chance to play here to save the function calls |
| 238 | if ( ( pctChanceToPlay > 0.99f ) || ( Random.value < pctChanceToPlay ) ) { |
| 239 | if ( delay.y > 0.0f ) { |
| 240 | delaySecs = Random.Range( delay.x, delay.y ); |
| 241 | } |
| 242 | playingIdx = AudioManager.PlaySoundAt( pos, this, EmitterChannel.Any, delaySecs, volumeOverride, pitchMultiplier ); |
| 243 | } |
| 244 | |
| 245 | return playingIdx; |
| 246 | } |
| 247 | |
| 248 | /* |
| 249 | ----------------------- |
| 250 | SetOnFinished() |
| 251 | get a callback when the sound is finished playing |
| 252 | ----------------------- |
| 253 | */ |
| 254 | public void SetOnFinished( System.Action onFinished ) { |
| 255 | if ( playingIdx > -1 ) { |
| 256 | AudioManager.SetOnFinished( playingIdx, onFinished ); |
| 257 | } |
| 258 | } |
| 259 | |
| 260 | /* |
| 261 | ----------------------- |
| 262 | SetOnFinished() |
| 263 | get a callback with an object parameter when the sound is finished playing |
| 264 | ----------------------- |
| 265 | */ |
| 266 | public void SetOnFinished( System.Action<object> onFinished, object obj ) { |
| 267 | if ( playingIdx > -1 ) { |
| 268 | AudioManager.SetOnFinished( playingIdx, onFinished, obj ); |
| 269 | } |
| 270 | } |
| 271 | |
| 272 | /* |
| 273 | ----------------------- |
| 274 | StopSound() |
| 275 | ----------------------- |
| 276 | */ |
| 277 | public bool StopSound() { |
| 278 | bool stopped = false; |
| 279 | |
| 280 | if (playingIdx > -1){ |
| 281 | stopped = AudioManager.StopSound(playingIdx); |
| 282 | playingIdx = -1; |
| 283 | } |
| 284 | |
| 285 | return stopped; |
| 286 | } |
| 287 | |
| 288 | /* |
| 289 | ----------------------- |
| 290 | AttachToParent() |
| 291 | ----------------------- |
| 292 | */ |
| 293 | public void AttachToParent( Transform parent) { |
| 294 | if (playingIdx > -1) { |
| 295 | AudioManager.AttachSoundToParent(playingIdx, parent); |
| 296 | } |
| 297 | } |
| 298 | |
| 299 | /* |
| 300 | ----------------------- |
| 301 | DetachFromParent() |
| 302 | ----------------------- |
| 303 | */ |
| 304 | public void DetachFromParent() { |
| 305 | if (playingIdx > -1) { |
| 306 | AudioManager.DetachSoundFromParent(playingIdx); |
| 307 | } |
| 308 | } |
| 309 | } |
| 310 | |
| 311 | } // namespace OVR |