SoundIOChannelLayout.cs 2.7 KB

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