AudioOutput.cs 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  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. AudioSettings.OnAudioConfigurationChanged += OnAudioConfigurationChanged;
  50. ChangeMediaPlayer(_mediaPlayer);
  51. }
  52. void OnAudioConfigurationChanged(bool deviceChanged)
  53. {
  54. if (_mediaPlayer.Control == null)
  55. return;
  56. _mediaPlayer.Control.AudioConfigurationChanged(deviceChanged);
  57. }
  58. void OnDestroy()
  59. {
  60. ChangeMediaPlayer(null);
  61. }
  62. void Update()
  63. {
  64. if (_mediaPlayer != null && _mediaPlayer.Control != null && _mediaPlayer.Control.IsPlaying())
  65. {
  66. ApplyAudioSettings(_mediaPlayer, _audioSource);
  67. }
  68. }
  69. public AudioSource GetAudioSource()
  70. {
  71. return _audioSource;
  72. }
  73. public void ChangeMediaPlayer(MediaPlayer newPlayer)
  74. {
  75. #if UNITY_EDITOR_OSX || UNITY_STANDALONE_OSX || (!UNITY_EDITOR && (UNITY_IOS || UNITY_TVOS))
  76. if (newPlayer != null)
  77. {
  78. MediaPlayer.OptionsApple options = (MediaPlayer.OptionsApple)newPlayer.GetCurrentPlatformOptions();
  79. if (options.audioMode == MediaPlayer.OptionsApple.AudioMode.Unity)
  80. {
  81. this.enabled = true;
  82. }
  83. else
  84. {
  85. Debug.LogWarning("[AVProVideo] Unity audio output is not supported when 'Audio Output Mode' is not set to 'Unity' in the MediaPlayer platform options");
  86. this.enabled = false;
  87. return;
  88. }
  89. }
  90. #endif
  91. // When changing the media player, handle event subscriptions
  92. if (_mediaPlayer != null)
  93. {
  94. _mediaPlayer.AudioSource = null;
  95. _mediaPlayer.Events.RemoveListener(OnMediaPlayerEvent);
  96. _mediaPlayer = null;
  97. }
  98. _mediaPlayer = newPlayer;
  99. if (_mediaPlayer != null)
  100. {
  101. _mediaPlayer.Events.AddListener(OnMediaPlayerEvent);
  102. _mediaPlayer.AudioSource = _audioSource;
  103. }
  104. if (_supportPositionalAudio)
  105. {
  106. if (_audioSource.clip == null)
  107. {
  108. // Position audio is implemented from hints found on this thread:
  109. // https://forum.unity.com/threads/onaudiofilterread-sound-spatialisation.362782/
  110. int frameCount = 2048 * 10;
  111. int sampleCount = frameCount * Helper.GetUnityAudioSpeakerCount();
  112. AudioClip clip = AudioClip.Create("dummy", frameCount, Helper.GetUnityAudioSpeakerCount(), Helper.GetUnityAudioSampleRate(), false);
  113. float[] samples = new float[sampleCount];
  114. for (int i = 0; i < samples.Length; i++) { samples[i] = 1f; }
  115. clip.SetData(samples, 0);
  116. _audioSource.clip = clip;
  117. _audioSource.loop = true;
  118. }
  119. }
  120. else if (_audioSource.clip != null)
  121. {
  122. _audioSource.clip = null;
  123. }
  124. }
  125. // Callback function to handle events
  126. private void OnMediaPlayerEvent(MediaPlayer mp, MediaPlayerEvent.EventType et, ErrorCode errorCode)
  127. {
  128. switch (et)
  129. {
  130. case MediaPlayerEvent.EventType.Closing:
  131. _audioSource.Stop();
  132. break;
  133. case MediaPlayerEvent.EventType.Started:
  134. ApplyAudioSettings(_mediaPlayer, _audioSource);
  135. _audioSource.Play();
  136. break;
  137. }
  138. }
  139. private static void ApplyAudioSettings(MediaPlayer player, AudioSource audioSource)
  140. {
  141. // Apply volume and mute from the MediaPlayer to the AudioSource
  142. if (audioSource != null && player != null && player.Control != null)
  143. {
  144. float volume = player.Control.GetVolume();
  145. bool isMuted = player.Control.IsMuted();
  146. float rate = player.Control.GetPlaybackRate();
  147. audioSource.volume = volume;
  148. audioSource.mute = isMuted;
  149. audioSource.pitch = rate;
  150. }
  151. }
  152. #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))
  153. void OnAudioFilterRead(float[] audioData, int channelCount)
  154. {
  155. AudioOutputManager.Instance.RequestAudio(this, _mediaPlayer, audioData, channelCount, _channelMask, _audioOutputMode, _supportPositionalAudio);
  156. }
  157. #endif
  158. }
  159. }