SoundEmitter.cs 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433
  1. using UnityEngine;
  2. using UnityEngine.Audio;
  3. using System.Collections;
  4. namespace OVR
  5. {
  6. /*
  7. -----------------------
  8. SoundEmitter()
  9. -----------------------
  10. */
  11. public class SoundEmitter : MonoBehaviour {
  12. public enum FadeState {
  13. Null,
  14. FadingIn,
  15. FadingOut,
  16. Ducking,
  17. }
  18. // OPTIMIZE
  19. public float volume { get { return audioSource.volume; } set { audioSource.volume = value; } }
  20. public float pitch { get { return audioSource.pitch; } set { audioSource.pitch = value; } }
  21. public AudioClip clip { get { return audioSource.clip; } set { audioSource.clip = value; } }
  22. public float time { get { return audioSource.time; } set { audioSource.time = value; } }
  23. public float length { get { return ( audioSource.clip != null ) ? audioSource.clip.length : 0.0f; } }
  24. public bool loop { get { return audioSource.loop; } set { audioSource.loop = value; } }
  25. public bool mute { get { return audioSource.mute; } set { audioSource.mute = value; } }
  26. public AudioVelocityUpdateMode velocityUpdateMode { get { return audioSource.velocityUpdateMode; } set { audioSource.velocityUpdateMode = value; } }
  27. public bool isPlaying { get { return audioSource.isPlaying; } }
  28. public EmitterChannel channel = EmitterChannel.Reserved;
  29. public bool disableSpatialization = false;
  30. private FadeState state = FadeState.Null;
  31. [System.NonSerialized]
  32. [HideInInspector]
  33. public AudioSource audioSource = null;
  34. [System.NonSerialized]
  35. [HideInInspector]
  36. public SoundPriority priority = SoundPriority.Default;
  37. [System.NonSerialized]
  38. [HideInInspector]
  39. public ONSPAudioSource osp = null;
  40. [System.NonSerialized]
  41. [HideInInspector]
  42. public float endPlayTime = 0.0f;
  43. private Transform lastParentTransform = null;
  44. [System.NonSerialized]
  45. [HideInInspector]
  46. public float defaultVolume = 1.0f;
  47. [System.NonSerialized]
  48. [HideInInspector]
  49. public Transform defaultParent = null;
  50. [System.NonSerialized]
  51. [HideInInspector]
  52. public int originalIdx = -1;
  53. [System.NonSerialized]
  54. [HideInInspector]
  55. public System.Action onFinished = null;
  56. [System.NonSerialized]
  57. [HideInInspector]
  58. public System.Action<object> onFinishedObject = null;
  59. [System.NonSerialized]
  60. [HideInInspector]
  61. public object onFinishedParam;
  62. [System.NonSerialized]
  63. [HideInInspector]
  64. public SoundGroup playingSoundGroup = null;
  65. /*
  66. -----------------------
  67. Awake()
  68. -----------------------
  69. */
  70. void Awake() {
  71. // unity defaults to 'playOnAwake = true'
  72. audioSource = GetComponent<AudioSource>();
  73. if ( audioSource == null ) {
  74. audioSource = gameObject.AddComponent<AudioSource>();
  75. }
  76. // is the spatialized audio enabled?
  77. if ( AudioManager.enableSpatialization && !disableSpatialization ) {
  78. osp = GetComponent<ONSPAudioSource>();
  79. if ( osp == null ) {
  80. osp = gameObject.AddComponent<ONSPAudioSource>();
  81. }
  82. }
  83. audioSource.playOnAwake = false;
  84. audioSource.Stop();
  85. }
  86. /*
  87. -----------------------
  88. SetPlayingSoundGroup()
  89. -----------------------
  90. */
  91. public void SetPlayingSoundGroup( SoundGroup soundGroup ) {
  92. playingSoundGroup = soundGroup;
  93. if ( soundGroup != null ) {
  94. soundGroup.IncrementPlayCount();
  95. }
  96. }
  97. /*
  98. -----------------------
  99. SetOnFinished()
  100. -----------------------
  101. */
  102. public void SetOnFinished( System.Action onFinished ) {
  103. this.onFinished = onFinished;
  104. }
  105. /*
  106. -----------------------
  107. SetOnFinished()
  108. -----------------------
  109. */
  110. public void SetOnFinished( System.Action<object> onFinished, object obj ) {
  111. onFinishedObject = onFinished;
  112. onFinishedParam = obj;
  113. }
  114. /*
  115. -----------------------
  116. SetChannel()
  117. -----------------------
  118. */
  119. public void SetChannel( int _channel ) {
  120. channel = (EmitterChannel)_channel;
  121. }
  122. /*
  123. -----------------------
  124. SetDefaultParent()
  125. -----------------------
  126. */
  127. public void SetDefaultParent( Transform parent ) {
  128. defaultParent = parent;
  129. }
  130. /*
  131. -----------------------
  132. SetAudioMixer()
  133. -----------------------
  134. */
  135. public void SetAudioMixer( AudioMixerGroup _mixer ) {
  136. if ( audioSource != null ) {
  137. audioSource.outputAudioMixerGroup = _mixer;
  138. }
  139. }
  140. /*
  141. -----------------------
  142. IsPlaying()
  143. -----------------------
  144. */
  145. public bool IsPlaying() {
  146. if ( loop && audioSource.isPlaying ) {
  147. return true;
  148. }
  149. return endPlayTime > Time.time;
  150. }
  151. /*
  152. -----------------------
  153. Play()
  154. -----------------------
  155. */
  156. public void Play() {
  157. // overrides everything
  158. state = FadeState.Null;
  159. endPlayTime = Time.time + length;
  160. StopAllCoroutines();
  161. audioSource.Play();
  162. }
  163. /*
  164. -----------------------
  165. Pause()
  166. -----------------------
  167. */
  168. public void Pause() {
  169. // overrides everything
  170. state = FadeState.Null;
  171. StopAllCoroutines();
  172. audioSource.Pause();
  173. }
  174. /*
  175. -----------------------
  176. Stop()
  177. -----------------------
  178. */
  179. public void Stop() {
  180. // overrides everything
  181. state = FadeState.Null;
  182. StopAllCoroutines();
  183. if ( audioSource != null ) {
  184. audioSource.Stop();
  185. }
  186. if ( onFinished != null ) {
  187. onFinished();
  188. onFinished = null;
  189. }
  190. if ( onFinishedObject != null ) {
  191. onFinishedObject( onFinishedParam );
  192. onFinishedObject = null;
  193. }
  194. if ( playingSoundGroup != null ) {
  195. playingSoundGroup.DecrementPlayCount();
  196. playingSoundGroup = null;
  197. }
  198. }
  199. /*
  200. -----------------------
  201. GetSampleTime()
  202. -----------------------
  203. */
  204. int GetSampleTime() {
  205. return audioSource.clip.samples - audioSource.timeSamples;
  206. }
  207. /*
  208. -----------------------
  209. ParentTo()
  210. -----------------------
  211. */
  212. public void ParentTo( Transform parent ) {
  213. if ( lastParentTransform != null ) {
  214. Debug.LogError( "[SoundEmitter] You must detach the sound emitter before parenting to another object!" );
  215. return;
  216. }
  217. lastParentTransform = transform.parent;
  218. transform.parent = parent;
  219. }
  220. /*
  221. -----------------------
  222. DetachFromParent()
  223. -----------------------
  224. */
  225. public void DetachFromParent() {
  226. if ( lastParentTransform == null ) {
  227. transform.parent = defaultParent;
  228. return;
  229. }
  230. transform.parent = lastParentTransform;
  231. lastParentTransform = null;
  232. }
  233. /*
  234. -----------------------
  235. ResetParent()
  236. -----------------------
  237. */
  238. public void ResetParent( Transform parent ) {
  239. transform.parent = parent;
  240. lastParentTransform = null;
  241. }
  242. /*
  243. -----------------------
  244. SyncTo()
  245. -----------------------
  246. */
  247. public void SyncTo( SoundEmitter other, float fadeTime, float toVolume ) {
  248. StartCoroutine( DelayedSyncTo( other, fadeTime, toVolume ) );
  249. }
  250. /*
  251. -----------------------
  252. DelayedSyncTo()
  253. have to wait until the end of frame to do proper sync'ing
  254. -----------------------
  255. */
  256. IEnumerator DelayedSyncTo( SoundEmitter other, float fadeTime, float toVolume ) {
  257. yield return new WaitForEndOfFrame();
  258. //audio.timeSamples = other.GetSampleTime();
  259. //audio.time = Mathf.Min( Mathf.Max( 0.0f, other.time - other.length ), other.time );
  260. audioSource.time = other.time;
  261. audioSource.Play();
  262. FadeTo( fadeTime, toVolume );
  263. }
  264. /*
  265. -----------------------
  266. FadeTo()
  267. -----------------------
  268. */
  269. public void FadeTo( float fadeTime, float toVolume ) {
  270. //Log.Print( ">>> FADE TO: " + channel );
  271. // don't override a fade out
  272. if ( state == FadeState.FadingOut ) {
  273. //Log.Print( " ....ABORTED" );
  274. return;
  275. }
  276. state = FadeState.Ducking;
  277. StopAllCoroutines();
  278. StartCoroutine( FadeSoundChannelTo( fadeTime, toVolume ) );
  279. }
  280. /*
  281. -----------------------
  282. FadeIn()
  283. -----------------------
  284. */
  285. public void FadeIn( float fadeTime, float defaultVolume ) {
  286. //Log.Print( ">>> FADE IN: " + channel );
  287. audioSource.volume = 0.0f;
  288. state = FadeState.FadingIn;
  289. StopAllCoroutines();
  290. StartCoroutine( FadeSoundChannel( 0.0f, fadeTime, Fade.In, defaultVolume ) );
  291. }
  292. /*
  293. -----------------------
  294. FadeIn()
  295. -----------------------
  296. */
  297. public void FadeIn( float fadeTime ) {
  298. //Log.Print( ">>> FADE IN: " + channel );
  299. audioSource.volume = 0.0f;
  300. state = FadeState.FadingIn;
  301. StopAllCoroutines();
  302. StartCoroutine( FadeSoundChannel( 0.0f, fadeTime, Fade.In, defaultVolume ) );
  303. }
  304. /*
  305. -----------------------
  306. FadeOut()
  307. -----------------------
  308. */
  309. public void FadeOut( float fadeTime ) {
  310. //Log.Print( ">>> FADE OUT: " + channel );
  311. if ( !audioSource.isPlaying ) {
  312. //Log.Print( " ... SKIPPING" );
  313. return;
  314. }
  315. state = FadeState.FadingOut;
  316. StopAllCoroutines();
  317. StartCoroutine( FadeSoundChannel( 0.0f, fadeTime, Fade.Out, audioSource.volume ) );
  318. }
  319. /*
  320. -----------------------
  321. FadeOutDelayed()
  322. -----------------------
  323. */
  324. public void FadeOutDelayed( float delayedSecs, float fadeTime ) {
  325. //Log.Print( ">>> FADE OUT DELAYED: " + channel );
  326. if ( !audioSource.isPlaying ) {
  327. //Log.Print( " ... SKIPPING" );
  328. return;
  329. }
  330. state = FadeState.FadingOut;
  331. StopAllCoroutines();
  332. StartCoroutine( FadeSoundChannel( delayedSecs, fadeTime, Fade.Out, audioSource.volume ) );
  333. }
  334. /*
  335. -----------------------
  336. FadeSoundChannelTo()
  337. -----------------------
  338. */
  339. IEnumerator FadeSoundChannelTo( float fadeTime, float toVolume ) {
  340. float start = audioSource.volume;
  341. float end = toVolume;
  342. float startTime = Time.realtimeSinceStartup;
  343. float elapsedTime = 0.0f;
  344. while ( elapsedTime < fadeTime ) {
  345. elapsedTime = Time.realtimeSinceStartup - startTime;
  346. float t = elapsedTime / fadeTime;
  347. audioSource.volume = Mathf.Lerp( start, end, t );
  348. yield return 0;
  349. }
  350. state = FadeState.Null;
  351. }
  352. /*
  353. -----------------------
  354. FadeSoundChannel()
  355. -----------------------
  356. */
  357. IEnumerator FadeSoundChannel( float delaySecs, float fadeTime, Fade fadeType, float defaultVolume ) {
  358. if ( delaySecs > 0.0f ) {
  359. yield return new WaitForSeconds( delaySecs );
  360. }
  361. float start = ( fadeType == Fade.In ) ? 0.0f : defaultVolume;
  362. float end = ( fadeType == Fade.In ) ? defaultVolume : 0.0f;
  363. bool restartPlay = false;
  364. if ( fadeType == Fade.In ) {
  365. if ( Time.time == 0.0f ) {
  366. restartPlay = true;
  367. }
  368. audioSource.volume = 0.0f;
  369. audioSource.Play();
  370. }
  371. float startTime = Time.realtimeSinceStartup;
  372. float elapsedTime = 0.0f;
  373. while ( elapsedTime < fadeTime ) {
  374. elapsedTime = Time.realtimeSinceStartup - startTime;
  375. float t = elapsedTime / fadeTime;
  376. audioSource.volume = Mathf.Lerp( start, end, t );
  377. yield return 0;
  378. if ( restartPlay && ( Time.time > 0.0f ) ) {
  379. audioSource.Play();
  380. restartPlay = false;
  381. }
  382. if ( !audioSource.isPlaying ) {
  383. break;
  384. }
  385. }
  386. if ( fadeType == Fade.Out ) {
  387. Stop();
  388. }
  389. state = FadeState.Null;
  390. }
  391. }
  392. } // namespace OVR