AudioSource.cs 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340
  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 : ManagedComponent
  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. /// Determines if the playback of the audio clip should start as soon as the component is initialized.
  167. /// </summary>
  168. public bool PlayOnStart
  169. {
  170. get { return serializableData.playOnStart; }
  171. set { serializableData.playOnStart = value; }
  172. }
  173. /// <summary>
  174. /// Returns the current state of the audio playback (playing/paused/stopped).
  175. /// </summary>
  176. public AudioSourceState State
  177. {
  178. get
  179. {
  180. if (native != null)
  181. return native.State;
  182. return AudioSourceState.Stopped;
  183. }
  184. }
  185. /// <summary>
  186. /// Starts playing the currently assigned audio clip.
  187. /// </summary>
  188. public void Play()
  189. {
  190. if (native != null)
  191. native.Play();
  192. }
  193. /// <summary>
  194. /// Pauses audio playback.
  195. /// </summary>
  196. public void Pause()
  197. {
  198. if (native != null)
  199. native.Pause();
  200. }
  201. /// <summary>
  202. /// Stops audio playback, rewinding it to the start.
  203. /// </summary>
  204. public void Stop()
  205. {
  206. if (native != null)
  207. native.Stop();
  208. }
  209. /// <summary>
  210. /// Updates the transform of the internal source representation from the transform of the component's scene
  211. /// object.
  212. /// </summary>
  213. protected void UpdateTransform()
  214. {
  215. native.Position = SceneObject.Position;
  216. native.Velocity = velocity;
  217. }
  218. private void OnInitialize()
  219. {
  220. NotifyFlags = TransformChangedFlags.Transform;
  221. }
  222. private void OnUpdate()
  223. {
  224. Vector3 worldPos = SceneObject.Position;
  225. velocity = (worldPos - lastPosition) / BansheeEngine.Time.FrameDelta;
  226. lastPosition = worldPos;
  227. }
  228. private void OnEnable()
  229. {
  230. RestoreNative();
  231. if (serializableData.playOnStart)
  232. Play();
  233. }
  234. private void OnDisable()
  235. {
  236. DestroyNative();
  237. }
  238. private void OnDestroy()
  239. {
  240. DestroyNative();
  241. }
  242. private void OnTransformChanged(TransformChangedFlags flags)
  243. {
  244. if (!SceneObject.Active)
  245. return;
  246. if ((flags & (TransformChangedFlags.Parent | TransformChangedFlags.Transform)) != 0)
  247. UpdateTransform();
  248. }
  249. /// <summary>
  250. /// Destroys the internal source representation.
  251. /// </summary>
  252. private void DestroyNative()
  253. {
  254. if (native != null)
  255. {
  256. native.Destroy();
  257. native = null;
  258. }
  259. }
  260. /// <summary>
  261. /// Creates the internal representation of the source and restores the values saved by the component.
  262. /// </summary>
  263. private void RestoreNative()
  264. {
  265. native = new NativeAudioSource();
  266. native.Clip = serializableData.audioClip;
  267. native.Volume = serializableData.volume;
  268. native.Pitch = serializableData.pitch;
  269. native.Loop = serializableData.loop;
  270. native.Priority = serializableData.priority;
  271. native.MinDistance = serializableData.minDistance;
  272. native.Attenuation = serializableData.attenuation;
  273. UpdateTransform();
  274. }
  275. /// <summary>
  276. /// Holds all data the listener component needs to persist through serialization.
  277. /// </summary>
  278. [SerializeObject]
  279. internal class SerializableData
  280. {
  281. public AudioClip audioClip;
  282. public float volume = 1.0f;
  283. public float pitch = 1.0f;
  284. public bool loop = false;
  285. public uint priority = 0;
  286. public float minDistance = 1.0f;
  287. public float attenuation = 1.0f;
  288. public bool playOnStart = true;
  289. }
  290. }
  291. /** @} */
  292. }