BlueGit

blue-star ggj-2020

Files

ggj-2020 / Assets / Oculus / VR / Scripts / OVRHapticsClip.cs

OVRHapticsClip.cs

Project Import
timfa Tim Falken · pushed by timfa · 6 years ago
b77834a1 History
OVRHapticsClip.cs 5.0 KiB Download Raw
1/************************************************************************************
2Copyright : Copyright (c) Facebook Technologies, LLC and its affiliates. All rights reserved.
3
4Licensed under the Oculus Utilities SDK License Version 1.31 (the "License"); you may not use
5the Utilities SDK except in compliance with the License, which is provided at the time of installation
6or download, or which otherwise accompanies this software in either electronic or hard copy form.
7
8You may obtain a copy of the License at
9https://developer.oculus.com/licenses/utilities-1.31
10
11Unless required by applicable law or agreed to in writing, the Utilities SDK distributed
12under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF
13ANY KIND, either express or implied. See the License for the specific language governing
14permissions and limitations under the License.
15************************************************************************************/
16
17using UnityEngine;
18using System.Collections;
19
20/// <summary>
21/// A PCM buffer of data for a haptics effect.
22/// </summary>
23public class OVRHapticsClip
24{
25 /// <summary>
26 /// The current number of samples in the clip.
27 /// </summary>
28 public int Count { get; private set; }
29
30 /// <summary>
31 /// The maximum number of samples the clip can store.
32 /// </summary>
33 public int Capacity { get; private set; }
34
35 /// <summary>
36 /// The raw haptics data.
37 /// </summary>
38 public byte[] Samples { get; private set; }
39
40 public OVRHapticsClip()
41 {
42 Capacity = OVRHaptics.Config.MaximumBufferSamplesCount;
43 Samples = new byte[Capacity * OVRHaptics.Config.SampleSizeInBytes];
44 }
45
46 /// <summary>
47 /// Creates a clip with the specified capacity.
48 /// </summary>
49 public OVRHapticsClip(int capacity)
50 {
51 Capacity = (capacity >= 0) ? capacity : 0;
52 Samples = new byte[Capacity * OVRHaptics.Config.SampleSizeInBytes];
53 }
54
55 /// <summary>
56 /// Creates a clip with the specified data.
57 /// </summary>
58 public OVRHapticsClip(byte[] samples, int samplesCount)
59 {
60 Samples = samples;
61 Capacity = Samples.Length / OVRHaptics.Config.SampleSizeInBytes;
62 Count = (samplesCount >= 0) ? samplesCount : 0;
63 }
64
65 /// <summary>
66 /// Creates a clip by mixing the specified clips.
67 /// </summary>
68 public OVRHapticsClip(OVRHapticsClip a, OVRHapticsClip b)
69 {
70 int maxCount = a.Count;
71 if (b.Count > maxCount)
72 maxCount = b.Count;
73
74 Capacity = maxCount;
75 Samples = new byte[Capacity * OVRHaptics.Config.SampleSizeInBytes];
76
77 for (int i = 0; i < a.Count || i < b.Count; i++)
78 {
79 if (OVRHaptics.Config.SampleSizeInBytes == 1)
80 {
81 byte sample = 0; // TODO support multi-byte samples
82 if ((i < a.Count) && (i < b.Count))
83 sample = (byte)(Mathf.Clamp(a.Samples[i] + b.Samples[i], 0, System.Byte.MaxValue)); // TODO support multi-byte samples
84 else if (i < a.Count)
85 sample = a.Samples[i]; // TODO support multi-byte samples
86 else if (i < b.Count)
87 sample = b.Samples[i]; // TODO support multi-byte samples
88
89 WriteSample(sample); // TODO support multi-byte samples
90 }
91 }
92 }
93
94 /// <summary>
95 /// Creates a haptics clip from the specified audio clip.
96 /// </summary>
97 public OVRHapticsClip(AudioClip audioClip, int channel = 0)
98 {
99 float[] audioData = new float[audioClip.samples * audioClip.channels];
100 audioClip.GetData(audioData, 0);
101
102 InitializeFromAudioFloatTrack(audioData, audioClip.frequency, audioClip.channels, channel);
103 }
104
105 /// <summary>
106 /// Adds the specified sample to the end of the clip.
107 /// </summary>
108 public void WriteSample(byte sample) // TODO support multi-byte samples
109 {
110 if (Count >= Capacity)
111 {
112 //Debug.LogError("Attempted to write OVRHapticsClip sample out of range - Count:" + Count + " Capacity:" + Capacity);
113 return;
114 }
115
116 if (OVRHaptics.Config.SampleSizeInBytes == 1)
117 {
118 Samples[Count * OVRHaptics.Config.SampleSizeInBytes] = sample; // TODO support multi-byte samples
119 }
120
121 Count++;
122 }
123
124 /// <summary>
125 /// Clears the clip and resets its size to 0.
126 /// </summary>
127 public void Reset()
128 {
129 Count = 0;
130 }
131
132 private void InitializeFromAudioFloatTrack(float[] sourceData, double sourceFrequency, int sourceChannelCount, int sourceChannel)
133 {
134 double stepSizePrecise = (sourceFrequency + 1e-6) / OVRHaptics.Config.SampleRateHz;
135
136 if (stepSizePrecise < 1.0)
137 return;
138
139 int stepSize = (int)stepSizePrecise;
140 double stepSizeError = stepSizePrecise - stepSize;
141 double accumulatedStepSizeError = 0.0f;
142 int length = sourceData.Length;
143
144 Count = 0;
145 Capacity = length / sourceChannelCount / stepSize + 1;
146 Samples = new byte[Capacity * OVRHaptics.Config.SampleSizeInBytes];
147
148 int i = sourceChannel % sourceChannelCount;
149 while (i < length)
150 {
151 if (OVRHaptics.Config.SampleSizeInBytes == 1)
152 {
153 WriteSample((byte)(Mathf.Clamp01(Mathf.Abs(sourceData[i])) * System.Byte.MaxValue)); // TODO support multi-byte samples
154 }
155 i+= stepSize * sourceChannelCount;
156 accumulatedStepSizeError += stepSizeError;
157 if ((int)accumulatedStepSizeError > 0)
158 {
159 i+= (int)accumulatedStepSizeError * sourceChannelCount;
160 accumulatedStepSizeError = accumulatedStepSizeError - (int)accumulatedStepSizeError;
161 }
162 }
163 }
164}