SoundIODevice.cs 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  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 obj1 is null ? obj2 is null : obj1.Equals(obj2);
  32. }
  33. public static bool operator != (SoundIODevice obj1, SoundIODevice obj2)
  34. {
  35. return obj1 is null ? obj2 is object : !obj1.Equals(obj2);
  36. }
  37. // fields
  38. public SoundIODeviceAim Aim
  39. {
  40. get { return (SoundIODeviceAim)Marshal.ReadInt32(handle, aim_offset); }
  41. }
  42. static readonly int aim_offset = (int)Marshal.OffsetOf<SoundIoDevice>("aim");
  43. public SoundIOFormat CurrentFormat
  44. {
  45. get { return (SoundIOFormat)Marshal.ReadInt32(handle, current_format_offset); }
  46. }
  47. static readonly int current_format_offset = (int)Marshal.OffsetOf<SoundIoDevice>("current_format");
  48. public SoundIOChannelLayout CurrentLayout
  49. {
  50. get { return new SoundIOChannelLayout((IntPtr)handle + current_layout_offset); }
  51. }
  52. static readonly int current_layout_offset = (int)Marshal.OffsetOf<SoundIoDevice>("current_layout");
  53. public int FormatCount
  54. {
  55. get { return Marshal.ReadInt32(handle, format_count_offset); }
  56. }
  57. static readonly int format_count_offset = (int)Marshal.OffsetOf<SoundIoDevice>("format_count");
  58. public IEnumerable<SoundIOFormat> Formats
  59. {
  60. get
  61. {
  62. var ptr = Marshal.ReadIntPtr(handle, formats_offset);
  63. for (int i = 0; i < FormatCount; i++)
  64. {
  65. yield return (SoundIOFormat)Marshal.ReadInt32(ptr, i);
  66. }
  67. }
  68. }
  69. static readonly int formats_offset = (int)Marshal.OffsetOf<SoundIoDevice>("formats");
  70. public string Id
  71. {
  72. get { return Marshal.PtrToStringAnsi(Marshal.ReadIntPtr(handle, id_offset)); }
  73. }
  74. static readonly int id_offset = (int)Marshal.OffsetOf<SoundIoDevice>("id");
  75. public bool IsRaw
  76. {
  77. get { return Marshal.ReadInt32(handle, is_raw_offset) != 0; }
  78. }
  79. static readonly int is_raw_offset = (int)Marshal.OffsetOf<SoundIoDevice>("is_raw");
  80. public int LayoutCount
  81. {
  82. get { return Marshal.ReadInt32(handle, layout_count_offset); }
  83. }
  84. static readonly int layout_count_offset = (int)Marshal.OffsetOf<SoundIoDevice>("layout_count");
  85. public IEnumerable<SoundIOChannelLayout> Layouts
  86. {
  87. get
  88. {
  89. var ptr = Marshal.ReadIntPtr (handle, layouts_offset);
  90. for (int i = 0; i < LayoutCount; i++)
  91. {
  92. yield return new SoundIOChannelLayout(ptr + i * Marshal.SizeOf<SoundIoChannelLayout>());
  93. }
  94. }
  95. }
  96. static readonly int layouts_offset = (int)Marshal.OffsetOf<SoundIoDevice>("layouts");
  97. public string Name
  98. {
  99. get { return Marshal.PtrToStringAnsi(Marshal.ReadIntPtr(handle, name_offset)); }
  100. }
  101. static readonly int name_offset = (int)Marshal.OffsetOf<SoundIoDevice>("name");
  102. public int ProbeError
  103. {
  104. get { return Marshal.ReadInt32(handle, probe_error_offset); }
  105. }
  106. static readonly int probe_error_offset = (int)Marshal.OffsetOf<SoundIoDevice>("probe_error");
  107. public int ReferenceCount
  108. {
  109. get { return Marshal.ReadInt32(handle, ref_count_offset); }
  110. }
  111. static readonly int ref_count_offset = (int)Marshal.OffsetOf<SoundIoDevice>("ref_count");
  112. public int SampleRateCount
  113. {
  114. get { return Marshal.ReadInt32(handle, sample_rate_count_offset); }
  115. }
  116. static readonly int sample_rate_count_offset = (int)Marshal.OffsetOf<SoundIoDevice>("sample_rate_count");
  117. public IEnumerable<SoundIOSampleRateRange> SampleRates
  118. {
  119. get
  120. {
  121. var ptr = Marshal.ReadIntPtr(handle, sample_rates_offset);
  122. for (int i = 0; i < SampleRateCount; i++)
  123. {
  124. yield return new SoundIOSampleRateRange(Marshal.ReadInt32(ptr, i * 2), Marshal.ReadInt32(ptr, i * 2 + 1));
  125. }
  126. }
  127. }
  128. static readonly int sample_rates_offset = (int)Marshal.OffsetOf<SoundIoDevice>("sample_rates");
  129. public double SoftwareLatencyCurrent
  130. {
  131. get { return MarshalEx.ReadDouble(handle, software_latency_current_offset); }
  132. set { MarshalEx.WriteDouble(handle, software_latency_current_offset, value); }
  133. }
  134. static readonly int software_latency_current_offset = (int)Marshal.OffsetOf<SoundIoDevice>("software_latency_current");
  135. public double SoftwareLatencyMin
  136. {
  137. get { return MarshalEx.ReadDouble(handle, software_latency_min_offset); }
  138. set { MarshalEx.WriteDouble(handle, software_latency_min_offset, value); }
  139. }
  140. static readonly int software_latency_min_offset = (int)Marshal.OffsetOf<SoundIoDevice>("software_latency_min");
  141. public double SoftwareLatencyMax
  142. {
  143. get { return MarshalEx.ReadDouble(handle, software_latency_max_offset); }
  144. set { MarshalEx.WriteDouble(handle, software_latency_max_offset, value); }
  145. }
  146. static readonly int software_latency_max_offset = (int)Marshal.OffsetOf<SoundIoDevice>("software_latency_max");
  147. public SoundIO SoundIO
  148. {
  149. get { return new SoundIO(Marshal.ReadIntPtr(handle, soundio_offset)); }
  150. }
  151. static readonly int soundio_offset = (int)Marshal.OffsetOf<SoundIoDevice>("soundio");
  152. // functions
  153. public void AddReference()
  154. {
  155. Natives.soundio_device_ref(handle);
  156. }
  157. public void RemoveReference()
  158. {
  159. Natives.soundio_device_unref(handle);
  160. }
  161. public void SortDeviceChannelLayouts()
  162. {
  163. Natives.soundio_device_sort_channel_layouts(handle);
  164. }
  165. public static readonly SoundIOFormat S16NE = BitConverter.IsLittleEndian ? SoundIOFormat.S16LE : SoundIOFormat.S16BE;
  166. public static readonly SoundIOFormat U16NE = BitConverter.IsLittleEndian ? SoundIOFormat.U16LE : SoundIOFormat.U16BE;
  167. public static readonly SoundIOFormat S24NE = BitConverter.IsLittleEndian ? SoundIOFormat.S24LE : SoundIOFormat.S24BE;
  168. public static readonly SoundIOFormat U24NE = BitConverter.IsLittleEndian ? SoundIOFormat.U24LE : SoundIOFormat.U24BE;
  169. public static readonly SoundIOFormat S32NE = BitConverter.IsLittleEndian ? SoundIOFormat.S32LE : SoundIOFormat.S32BE;
  170. public static readonly SoundIOFormat U32NE = BitConverter.IsLittleEndian ? SoundIOFormat.U32LE : SoundIOFormat.U32BE;
  171. public static readonly SoundIOFormat Float32NE = BitConverter.IsLittleEndian ? SoundIOFormat.Float32LE : SoundIOFormat.Float32BE;
  172. public static readonly SoundIOFormat Float64NE = BitConverter.IsLittleEndian ? SoundIOFormat.Float64LE : SoundIOFormat.Float64BE;
  173. public static readonly SoundIOFormat S16FE = !BitConverter.IsLittleEndian ? SoundIOFormat.S16LE : SoundIOFormat.S16BE;
  174. public static readonly SoundIOFormat U16FE = !BitConverter.IsLittleEndian ? SoundIOFormat.U16LE : SoundIOFormat.U16BE;
  175. public static readonly SoundIOFormat S24FE = !BitConverter.IsLittleEndian ? SoundIOFormat.S24LE : SoundIOFormat.S24BE;
  176. public static readonly SoundIOFormat U24FE = !BitConverter.IsLittleEndian ? SoundIOFormat.U24LE : SoundIOFormat.U24BE;
  177. public static readonly SoundIOFormat S32FE = !BitConverter.IsLittleEndian ? SoundIOFormat.S32LE : SoundIOFormat.S32BE;
  178. public static readonly SoundIOFormat U32FE = !BitConverter.IsLittleEndian ? SoundIOFormat.U32LE : SoundIOFormat.U32BE;
  179. public static readonly SoundIOFormat Float32FE = !BitConverter.IsLittleEndian ? SoundIOFormat.Float32LE : SoundIOFormat.Float32BE;
  180. public static readonly SoundIOFormat Float64FE = !BitConverter.IsLittleEndian ? SoundIOFormat.Float64LE : SoundIOFormat.Float64BE;
  181. public bool SupportsFormat(SoundIOFormat format)
  182. {
  183. return Natives.soundio_device_supports_format(handle, (SoundIoFormat)format);
  184. }
  185. public bool SupportsSampleRate(int sampleRate)
  186. {
  187. return Natives.soundio_device_supports_sample_rate(handle, sampleRate);
  188. }
  189. public bool SupportsChannelCount(int channelCount)
  190. {
  191. return Natives.soundio_device_supports_layout(handle, SoundIOChannelLayout.GetDefault(channelCount).Handle);
  192. }
  193. public int GetNearestSampleRate(int sampleRate)
  194. {
  195. return Natives.soundio_device_nearest_sample_rate(handle, sampleRate);
  196. }
  197. public SoundIOInStream CreateInStream()
  198. {
  199. return new SoundIOInStream(Natives.soundio_instream_create(handle));
  200. }
  201. public SoundIOOutStream CreateOutStream()
  202. {
  203. return new SoundIOOutStream(Natives.soundio_outstream_create(handle));
  204. }
  205. }
  206. }