SoundIOOutStream.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Runtime.InteropServices;
  4. namespace SoundIOSharp
  5. {
  6. public class SoundIOOutStream : IDisposable
  7. {
  8. internal SoundIOOutStream (Pointer<SoundIoOutStream> handle)
  9. {
  10. this.handle = handle;
  11. }
  12. Pointer<SoundIoOutStream> handle;
  13. public void Dispose ()
  14. {
  15. Natives.soundio_outstream_destroy (handle);
  16. }
  17. // Equality (based on handle)
  18. public override bool Equals (object other)
  19. {
  20. var d = other as SoundIOOutStream;
  21. return d != null && (this.handle == d.handle);
  22. }
  23. public override int GetHashCode ()
  24. {
  25. return (int)(IntPtr)handle;
  26. }
  27. public static bool operator == (SoundIOOutStream obj1, SoundIOOutStream obj2)
  28. {
  29. return obj1 is null ? obj2 is null : obj1.Equals(obj2);
  30. }
  31. public static bool operator != (SoundIOOutStream obj1, SoundIOOutStream obj2)
  32. {
  33. return obj1 is null ? obj2 is object : !obj1.Equals(obj2);
  34. }
  35. // fields
  36. public SoundIODevice Device
  37. {
  38. get { return new SoundIODevice(Marshal.ReadIntPtr(handle, device_offset)); }
  39. }
  40. static readonly int device_offset = (int)Marshal.OffsetOf<SoundIoOutStream>("device");
  41. public SoundIOFormat Format
  42. {
  43. get { return (SoundIOFormat) Marshal.ReadInt32(handle, format_offset); }
  44. set { Marshal.WriteInt32(handle, format_offset, (int) value); }
  45. }
  46. static readonly int format_offset = (int)Marshal.OffsetOf<SoundIoOutStream>("format");
  47. public int SampleRate
  48. {
  49. get { return Marshal.ReadInt32(handle, sample_rate_offset); }
  50. set { Marshal.WriteInt32(handle, sample_rate_offset, value); }
  51. }
  52. static readonly int sample_rate_offset = (int)Marshal.OffsetOf<SoundIoOutStream>("sample_rate");
  53. public SoundIOChannelLayout Layout
  54. {
  55. get { unsafe { return new SoundIOChannelLayout((IntPtr) (void*)((IntPtr)handle + layout_offset)); } }
  56. set
  57. {
  58. unsafe
  59. {
  60. Buffer.MemoryCopy((void*)value.Handle, (void*)((IntPtr)handle + layout_offset), Marshal.SizeOf<SoundIoChannelLayout>(), Marshal.SizeOf<SoundIoChannelLayout>());
  61. }
  62. }
  63. }
  64. static readonly int layout_offset = (int)Marshal.OffsetOf<SoundIoOutStream>("layout");
  65. public double SoftwareLatency
  66. {
  67. get { return MarshalEx.ReadDouble (handle, software_latency_offset); }
  68. set { MarshalEx.WriteDouble (handle, software_latency_offset, value); }
  69. }
  70. static readonly int software_latency_offset = (int)Marshal.OffsetOf<SoundIoOutStream>("software_latency");
  71. public float Volume
  72. {
  73. get { return MarshalEx.ReadFloat(handle, volume_offset); }
  74. set { MarshalEx.WriteFloat(handle, volume_offset, value); }
  75. }
  76. static readonly int volume_offset = (int)Marshal.OffsetOf<SoundIoOutStream>("volume");
  77. // error_callback
  78. public Action ErrorCallback
  79. {
  80. get { return error_callback; }
  81. set
  82. {
  83. error_callback = value;
  84. if (value == null)
  85. {
  86. error_callback_native = null;
  87. }
  88. else
  89. {
  90. error_callback_native = stream => error_callback();
  91. }
  92. var ptr = Marshal.GetFunctionPointerForDelegate(error_callback_native);
  93. Marshal.WriteIntPtr(handle, error_callback_offset, ptr);
  94. }
  95. }
  96. static readonly int error_callback_offset = (int)Marshal.OffsetOf<SoundIoOutStream>("error_callback");
  97. Action error_callback;
  98. delegate void error_callback_delegate (IntPtr handle);
  99. error_callback_delegate error_callback_native;
  100. // write_callback
  101. public Action<int, int> WriteCallback
  102. {
  103. get { return write_callback; }
  104. set
  105. {
  106. write_callback = value;
  107. if (value == null)
  108. {
  109. write_callback_native = null;
  110. }
  111. else
  112. {
  113. write_callback_native = (h, frame_count_min, frame_count_max) => write_callback(frame_count_min, frame_count_max);
  114. }
  115. var ptr = Marshal.GetFunctionPointerForDelegate (write_callback_native);
  116. Marshal.WriteIntPtr (handle, write_callback_offset, ptr);
  117. }
  118. }
  119. static readonly int write_callback_offset = (int)Marshal.OffsetOf<SoundIoOutStream>("write_callback");
  120. Action<int, int> write_callback;
  121. delegate void write_callback_delegate(IntPtr handle, int min, int max);
  122. write_callback_delegate write_callback_native;
  123. // underflow_callback
  124. public Action UnderflowCallback
  125. {
  126. get { return underflow_callback; }
  127. set
  128. {
  129. underflow_callback = value;
  130. if (value == null)
  131. {
  132. underflow_callback_native = null;
  133. }
  134. else
  135. {
  136. underflow_callback_native = h => underflow_callback();
  137. }
  138. var ptr = Marshal.GetFunctionPointerForDelegate (underflow_callback_native);
  139. Marshal.WriteIntPtr (handle, underflow_callback_offset, ptr);
  140. }
  141. }
  142. static readonly int underflow_callback_offset = (int)Marshal.OffsetOf<SoundIoOutStream>("underflow_callback");
  143. Action underflow_callback;
  144. delegate void underflow_callback_delegate(IntPtr handle);
  145. underflow_callback_delegate underflow_callback_native;
  146. // FIXME: this should be taken care in more centralized/decent manner... we don't want to write
  147. // this kind of code anywhere we need string marshaling.
  148. List<IntPtr> allocated_hglobals = new List<IntPtr>();
  149. public string Name {
  150. get { return Marshal.PtrToStringAnsi(Marshal.ReadIntPtr(handle, name_offset)); }
  151. set
  152. {
  153. unsafe
  154. {
  155. var existing = Marshal.ReadIntPtr(handle, name_offset);
  156. if (allocated_hglobals.Contains(existing))
  157. {
  158. allocated_hglobals.Remove(existing);
  159. Marshal.FreeHGlobal(existing);
  160. }
  161. var ptr = Marshal.StringToHGlobalAnsi(value);
  162. Marshal.WriteIntPtr(handle, name_offset, ptr);
  163. allocated_hglobals.Add(ptr);
  164. }
  165. }
  166. }
  167. static readonly int name_offset = (int)Marshal.OffsetOf<SoundIoOutStream>("name");
  168. public bool NonTerminalHint
  169. {
  170. get { return Marshal.ReadInt32(handle, non_terminal_hint_offset) != 0; }
  171. }
  172. static readonly int non_terminal_hint_offset = (int)Marshal.OffsetOf<SoundIoOutStream>("non_terminal_hint");
  173. public int BytesPerFrame
  174. {
  175. get { return Marshal.ReadInt32(handle, bytes_per_frame_offset); }
  176. }
  177. static readonly int bytes_per_frame_offset = (int)Marshal.OffsetOf<SoundIoOutStream>("bytes_per_frame");
  178. public int BytesPerSample
  179. {
  180. get { return Marshal.ReadInt32(handle, bytes_per_sample_offset); }
  181. }
  182. static readonly int bytes_per_sample_offset = (int)Marshal.OffsetOf<SoundIoOutStream>("bytes_per_sample");
  183. public string LayoutErrorMessage
  184. {
  185. get
  186. {
  187. var code = (SoundIoError)Marshal.ReadInt32(handle, layout_error_offset);
  188. return code == SoundIoError.SoundIoErrorNone ? null : Marshal.PtrToStringAnsi(Natives.soundio_strerror((int)code));
  189. }
  190. }
  191. static readonly int layout_error_offset = (int)Marshal.OffsetOf<SoundIoOutStream> ("layout_error");
  192. // functions
  193. public void Open ()
  194. {
  195. var ret = (SoundIoError)Natives.soundio_outstream_open(handle);
  196. if (ret != SoundIoError.SoundIoErrorNone)
  197. {
  198. throw new SoundIOException(ret);
  199. }
  200. }
  201. public void Start ()
  202. {
  203. var ret = (SoundIoError)Natives.soundio_outstream_start(handle);
  204. if (ret != SoundIoError.SoundIoErrorNone)
  205. {
  206. throw new SoundIOException(ret);
  207. }
  208. }
  209. public SoundIOChannelAreas BeginWrite(ref int frameCount)
  210. {
  211. IntPtr ptrs = default;
  212. int nativeFrameCount = frameCount;
  213. unsafe
  214. {
  215. var frameCountPtr = &nativeFrameCount;
  216. var ptrptr = &ptrs;
  217. var ret = (SoundIoError)Natives.soundio_outstream_begin_write(handle, (IntPtr)ptrptr, (IntPtr)frameCountPtr);
  218. frameCount = *frameCountPtr;
  219. if (ret != SoundIoError.SoundIoErrorNone)
  220. {
  221. throw new SoundIOException(ret);
  222. }
  223. return new SoundIOChannelAreas(ptrs, Layout.ChannelCount, frameCount);
  224. }
  225. }
  226. public void EndWrite ()
  227. {
  228. var ret = (SoundIoError)Natives.soundio_outstream_end_write(handle);
  229. if (ret != SoundIoError.SoundIoErrorNone)
  230. {
  231. throw new SoundIOException(ret);
  232. }
  233. }
  234. public void ClearBuffer ()
  235. {
  236. _ = Natives.soundio_outstream_clear_buffer(handle);
  237. }
  238. public void Pause (bool pause)
  239. {
  240. var ret = (SoundIoError)Natives.soundio_outstream_pause(handle, pause);
  241. if (ret != SoundIoError.SoundIoErrorNone)
  242. {
  243. throw new SoundIOException(ret);
  244. }
  245. }
  246. public double GetLatency ()
  247. {
  248. unsafe
  249. {
  250. double* dptr = null;
  251. IntPtr p = new IntPtr(dptr);
  252. var ret = (SoundIoError)Natives.soundio_outstream_get_latency(handle, p);
  253. if (ret != SoundIoError.SoundIoErrorNone)
  254. {
  255. throw new SoundIOException(ret);
  256. }
  257. dptr = (double*)p;
  258. return *dptr;
  259. }
  260. }
  261. public void SetVolume (double volume)
  262. {
  263. var ret = (SoundIoError)Natives.soundio_outstream_set_volume(handle, volume);
  264. if (ret != SoundIoError.SoundIoErrorNone)
  265. {
  266. throw new SoundIOException(ret);
  267. }
  268. }
  269. }
  270. }