SoundIODevice.cs 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Runtime.InteropServices;
  4. namespace SoundIOSharp
  5. {
  6. public class SoundIODevice
  7. {
  8. public static SoundIOChannelLayout BestMatchingChannelLayout (SoundIODevice device1, SoundIODevice device2)
  9. {
  10. var ptr1 = Marshal.ReadIntPtr (device1.handle, layouts_offset);
  11. var ptr2 = Marshal.ReadIntPtr (device2.handle, layouts_offset);
  12. return new SoundIOChannelLayout (Natives.soundio_best_matching_channel_layout (ptr1, device1.LayoutCount, ptr2, device2.LayoutCount));
  13. }
  14. internal SoundIODevice (Pointer<SoundIoDevice> handle)
  15. {
  16. this.handle = handle;
  17. }
  18. readonly Pointer<SoundIoDevice> handle;
  19. // Equality (based on handle and native func)
  20. public override bool Equals (object other)
  21. {
  22. var d = other as SoundIODevice;
  23. return d != null && (this.handle == d.handle || Natives.soundio_device_equal (this.handle, d.handle));
  24. }
  25. public override int GetHashCode ()
  26. {
  27. return (int) (IntPtr) handle;
  28. }
  29. public static bool operator == (SoundIODevice obj1, SoundIODevice obj2)
  30. {
  31. return (object)obj1 == null ? (object)obj2 == null : obj1.Equals (obj2);
  32. }
  33. public static bool operator != (SoundIODevice obj1, SoundIODevice obj2)
  34. {
  35. return (object)obj1 == null ? (object) obj2 != null : !obj1.Equals (obj2);
  36. }
  37. // fields
  38. public SoundIODeviceAim Aim {
  39. get { return (SoundIODeviceAim) Marshal.ReadInt32 (handle, aim_offset); }
  40. }
  41. static readonly int aim_offset = (int)Marshal.OffsetOf<SoundIoDevice> ("aim");
  42. public SoundIOFormat CurrentFormat {
  43. get { return (SoundIOFormat) Marshal.ReadInt32 (handle, current_format_offset); }
  44. }
  45. static readonly int current_format_offset = (int)Marshal.OffsetOf<SoundIoDevice> ("current_format");
  46. public SoundIOChannelLayout CurrentLayout {
  47. get { return new SoundIOChannelLayout ((IntPtr) handle + current_layout_offset);
  48. }
  49. }
  50. static readonly int current_layout_offset = (int)Marshal.OffsetOf<SoundIoDevice> ("current_layout");
  51. public int FormatCount {
  52. get { return Marshal.ReadInt32 (handle, format_count_offset); }
  53. }
  54. static readonly int format_count_offset = (int)Marshal.OffsetOf<SoundIoDevice> ("format_count");
  55. public IEnumerable<SoundIOFormat> Formats {
  56. get {
  57. var ptr = Marshal.ReadIntPtr (handle, formats_offset);
  58. for (int i = 0; i < FormatCount; i++)
  59. yield return (SoundIOFormat) Marshal.ReadInt32 (ptr, i);
  60. }
  61. }
  62. static readonly int formats_offset = (int)Marshal.OffsetOf<SoundIoDevice> ("formats");
  63. public string Id {
  64. get { return Marshal.PtrToStringAnsi (Marshal.ReadIntPtr (handle, id_offset)); }
  65. }
  66. static readonly int id_offset = (int)Marshal.OffsetOf<SoundIoDevice> ("id");
  67. public bool IsRaw {
  68. get { return Marshal.ReadInt32 (handle, is_raw_offset) != 0; }
  69. }
  70. static readonly int is_raw_offset = (int)Marshal.OffsetOf<SoundIoDevice> ("is_raw");
  71. public int LayoutCount {
  72. get { return Marshal.ReadInt32 (handle, layout_count_offset); }
  73. }
  74. static readonly int layout_count_offset = (int)Marshal.OffsetOf<SoundIoDevice> ("layout_count");
  75. public IEnumerable<SoundIOChannelLayout> Layouts {
  76. get {
  77. var ptr = Marshal.ReadIntPtr (handle, layouts_offset);
  78. for (int i = 0; i < LayoutCount; i++)
  79. yield return new SoundIOChannelLayout (ptr + i * Marshal.SizeOf<SoundIoChannelLayout> ());
  80. }
  81. }
  82. static readonly int layouts_offset = (int) Marshal.OffsetOf<SoundIoDevice> ("layouts");
  83. public string Name {
  84. get { return Marshal.PtrToStringAnsi (Marshal.ReadIntPtr (handle, name_offset)); }
  85. }
  86. static readonly int name_offset = (int)Marshal.OffsetOf<SoundIoDevice> ("name");
  87. public int ProbeError {
  88. get { return Marshal.ReadInt32 (handle, probe_error_offset); }
  89. }
  90. static readonly int probe_error_offset = (int)Marshal.OffsetOf<SoundIoDevice> ("probe_error");
  91. public int ReferenceCount {
  92. get { return Marshal.ReadInt32 (handle, ref_count_offset); }
  93. }
  94. static readonly int ref_count_offset = (int)Marshal.OffsetOf<SoundIoDevice> ("ref_count");
  95. public int SampleRateCount {
  96. get { return Marshal.ReadInt32 (handle, sample_rate_count_offset); }
  97. }
  98. static readonly int sample_rate_count_offset = (int)Marshal.OffsetOf<SoundIoDevice> ("sample_rate_count");
  99. public IEnumerable<SoundIOSampleRateRange> SampleRates {
  100. get {
  101. var ptr = Marshal.ReadIntPtr (handle, sample_rates_offset);
  102. for (int i = 0; i < SampleRateCount; i++)
  103. yield return new SoundIOSampleRateRange (
  104. Marshal.ReadInt32 (ptr, i * 2),
  105. Marshal.ReadInt32 (ptr, i * 2 + 1));
  106. }
  107. }
  108. static readonly int sample_rates_offset = (int)Marshal.OffsetOf<SoundIoDevice> ("sample_rates");
  109. public double SoftwareLatencyCurrent {
  110. get { return MarshalEx.ReadDouble (handle, software_latency_current_offset); }
  111. set { MarshalEx.WriteDouble (handle, software_latency_current_offset, value); }
  112. }
  113. static readonly int software_latency_current_offset = (int)Marshal.OffsetOf<SoundIoDevice> ("software_latency_current");
  114. public double SoftwareLatencyMin {
  115. get { return MarshalEx.ReadDouble (handle, software_latency_min_offset); }
  116. set { MarshalEx.WriteDouble (handle, software_latency_min_offset, value); }
  117. }
  118. static readonly int software_latency_min_offset = (int)Marshal.OffsetOf<SoundIoDevice> ("software_latency_min");
  119. public double SoftwareLatencyMax {
  120. get { return MarshalEx.ReadDouble (handle, software_latency_max_offset); }
  121. set { MarshalEx.WriteDouble (handle, software_latency_max_offset, value); }
  122. }
  123. static readonly int software_latency_max_offset = (int)Marshal.OffsetOf<SoundIoDevice> ("software_latency_max");
  124. public SoundIO SoundIO {
  125. get { return new SoundIO (Marshal.ReadIntPtr (handle, soundio_offset)); }
  126. }
  127. static readonly int soundio_offset = (int)Marshal.OffsetOf<SoundIoDevice> ("soundio");
  128. // functions
  129. public void AddReference ()
  130. {
  131. Natives.soundio_device_ref (handle);
  132. }
  133. public void RemoveReference ()
  134. {
  135. Natives.soundio_device_unref (handle);
  136. }
  137. public void SortDeviceChannelLayouts ()
  138. {
  139. Natives.soundio_device_sort_channel_layouts (handle);
  140. }
  141. public static readonly SoundIOFormat S16NE = BitConverter.IsLittleEndian ? SoundIOFormat.S16LE : SoundIOFormat.S16BE;
  142. public static readonly SoundIOFormat U16NE = BitConverter.IsLittleEndian ? SoundIOFormat.U16LE : SoundIOFormat.U16BE;
  143. public static readonly SoundIOFormat S24NE = BitConverter.IsLittleEndian ? SoundIOFormat.S24LE : SoundIOFormat.S24BE;
  144. public static readonly SoundIOFormat U24NE = BitConverter.IsLittleEndian ? SoundIOFormat.U24LE : SoundIOFormat.U24BE;
  145. public static readonly SoundIOFormat S32NE = BitConverter.IsLittleEndian ? SoundIOFormat.S32LE : SoundIOFormat.S32BE;
  146. public static readonly SoundIOFormat U32NE = BitConverter.IsLittleEndian ? SoundIOFormat.U32LE : SoundIOFormat.U32BE;
  147. public static readonly SoundIOFormat Float32NE = BitConverter.IsLittleEndian ? SoundIOFormat.Float32LE : SoundIOFormat.Float32BE;
  148. public static readonly SoundIOFormat Float64NE = BitConverter.IsLittleEndian ? SoundIOFormat.Float64LE : SoundIOFormat.Float64BE;
  149. public static readonly SoundIOFormat S16FE = !BitConverter.IsLittleEndian ? SoundIOFormat.S16LE : SoundIOFormat.S16BE;
  150. public static readonly SoundIOFormat U16FE = !BitConverter.IsLittleEndian ? SoundIOFormat.U16LE : SoundIOFormat.U16BE;
  151. public static readonly SoundIOFormat S24FE = !BitConverter.IsLittleEndian ? SoundIOFormat.S24LE : SoundIOFormat.S24BE;
  152. public static readonly SoundIOFormat U24FE = !BitConverter.IsLittleEndian ? SoundIOFormat.U24LE : SoundIOFormat.U24BE;
  153. public static readonly SoundIOFormat S32FE = !BitConverter.IsLittleEndian ? SoundIOFormat.S32LE : SoundIOFormat.S32BE;
  154. public static readonly SoundIOFormat U32FE = !BitConverter.IsLittleEndian ? SoundIOFormat.U32LE : SoundIOFormat.U32BE;
  155. public static readonly SoundIOFormat Float32FE = !BitConverter.IsLittleEndian ? SoundIOFormat.Float32LE : SoundIOFormat.Float32BE;
  156. public static readonly SoundIOFormat Float64FE = !BitConverter.IsLittleEndian ? SoundIOFormat.Float64LE : SoundIOFormat.Float64BE;
  157. public bool SupportsFormat (SoundIOFormat format)
  158. {
  159. return Natives.soundio_device_supports_format (handle, (SoundIoFormat) format);
  160. }
  161. public bool SupportsSampleRate (int sampleRate)
  162. {
  163. return Natives.soundio_device_supports_sample_rate (handle, sampleRate);
  164. }
  165. public int GetNearestSampleRate (int sampleRate)
  166. {
  167. return Natives.soundio_device_nearest_sample_rate (handle, sampleRate);
  168. }
  169. public SoundIOInStream CreateInStream ()
  170. {
  171. return new SoundIOInStream (Natives.soundio_instream_create (handle));
  172. }
  173. public SoundIOOutStream CreateOutStream ()
  174. {
  175. return new SoundIOOutStream (Natives.soundio_outstream_create (handle));
  176. }
  177. }
  178. }