LowLatencyLipSyncContext.cs 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. using System;
  2. using UnityEngine;
  3. namespace XVI.AniLipSync
  4. {
  5. public class LowLatencyLipSyncContext : OVRLipSyncContextBase
  6. {
  7. private OVRLipSync.VariableData m_Variable
  8. {
  9. get
  10. {
  11. return GameMain.Instance.LipSyncMgr.Variable;
  12. }
  13. }
  14. public override void Init()
  15. {
  16. base.Init();
  17. this.m_MicInput = base.GetComponent<OVRLipSyncMicInput>();
  18. this.processBuffer = new float[OVRLipSync.BuffSize];
  19. this.microphoneBuffer = new float[OVRLipSync.SamplingRate * 10];
  20. }
  21. private void Update()
  22. {
  23. if (!this.audioSource.clip)
  24. {
  25. return;
  26. }
  27. if (this.firstFreqCheck)
  28. {
  29. int frequency = this.audioSource.clip.frequency;
  30. int samplingRate = OVRLipSync.SamplingRate;
  31. if (frequency < samplingRate)
  32. {
  33. NUty.WinMessageBox(NUty.GetWindowHandle(), string.Concat(new object[]
  34. {
  35. "マイクのサンプリングレート ",
  36. frequency,
  37. "Hz がスピーカーのサンプリングレート\u3000",
  38. samplingRate,
  39. "Hz より低い設定になっています。マイクのサンプリングレートをスピーカー側に合せて下さい。"
  40. }), "警告", 48);
  41. }
  42. this.firstFreqCheck = false;
  43. }
  44. int position = Microphone.GetPosition(this.m_MicInput.selectedDevice);
  45. if (position < 0 || this.head == position)
  46. {
  47. return;
  48. }
  49. this.audioSource.clip.GetData(this.microphoneBuffer, 0);
  50. while (LowLatencyLipSyncContext.GetDataLength(this.microphoneBuffer.Length, this.head, position) > this.processBuffer.Length)
  51. {
  52. int num = this.microphoneBuffer.Length - this.head;
  53. if (num < this.processBuffer.Length)
  54. {
  55. Array.Copy(this.microphoneBuffer, this.head, this.processBuffer, 0, num);
  56. Array.Copy(this.microphoneBuffer, 0, this.processBuffer, num, this.processBuffer.Length - num);
  57. }
  58. else
  59. {
  60. Array.Copy(this.microphoneBuffer, this.head, this.processBuffer, 0, this.processBuffer.Length);
  61. }
  62. float[] array = new float[this.processBuffer.Length];
  63. Array.Copy(this.processBuffer, array, array.Length);
  64. for (int i = 0; i < array.Length; i++)
  65. {
  66. array[i] *= this.m_Variable.MicSensitivity;
  67. }
  68. OVRLipSync.ProcessFrame(base.Context, array, OVRLipSync.Flags.None, base.Frame);
  69. this.head += this.processBuffer.Length;
  70. if (this.head > this.microphoneBuffer.Length)
  71. {
  72. this.head -= this.microphoneBuffer.Length;
  73. }
  74. }
  75. }
  76. public float GetMicVolume()
  77. {
  78. float num = 0f;
  79. foreach (float f in this.processBuffer)
  80. {
  81. num += Mathf.Abs(f);
  82. }
  83. return num / (float)this.processBuffer.Length;
  84. }
  85. private static int GetDataLength(int bufferLength, int head, int tail)
  86. {
  87. if (head < tail)
  88. {
  89. return tail - head;
  90. }
  91. return bufferLength - head + tail;
  92. }
  93. private AudioClip clip;
  94. private int head;
  95. private float[] processBuffer = new float[512];
  96. private float[] microphoneBuffer = new float[441000];
  97. private OVRLipSyncMicInput m_MicInput;
  98. private bool firstFreqCheck = true;
  99. }
  100. }