| 1 | /************************************************************************************ |
| 2 | Copyright : Copyright (c) Facebook Technologies, LLC and its affiliates. All rights reserved. |
| 3 | |
| 4 | Licensed under the Oculus Utilities SDK License Version 1.31 (the "License"); you may not use |
| 5 | the Utilities SDK except in compliance with the License, which is provided at the time of installation |
| 6 | or download, or which otherwise accompanies this software in either electronic or hard copy form. |
| 7 | |
| 8 | You may obtain a copy of the License at |
| 9 | https://developer.oculus.com/licenses/utilities-1.31 |
| 10 | |
| 11 | Unless required by applicable law or agreed to in writing, the Utilities SDK distributed |
| 12 | under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF |
| 13 | ANY KIND, either express or implied. See the License for the specific language governing |
| 14 | permissions and limitations under the License. |
| 15 | ************************************************************************************/ |
| 16 | |
| 17 | using UnityEngine; |
| 18 | using System; |
| 19 | using System.IO; |
| 20 | using System.Collections; |
| 21 | using System.Collections.Generic; |
| 22 | using System.Runtime.InteropServices; |
| 23 | |
| 24 | /// <summary> |
| 25 | /// Plays tactile effects on a tracked VR controller. |
| 26 | /// </summary> |
| 27 | public static class OVRHaptics |
| 28 | { |
| 29 | public readonly static OVRHapticsChannel[] Channels; |
| 30 | public readonly static OVRHapticsChannel LeftChannel; |
| 31 | public readonly static OVRHapticsChannel RightChannel; |
| 32 | |
| 33 | private readonly static OVRHapticsOutput[] m_outputs; |
| 34 | |
| 35 | static OVRHaptics() |
| 36 | { |
| 37 | Config.Load(); |
| 38 | |
| 39 | m_outputs = new OVRHapticsOutput[] |
| 40 | { |
| 41 | new OVRHapticsOutput((uint)OVRPlugin.Controller.LTouch), |
| 42 | new OVRHapticsOutput((uint)OVRPlugin.Controller.RTouch), |
| 43 | }; |
| 44 | |
| 45 | Channels = new OVRHapticsChannel[] |
| 46 | { |
| 47 | LeftChannel = new OVRHapticsChannel(0), |
| 48 | RightChannel = new OVRHapticsChannel(1), |
| 49 | }; |
| 50 | } |
| 51 | |
| 52 | /// <summary> |
| 53 | /// Determines the target format for haptics data on a specific device. |
| 54 | /// </summary> |
| 55 | public static class Config |
| 56 | { |
| 57 | public static int SampleRateHz { get; private set; } |
| 58 | public static int SampleSizeInBytes { get; private set; } |
| 59 | public static int MinimumSafeSamplesQueued { get; private set; } |
| 60 | public static int MinimumBufferSamplesCount { get; private set; } |
| 61 | public static int OptimalBufferSamplesCount { get; private set; } |
| 62 | public static int MaximumBufferSamplesCount { get; private set; } |
| 63 | |
| 64 | static Config() |
| 65 | { |
| 66 | Load(); |
| 67 | } |
| 68 | |
| 69 | public static void Load() |
| 70 | { |
| 71 | OVRPlugin.HapticsDesc desc = OVRPlugin.GetControllerHapticsDesc((uint)OVRPlugin.Controller.RTouch); |
| 72 | |
| 73 | SampleRateHz = desc.SampleRateHz; |
| 74 | SampleSizeInBytes = desc.SampleSizeInBytes; |
| 75 | MinimumSafeSamplesQueued = desc.MinimumSafeSamplesQueued; |
| 76 | MinimumBufferSamplesCount = desc.MinimumBufferSamplesCount; |
| 77 | OptimalBufferSamplesCount = desc.OptimalBufferSamplesCount; |
| 78 | MaximumBufferSamplesCount = desc.MaximumBufferSamplesCount; |
| 79 | } |
| 80 | } |
| 81 | |
| 82 | /// <summary> |
| 83 | /// A track of haptics data that can be mixed or sequenced with another track. |
| 84 | /// </summary> |
| 85 | public class OVRHapticsChannel |
| 86 | { |
| 87 | private OVRHapticsOutput m_output; |
| 88 | |
| 89 | /// <summary> |
| 90 | /// Constructs a channel targeting the specified output. |
| 91 | /// </summary> |
| 92 | public OVRHapticsChannel(uint outputIndex) |
| 93 | { |
| 94 | m_output = m_outputs[outputIndex]; |
| 95 | } |
| 96 | |
| 97 | /// <summary> |
| 98 | /// Cancels any currently-playing clips and immediatly plays the specified clip instead. |
| 99 | /// </summary> |
| 100 | public void Preempt(OVRHapticsClip clip) |
| 101 | { |
| 102 | m_output.Preempt(clip); |
| 103 | } |
| 104 | |
| 105 | /// <summary> |
| 106 | /// Enqueues the specified clip to play after any currently-playing clips finish. |
| 107 | /// </summary> |
| 108 | public void Queue(OVRHapticsClip clip) |
| 109 | { |
| 110 | m_output.Queue(clip); |
| 111 | } |
| 112 | |
| 113 | /// <summary> |
| 114 | /// Adds the specified clip to play simultaneously to the currently-playing clip(s). |
| 115 | /// </summary> |
| 116 | public void Mix(OVRHapticsClip clip) |
| 117 | { |
| 118 | m_output.Mix(clip); |
| 119 | } |
| 120 | |
| 121 | /// <summary> |
| 122 | /// Cancels any currently-playing clips. |
| 123 | /// </summary> |
| 124 | public void Clear() |
| 125 | { |
| 126 | m_output.Clear(); |
| 127 | } |
| 128 | } |
| 129 | |
| 130 | private class OVRHapticsOutput |
| 131 | { |
| 132 | private class ClipPlaybackTracker |
| 133 | { |
| 134 | public int ReadCount { get; set; } |
| 135 | public OVRHapticsClip Clip { get; set; } |
| 136 | |
| 137 | public ClipPlaybackTracker(OVRHapticsClip clip) |
| 138 | { |
| 139 | Clip = clip; |
| 140 | } |
| 141 | } |
| 142 | |
| 143 | private bool m_lowLatencyMode = true; |
| 144 | private bool m_paddingEnabled = true; |
| 145 | private int m_prevSamplesQueued = 0; |
| 146 | private float m_prevSamplesQueuedTime = 0; |
| 147 | private int m_numPredictionHits = 0; |
| 148 | private int m_numPredictionMisses = 0; |
| 149 | private int m_numUnderruns = 0; |
| 150 | private List<ClipPlaybackTracker> m_pendingClips = new List<ClipPlaybackTracker>(); |
| 151 | private uint m_controller = 0; |
| 152 | private OVRNativeBuffer m_nativeBuffer = new OVRNativeBuffer(OVRHaptics.Config.MaximumBufferSamplesCount * OVRHaptics.Config.SampleSizeInBytes); |
| 153 | private OVRHapticsClip m_paddingClip = new OVRHapticsClip(); |
| 154 | |
| 155 | public OVRHapticsOutput(uint controller) |
| 156 | { |
| 157 | #if UNITY_ANDROID |
| 158 | m_paddingEnabled = false; |
| 159 | #endif |
| 160 | m_controller = controller; |
| 161 | } |
| 162 | |
| 163 | /// <summary> |
| 164 | /// The system calls this each frame to update haptics playback. |
| 165 | /// </summary> |
| 166 | public void Process() |
| 167 | { |
| 168 | var hapticsState = OVRPlugin.GetControllerHapticsState(m_controller); |
| 169 | |
| 170 | float elapsedTime = Time.realtimeSinceStartup - m_prevSamplesQueuedTime; |
| 171 | if (m_prevSamplesQueued > 0) |
| 172 | { |
| 173 | int expectedSamples = m_prevSamplesQueued - (int)(elapsedTime * OVRHaptics.Config.SampleRateHz + 0.5f); |
| 174 | if (expectedSamples < 0) |
| 175 | expectedSamples = 0; |
| 176 | |
| 177 | if ((hapticsState.SamplesQueued - expectedSamples) == 0) |
| 178 | m_numPredictionHits++; |
| 179 | else |
| 180 | m_numPredictionMisses++; |
| 181 | |
| 182 | //Debug.Log(hapticsState.SamplesAvailable + "a " + hapticsState.SamplesQueued + "q " + expectedSamples + "e " |
| 183 | //+ "Prediction Accuracy: " + m_numPredictionHits / (float)(m_numPredictionMisses + m_numPredictionHits)); |
| 184 | |
| 185 | if ((expectedSamples > 0) && (hapticsState.SamplesQueued == 0)) |
| 186 | { |
| 187 | m_numUnderruns++; |
| 188 | //Debug.LogError("Samples Underrun (" + m_controller + " #" + m_numUnderruns + ") -" |
| 189 | // + " Expected: " + expectedSamples |
| 190 | // + " Actual: " + hapticsState.SamplesQueued); |
| 191 | } |
| 192 | |
| 193 | m_prevSamplesQueued = hapticsState.SamplesQueued; |
| 194 | m_prevSamplesQueuedTime = Time.realtimeSinceStartup; |
| 195 | } |
| 196 | |
| 197 | int desiredSamplesCount = OVRHaptics.Config.OptimalBufferSamplesCount; |
| 198 | if (m_lowLatencyMode) |
| 199 | { |
| 200 | float sampleRateMs = 1000.0f / (float)OVRHaptics.Config.SampleRateHz; |
| 201 | float elapsedMs = elapsedTime * 1000.0f; |
| 202 | int samplesNeededPerFrame = (int)Mathf.Ceil(elapsedMs / sampleRateMs); |
| 203 | int lowLatencySamplesCount = OVRHaptics.Config.MinimumSafeSamplesQueued + samplesNeededPerFrame; |
| 204 | |
| 205 | if (lowLatencySamplesCount < desiredSamplesCount) |
| 206 | desiredSamplesCount = lowLatencySamplesCount; |
| 207 | } |
| 208 | |
| 209 | if (hapticsState.SamplesQueued > desiredSamplesCount) |
| 210 | return; |
| 211 | |
| 212 | if (desiredSamplesCount > OVRHaptics.Config.MaximumBufferSamplesCount) |
| 213 | desiredSamplesCount = OVRHaptics.Config.MaximumBufferSamplesCount; |
| 214 | if (desiredSamplesCount > hapticsState.SamplesAvailable) |
| 215 | desiredSamplesCount = hapticsState.SamplesAvailable; |
| 216 | |
| 217 | int acquiredSamplesCount = 0; |
| 218 | int clipIndex = 0; |
| 219 | while(acquiredSamplesCount < desiredSamplesCount && clipIndex < m_pendingClips.Count) |
| 220 | { |
| 221 | int numSamplesToCopy = desiredSamplesCount - acquiredSamplesCount; |
| 222 | int remainingSamplesInClip = m_pendingClips[clipIndex].Clip.Count - m_pendingClips[clipIndex].ReadCount; |
| 223 | if (numSamplesToCopy > remainingSamplesInClip) |
| 224 | numSamplesToCopy = remainingSamplesInClip; |
| 225 | |
| 226 | if (numSamplesToCopy > 0) |
| 227 | { |
| 228 | int numBytes = numSamplesToCopy * OVRHaptics.Config.SampleSizeInBytes; |
| 229 | int dstOffset = acquiredSamplesCount * OVRHaptics.Config.SampleSizeInBytes; |
| 230 | int srcOffset = m_pendingClips[clipIndex].ReadCount * OVRHaptics.Config.SampleSizeInBytes; |
| 231 | Marshal.Copy(m_pendingClips[clipIndex].Clip.Samples, srcOffset, m_nativeBuffer.GetPointer(dstOffset), numBytes); |
| 232 | |
| 233 | m_pendingClips[clipIndex].ReadCount += numSamplesToCopy; |
| 234 | acquiredSamplesCount += numSamplesToCopy; |
| 235 | } |
| 236 | |
| 237 | clipIndex++; |
| 238 | } |
| 239 | |
| 240 | for (int i = m_pendingClips.Count - 1; i >= 0 && m_pendingClips.Count > 0; i--) |
| 241 | { |
| 242 | if (m_pendingClips[i].ReadCount >= m_pendingClips[i].Clip.Count) |
| 243 | m_pendingClips.RemoveAt(i); |
| 244 | } |
| 245 | |
| 246 | if (m_paddingEnabled) |
| 247 | { |
| 248 | int desiredPadding = desiredSamplesCount - (hapticsState.SamplesQueued + acquiredSamplesCount); |
| 249 | if (desiredPadding < (OVRHaptics.Config.MinimumBufferSamplesCount - acquiredSamplesCount)) |
| 250 | desiredPadding = (OVRHaptics.Config.MinimumBufferSamplesCount - acquiredSamplesCount); |
| 251 | if (desiredPadding > hapticsState.SamplesAvailable) |
| 252 | desiredPadding = hapticsState.SamplesAvailable; |
| 253 | |
| 254 | if (desiredPadding > 0) |
| 255 | { |
| 256 | int numBytes = desiredPadding * OVRHaptics.Config.SampleSizeInBytes; |
| 257 | int dstOffset = acquiredSamplesCount * OVRHaptics.Config.SampleSizeInBytes; |
| 258 | int srcOffset = 0; |
| 259 | Marshal.Copy(m_paddingClip.Samples, srcOffset, m_nativeBuffer.GetPointer(dstOffset), numBytes); |
| 260 | |
| 261 | acquiredSamplesCount += desiredPadding; |
| 262 | } |
| 263 | } |
| 264 | |
| 265 | if (acquiredSamplesCount > 0) |
| 266 | { |
| 267 | OVRPlugin.HapticsBuffer hapticsBuffer; |
| 268 | hapticsBuffer.Samples = m_nativeBuffer.GetPointer(); |
| 269 | hapticsBuffer.SamplesCount = acquiredSamplesCount; |
| 270 | |
| 271 | OVRPlugin.SetControllerHaptics(m_controller, hapticsBuffer); |
| 272 | |
| 273 | hapticsState = OVRPlugin.GetControllerHapticsState(m_controller); |
| 274 | m_prevSamplesQueued = hapticsState.SamplesQueued; |
| 275 | m_prevSamplesQueuedTime = Time.realtimeSinceStartup; |
| 276 | } |
| 277 | } |
| 278 | |
| 279 | /// <summary> |
| 280 | /// Immediately plays the specified clip without waiting for any currently-playing clip to finish. |
| 281 | /// </summary> |
| 282 | public void Preempt(OVRHapticsClip clip) |
| 283 | { |
| 284 | m_pendingClips.Clear(); |
| 285 | m_pendingClips.Add(new ClipPlaybackTracker(clip)); |
| 286 | } |
| 287 | |
| 288 | /// <summary> |
| 289 | /// Enqueues the specified clip to play after any currently-playing clip finishes. |
| 290 | /// </summary> |
| 291 | public void Queue(OVRHapticsClip clip) |
| 292 | { |
| 293 | m_pendingClips.Add(new ClipPlaybackTracker(clip)); |
| 294 | } |
| 295 | |
| 296 | /// <summary> |
| 297 | /// Adds the samples from the specified clip to the ones in the currently-playing clip(s). |
| 298 | /// </summary> |
| 299 | public void Mix(OVRHapticsClip clip) |
| 300 | { |
| 301 | int numClipsToMix = 0; |
| 302 | int numSamplesToMix = 0; |
| 303 | int numSamplesRemaining = clip.Count; |
| 304 | |
| 305 | while (numSamplesRemaining > 0 && numClipsToMix < m_pendingClips.Count) |
| 306 | { |
| 307 | int numSamplesRemainingInClip = m_pendingClips[numClipsToMix].Clip.Count - m_pendingClips[numClipsToMix].ReadCount; |
| 308 | numSamplesRemaining -= numSamplesRemainingInClip; |
| 309 | numSamplesToMix += numSamplesRemainingInClip; |
| 310 | numClipsToMix++; |
| 311 | } |
| 312 | |
| 313 | if (numSamplesRemaining > 0) |
| 314 | { |
| 315 | numSamplesToMix += numSamplesRemaining; |
| 316 | numSamplesRemaining = 0; |
| 317 | } |
| 318 | |
| 319 | if (numClipsToMix > 0) |
| 320 | { |
| 321 | OVRHapticsClip mixClip = new OVRHapticsClip(numSamplesToMix); |
| 322 | |
| 323 | OVRHapticsClip a = clip; |
| 324 | int aReadCount = 0; |
| 325 | |
| 326 | for (int i = 0; i < numClipsToMix; i++) |
| 327 | { |
| 328 | OVRHapticsClip b = m_pendingClips[i].Clip; |
| 329 | for(int bReadCount = m_pendingClips[i].ReadCount; bReadCount < b.Count; bReadCount++) |
| 330 | { |
| 331 | if (OVRHaptics.Config.SampleSizeInBytes == 1) |
| 332 | { |
| 333 | byte sample = 0; // TODO support multi-byte samples |
| 334 | if ((aReadCount < a.Count) && (bReadCount < b.Count)) |
| 335 | { |
| 336 | sample = (byte)(Mathf.Clamp(a.Samples[aReadCount] + b.Samples[bReadCount], 0, System.Byte.MaxValue)); // TODO support multi-byte samples |
| 337 | aReadCount++; |
| 338 | } |
| 339 | else if (bReadCount < b.Count) |
| 340 | { |
| 341 | sample = b.Samples[bReadCount]; // TODO support multi-byte samples |
| 342 | } |
| 343 | |
| 344 | mixClip.WriteSample(sample); // TODO support multi-byte samples |
| 345 | } |
| 346 | } |
| 347 | } |
| 348 | |
| 349 | while (aReadCount < a.Count) |
| 350 | { |
| 351 | if (OVRHaptics.Config.SampleSizeInBytes == 1) |
| 352 | { |
| 353 | mixClip.WriteSample(a.Samples[aReadCount]); // TODO support multi-byte samples |
| 354 | } |
| 355 | aReadCount++; |
| 356 | } |
| 357 | |
| 358 | m_pendingClips[0] = new ClipPlaybackTracker(mixClip); |
| 359 | for (int i = 1; i < numClipsToMix; i++) |
| 360 | { |
| 361 | m_pendingClips.RemoveAt(1); |
| 362 | } |
| 363 | } |
| 364 | else |
| 365 | { |
| 366 | m_pendingClips.Add(new ClipPlaybackTracker(clip)); |
| 367 | } |
| 368 | } |
| 369 | |
| 370 | public void Clear() |
| 371 | { |
| 372 | m_pendingClips.Clear(); |
| 373 | } |
| 374 | } |
| 375 | |
| 376 | /// <summary> |
| 377 | /// The system calls this each frame to update haptics playback. |
| 378 | /// </summary> |
| 379 | public static void Process() |
| 380 | { |
| 381 | Config.Load(); |
| 382 | |
| 383 | for (int i = 0; i < m_outputs.Length; i++) |
| 384 | { |
| 385 | m_outputs[i].Process(); |
| 386 | } |
| 387 | } |
| 388 | } |