AudioSource.cs 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361
  1. using RootSystem = System;
  2. using System.Linq;
  3. using System.Collections.Generic;
  4. namespace Windows.Kinect
  5. {
  6. //
  7. // Windows.Kinect.AudioSource
  8. //
  9. public sealed partial class AudioSource : Helper.INativeWrapper
  10. {
  11. internal RootSystem.IntPtr _pNative;
  12. RootSystem.IntPtr Helper.INativeWrapper.nativePtr { get { return _pNative; } }
  13. // Constructors and Finalizers
  14. internal AudioSource(RootSystem.IntPtr pNative)
  15. {
  16. _pNative = pNative;
  17. Windows_Kinect_AudioSource_AddRefObject(ref _pNative);
  18. }
  19. ~AudioSource()
  20. {
  21. Dispose(false);
  22. }
  23. [RootSystem.Runtime.InteropServices.DllImport("KinectUnityAddin", CallingConvention=RootSystem.Runtime.InteropServices.CallingConvention.Cdecl, SetLastError=true)]
  24. private static extern void Windows_Kinect_AudioSource_ReleaseObject(ref RootSystem.IntPtr pNative);
  25. [RootSystem.Runtime.InteropServices.DllImport("KinectUnityAddin", CallingConvention=RootSystem.Runtime.InteropServices.CallingConvention.Cdecl, SetLastError=true)]
  26. private static extern void Windows_Kinect_AudioSource_AddRefObject(ref RootSystem.IntPtr pNative);
  27. private void Dispose(bool disposing)
  28. {
  29. if (_pNative == RootSystem.IntPtr.Zero)
  30. {
  31. return;
  32. }
  33. __EventCleanup();
  34. Helper.NativeObjectCache.RemoveObject<AudioSource>(_pNative);
  35. Windows_Kinect_AudioSource_ReleaseObject(ref _pNative);
  36. _pNative = RootSystem.IntPtr.Zero;
  37. }
  38. // Public Properties
  39. [RootSystem.Runtime.InteropServices.DllImport("KinectUnityAddin", CallingConvention=RootSystem.Runtime.InteropServices.CallingConvention.Cdecl, SetLastError=true)]
  40. private static extern int Windows_Kinect_AudioSource_get_AudioBeams(RootSystem.IntPtr pNative, [RootSystem.Runtime.InteropServices.Out] RootSystem.IntPtr[] outCollection, int outCollectionSize);
  41. [RootSystem.Runtime.InteropServices.DllImport("KinectUnityAddin", CallingConvention=RootSystem.Runtime.InteropServices.CallingConvention.Cdecl, SetLastError=true)]
  42. private static extern int Windows_Kinect_AudioSource_get_AudioBeams_Length(RootSystem.IntPtr pNative);
  43. public RootSystem.Collections.Generic.IList<Windows.Kinect.AudioBeam> AudioBeams
  44. {
  45. get
  46. {
  47. if (_pNative == RootSystem.IntPtr.Zero)
  48. {
  49. throw new RootSystem.ObjectDisposedException("AudioSource");
  50. }
  51. int outCollectionSize = Windows_Kinect_AudioSource_get_AudioBeams_Length(_pNative);
  52. var outCollection = new RootSystem.IntPtr[outCollectionSize];
  53. var managedCollection = new Windows.Kinect.AudioBeam[outCollectionSize];
  54. outCollectionSize = Windows_Kinect_AudioSource_get_AudioBeams(_pNative, outCollection, outCollectionSize);
  55. Helper.ExceptionHelper.CheckLastError();
  56. for(int i=0;i<outCollectionSize;i++)
  57. {
  58. if(outCollection[i] == RootSystem.IntPtr.Zero)
  59. {
  60. continue;
  61. }
  62. var obj = Helper.NativeObjectCache.CreateOrGetObject<Windows.Kinect.AudioBeam>(outCollection[i], n => new Windows.Kinect.AudioBeam(n));
  63. managedCollection[i] = obj;
  64. }
  65. return managedCollection;
  66. }
  67. }
  68. [RootSystem.Runtime.InteropServices.DllImport("KinectUnityAddin", CallingConvention=RootSystem.Runtime.InteropServices.CallingConvention.Cdecl, SetLastError=true)]
  69. private static extern bool Windows_Kinect_AudioSource_get_IsActive(RootSystem.IntPtr pNative);
  70. public bool IsActive
  71. {
  72. get
  73. {
  74. if (_pNative == RootSystem.IntPtr.Zero)
  75. {
  76. throw new RootSystem.ObjectDisposedException("AudioSource");
  77. }
  78. return Windows_Kinect_AudioSource_get_IsActive(_pNative);
  79. }
  80. }
  81. [RootSystem.Runtime.InteropServices.DllImport("KinectUnityAddin", CallingConvention=RootSystem.Runtime.InteropServices.CallingConvention.Cdecl, SetLastError=true)]
  82. private static extern RootSystem.IntPtr Windows_Kinect_AudioSource_get_KinectSensor(RootSystem.IntPtr pNative);
  83. public Windows.Kinect.KinectSensor KinectSensor
  84. {
  85. get
  86. {
  87. if (_pNative == RootSystem.IntPtr.Zero)
  88. {
  89. throw new RootSystem.ObjectDisposedException("AudioSource");
  90. }
  91. RootSystem.IntPtr objectPointer = Windows_Kinect_AudioSource_get_KinectSensor(_pNative);
  92. Helper.ExceptionHelper.CheckLastError();
  93. if (objectPointer == RootSystem.IntPtr.Zero)
  94. {
  95. return null;
  96. }
  97. return Helper.NativeObjectCache.CreateOrGetObject<Windows.Kinect.KinectSensor>(objectPointer, n => new Windows.Kinect.KinectSensor(n));
  98. }
  99. }
  100. [RootSystem.Runtime.InteropServices.DllImport("KinectUnityAddin", CallingConvention=RootSystem.Runtime.InteropServices.CallingConvention.Cdecl, SetLastError=true)]
  101. private static extern uint Windows_Kinect_AudioSource_get_MaxSubFrameCount(RootSystem.IntPtr pNative);
  102. public uint MaxSubFrameCount
  103. {
  104. get
  105. {
  106. if (_pNative == RootSystem.IntPtr.Zero)
  107. {
  108. throw new RootSystem.ObjectDisposedException("AudioSource");
  109. }
  110. return Windows_Kinect_AudioSource_get_MaxSubFrameCount(_pNative);
  111. }
  112. }
  113. [RootSystem.Runtime.InteropServices.DllImport("KinectUnityAddin", CallingConvention=RootSystem.Runtime.InteropServices.CallingConvention.Cdecl, SetLastError=true)]
  114. private static extern long Windows_Kinect_AudioSource_get_SubFrameDuration(RootSystem.IntPtr pNative);
  115. public RootSystem.TimeSpan SubFrameDuration
  116. {
  117. get
  118. {
  119. if (_pNative == RootSystem.IntPtr.Zero)
  120. {
  121. throw new RootSystem.ObjectDisposedException("AudioSource");
  122. }
  123. return RootSystem.TimeSpan.FromMilliseconds(Windows_Kinect_AudioSource_get_SubFrameDuration(_pNative));
  124. }
  125. }
  126. [RootSystem.Runtime.InteropServices.DllImport("KinectUnityAddin", CallingConvention=RootSystem.Runtime.InteropServices.CallingConvention.Cdecl, SetLastError=true)]
  127. private static extern uint Windows_Kinect_AudioSource_get_SubFrameLengthInBytes(RootSystem.IntPtr pNative);
  128. public uint SubFrameLengthInBytes
  129. {
  130. get
  131. {
  132. if (_pNative == RootSystem.IntPtr.Zero)
  133. {
  134. throw new RootSystem.ObjectDisposedException("AudioSource");
  135. }
  136. return Windows_Kinect_AudioSource_get_SubFrameLengthInBytes(_pNative);
  137. }
  138. }
  139. [RootSystem.Runtime.InteropServices.DllImport("KinectUnityAddin", CallingConvention=RootSystem.Runtime.InteropServices.CallingConvention.Cdecl, SetLastError=true)]
  140. private static extern Windows.Kinect.KinectAudioCalibrationState Windows_Kinect_AudioSource_get_AudioCalibrationState(RootSystem.IntPtr pNative);
  141. public Windows.Kinect.KinectAudioCalibrationState AudioCalibrationState
  142. {
  143. get
  144. {
  145. if (_pNative == RootSystem.IntPtr.Zero)
  146. {
  147. throw new RootSystem.ObjectDisposedException("AudioSource");
  148. }
  149. return Windows_Kinect_AudioSource_get_AudioCalibrationState(_pNative);
  150. }
  151. }
  152. // Events
  153. private static RootSystem.Runtime.InteropServices.GCHandle _Windows_Kinect_FrameCapturedEventArgs_Delegate_Handle;
  154. [RootSystem.Runtime.InteropServices.UnmanagedFunctionPointer(RootSystem.Runtime.InteropServices.CallingConvention.Cdecl)]
  155. private delegate void _Windows_Kinect_FrameCapturedEventArgs_Delegate(RootSystem.IntPtr args, RootSystem.IntPtr pNative);
  156. private static Helper.CollectionMap<RootSystem.IntPtr, List<RootSystem.EventHandler<Windows.Kinect.FrameCapturedEventArgs>>> Windows_Kinect_FrameCapturedEventArgs_Delegate_callbacks = new Helper.CollectionMap<RootSystem.IntPtr, List<RootSystem.EventHandler<Windows.Kinect.FrameCapturedEventArgs>>>();
  157. [AOT.MonoPInvokeCallbackAttribute(typeof(_Windows_Kinect_FrameCapturedEventArgs_Delegate))]
  158. private static void Windows_Kinect_FrameCapturedEventArgs_Delegate_Handler(RootSystem.IntPtr result, RootSystem.IntPtr pNative)
  159. {
  160. List<RootSystem.EventHandler<Windows.Kinect.FrameCapturedEventArgs>> callbackList = null;
  161. Windows_Kinect_FrameCapturedEventArgs_Delegate_callbacks.TryGetValue(pNative, out callbackList);
  162. lock(callbackList)
  163. {
  164. var objThis = Helper.NativeObjectCache.GetObject<AudioSource>(pNative);
  165. var args = new Windows.Kinect.FrameCapturedEventArgs(result);
  166. foreach(var func in callbackList)
  167. {
  168. Helper.EventPump.Instance.Enqueue(() => { try { func(objThis, args); } catch { } });
  169. }
  170. }
  171. }
  172. [RootSystem.Runtime.InteropServices.DllImport("KinectUnityAddin", CallingConvention=RootSystem.Runtime.InteropServices.CallingConvention.Cdecl, SetLastError=true)]
  173. private static extern void Windows_Kinect_AudioSource_add_FrameCaptured(RootSystem.IntPtr pNative, _Windows_Kinect_FrameCapturedEventArgs_Delegate eventCallback, bool unsubscribe);
  174. public event RootSystem.EventHandler<Windows.Kinect.FrameCapturedEventArgs> FrameCaptured
  175. {
  176. add
  177. {
  178. Helper.EventPump.EnsureInitialized();
  179. Windows_Kinect_FrameCapturedEventArgs_Delegate_callbacks.TryAddDefault(_pNative);
  180. var callbackList = Windows_Kinect_FrameCapturedEventArgs_Delegate_callbacks[_pNative];
  181. lock (callbackList)
  182. {
  183. callbackList.Add(value);
  184. if(callbackList.Count == 1)
  185. {
  186. var del = new _Windows_Kinect_FrameCapturedEventArgs_Delegate(Windows_Kinect_FrameCapturedEventArgs_Delegate_Handler);
  187. _Windows_Kinect_FrameCapturedEventArgs_Delegate_Handle = RootSystem.Runtime.InteropServices.GCHandle.Alloc(del);
  188. Windows_Kinect_AudioSource_add_FrameCaptured(_pNative, del, false);
  189. }
  190. }
  191. }
  192. remove
  193. {
  194. if (_pNative == RootSystem.IntPtr.Zero)
  195. {
  196. return;
  197. }
  198. Windows_Kinect_FrameCapturedEventArgs_Delegate_callbacks.TryAddDefault(_pNative);
  199. var callbackList = Windows_Kinect_FrameCapturedEventArgs_Delegate_callbacks[_pNative];
  200. lock (callbackList)
  201. {
  202. callbackList.Remove(value);
  203. if(callbackList.Count == 0)
  204. {
  205. Windows_Kinect_AudioSource_add_FrameCaptured(_pNative, Windows_Kinect_FrameCapturedEventArgs_Delegate_Handler, true);
  206. _Windows_Kinect_FrameCapturedEventArgs_Delegate_Handle.Free();
  207. }
  208. }
  209. }
  210. }
  211. private static RootSystem.Runtime.InteropServices.GCHandle _Windows_Data_PropertyChangedEventArgs_Delegate_Handle;
  212. [RootSystem.Runtime.InteropServices.UnmanagedFunctionPointer(RootSystem.Runtime.InteropServices.CallingConvention.Cdecl)]
  213. private delegate void _Windows_Data_PropertyChangedEventArgs_Delegate(RootSystem.IntPtr args, RootSystem.IntPtr pNative);
  214. private static Helper.CollectionMap<RootSystem.IntPtr, List<RootSystem.EventHandler<Windows.Data.PropertyChangedEventArgs>>> Windows_Data_PropertyChangedEventArgs_Delegate_callbacks = new Helper.CollectionMap<RootSystem.IntPtr, List<RootSystem.EventHandler<Windows.Data.PropertyChangedEventArgs>>>();
  215. [AOT.MonoPInvokeCallbackAttribute(typeof(_Windows_Data_PropertyChangedEventArgs_Delegate))]
  216. private static void Windows_Data_PropertyChangedEventArgs_Delegate_Handler(RootSystem.IntPtr result, RootSystem.IntPtr pNative)
  217. {
  218. List<RootSystem.EventHandler<Windows.Data.PropertyChangedEventArgs>> callbackList = null;
  219. Windows_Data_PropertyChangedEventArgs_Delegate_callbacks.TryGetValue(pNative, out callbackList);
  220. lock(callbackList)
  221. {
  222. var objThis = Helper.NativeObjectCache.GetObject<AudioSource>(pNative);
  223. var args = new Windows.Data.PropertyChangedEventArgs(result);
  224. foreach(var func in callbackList)
  225. {
  226. Helper.EventPump.Instance.Enqueue(() => { try { func(objThis, args); } catch { } });
  227. }
  228. }
  229. }
  230. [RootSystem.Runtime.InteropServices.DllImport("KinectUnityAddin", CallingConvention=RootSystem.Runtime.InteropServices.CallingConvention.Cdecl, SetLastError=true)]
  231. private static extern void Windows_Kinect_AudioSource_add_PropertyChanged(RootSystem.IntPtr pNative, _Windows_Data_PropertyChangedEventArgs_Delegate eventCallback, bool unsubscribe);
  232. public event RootSystem.EventHandler<Windows.Data.PropertyChangedEventArgs> PropertyChanged
  233. {
  234. add
  235. {
  236. Helper.EventPump.EnsureInitialized();
  237. Windows_Data_PropertyChangedEventArgs_Delegate_callbacks.TryAddDefault(_pNative);
  238. var callbackList = Windows_Data_PropertyChangedEventArgs_Delegate_callbacks[_pNative];
  239. lock (callbackList)
  240. {
  241. callbackList.Add(value);
  242. if(callbackList.Count == 1)
  243. {
  244. var del = new _Windows_Data_PropertyChangedEventArgs_Delegate(Windows_Data_PropertyChangedEventArgs_Delegate_Handler);
  245. _Windows_Data_PropertyChangedEventArgs_Delegate_Handle = RootSystem.Runtime.InteropServices.GCHandle.Alloc(del);
  246. Windows_Kinect_AudioSource_add_PropertyChanged(_pNative, del, false);
  247. }
  248. }
  249. }
  250. remove
  251. {
  252. if (_pNative == RootSystem.IntPtr.Zero)
  253. {
  254. return;
  255. }
  256. Windows_Data_PropertyChangedEventArgs_Delegate_callbacks.TryAddDefault(_pNative);
  257. var callbackList = Windows_Data_PropertyChangedEventArgs_Delegate_callbacks[_pNative];
  258. lock (callbackList)
  259. {
  260. callbackList.Remove(value);
  261. if(callbackList.Count == 0)
  262. {
  263. Windows_Kinect_AudioSource_add_PropertyChanged(_pNative, Windows_Data_PropertyChangedEventArgs_Delegate_Handler, true);
  264. _Windows_Data_PropertyChangedEventArgs_Delegate_Handle.Free();
  265. }
  266. }
  267. }
  268. }
  269. // Public Methods
  270. [RootSystem.Runtime.InteropServices.DllImport("KinectUnityAddin", CallingConvention=RootSystem.Runtime.InteropServices.CallingConvention.Cdecl, SetLastError=true)]
  271. private static extern RootSystem.IntPtr Windows_Kinect_AudioSource_OpenReader(RootSystem.IntPtr pNative);
  272. public Windows.Kinect.AudioBeamFrameReader OpenReader()
  273. {
  274. if (_pNative == RootSystem.IntPtr.Zero)
  275. {
  276. throw new RootSystem.ObjectDisposedException("AudioSource");
  277. }
  278. RootSystem.IntPtr objectPointer = Windows_Kinect_AudioSource_OpenReader(_pNative);
  279. Helper.ExceptionHelper.CheckLastError();
  280. if (objectPointer == RootSystem.IntPtr.Zero)
  281. {
  282. return null;
  283. }
  284. return Helper.NativeObjectCache.CreateOrGetObject<Windows.Kinect.AudioBeamFrameReader>(objectPointer, n => new Windows.Kinect.AudioBeamFrameReader(n));
  285. }
  286. private void __EventCleanup()
  287. {
  288. {
  289. Windows_Kinect_FrameCapturedEventArgs_Delegate_callbacks.TryAddDefault(_pNative);
  290. var callbackList = Windows_Kinect_FrameCapturedEventArgs_Delegate_callbacks[_pNative];
  291. lock (callbackList)
  292. {
  293. if (callbackList.Count > 0)
  294. {
  295. callbackList.Clear();
  296. if (_pNative != RootSystem.IntPtr.Zero)
  297. {
  298. Windows_Kinect_AudioSource_add_FrameCaptured(_pNative, Windows_Kinect_FrameCapturedEventArgs_Delegate_Handler, true);
  299. }
  300. _Windows_Kinect_FrameCapturedEventArgs_Delegate_Handle.Free();
  301. }
  302. }
  303. }
  304. {
  305. Windows_Data_PropertyChangedEventArgs_Delegate_callbacks.TryAddDefault(_pNative);
  306. var callbackList = Windows_Data_PropertyChangedEventArgs_Delegate_callbacks[_pNative];
  307. lock (callbackList)
  308. {
  309. if (callbackList.Count > 0)
  310. {
  311. callbackList.Clear();
  312. if (_pNative != RootSystem.IntPtr.Zero)
  313. {
  314. Windows_Kinect_AudioSource_add_PropertyChanged(_pNative, Windows_Data_PropertyChangedEventArgs_Delegate_Handler, true);
  315. }
  316. _Windows_Data_PropertyChangedEventArgs_Delegate_Handle.Free();
  317. }
  318. }
  319. }
  320. }
  321. }
  322. }