SoundIOOutStream.cs 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  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 (object)obj1 == null ? (object)obj2 == null : obj1.Equals (obj2);
  30. }
  31. public static bool operator != (SoundIOOutStream obj1, SoundIOOutStream obj2)
  32. {
  33. return (object)obj1 == null ? (object)obj2 != null : !obj1.Equals (obj2);
  34. }
  35. // fields
  36. public SoundIODevice Device {
  37. get { return new SoundIODevice (Marshal.ReadIntPtr (handle, device_offset)); }
  38. }
  39. static readonly int device_offset = (int)Marshal.OffsetOf<SoundIoOutStream> ("device");
  40. public SoundIOFormat Format {
  41. get { return (SoundIOFormat) Marshal.ReadInt32 (handle, format_offset); }
  42. set { Marshal.WriteInt32 (handle, format_offset, (int) value); }
  43. }
  44. static readonly int format_offset = (int)Marshal.OffsetOf<SoundIoOutStream> ("format");
  45. public int SampleRate {
  46. get { return Marshal.ReadInt32 (handle, sample_rate_offset); }
  47. set { Marshal.WriteInt32 (handle, sample_rate_offset, value); }
  48. }
  49. static readonly int sample_rate_offset = (int)Marshal.OffsetOf<SoundIoOutStream> ("sample_rate");
  50. public SoundIOChannelLayout Layout {
  51. get { unsafe { return new SoundIOChannelLayout ((IntPtr) ((void*) ((IntPtr) handle + layout_offset))); } }
  52. set {
  53. unsafe {
  54. Buffer.MemoryCopy ((void*)((IntPtr)handle + layout_offset), (void*)value.Handle,
  55. Marshal.SizeOf<SoundIoChannelLayout> (), Marshal.SizeOf<SoundIoChannelLayout> ());
  56. }
  57. }
  58. }
  59. static readonly int layout_offset = (int)Marshal.OffsetOf<SoundIoOutStream> ("layout");
  60. public double SoftwareLatency {
  61. get { return MarshalEx.ReadDouble (handle, software_latency_offset); }
  62. set { MarshalEx.WriteDouble (handle, software_latency_offset, value); }
  63. }
  64. static readonly int software_latency_offset = (int)Marshal.OffsetOf<SoundIoOutStream> ("software_latency");
  65. // error_callback
  66. public Action ErrorCallback {
  67. get { return error_callback; }
  68. set {
  69. error_callback = value;
  70. if (value == null)
  71. error_callback_native = null;
  72. else
  73. error_callback_native = stream => error_callback ();
  74. var ptr = Marshal.GetFunctionPointerForDelegate (error_callback_native);
  75. Marshal.WriteIntPtr (handle, error_callback_offset, ptr);
  76. }
  77. }
  78. static readonly int error_callback_offset = (int)Marshal.OffsetOf<SoundIoOutStream> ("error_callback");
  79. Action error_callback;
  80. delegate void error_callback_delegate (IntPtr handle);
  81. error_callback_delegate error_callback_native;
  82. // write_callback
  83. public Action<int, int> WriteCallback {
  84. get { return write_callback; }
  85. set {
  86. write_callback = value;
  87. if (value == null)
  88. write_callback_native = null;
  89. else
  90. write_callback_native = (h, frame_count_min, frame_count_max) => write_callback (frame_count_min, frame_count_max);
  91. var ptr = Marshal.GetFunctionPointerForDelegate (write_callback_native);
  92. Marshal.WriteIntPtr (handle, write_callback_offset, ptr);
  93. }
  94. }
  95. static readonly int write_callback_offset = (int)Marshal.OffsetOf<SoundIoOutStream> ("write_callback");
  96. Action<int, int> write_callback;
  97. delegate void write_callback_delegate (IntPtr handle, int min, int max);
  98. write_callback_delegate write_callback_native;
  99. // underflow_callback
  100. public Action UnderflowCallback {
  101. get { return underflow_callback; }
  102. set {
  103. underflow_callback = value;
  104. if (value == null)
  105. underflow_callback_native = null;
  106. else
  107. underflow_callback_native = h => underflow_callback ();
  108. var ptr = Marshal.GetFunctionPointerForDelegate (underflow_callback_native);
  109. Marshal.WriteIntPtr (handle, underflow_callback_offset, ptr);
  110. }
  111. }
  112. static readonly int underflow_callback_offset = (int)Marshal.OffsetOf<SoundIoOutStream> ("underflow_callback");
  113. Action underflow_callback;
  114. delegate void underflow_callback_delegate (IntPtr handle);
  115. underflow_callback_delegate underflow_callback_native;
  116. // FIXME: this should be taken care in more centralized/decent manner... we don't want to write
  117. // this kind of code anywhere we need string marshaling.
  118. List<IntPtr> allocated_hglobals = new List<IntPtr> ();
  119. public string Name {
  120. get { return Marshal.PtrToStringAnsi (Marshal.ReadIntPtr (handle, name_offset)); }
  121. set {
  122. unsafe {
  123. var existing = Marshal.ReadIntPtr (handle, name_offset);
  124. if (allocated_hglobals.Contains (existing)) {
  125. allocated_hglobals.Remove (existing);
  126. Marshal.FreeHGlobal (existing);
  127. }
  128. var ptr = Marshal.StringToHGlobalAnsi (value);
  129. Marshal.WriteIntPtr (handle, name_offset, ptr);
  130. allocated_hglobals.Add (ptr);
  131. }
  132. }
  133. }
  134. static readonly int name_offset = (int)Marshal.OffsetOf<SoundIoOutStream> ("name");
  135. public bool NonTerminalHint {
  136. get { return Marshal.ReadInt32 (handle, non_terminal_hint_offset) != 0; }
  137. }
  138. static readonly int non_terminal_hint_offset = (int)Marshal.OffsetOf<SoundIoOutStream> ("non_terminal_hint");
  139. public int BytesPerFrame {
  140. get { return Marshal.ReadInt32 (handle, bytes_per_frame_offset); }
  141. }
  142. static readonly int bytes_per_frame_offset = (int)Marshal.OffsetOf<SoundIoOutStream> ("bytes_per_frame");
  143. public int BytesPerSample {
  144. get { return Marshal.ReadInt32 (handle, bytes_per_sample_offset); }
  145. }
  146. static readonly int bytes_per_sample_offset = (int)Marshal.OffsetOf<SoundIoOutStream> ("bytes_per_sample");
  147. public string LayoutErrorMessage {
  148. get {
  149. var code = (SoundIoError) Marshal.ReadInt32 (handle, layout_error_offset);
  150. return code == SoundIoError.SoundIoErrorNone ? null : Marshal.PtrToStringAnsi (Natives.soundio_strerror ((int) code));
  151. }
  152. }
  153. static readonly int layout_error_offset = (int)Marshal.OffsetOf<SoundIoOutStream> ("layout_error");
  154. // functions
  155. public void Open ()
  156. {
  157. var ret = (SoundIoError) Natives.soundio_outstream_open (handle);
  158. if (ret != SoundIoError.SoundIoErrorNone)
  159. throw new SoundIOException (ret);
  160. }
  161. public void Start ()
  162. {
  163. var ret = (SoundIoError)Natives.soundio_outstream_start (handle);
  164. if (ret != SoundIoError.SoundIoErrorNone)
  165. throw new SoundIOException (ret);
  166. }
  167. public SoundIOChannelAreas BeginWrite (ref int frameCount)
  168. {
  169. IntPtr ptrs = default (IntPtr);
  170. int nativeFrameCount = frameCount;
  171. unsafe {
  172. var frameCountPtr = &nativeFrameCount;
  173. var ptrptr = &ptrs;
  174. var ret = (SoundIoError)Natives.soundio_outstream_begin_write (handle, (IntPtr) ptrptr, (IntPtr) frameCountPtr);
  175. frameCount = *frameCountPtr;
  176. if (ret != SoundIoError.SoundIoErrorNone)
  177. throw new SoundIOException (ret);
  178. return new SoundIOChannelAreas (ptrs, Layout.ChannelCount, frameCount);
  179. }
  180. }
  181. public void EndWrite ()
  182. {
  183. var ret = (SoundIoError) Natives.soundio_outstream_end_write (handle);
  184. if (ret != SoundIoError.SoundIoErrorNone)
  185. throw new SoundIOException (ret);
  186. }
  187. public void ClearBuffer ()
  188. {
  189. Natives.soundio_outstream_clear_buffer (handle);
  190. }
  191. public void Pause (bool pause)
  192. {
  193. var ret = (SoundIoError) Natives.soundio_outstream_pause (handle, pause);
  194. if (ret != SoundIoError.SoundIoErrorNone)
  195. throw new SoundIOException (ret);
  196. }
  197. public double GetLatency ()
  198. {
  199. unsafe {
  200. double* dptr = null;
  201. IntPtr p = new IntPtr (dptr);
  202. var ret = (SoundIoError) Natives.soundio_outstream_get_latency (handle, p);
  203. if (ret != SoundIoError.SoundIoErrorNone)
  204. throw new SoundIOException (ret);
  205. dptr = (double*) p;
  206. return *dptr;
  207. }
  208. }
  209. }
  210. }