OVRLipSync.cs 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Runtime.InteropServices;
  5. using UnityEngine;
  6. public class OVRLipSync : MonoBehaviour
  7. {
  8. [DllImport("OVRLipSync")]
  9. private static extern int ovrLipSyncDll_Initialize(int samplerate, int buffersize);
  10. [DllImport("OVRLipSync")]
  11. private static extern void ovrLipSyncDll_Shutdown();
  12. [DllImport("OVRLipSync")]
  13. private static extern IntPtr ovrLipSyncDll_GetVersion(ref int Major, ref int Minor, ref int Patch);
  14. [DllImport("OVRLipSync")]
  15. private static extern int ovrLipSyncDll_CreateContext(ref uint context, OVRLipSync.ContextProviders provider);
  16. [DllImport("OVRLipSync")]
  17. private static extern int ovrLipSyncDll_DestroyContext(uint context);
  18. [DllImport("OVRLipSync")]
  19. private static extern int ovrLipSyncDll_ResetContext(uint context);
  20. [DllImport("OVRLipSync")]
  21. private static extern int ovrLipSyncDll_SendSignal(uint context, OVRLipSync.Signals signal, int arg1, int arg2);
  22. [DllImport("OVRLipSync")]
  23. private static extern int ovrLipSyncDll_ProcessFrame(uint context, float[] audioBuffer, OVRLipSync.Flags flags, ref int frameNumber, ref int frameDelay, float[] visemes, int visemeCount);
  24. [DllImport("OVRLipSync")]
  25. private static extern int ovrLipSyncDll_ProcessFrameInterleaved(uint context, float[] audioBuffer, OVRLipSync.Flags flags, ref int frameNumber, ref int frameDelay, float[] visemes, int visemeCount);
  26. public OVRLipSync.VariableData Variable
  27. {
  28. get
  29. {
  30. return this.m_Variable;
  31. }
  32. }
  33. public static int SamplingRate { get; private set; }
  34. public static int BuffSize { get; private set; }
  35. public void Init()
  36. {
  37. if (OVRLipSync.sInstance == null)
  38. {
  39. OVRLipSync.sInstance = this;
  40. int outputSampleRate = AudioSettings.outputSampleRate;
  41. int num;
  42. int num2;
  43. AudioSettings.GetDSPBufferSize(out num, out num2);
  44. OVRLipSync.SamplingRate = outputSampleRate;
  45. OVRLipSync.BuffSize = num;
  46. string message = string.Format("OvrLipSync Awake: Queried SampleRate: {0:F0} BufferSize: {1:F0}", outputSampleRate, num);
  47. Debug.LogWarning(message);
  48. OVRLipSync.sInitialized = OVRLipSync.Initialize(outputSampleRate, num);
  49. if (OVRLipSync.sInitialized != OVRLipSync.Result.Success)
  50. {
  51. Debug.LogWarning(string.Format("OvrLipSync Awake: Failed to init Speech Rec library", new object[0]));
  52. }
  53. return;
  54. }
  55. Debug.LogWarning(string.Format("OVRLipSync Awake: Only one instance of OVRPLipSync can exist in the scene.", new object[0]));
  56. }
  57. private void OnDestroy()
  58. {
  59. if (OVRLipSync.sInstance != this)
  60. {
  61. Debug.LogWarning("OVRLipSync OnDestroy: This is not the correct OVRLipSync instance.");
  62. return;
  63. }
  64. }
  65. public static OVRLipSync.Result Initialize(int sampleRate, int bufferSize)
  66. {
  67. OVRLipSync.sInitialized = (OVRLipSync.Result)OVRLipSync.ovrLipSyncDll_Initialize(sampleRate, bufferSize);
  68. return OVRLipSync.sInitialized;
  69. }
  70. public static void Shutdown()
  71. {
  72. OVRLipSync.ovrLipSyncDll_Shutdown();
  73. OVRLipSync.sInitialized = OVRLipSync.Result.Unknown;
  74. }
  75. public static OVRLipSync.Result IsInitialized()
  76. {
  77. return OVRLipSync.sInitialized;
  78. }
  79. public static OVRLipSync.Result CreateContext(ref uint context, OVRLipSync.ContextProviders provider)
  80. {
  81. if (OVRLipSync.IsInitialized() != OVRLipSync.Result.Success)
  82. {
  83. return OVRLipSync.Result.CannotCreateContext;
  84. }
  85. return (OVRLipSync.Result)OVRLipSync.ovrLipSyncDll_CreateContext(ref context, provider);
  86. }
  87. public static OVRLipSync.Result DestroyContext(uint context)
  88. {
  89. if (OVRLipSync.IsInitialized() != OVRLipSync.Result.Success)
  90. {
  91. return OVRLipSync.Result.Unknown;
  92. }
  93. return (OVRLipSync.Result)OVRLipSync.ovrLipSyncDll_DestroyContext(context);
  94. }
  95. public static OVRLipSync.Result ResetContext(uint context)
  96. {
  97. if (OVRLipSync.IsInitialized() != OVRLipSync.Result.Success)
  98. {
  99. return OVRLipSync.Result.Unknown;
  100. }
  101. return (OVRLipSync.Result)OVRLipSync.ovrLipSyncDll_ResetContext(context);
  102. }
  103. public static OVRLipSync.Result SendSignal(uint context, OVRLipSync.Signals signal, int arg1, int arg2)
  104. {
  105. if (OVRLipSync.IsInitialized() != OVRLipSync.Result.Success)
  106. {
  107. return OVRLipSync.Result.Unknown;
  108. }
  109. return (OVRLipSync.Result)OVRLipSync.ovrLipSyncDll_SendSignal(context, signal, arg1, arg2);
  110. }
  111. public static OVRLipSync.Result ProcessFrame(uint context, float[] audioBuffer, OVRLipSync.Flags flags, OVRLipSync.Frame frame)
  112. {
  113. if (OVRLipSync.IsInitialized() != OVRLipSync.Result.Success)
  114. {
  115. return OVRLipSync.Result.Unknown;
  116. }
  117. return (OVRLipSync.Result)OVRLipSync.ovrLipSyncDll_ProcessFrame(context, audioBuffer, flags, ref frame.frameNumber, ref frame.frameDelay, frame.Visemes, frame.Visemes.Length);
  118. }
  119. public static OVRLipSync.Result ProcessFrameInterleaved(uint context, float[] audioBuffer, OVRLipSync.Flags flags, OVRLipSync.Frame frame)
  120. {
  121. if (OVRLipSync.IsInitialized() != OVRLipSync.Result.Success)
  122. {
  123. return OVRLipSync.Result.Unknown;
  124. }
  125. return (OVRLipSync.Result)OVRLipSync.ovrLipSyncDll_ProcessFrameInterleaved(context, audioBuffer, flags, ref frame.frameNumber, ref frame.frameDelay, frame.Visemes, frame.Visemes.Length);
  126. }
  127. public void SetLipSync(Maid maid, bool is_start = true)
  128. {
  129. maid.SetMicLipSync(is_start);
  130. }
  131. public void SetLipSync(int maid_no = 0, bool is_start = true)
  132. {
  133. Maid maid = GameMain.Instance.CharacterMgr.GetMaid(maid_no);
  134. if (!maid)
  135. {
  136. Debug.LogErrorFormat("メイド{0}番は存在しません", new object[]
  137. {
  138. maid_no
  139. });
  140. return;
  141. }
  142. this.SetLipSync(maid, is_start);
  143. }
  144. public OVRLipSync.MorphData GetMorphData(OVRLipSync.Viseme viseme)
  145. {
  146. return this.m_VisemeMorphPair[viseme];
  147. }
  148. public OVRLipSync.MorphData GetMorphData(int index)
  149. {
  150. return this.m_VisemeMorphPair.ElementAt(index).Value;
  151. }
  152. private Dictionary<OVRLipSync.Viseme, OVRLipSync.MorphData> m_VisemeMorphPair = new Dictionary<OVRLipSync.Viseme, OVRLipSync.MorphData>
  153. {
  154. {
  155. OVRLipSync.Viseme.sil,
  156. new OVRLipSync.MorphData(0f, 0f, 0f)
  157. },
  158. {
  159. OVRLipSync.Viseme.PP,
  160. new OVRLipSync.MorphData(0f, 0f, 1f)
  161. },
  162. {
  163. OVRLipSync.Viseme.FF,
  164. new OVRLipSync.MorphData(0f, 0f, 1f)
  165. },
  166. {
  167. OVRLipSync.Viseme.TH,
  168. new OVRLipSync.MorphData(0f, 0.5f, 0f)
  169. },
  170. {
  171. OVRLipSync.Viseme.DD,
  172. new OVRLipSync.MorphData(0f, 0f, 1f)
  173. },
  174. {
  175. OVRLipSync.Viseme.kk,
  176. new OVRLipSync.MorphData(0f, 0f, 1f)
  177. },
  178. {
  179. OVRLipSync.Viseme.CH,
  180. new OVRLipSync.MorphData(0f, 0.5f, 0f)
  181. },
  182. {
  183. OVRLipSync.Viseme.SS,
  184. new OVRLipSync.MorphData(0f, 0f, 1f)
  185. },
  186. {
  187. OVRLipSync.Viseme.nn,
  188. new OVRLipSync.MorphData(0f, 0f, 0.7f)
  189. },
  190. {
  191. OVRLipSync.Viseme.RR,
  192. new OVRLipSync.MorphData(0f, 0f, 0f)
  193. },
  194. {
  195. OVRLipSync.Viseme.aa,
  196. new OVRLipSync.MorphData(1f, 0f, 0f)
  197. },
  198. {
  199. OVRLipSync.Viseme.E,
  200. new OVRLipSync.MorphData(0f, 1f, 0f)
  201. },
  202. {
  203. OVRLipSync.Viseme.ih,
  204. new OVRLipSync.MorphData(0f, 0.5f, 0f)
  205. },
  206. {
  207. OVRLipSync.Viseme.oh,
  208. new OVRLipSync.MorphData(1f, 0f, 0.4f)
  209. },
  210. {
  211. OVRLipSync.Viseme.ou,
  212. new OVRLipSync.MorphData(0f, 0f, 1f)
  213. }
  214. };
  215. public static readonly int VisemeCount = Enum.GetNames(typeof(OVRLipSync.Viseme)).Length;
  216. public static readonly int SignalCount = Enum.GetNames(typeof(OVRLipSync.Signals)).Length;
  217. public const string strOVRLS = "OVRLipSync";
  218. private static OVRLipSync.Result sInitialized = OVRLipSync.Result.Unknown;
  219. public static OVRLipSync sInstance = null;
  220. public const float SensitivityMin = 1f;
  221. public const float SensitivityMax = 10f;
  222. [SerializeField]
  223. [Header("各数値設定")]
  224. private OVRLipSync.VariableData m_Variable = new OVRLipSync.VariableData();
  225. public enum Result
  226. {
  227. Success,
  228. Unknown = -2200,
  229. CannotCreateContext = -2201,
  230. InvalidParam = -2202,
  231. BadSampleRate = -2203,
  232. MissingDLL = -2204,
  233. BadVersion = -2205,
  234. UndefinedFunction = -2206
  235. }
  236. public enum Viseme
  237. {
  238. sil,
  239. PP,
  240. FF,
  241. TH,
  242. DD,
  243. kk,
  244. CH,
  245. SS,
  246. nn,
  247. RR,
  248. aa,
  249. E,
  250. ih,
  251. oh,
  252. ou
  253. }
  254. public class MorphData
  255. {
  256. public MorphData(float lip1 = 0f, float lip2 = 0f, float lip3 = 0f)
  257. {
  258. this.LipSync1 = lip1;
  259. this.LipSync2 = lip2;
  260. this.LipSync3 = lip3;
  261. }
  262. public float LipSync1;
  263. public float LipSync2;
  264. public float LipSync3;
  265. }
  266. public enum Flags
  267. {
  268. None,
  269. DelayCompensateAudio
  270. }
  271. public enum Signals
  272. {
  273. VisemeOn,
  274. VisemeOff,
  275. VisemeAmount,
  276. VisemeSmoothing
  277. }
  278. public enum ContextProviders
  279. {
  280. Main,
  281. Other
  282. }
  283. [Serializable]
  284. public class Frame
  285. {
  286. public void CopyInput(OVRLipSync.Frame input)
  287. {
  288. this.frameNumber = input.frameNumber;
  289. this.frameDelay = input.frameDelay;
  290. input.Visemes.CopyTo(this.Visemes, 0);
  291. }
  292. public int frameNumber;
  293. public int frameDelay;
  294. public float[] Visemes = new float[OVRLipSync.VisemeCount];
  295. }
  296. [Serializable]
  297. public class VariableData
  298. {
  299. public float MicSensitivity
  300. {
  301. get
  302. {
  303. return this.m_MicSensitivity;
  304. }
  305. set
  306. {
  307. this.m_MicSensitivity = Mathf.Clamp(value, 1f, 10f);
  308. }
  309. }
  310. public float MicVolume
  311. {
  312. get
  313. {
  314. return this.m_MicVolume;
  315. }
  316. set
  317. {
  318. this.m_MicVolume = Mathf.Clamp01(value);
  319. }
  320. }
  321. [SerializeField]
  322. [Header("マイクの感度")]
  323. [Range(1f, 10f)]
  324. private float m_MicSensitivity = 1f;
  325. [Header("入力された音声を再生しない")]
  326. public bool Mute = true;
  327. [SerializeField]
  328. [Header("出力するマイクの音量")]
  329. [Range(0f, 1f)]
  330. private float m_MicVolume = 1f;
  331. }
  332. }