ONSPAudioSource.cs 9.4 KB

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