AudioSource.cs 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327
  1. //********************************** Banshee Engine (www.banshee3d.com) **************************************************//
  2. //**************** Copyright (c) 2016 Marko Pintera ([email protected]). All rights reserved. **********************//
  3. namespace BansheeEngine
  4. {
  5. /** @addtogroup Audio
  6. * @{
  7. */
  8. /// <summary>
  9. /// Valid states in which AudioSource can be in.
  10. /// </summary>
  11. public enum AudioSourceState // Note: Must match C++ enum AudioSourceState
  12. {
  13. /// <summary>
  14. /// Source is currently playing.
  15. /// </summary>
  16. Playing,
  17. /// <summary>
  18. /// Source is currently paused (play will resume from paused point).
  19. /// </summary>
  20. Paused,
  21. /// <summary>
  22. /// Source is currently stopped (play will resume from start).
  23. /// </summary>
  24. Stopped
  25. }
  26. /// <summary>
  27. /// Represents a source for emitting audio. Audio can be played spatially (gun shot), or normally (music). Each audio
  28. /// source must have an AudioClip to play-back, and it can also have a position in the case of spatial(3D) audio.
  29. ///
  30. /// Whether or not an audio source is spatial is controlled by the assigned AudioClip.The volume and the pitch of a
  31. /// spatial audio source is controlled by its position and the AudioListener's position/direction/velocity.
  32. /// </summary>
  33. public abstract class AudioSource : Component
  34. {
  35. internal NativeAudioSource native;
  36. [SerializeField]
  37. internal SerializableData serializableData = new SerializableData();
  38. private Vector3 lastPosition;
  39. private Vector3 velocity;
  40. /// <summary>
  41. /// Audio clip that will be played.
  42. /// </summary>
  43. public AudioClip Clip
  44. {
  45. get { return serializableData.audioClip; }
  46. set
  47. {
  48. if (serializableData.audioClip == value)
  49. return;
  50. serializableData.audioClip = value;
  51. if (native != null)
  52. native.Clip = value;
  53. }
  54. }
  55. /// <summary>
  56. /// Volume of the audio source, in [0, 1] range.
  57. /// </summary>
  58. public float Volume
  59. {
  60. get { return serializableData.volume; }
  61. set
  62. {
  63. if (serializableData.volume == value)
  64. return;
  65. serializableData.volume = value;
  66. if (native != null)
  67. native.Volume = value;
  68. }
  69. }
  70. /// <summary>
  71. /// Pitch of the audio source.
  72. /// </summary>
  73. public float Pitch
  74. {
  75. get { return serializableData.pitch; }
  76. set
  77. {
  78. if (serializableData.pitch == value)
  79. return;
  80. serializableData.pitch = value;
  81. if (native != null)
  82. native.Pitch = value;
  83. }
  84. }
  85. /// <summary>
  86. /// Determines whether the audio clip should loop when it finishes playing.
  87. /// </summary>
  88. public bool Loop
  89. {
  90. get { return serializableData.loop; }
  91. set
  92. {
  93. if (serializableData.loop == value)
  94. return;
  95. serializableData.loop = value;
  96. if (native != null)
  97. native.Loop = value;
  98. }
  99. }
  100. /// <summary>
  101. /// Sets the priority of the audio source. If more audio sources are playing than supported by the hardware, some
  102. /// might get disabled. By setting a higher priority the audio source is guaranteed to be disabled after sources
  103. /// with lower priority.
  104. /// </summary>
  105. public uint Priority
  106. {
  107. get { return serializableData.priority; }
  108. set
  109. {
  110. if (serializableData.priority == value)
  111. return;
  112. serializableData.priority = value;
  113. if (native != null)
  114. native.Priority = value;
  115. }
  116. }
  117. /// <summary>
  118. /// Minimum distance at which audio attenuation starts. When the listener is closer to the source than this value,
  119. /// audio is heard at full volume. Once farther away the audio starts attenuating.
  120. /// </summary>
  121. public float MinDistance
  122. {
  123. get { return serializableData.minDistance; }
  124. set
  125. {
  126. if (serializableData.minDistance == value)
  127. return;
  128. serializableData.minDistance = value;
  129. if (native != null)
  130. native.MinDistance = value;
  131. }
  132. }
  133. /// <summary>
  134. /// Determines how quickly does audio volume drop off as the listener moves further from the source.
  135. /// </summary>
  136. public float Attenuation
  137. {
  138. get { return serializableData.attenuation; }
  139. set
  140. {
  141. if (serializableData.attenuation == value)
  142. return;
  143. serializableData.attenuation = value;
  144. if (native != null)
  145. native.Attenuation = value;
  146. }
  147. }
  148. /// <summary>
  149. /// Playback position in seconds.
  150. /// </summary>
  151. public float Time
  152. {
  153. get
  154. {
  155. if (native != null)
  156. return native.Time;
  157. return 0.0f;
  158. }
  159. set
  160. {
  161. if (native != null)
  162. native.Time = value;
  163. }
  164. }
  165. /// <summary>
  166. /// Returns the current state of the audio playback (playing/paused/stopped).
  167. /// </summary>
  168. public AudioSourceState State
  169. {
  170. get
  171. {
  172. if (native != null)
  173. return native.State;
  174. return AudioSourceState.Stopped;
  175. }
  176. }
  177. /// <summary>
  178. /// Starts playing the currently assigned audio clip.
  179. /// </summary>
  180. public void Play()
  181. {
  182. if (native != null)
  183. native.Play();
  184. }
  185. /// <summary>
  186. /// Pauses audio playback.
  187. /// </summary>
  188. public void Pause()
  189. {
  190. if (native != null)
  191. native.Pause();
  192. }
  193. /// <summary>
  194. /// Stops audio playback, rewinding it to the start.
  195. /// </summary>
  196. public void Stop()
  197. {
  198. if (native != null)
  199. native.Stop();
  200. }
  201. /// <summary>
  202. /// Updates the transform of the internal source representation from the transform of the component's scene
  203. /// object.
  204. /// </summary>
  205. protected void UpdateTransform()
  206. {
  207. native.Position = SceneObject.Position;
  208. native.Velocity = velocity;
  209. }
  210. private void OnInitialize()
  211. {
  212. NotifyFlags = TransformChangedFlags.Transform;
  213. }
  214. private void OnUpdate()
  215. {
  216. Vector3 worldPos = SceneObject.Position;
  217. velocity = (worldPos - lastPosition) / BansheeEngine.Time.FrameDelta;
  218. lastPosition = worldPos;
  219. }
  220. private void OnEnable()
  221. {
  222. RestoreNative();
  223. }
  224. private void OnDisable()
  225. {
  226. DestroyNative();
  227. }
  228. private void OnDestroy()
  229. {
  230. DestroyNative();
  231. }
  232. private void OnTransformChanged(TransformChangedFlags flags)
  233. {
  234. if (!SceneObject.Active)
  235. return;
  236. if ((flags & (TransformChangedFlags.Parent | TransformChangedFlags.Transform)) != 0)
  237. UpdateTransform();
  238. }
  239. /// <summary>
  240. /// Destroys the internal source representation.
  241. /// </summary>
  242. private void DestroyNative()
  243. {
  244. if (native != null)
  245. {
  246. native.Destroy();
  247. native = null;
  248. }
  249. }
  250. /// <summary>
  251. /// Creates the internal representation of the source and restores the values saved by the component.
  252. /// </summary>
  253. private void RestoreNative()
  254. {
  255. native = new NativeAudioSource();
  256. native.Clip = serializableData.audioClip;
  257. native.Volume = serializableData.volume;
  258. native.Pitch = serializableData.pitch;
  259. native.Loop = serializableData.loop;
  260. native.Priority = serializableData.priority;
  261. native.MinDistance = serializableData.minDistance;
  262. native.Attenuation = serializableData.attenuation;
  263. UpdateTransform();
  264. }
  265. /// <summary>
  266. /// Holds all data the listener component needs to persist through serialization.
  267. /// </summary>
  268. [SerializeObject]
  269. internal class SerializableData
  270. {
  271. public AudioClip audioClip;
  272. public float volume = 1.0f;
  273. public float pitch = 1.0f;
  274. public bool loop = false;
  275. public uint priority = 0;
  276. public float minDistance = 1.0f;
  277. public float attenuation = 1.0f;
  278. }
  279. }
  280. /** @} */
  281. }