ONSPAmbisonicsNative.cs 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. /************************************************************************************
  2. Filename : ONSPAmbisonicsNative.cs
  3. Content : Native interface into the Oculus Ambisonics
  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. using UnityEngine;
  18. using System.Collections;
  19. using System.Runtime.InteropServices;
  20. public class ONSPAmbisonicsNative : MonoBehaviour
  21. {
  22. #if !UNITY_5
  23. static int numFOAChannels = 4; // we are only dealing with 1st order Ambisonics at this time
  24. static int paramVSpeakerMode = 6; // set speaker mode (OculusAmbi or VSpeaker)
  25. static int paramAmbiStat = 7; // use this to return internal Ambisonic status
  26. // Staus codes that may return from Ambisonic engine
  27. public enum ovrAmbisonicsNativeStatus
  28. {
  29. Uninitialized = -1, // Ambisonic stream not initialized (inital status)
  30. NotEnabled, // Ambisonic has not been enabled on clip
  31. Success, // Stream initialized and playing
  32. StreamError, // Something wrong with input stream (not a 4-channel AmbiX format stream?)
  33. ProcessError, // Handling of stream error
  34. MaxStatValue
  35. };
  36. // current status
  37. ovrAmbisonicsNativeStatus currentStatus = ovrAmbisonicsNativeStatus.Uninitialized;
  38. // true to use Virtual Speaker output. Otherwise use OculusAmbi
  39. [SerializeField]
  40. private bool useVirtualSpeakers = false;
  41. public bool UseVirtualSpeakers
  42. {
  43. get
  44. {
  45. return useVirtualSpeakers;
  46. }
  47. set
  48. {
  49. useVirtualSpeakers = value;
  50. }
  51. }
  52. #endif
  53. /// <summary>
  54. /// OnEnable this instance.
  55. /// </summary>
  56. void OnEnable()
  57. {
  58. // Unity 4 is deprecated; UNITY_5 still valid with plug-in
  59. #if UNITY_5
  60. Debug.Log("Ambisonic ERROR: Ambisonic support in Unity 2017 or higher");
  61. #else
  62. AudioSource source = GetComponent<AudioSource>();
  63. currentStatus = ovrAmbisonicsNativeStatus.Uninitialized;
  64. if (source == null)
  65. {
  66. Debug.Log("Ambisonic ERROR: AudioSource does not exist.");
  67. }
  68. else
  69. {
  70. if(source.spatialize == true)
  71. {
  72. Debug.Log("Ambisonic WARNING: Turning spatialize field off for Ambisonic sources.");
  73. source.spatialize = false;
  74. }
  75. if (source.clip == null)
  76. {
  77. Debug.Log("Ambisonic ERROR: AudioSource does not contain an audio clip.");
  78. }
  79. else
  80. {
  81. if(source.clip.channels != numFOAChannels)
  82. {
  83. Debug.Log("Ambisonic ERROR: AudioSource clip does not have correct number of channels.");
  84. }
  85. }
  86. }
  87. #endif
  88. }
  89. // Unity 4 is deprecated; UNITY_5 still valid with plug-in
  90. #if !UNITY_5
  91. /// <summary>
  92. /// Update this instance.
  93. /// </summary>
  94. void Update()
  95. {
  96. AudioSource source = GetComponent<AudioSource>();
  97. if (source == null)
  98. {
  99. // We already caught the error in Awake so bail
  100. return;
  101. }
  102. // Set speaker mode
  103. if(useVirtualSpeakers == true)
  104. source.SetAmbisonicDecoderFloat(paramVSpeakerMode, 1.0f); // VSpeakerMode
  105. else
  106. source.SetAmbisonicDecoderFloat(paramVSpeakerMode, 0.0f); // OclusAmbi
  107. float statusF = 0.0f;
  108. // PGG 5/25/2017 There is a bug in the 2017.2 beta that does not
  109. // allow for ambisonic params to be passed through to native
  110. // from C# Get latest editor from Unity when available
  111. source.GetAmbisonicDecoderFloat(paramAmbiStat, out statusF);
  112. ovrAmbisonicsNativeStatus status = (ovrAmbisonicsNativeStatus)statusF;
  113. // TODO: Add native result/error codes
  114. if (status != currentStatus)
  115. {
  116. switch(status)
  117. {
  118. case (ovrAmbisonicsNativeStatus.NotEnabled):
  119. Debug.Log("Ambisonic Native: Ambisonic not enabled on clip. Check clip field and turn it on");
  120. break;
  121. case (ovrAmbisonicsNativeStatus.Uninitialized):
  122. Debug.Log("Ambisonic Native: Stream uninitialized");
  123. break;
  124. case (ovrAmbisonicsNativeStatus.Success):
  125. Debug.Log("Ambisonic Native: Stream successfully initialized and playing/playable");
  126. break;
  127. case (ovrAmbisonicsNativeStatus.StreamError):
  128. Debug.Log("Ambisonic Native WARNING: Stream error (bad input format?)");
  129. break;
  130. case (ovrAmbisonicsNativeStatus.ProcessError):
  131. Debug.Log("Ambisonic Native WARNING: Stream process error (check default speaker setup)");
  132. break;
  133. default:
  134. break;
  135. }
  136. }
  137. currentStatus = status;
  138. }
  139. #endif
  140. }