AudioOutput.cs 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. //-----------------------------------------------------------------------------
  2. // Copyright 2015-2023 RenderHeads Ltd. All rights reserved.
  3. //-----------------------------------------------------------------------------
  4. using UnityEngine;
  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 == null || _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. // When changing the media player, handle event subscriptions
  76. if (_mediaPlayer != null)
  77. {
  78. _mediaPlayer.AudioSource = null;
  79. _mediaPlayer.Events.RemoveListener(OnMediaPlayerEvent);
  80. _mediaPlayer = null;
  81. }
  82. _mediaPlayer = newPlayer;
  83. if (_mediaPlayer != null)
  84. {
  85. _mediaPlayer.Events.AddListener(OnMediaPlayerEvent);
  86. _mediaPlayer.AudioSource = _audioSource;
  87. }
  88. if (_supportPositionalAudio)
  89. {
  90. if (_audioSource.clip == null)
  91. {
  92. // Position audio is implemented from hints found on this thread:
  93. // https://forum.unity.com/threads/onaudiofilterread-sound-spatialisation.362782/
  94. int frameCount = 2048 * 10;
  95. int sampleCount = frameCount * Helper.GetUnityAudioSpeakerCount();
  96. AudioClip clip = AudioClip.Create("dummy", frameCount, Helper.GetUnityAudioSpeakerCount(), Helper.GetUnityAudioSampleRate(), false);
  97. float[] samples = new float[sampleCount];
  98. for (int i = 0; i < samples.Length; i++) { samples[i] = 1f; }
  99. clip.SetData(samples, 0);
  100. _audioSource.clip = clip;
  101. _audioSource.loop = true;
  102. }
  103. }
  104. else if (_audioSource.clip != null)
  105. {
  106. _audioSource.clip = null;
  107. }
  108. }
  109. // Callback function to handle events
  110. private void OnMediaPlayerEvent(MediaPlayer mp, MediaPlayerEvent.EventType et, ErrorCode errorCode)
  111. {
  112. switch (et)
  113. {
  114. case MediaPlayerEvent.EventType.Closing:
  115. _audioSource.Stop();
  116. break;
  117. case MediaPlayerEvent.EventType.Started:
  118. ApplyAudioSettings(_mediaPlayer, _audioSource);
  119. _audioSource.Play();
  120. break;
  121. }
  122. }
  123. private static void ApplyAudioSettings(MediaPlayer player, AudioSource audioSource)
  124. {
  125. // Apply volume and mute from the MediaPlayer to the AudioSource
  126. if (audioSource != null && player != null && player.Control != null)
  127. {
  128. float volume = player.Control.GetVolume();
  129. bool isMuted = player.Control.IsMuted();
  130. float rate = player.Control.GetPlaybackRate();
  131. audioSource.volume = volume;
  132. audioSource.mute = isMuted;
  133. audioSource.pitch = rate;
  134. }
  135. }
  136. #if (UNITY_EDITOR_WIN || UNITY_EDITOR_OSX) || (!UNITY_EDITOR && (UNITY_STANDALONE_WIN || UNITY_WSA_10_0 || UNITY_STANDALONE_OSX || UNITY_IOS || UNITY_TVOS || UNITY_VISIONOS || UNITY_ANDROID))
  137. void OnAudioFilterRead(float[] audioData, int channelCount)
  138. {
  139. AudioOutputManager.Instance.RequestAudio(this, _mediaPlayer, audioData, channelCount, _channelMask, _audioOutputMode, _supportPositionalAudio);
  140. }
  141. #endif
  142. }
  143. }