AudioOutputManager.cs 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. using System.Collections.Generic;
  2. using UnityEngine;
  3. using System;
  4. //-----------------------------------------------------------------------------
  5. // Copyright 2015-2021 RenderHeads Ltd. All rights reserved.
  6. //-----------------------------------------------------------------------------
  7. namespace RenderHeads.Media.AVProVideo
  8. {
  9. /// <summary>
  10. /// A singleton to handle multiple instances of the AudioOutput component
  11. /// </summary>
  12. public class AudioOutputManager
  13. {
  14. private static AudioOutputManager _instance = null;
  15. public static AudioOutputManager Instance
  16. {
  17. get
  18. {
  19. if (_instance == null)
  20. {
  21. _instance = new AudioOutputManager();
  22. }
  23. return _instance;
  24. }
  25. }
  26. protected class PlayerInstance
  27. {
  28. public HashSet<AudioOutput> outputs;
  29. public float[] pcmData;
  30. public bool isPcmDataReady;
  31. }
  32. private Dictionary<MediaPlayer, PlayerInstance> _instances;
  33. private AudioOutputManager()
  34. {
  35. _instances = new Dictionary<MediaPlayer, PlayerInstance>();
  36. }
  37. public void RequestAudio(AudioOutput outputComponent, MediaPlayer mediaPlayer, float[] audioData, int audioChannelCount, int channelMask, AudioOutput.AudioOutputMode audioOutputMode, bool supportPositionalAudio)
  38. {
  39. if (mediaPlayer == null || mediaPlayer.Control == null || !mediaPlayer.Control.IsPlaying())
  40. {
  41. if (supportPositionalAudio)
  42. {
  43. ZeroAudio(audioData, 0);
  44. }
  45. return;
  46. }
  47. int channels = mediaPlayer.Control.GetAudioChannelCount();
  48. if (channels <= 0)
  49. {
  50. if (supportPositionalAudio)
  51. {
  52. ZeroAudio(audioData, 0);
  53. }
  54. return;
  55. }
  56. // total samples requested should be multiple of channels
  57. Debug.Assert(audioData.Length % audioChannelCount == 0);
  58. // Find or create an instance
  59. PlayerInstance instance = null;
  60. if (!_instances.TryGetValue(mediaPlayer, out instance))
  61. {
  62. instance = _instances[mediaPlayer] = new PlayerInstance()
  63. {
  64. outputs = new HashSet<AudioOutput>(),
  65. pcmData = null
  66. };
  67. }
  68. // requests data if it hasn't been requested yet for the current cycle
  69. if (instance.outputs.Count == 0 || instance.outputs.Contains(outputComponent) || instance.pcmData == null)
  70. {
  71. instance.outputs.Clear();
  72. int actualDataRequired = (audioData.Length * channels) / audioChannelCount;
  73. if (instance.pcmData == null || actualDataRequired != instance.pcmData.Length)
  74. {
  75. instance.pcmData = new float[actualDataRequired];
  76. }
  77. instance.isPcmDataReady = GrabAudio(mediaPlayer, instance.pcmData, channels);
  78. instance.outputs.Add(outputComponent);
  79. }
  80. if (instance.isPcmDataReady)
  81. {
  82. // calculate how many samples and what channels are needed and then copy over the data
  83. int samples = Math.Min(audioData.Length / audioChannelCount, instance.pcmData.Length / channels);
  84. int storedPos = 0;
  85. int requestedPos = 0;
  86. // multiple mode, copies over audio from desired channels into the same channels on the audiosource
  87. if (audioOutputMode == AudioOutput.AudioOutputMode.MultipleChannels)
  88. {
  89. int lesserChannels = Math.Min(channels, audioChannelCount);
  90. if (!supportPositionalAudio)
  91. {
  92. for (int i = 0; i < samples; ++i)
  93. {
  94. for (int j = 0; j < lesserChannels; ++j)
  95. {
  96. if ((1 << j & channelMask) > 0)
  97. {
  98. audioData[requestedPos + j] = instance.pcmData[storedPos + j];
  99. }
  100. }
  101. storedPos += channels;
  102. requestedPos += audioChannelCount;
  103. }
  104. }
  105. else
  106. {
  107. for (int i = 0; i < samples; ++i)
  108. {
  109. for (int j = 0; j < lesserChannels; ++j)
  110. {
  111. if ((1 << j & channelMask) > 0)
  112. {
  113. audioData[requestedPos + j] *= instance.pcmData[storedPos + j];
  114. }
  115. }
  116. storedPos += channels;
  117. requestedPos += audioChannelCount;
  118. }
  119. }
  120. }
  121. //Mono mode, copies over single channel to all output channels
  122. else if (audioOutputMode == AudioOutput.AudioOutputMode.OneToAllChannels)
  123. {
  124. int desiredChannel = 0;
  125. for (int i = 0; i < 8; ++i)
  126. {
  127. if ((channelMask & (1 << i)) > 0)
  128. {
  129. desiredChannel = i;
  130. break;
  131. }
  132. }
  133. if (desiredChannel < channels)
  134. {
  135. if (!supportPositionalAudio)
  136. {
  137. for (int i = 0; i < samples; ++i)
  138. {
  139. for (int j = 0; j < audioChannelCount; ++j)
  140. {
  141. audioData[requestedPos + j] = instance.pcmData[storedPos + desiredChannel];
  142. }
  143. storedPos += channels;
  144. requestedPos += audioChannelCount;
  145. }
  146. }
  147. else
  148. {
  149. for (int i = 0; i < samples; ++i)
  150. {
  151. for (int j = 0; j < audioChannelCount; ++j)
  152. {
  153. audioData[requestedPos + j] *= instance.pcmData[storedPos + desiredChannel];
  154. }
  155. storedPos += channels;
  156. requestedPos += audioChannelCount;
  157. }
  158. }
  159. }
  160. }
  161. // If there is left over audio
  162. if (supportPositionalAudio && requestedPos != audioData.Length)
  163. {
  164. // Zero the remaining audio data otherwise there are pops
  165. ZeroAudio(audioData, requestedPos);
  166. }
  167. }
  168. else
  169. {
  170. if (supportPositionalAudio)
  171. {
  172. // Zero the remaining audio data otherwise there are pops
  173. ZeroAudio(audioData, 0);
  174. }
  175. }
  176. }
  177. private void ZeroAudio(float[] audioData, int startPosition)
  178. {
  179. for (int i = startPosition; i < audioData.Length; i++)
  180. {
  181. audioData[i] = 0f;
  182. }
  183. }
  184. private bool GrabAudio(MediaPlayer player, float[] audioData, int channelCount)
  185. {
  186. return (0 != player.Control.GrabAudio(audioData, audioData.Length, channelCount));
  187. }
  188. }
  189. }