AudioOutput.cs 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. using System;
  2. using UnityEngine;
  3. namespace UMP
  4. {
  5. [RequireComponent(typeof(AudioSource))]
  6. public abstract class AudioOutput : MonoBehaviour
  7. {
  8. public enum AudioChannels
  9. {
  10. Both,
  11. Left,
  12. Right
  13. };
  14. [SerializeField]
  15. private AudioChannels _audioChannel;
  16. public AudioChannels AudioChannel
  17. {
  18. get { return _audioChannel; }
  19. set { _audioChannel = value; }
  20. }
  21. private int _id;
  22. public int Id
  23. {
  24. get { return _id; }
  25. }
  26. private AudioSource _audioSource;
  27. public AudioSource AudioSource
  28. {
  29. get {
  30. if (_audioSource == null)
  31. _audioSource = GetComponent<AudioSource>();
  32. return _audioSource;
  33. }
  34. }
  35. private float[] _data;
  36. internal float[] Data
  37. {
  38. get { return _data; }
  39. set
  40. {
  41. _data = value;
  42. if (_outputDataListener != null && _data != null)
  43. _outputDataListener(_data, _audioChannel);
  44. }
  45. }
  46. internal void Init()
  47. {
  48. _id = GetInstanceID();
  49. }
  50. public void Play()
  51. {
  52. if (_audioSource == null)
  53. _audioSource = GetComponent<AudioSource>();
  54. _audioSource.Play();
  55. }
  56. public void Pause()
  57. {
  58. if (_audioSource == null)
  59. _audioSource = GetComponent<AudioSource>();
  60. _audioSource.Pause();
  61. }
  62. public void Stop()
  63. {
  64. if (_audioSource == null)
  65. _audioSource = GetComponent<AudioSource>();
  66. _audioSource.Stop();
  67. }
  68. public void RemoveAllListeners()
  69. {
  70. if (_audioFilterReadListener != null)
  71. {
  72. foreach (Action<int, float[], AudioChannels> eh in _audioFilterReadListener.GetInvocationList())
  73. _audioFilterReadListener -= eh;
  74. }
  75. if (_outputDataListener != null)
  76. {
  77. foreach (Action<float[], AudioChannels> eh in _outputDataListener.GetInvocationList())
  78. _outputDataListener -= eh;
  79. }
  80. }
  81. private event Action<float[], AudioChannels> _outputDataListener;
  82. public event Action<float[], AudioChannels> OutputDataListener
  83. {
  84. add
  85. {
  86. _outputDataListener = (Action<float[], AudioChannels>)Delegate.Combine(_outputDataListener, value);
  87. }
  88. remove
  89. {
  90. if (_outputDataListener != null)
  91. _outputDataListener = (Action<float[], AudioChannels>)Delegate.Remove(_outputDataListener, value);
  92. }
  93. }
  94. private event Action<int, float[], AudioChannels> _audioFilterReadListener;
  95. internal event Action<int, float[], AudioChannels> AudioFilterReadListener
  96. {
  97. add
  98. {
  99. _audioFilterReadListener = (Action<int, float[], AudioChannels>)Delegate.Combine(_audioFilterReadListener, value);
  100. }
  101. remove
  102. {
  103. if (_audioFilterReadListener != null)
  104. _audioFilterReadListener = (Action<int, float[], AudioChannels>)Delegate.Remove(_audioFilterReadListener, value);
  105. }
  106. }
  107. /// Native Unity "AudioSource" callback
  108. private void OnAudioFilterRead(float[] data, int nbChannels)
  109. {
  110. if (_audioFilterReadListener != null)
  111. _audioFilterReadListener(Id, data, _audioChannel);
  112. }
  113. }
  114. }