AudioOutputEditor.cs 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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 _propMediaPlayer;
  19. private SerializedProperty _propAudioOutputMode;
  20. private SerializedProperty _propSupportPositionalAudio;
  21. private SerializedProperty _propChannelMask;
  22. private int _unityAudioSampleRate;
  23. private int _unityAudioSpeakerCount;
  24. private string _bufferedMs;
  25. void OnEnable()
  26. {
  27. _propMediaPlayer = this.CheckFindProperty("_mediaPlayer");
  28. _propAudioOutputMode = this.CheckFindProperty("_audioOutputMode");
  29. _propSupportPositionalAudio = this.CheckFindProperty("_supportPositionalAudio");
  30. _propChannelMask = this.CheckFindProperty("_channelMask");
  31. _unityAudioSampleRate = Helper.GetUnityAudioSampleRate();
  32. _unityAudioSpeakerCount = Helper.GetUnityAudioSpeakerCount();
  33. }
  34. public override void OnInspectorGUI()
  35. {
  36. serializedObject.Update();
  37. AudioOutput audioOutput = (AudioOutput)this.target;
  38. EditorGUILayout.PropertyField(_propMediaPlayer);
  39. EditorGUILayout.PropertyField(_propAudioOutputMode);
  40. EditorGUILayout.PropertyField(_propSupportPositionalAudio);
  41. // Display the channel mask as either a bitfield or value slider
  42. if ((AudioOutput.AudioOutputMode)_propAudioOutputMode.enumValueIndex == AudioOutput.AudioOutputMode.MultipleChannels)
  43. {
  44. _propChannelMask.intValue = EditorGUILayout.MaskField(_guiTextChannels, _propChannelMask.intValue, _channelMaskOptions);
  45. }
  46. else
  47. {
  48. int prevVal = 0;
  49. for(int i = 0; i < 8; ++i)
  50. {
  51. if((_propChannelMask.intValue & (1 << i)) > 0)
  52. {
  53. prevVal = i;
  54. break;
  55. }
  56. }
  57. int newVal = Mathf.Clamp(EditorGUILayout.IntSlider(_guiTextChannel, prevVal, 0, 7), 0, 7);
  58. _propChannelMask.intValue = 1 << newVal;
  59. }
  60. GUILayout.Label("Unity Audio", EditorStyles.boldLabel);
  61. EditorGUILayout.LabelField("Speakers", _unityAudioSpeakerCount.ToString());
  62. EditorGUILayout.LabelField("Sample Rate", _unityAudioSampleRate.ToString() + "hz");
  63. EditorGUILayout.Space();
  64. if (audioOutput != null)
  65. {
  66. if (audioOutput.Player != null && audioOutput.Player.Control != null)
  67. {
  68. int channelCount = audioOutput.Player.Control.GetAudioChannelCount();
  69. if (channelCount >= 0)
  70. {
  71. GUILayout.Label("Media Audio", EditorStyles.boldLabel);
  72. EditorGUILayout.LabelField("Channels: " + channelCount);
  73. AudioChannelMaskFlags audioChannels = audioOutput.Player.Control.GetAudioChannelMask();
  74. GUILayout.Label(audioChannels.ToString(), EditorHelper.IMGUI.GetWordWrappedTextAreaStyle());
  75. if (Time.frameCount % 4 == 0)
  76. {
  77. int bufferedSampleCount = audioOutput.Player.Control.GetAudioBufferedSampleCount();
  78. float bufferedMs = (bufferedSampleCount * 1000f) / (_unityAudioSampleRate * channelCount);
  79. _bufferedMs = "Buffered: " + bufferedMs.ToString("F2") + "ms";
  80. }
  81. EditorGUILayout.LabelField(_bufferedMs);
  82. EditorGUILayout.Space();
  83. }
  84. }
  85. }
  86. serializedObject.ApplyModifiedProperties();
  87. }
  88. }
  89. }