AudioOutputEditor.cs 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. using UnityEditor;
  2. using UnityEngine;
  3. //-----------------------------------------------------------------------------
  4. // Copyright 2015-2021 RenderHeads Ltd. All rights reserved.
  5. //-----------------------------------------------------------------------------
  6. namespace RenderHeads.Media.AVProVideo.Editor
  7. {
  8. /// <summary>
  9. /// Editor for the AudioOutput component
  10. /// </summary>
  11. [CanEditMultipleObjects]
  12. [CustomEditor(typeof(AudioOutput))]
  13. public class AudioOutputEditor : UnityEditor.Editor
  14. {
  15. private static readonly GUIContent _guiTextChannel = new GUIContent("Channel");
  16. private static readonly GUIContent _guiTextChannels = new GUIContent("Channels");
  17. private static readonly string[] _channelMaskOptions = { "1", "2", "3", "4", "5", "6", "7", "8" };
  18. private SerializedProperty _propChannelMask;
  19. private SerializedProperty _propAudioOutputMode;
  20. private int _unityAudioSampleRate;
  21. private int _unityAudioSpeakerCount;
  22. private string _bufferedMs;
  23. void OnEnable()
  24. {
  25. _propChannelMask = this.CheckFindProperty("_channelMask");
  26. _propAudioOutputMode = this.CheckFindProperty("_audioOutputMode");
  27. _unityAudioSampleRate = Helper.GetUnityAudioSampleRate();
  28. _unityAudioSpeakerCount = Helper.GetUnityAudioSpeakerCount();
  29. }
  30. public override void OnInspectorGUI()
  31. {
  32. serializedObject.Update();
  33. DrawDefaultInspector();
  34. // Display the channel mask as either a bitfield or value slider
  35. if ((AudioOutput.AudioOutputMode)_propAudioOutputMode.enumValueIndex == AudioOutput.AudioOutputMode.MultipleChannels)
  36. {
  37. _propChannelMask.intValue = EditorGUILayout.MaskField(_guiTextChannels, _propChannelMask.intValue, _channelMaskOptions);
  38. }
  39. else
  40. {
  41. int prevVal = 0;
  42. for(int i = 0; i < 8; ++i)
  43. {
  44. if((_propChannelMask.intValue & (1 << i)) > 0)
  45. {
  46. prevVal = i;
  47. break;
  48. }
  49. }
  50. int newVal = Mathf.Clamp(EditorGUILayout.IntSlider(_guiTextChannel, prevVal, 0, 7), 0, 7);
  51. _propChannelMask.intValue = 1 << newVal;
  52. }
  53. GUILayout.Label("Unity Audio", EditorStyles.boldLabel);
  54. EditorGUILayout.LabelField("Speakers", _unityAudioSpeakerCount.ToString());
  55. EditorGUILayout.LabelField("Sample Rate", _unityAudioSampleRate.ToString() + "hz");
  56. EditorGUILayout.Space();
  57. AudioOutput audioOutput = (AudioOutput)this.target;
  58. if (audioOutput != null)
  59. {
  60. if (audioOutput.Player != null && audioOutput.Player.Control != null)
  61. {
  62. int channelCount = audioOutput.Player.Control.GetAudioChannelCount();
  63. if (channelCount >= 0)
  64. {
  65. GUILayout.Label("Media Audio", EditorStyles.boldLabel);
  66. EditorGUILayout.LabelField("Channels: " + channelCount);
  67. AudioChannelMaskFlags audioChannels = audioOutput.Player.Control.GetAudioChannelMask();
  68. GUILayout.Label(audioChannels.ToString(), EditorHelper.IMGUI.GetWordWrappedTextAreaStyle());
  69. if (Time.frameCount % 4 == 0)
  70. {
  71. int bufferedSampleCount = audioOutput.Player.Control.GetAudioBufferedSampleCount();
  72. float bufferedMs = (bufferedSampleCount * 1000f) / (_unityAudioSampleRate * channelCount);
  73. _bufferedMs = "Buffered: " + bufferedMs.ToString("F2") + "ms";
  74. }
  75. EditorGUILayout.LabelField(_bufferedMs);
  76. EditorGUILayout.Space();
  77. }
  78. }
  79. }
  80. serializedObject.ApplyModifiedProperties();
  81. }
  82. }
  83. }