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