OVRHapticsClip.cs 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. using System;
  2. using UnityEngine;
  3. public class OVRHapticsClip
  4. {
  5. public OVRHapticsClip()
  6. {
  7. this.Capacity = OVRHaptics.Config.MaximumBufferSamplesCount;
  8. this.Samples = new byte[this.Capacity * OVRHaptics.Config.SampleSizeInBytes];
  9. }
  10. public OVRHapticsClip(int capacity)
  11. {
  12. this.Capacity = ((capacity < 0) ? 0 : capacity);
  13. this.Samples = new byte[this.Capacity * OVRHaptics.Config.SampleSizeInBytes];
  14. }
  15. public OVRHapticsClip(byte[] samples, int samplesCount)
  16. {
  17. this.Samples = samples;
  18. this.Capacity = this.Samples.Length / OVRHaptics.Config.SampleSizeInBytes;
  19. this.Count = ((samplesCount < 0) ? 0 : samplesCount);
  20. }
  21. public OVRHapticsClip(OVRHapticsClip a, OVRHapticsClip b)
  22. {
  23. int count = a.Count;
  24. if (b.Count > count)
  25. {
  26. count = b.Count;
  27. }
  28. this.Capacity = count;
  29. this.Samples = new byte[this.Capacity * OVRHaptics.Config.SampleSizeInBytes];
  30. int num = 0;
  31. while (num < a.Count || num < b.Count)
  32. {
  33. if (OVRHaptics.Config.SampleSizeInBytes == 1)
  34. {
  35. byte sample = 0;
  36. if (num < a.Count && num < b.Count)
  37. {
  38. sample = (byte)Mathf.Clamp((int)(a.Samples[num] + b.Samples[num]), 0, 255);
  39. }
  40. else if (num < a.Count)
  41. {
  42. sample = a.Samples[num];
  43. }
  44. else if (num < b.Count)
  45. {
  46. sample = b.Samples[num];
  47. }
  48. this.WriteSample(sample);
  49. }
  50. num++;
  51. }
  52. }
  53. public OVRHapticsClip(AudioClip audioClip, int channel = 0)
  54. {
  55. float[] array = new float[audioClip.samples * audioClip.channels];
  56. audioClip.GetData(array, 0);
  57. this.InitializeFromAudioFloatTrack(array, (double)audioClip.frequency, audioClip.channels, channel);
  58. }
  59. public int Count { get; private set; }
  60. public int Capacity { get; private set; }
  61. public byte[] Samples { get; private set; }
  62. public void WriteSample(byte sample)
  63. {
  64. if (this.Count >= this.Capacity)
  65. {
  66. return;
  67. }
  68. if (OVRHaptics.Config.SampleSizeInBytes == 1)
  69. {
  70. this.Samples[this.Count * OVRHaptics.Config.SampleSizeInBytes] = sample;
  71. }
  72. this.Count++;
  73. }
  74. public void Reset()
  75. {
  76. this.Count = 0;
  77. }
  78. private void InitializeFromAudioFloatTrack(float[] sourceData, double sourceFrequency, int sourceChannelCount, int sourceChannel)
  79. {
  80. double num = sourceFrequency / (double)OVRHaptics.Config.SampleRateHz;
  81. int num2 = (int)num;
  82. double num3 = num - (double)num2;
  83. double num4 = 0.0;
  84. int num5 = sourceData.Length;
  85. this.Count = 0;
  86. this.Capacity = num5 / sourceChannelCount / num2 + 1;
  87. this.Samples = new byte[this.Capacity * OVRHaptics.Config.SampleSizeInBytes];
  88. int i = sourceChannel % sourceChannelCount;
  89. while (i < num5)
  90. {
  91. if (OVRHaptics.Config.SampleSizeInBytes == 1)
  92. {
  93. this.WriteSample((byte)(Mathf.Clamp01(Mathf.Abs(sourceData[i])) * 255f));
  94. }
  95. i += num2 * sourceChannelCount;
  96. num4 += num3;
  97. if ((int)num4 > 0)
  98. {
  99. i += (int)num4 * sourceChannelCount;
  100. num4 -= (double)((int)num4);
  101. }
  102. }
  103. }
  104. }