SoundIOChannelLayout.cs 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Runtime.InteropServices;
  4. namespace SoundIOSharp
  5. {
  6. public readonly struct SoundIOChannelLayout
  7. {
  8. public static int BuiltInCount
  9. {
  10. get { return Natives.soundio_channel_layout_builtin_count(); }
  11. }
  12. public static SoundIOChannelLayout GetBuiltIn(int index)
  13. {
  14. return new SoundIOChannelLayout(Natives.soundio_channel_layout_get_builtin(index));
  15. }
  16. public static SoundIOChannelLayout GetDefault(int channelCount)
  17. {
  18. var handle = Natives.soundio_channel_layout_get_default(channelCount);
  19. return new SoundIOChannelLayout (handle);
  20. }
  21. public static SoundIOChannelId ParseChannelId(string name)
  22. {
  23. var ptr = Marshal.StringToHGlobalAnsi(name);
  24. try
  25. {
  26. return (SoundIOChannelId)Natives.soundio_parse_channel_id(ptr, name.Length);
  27. }
  28. finally
  29. {
  30. Marshal.FreeHGlobal(ptr);
  31. }
  32. }
  33. // instance members
  34. internal SoundIOChannelLayout(Pointer<SoundIoChannelLayout> handle)
  35. {
  36. this.handle = handle;
  37. }
  38. readonly Pointer<SoundIoChannelLayout> handle;
  39. public bool IsNull
  40. {
  41. get { return handle.Handle == IntPtr.Zero; }
  42. }
  43. internal IntPtr Handle
  44. {
  45. get { return handle; }
  46. }
  47. public int ChannelCount
  48. {
  49. get { return IsNull ? 0 : Marshal.ReadInt32((IntPtr)handle + channel_count_offset); }
  50. }
  51. static readonly int channel_count_offset = (int)Marshal.OffsetOf<SoundIoChannelLayout>("channel_count");
  52. public string Name
  53. {
  54. get { return IsNull ? null : Marshal.PtrToStringAnsi(Marshal.ReadIntPtr((IntPtr)handle + name_offset)); }
  55. }
  56. static readonly int name_offset = (int)Marshal.OffsetOf<SoundIoChannelLayout>("name");
  57. public IEnumerable<SoundIOChannelId> Channels
  58. {
  59. get
  60. {
  61. if (IsNull) yield break;
  62. for (int i = 0; i < 24; i++)
  63. {
  64. yield return (SoundIOChannelId)Marshal.ReadInt32((IntPtr)handle + channels_offset + sizeof(SoundIoChannelId) * i);
  65. }
  66. }
  67. }
  68. static readonly int channels_offset = (int)Marshal.OffsetOf<SoundIoChannelLayout>("channels");
  69. public override bool Equals(object other)
  70. {
  71. if (!(other is SoundIOChannelLayout)) return false;
  72. var s = (SoundIOChannelLayout) other;
  73. return handle == s.handle || Natives.soundio_channel_layout_equal(handle, s.handle);
  74. }
  75. public override int GetHashCode()
  76. {
  77. return handle.GetHashCode();
  78. }
  79. public string DetectBuiltInName()
  80. {
  81. if (IsNull) throw new InvalidOperationException();
  82. return Natives.soundio_channel_layout_detect_builtin(handle) ? Name : null;
  83. }
  84. public int FindChannel(SoundIOChannelId channel)
  85. {
  86. if (IsNull) throw new InvalidOperationException();
  87. return Natives.soundio_channel_layout_find_channel(handle, (SoundIoChannelId)channel);
  88. }
  89. }
  90. }