| 1 | /************************************************************************************ |
| 2 | Filename : ONSPAudioSource.cs |
| 3 | Content : Interface into the Oculus Native Spatializer Plugin |
| 4 | Copyright : Copyright (c) Facebook Technologies, LLC and its affiliates. All rights reserved. |
| 5 | |
| 6 | Licensed under the Oculus SDK Version 3.5 (the "License"); |
| 7 | you may not use the Oculus SDK except in compliance with the License, |
| 8 | which is provided at the time of installation or download, or which |
| 9 | otherwise accompanies this software in either electronic or hard copy form. |
| 10 | |
| 11 | You may obtain a copy of the License at |
| 12 | |
| 13 | https://developer.oculus.com/licenses/sdk-3.5/ |
| 14 | |
| 15 | Unless required by applicable law or agreed to in writing, the Oculus SDK |
| 16 | distributed under the License is distributed on an "AS IS" BASIS, |
| 17 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 18 | See the License for the specific language governing permissions and |
| 19 | limitations under the License. |
| 20 | ************************************************************************************/ |
| 21 | |
| 22 | // Uncomment below to test access of read-only spatializer parameters |
| 23 | //#define TEST_READONLY_PARAMETERS |
| 24 | |
| 25 | using UnityEngine; |
| 26 | using System; |
| 27 | using System.Collections; |
| 28 | using System.Runtime.InteropServices; |
| 29 | |
| 30 | public class ONSPAudioSource : MonoBehaviour |
| 31 | { |
| 32 | #if TEST_READONLY_PARAMETERS |
| 33 | // Spatializer read-only system parameters (global) |
| 34 | static int readOnly_GlobalRelectionOn = 8; |
| 35 | static int readOnly_NumberOfUsedSpatializedVoices = 9; |
| 36 | #endif |
| 37 | |
| 38 | // Import functions |
| 39 | public const string strONSPS = "AudioPluginOculusSpatializer"; |
| 40 | |
| 41 | [DllImport(strONSPS)] |
| 42 | private static extern void ONSP_GetGlobalRoomReflectionValues(ref bool reflOn, ref bool reverbOn, |
| 43 | ref float width, ref float height, ref float length); |
| 44 | |
| 45 | // Public |
| 46 | |
| 47 | [SerializeField] |
| 48 | private bool enableSpatialization = true; |
| 49 | public bool EnableSpatialization |
| 50 | { |
| 51 | get |
| 52 | { |
| 53 | return enableSpatialization; |
| 54 | } |
| 55 | set |
| 56 | { |
| 57 | enableSpatialization = value; |
| 58 | } |
| 59 | } |
| 60 | |
| 61 | [SerializeField] |
| 62 | private float gain = 0.0f; |
| 63 | public float Gain |
| 64 | { |
| 65 | get |
| 66 | { |
| 67 | return gain; |
| 68 | } |
| 69 | set |
| 70 | { |
| 71 | gain = Mathf.Clamp(value, 0.0f, 24.0f); |
| 72 | } |
| 73 | } |
| 74 | |
| 75 | [SerializeField] |
| 76 | private bool useInvSqr = false; |
| 77 | public bool UseInvSqr |
| 78 | { |
| 79 | get |
| 80 | { |
| 81 | return useInvSqr; |
| 82 | } |
| 83 | set |
| 84 | { |
| 85 | useInvSqr = value; |
| 86 | } |
| 87 | } |
| 88 | |
| 89 | [SerializeField] |
| 90 | private float near = 0.25f; |
| 91 | public float Near |
| 92 | { |
| 93 | get |
| 94 | { |
| 95 | return near; |
| 96 | } |
| 97 | set |
| 98 | { |
| 99 | near = Mathf.Clamp(value, 0.0f, 1000000.0f); |
| 100 | } |
| 101 | } |
| 102 | |
| 103 | [SerializeField] |
| 104 | private float far = 250.0f; |
| 105 | public float Far |
| 106 | { |
| 107 | get |
| 108 | { |
| 109 | return far; |
| 110 | } |
| 111 | set |
| 112 | { |
| 113 | far = Mathf.Clamp(value, 0.0f, 1000000.0f); |
| 114 | } |
| 115 | } |
| 116 | |
| 117 | [SerializeField] |
| 118 | private float volumetricRadius = 0.0f; |
| 119 | public float VolumetricRadius |
| 120 | { |
| 121 | get |
| 122 | { |
| 123 | return volumetricRadius; |
| 124 | } |
| 125 | set |
| 126 | { |
| 127 | volumetricRadius = Mathf.Clamp(value, 0.0f, 1000.0f); |
| 128 | } |
| 129 | } |
| 130 | |
| 131 | [SerializeField] |
| 132 | private float reverbSend = 0.0f; |
| 133 | public float ReverbSend |
| 134 | { |
| 135 | get |
| 136 | { |
| 137 | return reverbSend; |
| 138 | } |
| 139 | set |
| 140 | { |
| 141 | reverbSend = Mathf.Clamp(value, -60.0f, 20.0f); |
| 142 | } |
| 143 | } |
| 144 | |
| 145 | |
| 146 | [SerializeField] |
| 147 | private bool enableRfl = false; |
| 148 | public bool EnableRfl |
| 149 | { |
| 150 | get |
| 151 | { |
| 152 | return enableRfl; |
| 153 | } |
| 154 | set |
| 155 | { |
| 156 | enableRfl = value; |
| 157 | } |
| 158 | } |
| 159 | |
| 160 | /// <summary> |
| 161 | /// Awake this instance. |
| 162 | /// </summary> |
| 163 | void Awake() |
| 164 | { |
| 165 | // We might iterate through multiple sources / game object |
| 166 | var source = GetComponent<AudioSource>(); |
| 167 | SetParameters(ref source); |
| 168 | } |
| 169 | |
| 170 | /// <summary> |
| 171 | /// Start this instance. |
| 172 | /// </summary> |
| 173 | void Start() |
| 174 | { |
| 175 | } |
| 176 | |
| 177 | /// <summary> |
| 178 | /// Update this instance. |
| 179 | /// </summary> |
| 180 | void Update() |
| 181 | { |
| 182 | // We might iterate through multiple sources / game object |
| 183 | var source = GetComponent<AudioSource>(); |
| 184 | |
| 185 | // READ-ONLY PARAMETER TEST |
| 186 | #if TEST_READONLY_PARAMETERS |
| 187 | float rfl_enabled = 0.0f; |
| 188 | source.GetSpatializerFloat(readOnly_GlobalRelectionOn, out rfl_enabled); |
| 189 | float num_voices = 0.0f; |
| 190 | source.GetSpatializerFloat(readOnly_NumberOfUsedSpatializedVoices, out num_voices); |
| 191 | |
| 192 | String readOnly = System.String.Format |
| 193 | ("Read only values: refl enabled: {0:F0} num voices: {1:F0}", rfl_enabled, num_voices); |
| 194 | Debug.Log(readOnly); |
| 195 | #endif |
| 196 | |
| 197 | // Check to see if we should disable spatializion |
| 198 | if ((Application.isPlaying == false) || |
| 199 | (AudioListener.pause == true) || |
| 200 | (source.isPlaying == false) || |
| 201 | (source.isActiveAndEnabled == false) |
| 202 | ) |
| 203 | { |
| 204 | source.spatialize = false; |
| 205 | return; |
| 206 | } |
| 207 | else |
| 208 | { |
| 209 | SetParameters(ref source); |
| 210 | } |
| 211 | } |
| 212 | |
| 213 | enum Parameters : int |
| 214 | { |
| 215 | P_GAIN = 0, |
| 216 | P_USEINVSQR, |
| 217 | P_NEAR, |
| 218 | P_FAR, |
| 219 | P_RADIUS, |
| 220 | P_DISABLE_RFL, |
| 221 | P_VSPEAKERMODE, |
| 222 | P_AMBISTAT, |
| 223 | P_READONLY_GLOBAL_RFL_ENABLED, // READ-ONLY |
| 224 | P_READONLY_NUM_VOICES, // READ-ONLY |
| 225 | P_SENDLEVEL, |
| 226 | P_NUM |
| 227 | }; |
| 228 | |
| 229 | /// <summary> |
| 230 | /// Sets the parameters. |
| 231 | /// </summary> |
| 232 | /// <param name="source">Source.</param> |
| 233 | public void SetParameters(ref AudioSource source) |
| 234 | { |
| 235 | // See if we should enable spatialization |
| 236 | source.spatialize = enableSpatialization; |
| 237 | |
| 238 | source.SetSpatializerFloat((int)Parameters.P_GAIN, gain); |
| 239 | // All inputs are floats; convert bool to 0.0 and 1.0 |
| 240 | if(useInvSqr == true) |
| 241 | source.SetSpatializerFloat((int)Parameters.P_USEINVSQR, 1.0f); |
| 242 | else |
| 243 | source.SetSpatializerFloat((int)Parameters.P_USEINVSQR, 0.0f); |
| 244 | |
| 245 | source.SetSpatializerFloat((int)Parameters.P_NEAR, near); |
| 246 | source.SetSpatializerFloat((int)Parameters.P_FAR, far); |
| 247 | |
| 248 | source.SetSpatializerFloat((int)Parameters.P_RADIUS, volumetricRadius); |
| 249 | |
| 250 | if(enableRfl == true) |
| 251 | source.SetSpatializerFloat((int)Parameters.P_DISABLE_RFL, 0.0f); |
| 252 | else |
| 253 | source.SetSpatializerFloat((int)Parameters.P_DISABLE_RFL, 1.0f); |
| 254 | |
| 255 | source.SetSpatializerFloat((int)Parameters.P_SENDLEVEL, reverbSend); |
| 256 | } |
| 257 | |
| 258 | private static ONSPAudioSource RoomReflectionGizmoAS = null; |
| 259 | |
| 260 | /// <summary> |
| 261 | /// |
| 262 | /// </summary> |
| 263 | void OnDrawGizmos() |
| 264 | { |
| 265 | // Are we the first one created? make sure to set our static ONSPAudioSource |
| 266 | // for drawing out room parameters once |
| 267 | if(RoomReflectionGizmoAS == null) |
| 268 | { |
| 269 | RoomReflectionGizmoAS = this; |
| 270 | } |
| 271 | |
| 272 | Color c; |
| 273 | const float colorSolidAlpha = 0.1f; |
| 274 | |
| 275 | // Draw the near/far spheres |
| 276 | |
| 277 | // Near (orange) |
| 278 | c.r = 1.0f; |
| 279 | c.g = 0.5f; |
| 280 | c.b = 0.0f; |
| 281 | c.a = 1.0f; |
| 282 | Gizmos.color = c; |
| 283 | Gizmos.DrawWireSphere(transform.position, Near); |
| 284 | c.a = colorSolidAlpha; |
| 285 | Gizmos.color = c; |
| 286 | Gizmos.DrawSphere(transform.position, Near); |
| 287 | |
| 288 | // Far (red) |
| 289 | c.r = 1.0f; |
| 290 | c.g = 0.0f; |
| 291 | c.b = 0.0f; |
| 292 | c.a = 1.0f; |
| 293 | Gizmos.color = Color.red; |
| 294 | Gizmos.DrawWireSphere(transform.position, Far); |
| 295 | c.a = colorSolidAlpha; |
| 296 | Gizmos.color = c; |
| 297 | Gizmos.DrawSphere(transform.position, Far); |
| 298 | |
| 299 | // VolumetricRadius (purple) |
| 300 | c.r = 1.0f; |
| 301 | c.g = 0.0f; |
| 302 | c.b = 1.0f; |
| 303 | c.a = 1.0f; |
| 304 | Gizmos.color = c; |
| 305 | Gizmos.DrawWireSphere(transform.position, VolumetricRadius); |
| 306 | c.a = colorSolidAlpha; |
| 307 | Gizmos.color = c; |
| 308 | Gizmos.DrawSphere(transform.position, VolumetricRadius); |
| 309 | |
| 310 | // Draw room parameters ONCE only, provided reflection engine is on |
| 311 | if (RoomReflectionGizmoAS == this) |
| 312 | { |
| 313 | // Get global room parameters (write new C api to get reflection values) |
| 314 | bool reflOn = false; |
| 315 | bool reverbOn = false; |
| 316 | float width = 1.0f; |
| 317 | float height = 1.0f; |
| 318 | float length = 1.0f; |
| 319 | |
| 320 | ONSP_GetGlobalRoomReflectionValues(ref reflOn, ref reverbOn, ref width, ref height, ref length); |
| 321 | |
| 322 | // TO DO: Get the room reflection values and render those out as well (like we do in the VST) |
| 323 | |
| 324 | if((Camera.main != null) && (reflOn == true)) |
| 325 | { |
| 326 | // Set color of cube (cyan is early reflections only, white is with reverb on) |
| 327 | if(reverbOn == true) |
| 328 | c = Color.white; |
| 329 | else |
| 330 | c = Color.cyan; |
| 331 | |
| 332 | Gizmos.color = c; |
| 333 | Gizmos.DrawWireCube(Camera.main.transform.position, new Vector3(width, height, length)); |
| 334 | c.a = colorSolidAlpha; |
| 335 | Gizmos.color = c; |
| 336 | Gizmos.DrawCube(Camera.main.transform.position, new Vector3(width, height, length)); |
| 337 | } |
| 338 | } |
| 339 | } |
| 340 | |
| 341 | /// <summary> |
| 342 | /// |
| 343 | /// </summary> |
| 344 | void OnDestroy() |
| 345 | { |
| 346 | // We will null out single pointer instance |
| 347 | // of the room reflection gizmo since we are being destroyed. |
| 348 | // Any ONSPAS that is alive or born will re-set this pointer |
| 349 | // so that we only draw it once |
| 350 | if(RoomReflectionGizmoAS == this) |
| 351 | { |
| 352 | RoomReflectionGizmoAS = null; |
| 353 | } |
| 354 | } |
| 355 | } |