AudioOutputManager.cs 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. using System;
  2. using System.Collections.Generic;
  3. namespace RenderHeads.Media.AVProVideo
  4. {
  5. public class AudioOutputManager
  6. {
  7. private AudioOutputManager()
  8. {
  9. this._accessTrackers = new Dictionary<MediaPlayer, HashSet<AudioOutput>>();
  10. this._pcmData = new Dictionary<MediaPlayer, float[]>();
  11. }
  12. public static AudioOutputManager Instance
  13. {
  14. get
  15. {
  16. if (AudioOutputManager._instance == null)
  17. {
  18. AudioOutputManager._instance = new AudioOutputManager();
  19. }
  20. return AudioOutputManager._instance;
  21. }
  22. }
  23. public void RequestAudio(AudioOutput _outputComponent, MediaPlayer mediaPlayer, float[] data, int channelMask, int totalChannels, AudioOutput.AudioOutputMode audioOutputMode)
  24. {
  25. if (mediaPlayer == null || mediaPlayer.Control == null || !mediaPlayer.Control.IsPlaying())
  26. {
  27. return;
  28. }
  29. int numAudioChannels = mediaPlayer.Control.GetNumAudioChannels();
  30. if (numAudioChannels <= 0)
  31. {
  32. return;
  33. }
  34. if (!this._accessTrackers.ContainsKey(mediaPlayer))
  35. {
  36. this._accessTrackers[mediaPlayer] = new HashSet<AudioOutput>();
  37. }
  38. if (this._accessTrackers[mediaPlayer].Contains(_outputComponent) || this._accessTrackers[mediaPlayer].Count == 0 || this._pcmData[mediaPlayer] == null)
  39. {
  40. this._accessTrackers[mediaPlayer].Clear();
  41. int num = data.Length / totalChannels * numAudioChannels;
  42. this._pcmData[mediaPlayer] = new float[num];
  43. this.GrabAudio(mediaPlayer, this._pcmData[mediaPlayer], numAudioChannels);
  44. this._accessTrackers[mediaPlayer].Add(_outputComponent);
  45. }
  46. int num2 = Math.Min(data.Length / totalChannels, this._pcmData[mediaPlayer].Length / numAudioChannels);
  47. int num3 = 0;
  48. int num4 = 0;
  49. if (audioOutputMode == AudioOutput.AudioOutputMode.Multiple)
  50. {
  51. int num5 = Math.Min(numAudioChannels, totalChannels);
  52. for (int i = 0; i < num2; i++)
  53. {
  54. for (int j = 0; j < num5; j++)
  55. {
  56. if ((1 << j & channelMask) > 0)
  57. {
  58. data[num4 + j] = this._pcmData[mediaPlayer][num3 + j];
  59. }
  60. }
  61. num3 += numAudioChannels;
  62. num4 += totalChannels;
  63. }
  64. }
  65. else if (audioOutputMode == AudioOutput.AudioOutputMode.Single)
  66. {
  67. int num6 = 0;
  68. for (int k = 0; k < 8; k++)
  69. {
  70. if ((channelMask & 1 << k) > 0)
  71. {
  72. num6 = k;
  73. break;
  74. }
  75. }
  76. if (num6 < numAudioChannels)
  77. {
  78. for (int l = 0; l < num2; l++)
  79. {
  80. for (int m = 0; m < totalChannels; m++)
  81. {
  82. data[num4 + m] = this._pcmData[mediaPlayer][num3 + num6];
  83. }
  84. num3 += numAudioChannels;
  85. num4 += totalChannels;
  86. }
  87. }
  88. }
  89. }
  90. private void GrabAudio(MediaPlayer player, float[] data, int channels)
  91. {
  92. player.Control.GrabAudio(data, data.Length, channels);
  93. }
  94. private static AudioOutputManager _instance;
  95. private Dictionary<MediaPlayer, HashSet<AudioOutput>> _accessTrackers;
  96. private Dictionary<MediaPlayer, float[]> _pcmData;
  97. }
  98. }