BlueGit

blue-star ggj-2020

Files

ggj-2020 / Assets / Oculus / VoiceMod / Scripts / OVRVoiceMod.cs

OVRVoiceMod.cs

Project Import
timfa Tim Falken · pushed by timfa · 6 years ago
b77834a1 History
OVRVoiceMod.cs 8.1 KiB Download Raw
1/************************************************************************************
2Filename : OVRVoiceMod.cs
3Content : Interface to Oculus voice mod
4Created : December 14th, 2015
5Copyright : Copyright 2015 Oculus VR, Inc. All Rights reserved.
6
7Licensed under the Oculus VR Rift SDK License Version 3.1 (the "License");
8you may not use the Oculus VR Rift SDK except in compliance with the License,
9which is provided at the time of installation or download, or which
10otherwise accompanies this software in either electronic or hard copy form.
11
12You may obtain a copy of the License at
13
14http://www.oculusvr.com/licenses/LICENSE-3.1
15
16Unless required by applicable law or agreed to in writing, the Oculus VR SDK
17distributed under the License is distributed on an "AS IS" BASIS,
18WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
19See the License for the specific language governing permissions and
20limitations under the License.
21************************************************************************************/
22using UnityEngine;
23using System;
24using System.Runtime.InteropServices;
25
26//-------------------------------------------------------------------------------------
27// ***** OVRVoiceMod
28//
29/// <summary>
30/// OVRVoiceMod interfaces into the Oculus voice-mod engine. This component should be added
31/// into the scene once.
32///
33/// </summary>
34public class OVRVoiceMod : MonoBehaviour
35{
36 public const int ovrVoiceModSuccess = 0;
37
38 // Error codes that may return from VoiceMod engine
39 public enum ovrVoiceModError
40 {
41 Unknown = -2250, //< An unknown error has occurred
42 CannotCreateContext = -2251, //< Unable to create a context
43 InvalidParam = -2252, //< An invalid parameter, e.g. NULL pointer or out of range
44 BadSampleRate = -2253, //< An unsupported sample rate was declared
45 MissingDLL = -2254, //< The DLL or shared library could not be found
46 BadVersion = -2255, //< Mismatched versions between header and libs
47 UndefinedFunction = -2256 //< An undefined function
48 };
49
50 /// Flags (unused at this time)
51 public enum ovrViceModFlag
52 {
53 None = 0x0000,
54 };
55
56 /// NOTE: Opaque typedef for voice mod context is an unsigned int (uint)
57
58 // * * * * * * * * * * * * *
59 // Import functions
60 public const string strOVRLS = "OVRVoiceMod";
61 [DllImport(strOVRLS)]
62 private static extern int ovrVoiceModDll_Initialize(int SampleRate, int BufferSize);
63 [DllImport(strOVRLS)]
64 private static extern void ovrVoiceModDll_Shutdown();
65 [DllImport(strOVRLS)]
66 private static extern IntPtr ovrVoicemodDll_GetVersion(ref int Major,
67 ref int Minor,
68 ref int Patch);
69 [DllImport(strOVRLS)]
70 private static extern int ovrVoiceModDll_CreateContext(ref uint Context);
71 [DllImport(strOVRLS)]
72 private static extern int ovrVoiceModDll_DestroyContext(uint Context);
73 [DllImport(strOVRLS)]
74 private static extern int ovrVoiceModDll_SendParameter(uint Context, int Parameter, int Value);
75 [DllImport(strOVRLS)]
76 private static extern int ovrVoiceModDll_ProcessFrame(uint Context, uint Flags, float [] AudioBuffer);
77 [DllImport(strOVRLS)]
78 private static extern int ovrVoiceModDll_ProcessFrameInterleaved(uint Context, uint Flags, float [] AudioBuffer);
79 [DllImport(strOVRLS)]
80 private static extern int ovrVoiceModDll_GetAverageAbsVolume(uint Context, ref float Volume);
81
82 // * * * * * * * * * * * * *
83 // Public members
84
85 // * * * * * * * * * * * * *
86 // Static members
87 private static int sOVRVoiceModInit = (int)ovrVoiceModError.Unknown;
88
89 // interface through this static member.
90 public static OVRVoiceMod sInstance = null;
91
92 // * * * * * * * * * * * * *
93 // MonoBehaviour overrides
94
95 /// <summary>
96 /// Awake this instance.
97 /// </summary>
98 void Awake ()
99 {
100 // We can only have one instance of OVRLipSync in a scene (use this for local property query)
101 if(sInstance == null)
102 {
103 sInstance = this;
104 }
105 else
106 {
107 Debug.LogWarning (System.String.Format ("OVRVoiceMod Awake: Only one instance of OVRVoiceMod can exist in the scene."));
108 return;
109 }
110
111 int samplerate;
112 int bufsize;
113 int numbuf;
114
115 // Get the current sample rate
116 samplerate = AudioSettings.outputSampleRate;
117 // Get the current buffer size and number of buffers
118 AudioSettings.GetDSPBufferSize (out bufsize, out numbuf);
119
120 String str = System.String.Format
121 ("OvrVoiceMod Awake: Queried SampleRate: {0:F0} BufferSize: {1:F0}", samplerate, bufsize);
122 Debug.LogWarning (str);
123
124 sOVRVoiceModInit = ovrVoiceModDll_Initialize(samplerate, bufsize);
125
126 if(sOVRVoiceModInit != ovrVoiceModSuccess)
127 {
128 Debug.LogWarning (System.String.Format
129 ("OvrVoiceMod Awake: Failed to init VoiceMod library"));
130 }
131
132 // Important: Use the touchpad mechanism for input, call Create on the OVRTouchpad helper class
133 OVRTouchpad.Create();
134 }
135
136 /// <summary>
137 /// Start this instance.
138 /// Note: make sure to always have a Start function for classes that have editor scripts.
139 /// </summary>
140 void Start()
141 {
142 }
143
144 /// <summary>
145 /// Run processes that need to be updated in our game thread
146 /// </summary>
147 void Update()
148 {
149 }
150
151 /// <summary>
152 /// Raises the destroy event.
153 /// </summary>
154 void OnDestroy()
155 {
156 if(sInstance != this)
157 {
158 Debug.LogWarning ("OVRVoiceMod OnDestroy: This is not the correct OVRVoiceMod instance.");
159 }
160
161 ovrVoiceModDll_Shutdown();
162 sOVRVoiceModInit = (int)ovrVoiceModError.Unknown;
163 }
164
165 // * * * * * * * * * * * * *
166 // Public Functions
167
168 /// <summary>
169 /// Determines if is initialized.
170 /// </summary>
171 /// <returns><c>true</c> if is initialized; otherwise, <c>false</c>.</returns>
172 public static int IsInitialized()
173 {
174 return sOVRVoiceModInit;
175 }
176
177 /// <summary>
178 /// Creates the context.
179 /// </summary>
180 /// <returns>The context.</returns>
181 /// <param name="context">Context.</param>
182 public static int CreateContext(ref uint context)
183 {
184 if(IsInitialized() != ovrVoiceModSuccess)
185 return (int)ovrVoiceModError.CannotCreateContext;
186
187 return ovrVoiceModDll_CreateContext(ref context);
188 }
189
190 /// <summary>
191 /// Destroies the context.
192 /// </summary>
193 /// <returns>The context.</returns>
194 /// <param name="context">Context.</param>
195 public static int DestroyContext (uint context)
196 {
197 if(IsInitialized() != ovrVoiceModSuccess)
198 return (int)ovrVoiceModError.Unknown;
199
200 return ovrVoiceModDll_DestroyContext(context);
201 }
202
203 /// <summary>
204 /// Sends the parameter.
205 /// </summary>
206 /// <returns>The parameter.</returns>
207 /// <param name="context">Context.</param>
208 /// <param name="parameter">Parameter.</param>
209 /// <param name="value">Value.</param>
210 public static int SendParameter(uint context, int parameter, int value)
211 {
212 if(IsInitialized() != ovrVoiceModSuccess)
213 return (int)ovrVoiceModError.Unknown;
214
215 return ovrVoiceModDll_SendParameter(context, parameter, value);
216 }
217
218 /// <summary>
219 /// Processes the frame.
220 /// </summary>
221 /// <returns>The frame.</returns>
222 /// <param name="context">Context.</param>
223 /// <param name="audioBuffer">Audio buffer.</param>
224 public static int ProcessFrame(uint context, float [] audioBuffer)
225 {
226 if(IsInitialized() != ovrVoiceModSuccess)
227 return (int)ovrVoiceModError.Unknown;
228
229 return ovrVoiceModDll_ProcessFrame(context, (uint)ovrViceModFlag.None , audioBuffer);
230 }
231
232 /// <summary>
233 /// Processes the frame interleaved.
234 /// </summary>
235 /// <returns>The frame interleaved.</returns>
236 /// <param name="context">Context.</param>
237 /// <param name="audioBuffer">Audio buffer.</param>
238 public static int ProcessFrameInterleaved(uint context, float [] audioBuffer)
239 {
240 if(IsInitialized() != ovrVoiceModSuccess)
241 return (int)ovrVoiceModError.Unknown;
242
243 return ovrVoiceModDll_ProcessFrameInterleaved(context, (uint)ovrViceModFlag.None, audioBuffer);
244 }
245
246 /// <summary>
247 /// Gets the average abs volume.
248 /// </summary>
249 /// <returns>The average abs volume.</returns>
250 /// <param name="context">Context.</param>
251 /// <param name="volume">Volume.</param>
252 public static float GetAverageAbsVolume(uint context)
253 {
254 if(IsInitialized() != ovrVoiceModSuccess)
255 return 0.0f;
256
257 float volume = 0;
258
259 int result = ovrVoiceModDll_GetAverageAbsVolume(context, ref volume);
260
261 return volume;
262 }
263}