OVRLipSync.cs 9.0 KB

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