SoundIOInStream.cs 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Runtime.InteropServices;
  4. namespace SoundIOSharp
  5. {
  6. public class SoundIOInStream : IDisposable
  7. {
  8. internal SoundIOInStream (Pointer<SoundIoInStream> handle)
  9. {
  10. this.handle = handle;
  11. }
  12. Pointer<SoundIoInStream> handle;
  13. public void Dispose ()
  14. {
  15. Natives.soundio_instream_destroy (handle);
  16. }
  17. // Equality (based on handle)
  18. public override bool Equals (object other)
  19. {
  20. var d = other as SoundIOInStream;
  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 == (SoundIOInStream obj1, SoundIOInStream obj2)
  28. {
  29. return (object)obj1 == null ? (object)obj2 == null : obj1.Equals (obj2);
  30. }
  31. public static bool operator != (SoundIOInStream obj1, SoundIOInStream 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<SoundIoInStream> ("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<SoundIoInStream> ("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<SoundIoInStream> ("sample_rate");
  50. public SoundIOChannelLayout Layout {
  51. get { return new SoundIOChannelLayout ((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<SoundIoInStream> ("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<SoundIoInStream> ("software_latency");
  65. // error_callback
  66. public Action ErrorCallback {
  67. get { return error_callback; }
  68. set {
  69. error_callback = value;
  70. error_callback_native = _ => error_callback ();
  71. var ptr = Marshal.GetFunctionPointerForDelegate (error_callback_native);
  72. Marshal.WriteIntPtr (handle, error_callback_offset, ptr);
  73. }
  74. }
  75. static readonly int error_callback_offset = (int)Marshal.OffsetOf<SoundIoInStream> ("error_callback");
  76. Action error_callback;
  77. delegate void error_callback_delegate (IntPtr handle);
  78. error_callback_delegate error_callback_native;
  79. // read_callback
  80. public Action<int,int> ReadCallback {
  81. get { return read_callback; }
  82. set {
  83. read_callback = value;
  84. read_callback_native = (_, minFrameCount, maxFrameCount) => read_callback (minFrameCount, maxFrameCount);
  85. var ptr = Marshal.GetFunctionPointerForDelegate (read_callback_native);
  86. Marshal.WriteIntPtr (handle, read_callback_offset, ptr);
  87. }
  88. }
  89. static readonly int read_callback_offset = (int)Marshal.OffsetOf<SoundIoInStream> ("read_callback");
  90. Action<int, int> read_callback;
  91. delegate void read_callback_delegate (IntPtr handle, int min, int max);
  92. read_callback_delegate read_callback_native;
  93. // overflow_callback
  94. public Action OverflowCallback {
  95. get { return overflow_callback; }
  96. set {
  97. overflow_callback = value;
  98. overflow_callback_native = _ => overflow_callback ();
  99. var ptr = Marshal.GetFunctionPointerForDelegate (overflow_callback_native);
  100. Marshal.WriteIntPtr (handle, overflow_callback_offset, ptr);
  101. }
  102. }
  103. static readonly int overflow_callback_offset = (int)Marshal.OffsetOf<SoundIoInStream> ("overflow_callback");
  104. Action overflow_callback;
  105. delegate void overflow_callback_delegate (IntPtr handle);
  106. overflow_callback_delegate overflow_callback_native;
  107. // FIXME: this should be taken care in more centralized/decent manner... we don't want to write
  108. // this kind of code anywhere we need string marshaling.
  109. List<IntPtr> allocated_hglobals = new List<IntPtr> ();
  110. public string Name {
  111. get { return Marshal.PtrToStringAnsi (Marshal.ReadIntPtr (handle, name_offset)); }
  112. set {
  113. unsafe {
  114. var existing = Marshal.ReadIntPtr (handle, name_offset);
  115. if (allocated_hglobals.Contains (existing)) {
  116. allocated_hglobals.Remove (existing);
  117. Marshal.FreeHGlobal (existing);
  118. }
  119. var ptr = Marshal.StringToHGlobalAnsi (value);
  120. Marshal.WriteIntPtr (handle, name_offset, ptr);
  121. allocated_hglobals.Add (ptr);
  122. }
  123. }
  124. }
  125. static readonly int name_offset = (int)Marshal.OffsetOf<SoundIoInStream> ("name");
  126. public bool NonTerminalHint {
  127. get { return Marshal.ReadInt32 (handle, non_terminal_hint_offset) != 0; }
  128. }
  129. static readonly int non_terminal_hint_offset = (int)Marshal.OffsetOf<SoundIoInStream> ("non_terminal_hint");
  130. public int BytesPerFrame {
  131. get { return Marshal.ReadInt32 (handle, bytes_per_frame_offset); }
  132. }
  133. static readonly int bytes_per_frame_offset = (int)Marshal.OffsetOf<SoundIoInStream> ("bytes_per_frame");
  134. public int BytesPerSample {
  135. get { return Marshal.ReadInt32 (handle, bytes_per_sample_offset); }
  136. }
  137. static readonly int bytes_per_sample_offset = (int)Marshal.OffsetOf<SoundIoInStream> ("bytes_per_sample");
  138. public string LayoutErrorMessage {
  139. get {
  140. var code = (SoundIoError) Marshal.ReadInt32 (handle, layout_error_offset);
  141. return code == SoundIoError.SoundIoErrorNone ? null : Marshal.PtrToStringAnsi (Natives.soundio_strerror ((int) code));
  142. }
  143. }
  144. static readonly int layout_error_offset = (int)Marshal.OffsetOf<SoundIoInStream> ("layout_error");
  145. // functions
  146. public void Open ()
  147. {
  148. var ret = (SoundIoError) Natives.soundio_instream_open (handle);
  149. if (ret != SoundIoError.SoundIoErrorNone)
  150. throw new SoundIOException (ret);
  151. }
  152. public void Start ()
  153. {
  154. var ret = (SoundIoError)Natives.soundio_instream_start (handle);
  155. if (ret != SoundIoError.SoundIoErrorNone)
  156. throw new SoundIOException (ret);
  157. }
  158. public SoundIOChannelAreas BeginRead (ref int frameCount)
  159. {
  160. IntPtr ptrs = default (IntPtr);
  161. int nativeFrameCount = frameCount;
  162. unsafe {
  163. var frameCountPtr = &nativeFrameCount;
  164. var ptrptr = &ptrs;
  165. var ret = (SoundIoError) Natives.soundio_instream_begin_read (handle, (IntPtr)ptrptr, (IntPtr)frameCountPtr);
  166. frameCount = *frameCountPtr;
  167. if (ret != SoundIoError.SoundIoErrorNone)
  168. throw new SoundIOException (ret);
  169. return new SoundIOChannelAreas (ptrs, Layout.ChannelCount, frameCount);
  170. }
  171. }
  172. public void EndRead ()
  173. {
  174. var ret = (SoundIoError) Natives.soundio_instream_end_read (handle);
  175. if (ret != SoundIoError.SoundIoErrorNone)
  176. throw new SoundIOException (ret);
  177. }
  178. public void Pause (bool pause)
  179. {
  180. var ret = (SoundIoError) Natives.soundio_instream_pause (handle, pause);
  181. if (ret != SoundIoError.SoundIoErrorNone)
  182. throw new SoundIOException (ret);
  183. }
  184. public double GetLatency ()
  185. {
  186. unsafe {
  187. double* dptr = null;
  188. IntPtr p = new IntPtr (dptr);
  189. var ret = (SoundIoError) Natives.soundio_instream_get_latency (handle, p);
  190. if (ret != SoundIoError.SoundIoErrorNone)
  191. throw new SoundIOException (ret);
  192. dptr = (double*) p;
  193. return *dptr;
  194. }
  195. }
  196. }
  197. }