AudioOutput.cs 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. using UnityEngine;
  2. //-----------------------------------------------------------------------------
  3. // Copyright 2015-2022 RenderHeads Ltd. All rights reserved.
  4. //-----------------------------------------------------------------------------
  5. namespace RenderHeads.Media.AVProVideo
  6. {
  7. /// <summary>
  8. /// Audio is grabbed from the MediaPlayer and rendered via Unity AudioSource
  9. /// This allows audio to have 3D spatial control, effects applied and to be spatialised for VR
  10. /// Currently supported on Windows and UWP (Media Foundation API only), macOS, iOS, tvOS and Android (ExoPlayer API only)
  11. /// </summary>
  12. [RequireComponent(typeof(AudioSource))]
  13. [AddComponentMenu("AVPro Video/Audio Output", 400)]
  14. [HelpURL("https://www.renderheads.com/products/avpro-video/")]
  15. public class AudioOutput : MonoBehaviour
  16. {
  17. public enum AudioOutputMode
  18. {
  19. OneToAllChannels,
  20. MultipleChannels
  21. }
  22. [SerializeField] MediaPlayer _mediaPlayer = null;
  23. [SerializeField] AudioOutputMode _audioOutputMode = AudioOutputMode.MultipleChannels;
  24. [HideInInspector, SerializeField] int _channelMask = 0xffff;
  25. [SerializeField] bool _supportPositionalAudio = false;
  26. public MediaPlayer Player
  27. {
  28. get { return _mediaPlayer; }
  29. set { ChangeMediaPlayer(value); }
  30. }
  31. public AudioOutputMode OutputMode
  32. {
  33. get { return _audioOutputMode; }
  34. set { _audioOutputMode = value; }
  35. }
  36. public int ChannelMask
  37. {
  38. get { return _channelMask; }
  39. set { _channelMask = value; }
  40. }
  41. private AudioSource _audioSource;
  42. void Awake()
  43. {
  44. _audioSource = this.GetComponent<AudioSource>();
  45. Debug.Assert(_audioSource != null);
  46. }
  47. void Start()
  48. {
  49. ChangeMediaPlayer(_mediaPlayer);
  50. }
  51. void OnDestroy()
  52. {
  53. ChangeMediaPlayer(null);
  54. }
  55. void Update()
  56. {
  57. if (_mediaPlayer != null && _mediaPlayer.Control != null && _mediaPlayer.Control.IsPlaying())
  58. {
  59. ApplyAudioSettings(_mediaPlayer, _audioSource);
  60. }
  61. }
  62. public AudioSource GetAudioSource()
  63. {
  64. return _audioSource;
  65. }
  66. public void ChangeMediaPlayer(MediaPlayer newPlayer)
  67. {
  68. #if UNITY_EDITOR_OSX || UNITY_STANDALONE_OSX || (!UNITY_EDITOR && (UNITY_IOS || UNITY_TVOS))
  69. if (newPlayer != null)
  70. {
  71. MediaPlayer.OptionsApple options = (MediaPlayer.OptionsApple)newPlayer.GetCurrentPlatformOptions();
  72. if (options.audioMode == MediaPlayer.OptionsApple.AudioMode.Unity)
  73. {
  74. this.enabled = true;
  75. }
  76. else
  77. {
  78. Debug.LogWarning("[AVProVideo] Unity audio output is not supported when 'Audio Output Mode' is not set to 'Unity' in the MediaPlayer platform options");
  79. this.enabled = false;
  80. return;
  81. }
  82. }
  83. #endif
  84. // When changing the media player, handle event subscriptions
  85. if (_mediaPlayer != null)
  86. {
  87. _mediaPlayer.AudioSource = null;
  88. _mediaPlayer.Events.RemoveListener(OnMediaPlayerEvent);
  89. _mediaPlayer = null;
  90. }
  91. _mediaPlayer = newPlayer;
  92. if (_mediaPlayer != null)
  93. {
  94. _mediaPlayer.Events.AddListener(OnMediaPlayerEvent);
  95. _mediaPlayer.AudioSource = _audioSource;
  96. }
  97. if (_supportPositionalAudio)
  98. {
  99. if (_audioSource.clip == null)
  100. {
  101. // Position audio is implemented from hints found on this thread:
  102. // https://forum.unity.com/threads/onaudiofilterread-sound-spatialisation.362782/
  103. int frameCount = 2048 * 10;
  104. int sampleCount = frameCount * Helper.GetUnityAudioSpeakerCount();
  105. AudioClip clip = AudioClip.Create("dummy", frameCount, Helper.GetUnityAudioSpeakerCount(), Helper.GetUnityAudioSampleRate(), false);
  106. float[] samples = new float[sampleCount];
  107. for (int i = 0; i < samples.Length; i++) { samples[i] = 1f; }
  108. clip.SetData(samples, 0);
  109. _audioSource.clip = clip;
  110. _audioSource.loop = true;
  111. }
  112. }
  113. else if (_audioSource.clip != null)
  114. {
  115. _audioSource.clip = null;
  116. }
  117. }
  118. // Callback function to handle events
  119. private void OnMediaPlayerEvent(MediaPlayer mp, MediaPlayerEvent.EventType et, ErrorCode errorCode)
  120. {
  121. switch (et)
  122. {
  123. case MediaPlayerEvent.EventType.Closing:
  124. _audioSource.Stop();
  125. break;
  126. case MediaPlayerEvent.EventType.Started:
  127. ApplyAudioSettings(_mediaPlayer, _audioSource);
  128. _audioSource.Play();
  129. break;
  130. }
  131. }
  132. private static void ApplyAudioSettings(MediaPlayer player, AudioSource audioSource)
  133. {
  134. // Apply volume and mute from the MediaPlayer to the AudioSource
  135. if (audioSource != null && player != null && player.Control != null)
  136. {
  137. float volume = player.Control.GetVolume();
  138. bool isMuted = player.Control.IsMuted();
  139. float rate = player.Control.GetPlaybackRate();
  140. audioSource.volume = volume;
  141. audioSource.mute = isMuted;
  142. audioSource.pitch = rate;
  143. }
  144. }
  145. #if (UNITY_EDITOR_WIN || UNITY_EDITOR_OSX) || (!UNITY_EDITOR && (UNITY_STANDALONE_WIN || UNITY_WSA_10_0 || UNITY_STANDALONE_OSX || UNITY_IOS || UNITY_TVOS || UNITY_ANDROID))
  146. void OnAudioFilterRead(float[] audioData, int channelCount)
  147. {
  148. AudioOutputManager.Instance.RequestAudio(this, _mediaPlayer, audioData, channelCount, _channelMask, _audioOutputMode, _supportPositionalAudio);
  149. }
  150. #endif
  151. }
  152. }