SoundIOOutStream.cs 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  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*)value.Handle, (void*)((IntPtr)handle + layout_offset),
  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. public float Volume {
  66. get { return MarshalEx.ReadFloat (handle, volume_offset); }
  67. set { MarshalEx.WriteFloat (handle, volume_offset, value); }
  68. }
  69. static readonly int volume_offset = (int)Marshal.OffsetOf<SoundIoOutStream> ("volume");
  70. // error_callback
  71. public Action ErrorCallback {
  72. get { return error_callback; }
  73. set {
  74. error_callback = value;
  75. if (value == null)
  76. error_callback_native = null;
  77. else
  78. error_callback_native = stream => error_callback ();
  79. var ptr = Marshal.GetFunctionPointerForDelegate (error_callback_native);
  80. Marshal.WriteIntPtr (handle, error_callback_offset, ptr);
  81. }
  82. }
  83. static readonly int error_callback_offset = (int)Marshal.OffsetOf<SoundIoOutStream> ("error_callback");
  84. Action error_callback;
  85. delegate void error_callback_delegate (IntPtr handle);
  86. error_callback_delegate error_callback_native;
  87. // write_callback
  88. public Action<int, int> WriteCallback {
  89. get { return write_callback; }
  90. set {
  91. write_callback = value;
  92. if (value == null)
  93. write_callback_native = null;
  94. else
  95. write_callback_native = (h, frame_count_min, frame_count_max) => write_callback (frame_count_min, frame_count_max);
  96. var ptr = Marshal.GetFunctionPointerForDelegate (write_callback_native);
  97. Marshal.WriteIntPtr (handle, write_callback_offset, ptr);
  98. }
  99. }
  100. static readonly int write_callback_offset = (int)Marshal.OffsetOf<SoundIoOutStream> ("write_callback");
  101. Action<int, int> write_callback;
  102. delegate void write_callback_delegate (IntPtr handle, int min, int max);
  103. write_callback_delegate write_callback_native;
  104. // underflow_callback
  105. public Action UnderflowCallback {
  106. get { return underflow_callback; }
  107. set {
  108. underflow_callback = value;
  109. if (value == null)
  110. underflow_callback_native = null;
  111. else
  112. underflow_callback_native = h => underflow_callback ();
  113. var ptr = Marshal.GetFunctionPointerForDelegate (underflow_callback_native);
  114. Marshal.WriteIntPtr (handle, underflow_callback_offset, ptr);
  115. }
  116. }
  117. static readonly int underflow_callback_offset = (int)Marshal.OffsetOf<SoundIoOutStream> ("underflow_callback");
  118. Action underflow_callback;
  119. delegate void underflow_callback_delegate (IntPtr handle);
  120. underflow_callback_delegate underflow_callback_native;
  121. // FIXME: this should be taken care in more centralized/decent manner... we don't want to write
  122. // this kind of code anywhere we need string marshaling.
  123. List<IntPtr> allocated_hglobals = new List<IntPtr> ();
  124. public string Name {
  125. get { return Marshal.PtrToStringAnsi (Marshal.ReadIntPtr (handle, name_offset)); }
  126. set {
  127. unsafe {
  128. var existing = Marshal.ReadIntPtr (handle, name_offset);
  129. if (allocated_hglobals.Contains (existing)) {
  130. allocated_hglobals.Remove (existing);
  131. Marshal.FreeHGlobal (existing);
  132. }
  133. var ptr = Marshal.StringToHGlobalAnsi (value);
  134. Marshal.WriteIntPtr (handle, name_offset, ptr);
  135. allocated_hglobals.Add (ptr);
  136. }
  137. }
  138. }
  139. static readonly int name_offset = (int)Marshal.OffsetOf<SoundIoOutStream> ("name");
  140. public bool NonTerminalHint {
  141. get { return Marshal.ReadInt32 (handle, non_terminal_hint_offset) != 0; }
  142. }
  143. static readonly int non_terminal_hint_offset = (int)Marshal.OffsetOf<SoundIoOutStream> ("non_terminal_hint");
  144. public int BytesPerFrame {
  145. get { return Marshal.ReadInt32 (handle, bytes_per_frame_offset); }
  146. }
  147. static readonly int bytes_per_frame_offset = (int)Marshal.OffsetOf<SoundIoOutStream> ("bytes_per_frame");
  148. public int BytesPerSample {
  149. get { return Marshal.ReadInt32 (handle, bytes_per_sample_offset); }
  150. }
  151. static readonly int bytes_per_sample_offset = (int)Marshal.OffsetOf<SoundIoOutStream> ("bytes_per_sample");
  152. public string LayoutErrorMessage {
  153. get {
  154. var code = (SoundIoError) Marshal.ReadInt32 (handle, layout_error_offset);
  155. return code == SoundIoError.SoundIoErrorNone ? null : Marshal.PtrToStringAnsi (Natives.soundio_strerror ((int) code));
  156. }
  157. }
  158. static readonly int layout_error_offset = (int)Marshal.OffsetOf<SoundIoOutStream> ("layout_error");
  159. // functions
  160. public void Open ()
  161. {
  162. var ret = (SoundIoError) Natives.soundio_outstream_open (handle);
  163. if (ret != SoundIoError.SoundIoErrorNone)
  164. throw new SoundIOException (ret);
  165. }
  166. public void Start ()
  167. {
  168. var ret = (SoundIoError)Natives.soundio_outstream_start (handle);
  169. if (ret != SoundIoError.SoundIoErrorNone)
  170. throw new SoundIOException (ret);
  171. }
  172. public SoundIOChannelAreas BeginWrite (ref int frameCount)
  173. {
  174. IntPtr ptrs = default (IntPtr);
  175. int nativeFrameCount = frameCount;
  176. unsafe {
  177. var frameCountPtr = &nativeFrameCount;
  178. var ptrptr = &ptrs;
  179. var ret = (SoundIoError)Natives.soundio_outstream_begin_write (handle, (IntPtr) ptrptr, (IntPtr) frameCountPtr);
  180. frameCount = *frameCountPtr;
  181. if (ret != SoundIoError.SoundIoErrorNone)
  182. throw new SoundIOException (ret);
  183. return new SoundIOChannelAreas (ptrs, Layout.ChannelCount, frameCount);
  184. }
  185. }
  186. public void EndWrite ()
  187. {
  188. var ret = (SoundIoError) Natives.soundio_outstream_end_write (handle);
  189. if (ret != SoundIoError.SoundIoErrorNone)
  190. throw new SoundIOException (ret);
  191. }
  192. public void ClearBuffer ()
  193. {
  194. Natives.soundio_outstream_clear_buffer (handle);
  195. }
  196. public void Pause (bool pause)
  197. {
  198. var ret = (SoundIoError) Natives.soundio_outstream_pause (handle, pause);
  199. if (ret != SoundIoError.SoundIoErrorNone)
  200. throw new SoundIOException (ret);
  201. }
  202. public double GetLatency ()
  203. {
  204. unsafe {
  205. double* dptr = null;
  206. IntPtr p = new IntPtr (dptr);
  207. var ret = (SoundIoError) Natives.soundio_outstream_get_latency (handle, p);
  208. if (ret != SoundIoError.SoundIoErrorNone)
  209. throw new SoundIOException (ret);
  210. dptr = (double*) p;
  211. return *dptr;
  212. }
  213. }
  214. public void SetVolume (double volume)
  215. {
  216. var ret = (SoundIoError) Natives.soundio_outstream_set_volume (handle, volume);
  217. if (ret != SoundIoError.SoundIoErrorNone)
  218. throw new SoundIOException (ret);
  219. }
  220. }
  221. }