KorolevDmitry123

Untitled

Apr 11th, 2016
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 12.00 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4.  
  5. /// <summary>
  6. /// An audio category represents a set of AudioItems. Categories allow to change the volume of all containing audio items.
  7. /// </summary>
  8. [System.Serializable]
  9. public class AudioCategory
  10. {
  11. /// <summary>
  12. /// The name of category ( = <c>categoryID</c> )
  13. /// </summary>
  14. public string Name;
  15.  
  16. /// <summary>
  17. /// The volume factor applied to all audio items in the category.
  18. /// You change the volume by script and the change will be apply to all
  19. /// playing audios immediately.
  20. /// </summary>
  21. public float Volume
  22. {
  23. get { return _volume; }
  24. set { _volume = value; _ApplyVolumeChange(); }
  25. }
  26.  
  27. /// <summary>
  28. /// Allows to define a specific audio object prefab for this category. If none is defined,
  29. /// the default prefab as set by <see cref="AudioController.AudioObjectPrefab"/> is taken.
  30. /// </summary>
  31. /// <remarks> This way you can e.g. use special effects such as the reverb filter for
  32. /// a specific category. Just add the respective filter component to the specified prefab.</remarks>
  33. public GameObject AudioObjectPrefab;
  34.  
  35. /// <summary>
  36. /// Define your AudioItems using Unity inspector.
  37. /// </summary>
  38. public AudioItem[ ] AudioItems;
  39.  
  40. [SerializeField]
  41. private float _volume = 1.0f;
  42.  
  43. internal void _AnalyseAudioItems( Dictionary<string, AudioItem> audioItemsDict )
  44. {
  45. if ( AudioItems == null ) return;
  46.  
  47. foreach ( AudioItem ai in AudioItems )
  48. {
  49. if ( ai != null )
  50. {
  51. ai._Initialize( this );
  52. #if AUDIO_TOOLKIT_DEMO
  53. int? demoMaxNumAudioItemsConst = 0x12345B;
  54.  
  55. int? demoMaxNumAudioItems = (demoMaxNumAudioItemsConst & 0xf);
  56. demoMaxNumAudioItems++;
  57.  
  58. if ( audioItemsDict.Count > demoMaxNumAudioItems )
  59. {
  60. Debug.LogError( "Audio Toolkit: The demo version does not allow more than " + demoMaxNumAudioItems + " audio items." );
  61. Debug.LogWarning( "Please buy the full version of Audio Toolkit!" );
  62. return;
  63. }
  64. #endif
  65.  
  66. //Debug.Log( string.Format( "SubItem {0}: {1} {2} {3}", fi.Name, ai.FixedOrder, ai.RandomOrderStart, ai._lastChosen ) );
  67.  
  68. if ( audioItemsDict != null )
  69. {
  70. try
  71. {
  72. audioItemsDict.Add( ai.Name, ai );
  73. }
  74. catch ( ArgumentException )
  75. {
  76. Debug.LogWarning( "Multiple audio items with name '" + ai.Name + "'");
  77. }
  78. }
  79. }
  80.  
  81. }
  82. }
  83.  
  84. private void _ApplyVolumeChange()
  85. {
  86. // TODO: change Volume into a property and call ApplyVolumeChange automatically (requires editor inspector adaption!)
  87.  
  88. AudioObject[ ] objs = AudioController.GetPlayingAudioObjects();
  89.  
  90. foreach ( AudioObject o in objs )
  91. {
  92. if ( o.category == this )
  93. {
  94. //if ( o.IsPlaying() )
  95. {
  96. o._ApplyVolume();
  97. }
  98. }
  99. }
  100. }
  101. }
  102.  
  103. /// <summary>
  104. /// Used by <see cref="AudioItem"/> to determine which <see cref="AudioSubItem"/> is chosen.
  105. /// </summary>
  106. public enum AudioPickSubItemMode
  107. {
  108. /// <summary>disables playback</summary>
  109. Disabled,
  110.  
  111. /// <summary>chooses a random subitem with a probability in proportion to <see cref="AudioSubItem.Probability"/> </summary>
  112. Random,
  113.  
  114. /// <summary>chooses a random subitem with a probability in proportion to <see cref="AudioSubItem.Probability"/> and makes sure it is not played twice in a row (if possible)</summary>
  115. RandomNotSameTwice,
  116.  
  117. /// <summary> chooses the subitems in a sequence one after the other starting with the first</summary>
  118. Sequence,
  119.  
  120. /// <summary> chooses the subitems in a sequence one after the other starting with a random subitem</summary>
  121. SequenceWithRandomStart,
  122.  
  123. /// <summary> chooses all subitems at the same time</summary>
  124. AllSimultaneously,
  125.  
  126. /// <summary> chooses two different subitems at the same time (if possible)</summary>
  127. TwoSimultaneously,
  128. }
  129.  
  130. /// <summary>
  131. /// The AudioItem class represents a uniquely named audio entity that can be played by scripts.
  132. /// </summary>
  133. /// <remarks>
  134. /// AudioItem objects are defined in an AudioCategory using the Unity inspector.
  135. /// </remarks>
  136. [System.Serializable]
  137. public class AudioItem
  138. {
  139. /// <summary>
  140. /// The unique name of the audio item ( = audioID )
  141. /// </summary>
  142. public string Name;
  143.  
  144. /// <summary>
  145. /// If enabled the audio item will get looped when played.
  146. /// </summary>
  147. public bool Loop = false;
  148.  
  149. /// <summary>
  150. /// If disabled, the audio will keep on playing if a new scene is loaded.
  151. /// </summary>
  152. public bool DestroyOnLoad = true;
  153.  
  154. /// <summary>
  155. /// The volume applied to all audio sub-items of this audio item.
  156. /// </summary>
  157. public float Volume = 1;
  158.  
  159. /// <summary>
  160. /// Determines which <see cref="AudioSubItem"/> is chosen when playing an <see cref="AudioItem"/>
  161. /// </summary>
  162. public AudioPickSubItemMode SubItemPickMode = AudioPickSubItemMode.RandomNotSameTwice;
  163.  
  164. /// <summary>
  165. /// Assures that the same audio item will not be played multiple times within this time frame. This is useful if several events triggered at almost the same time want to play the same audio item which can cause unwanted noise artifacts.
  166. /// </summary>
  167. public float MinTimeBetweenPlayCalls = 0.1f;
  168.  
  169. /// <summary>
  170. /// Assures that the same audio item will not be played more than <c>MaxInstanceCount</c> times simultaneously.
  171. /// </summary>
  172. /// <remarks>Set to 0 to disable.</remarks>
  173. public int MaxInstanceCount = 0;
  174.  
  175. /// <summary>
  176. /// Defers the playback of the audio item for <c>Delay</c> seconds.
  177. /// </summary>
  178. public float Delay = 0;
  179.  
  180.  
  181. /// <summary>
  182. /// Define your audio sub-items using the Unity inspector.
  183. /// </summary>
  184. public AudioSubItem[] subItems;
  185.  
  186. internal int _lastChosen = -1;
  187. internal double _lastPlayedTime = -1; // high precision system time
  188.  
  189. /// <summary>
  190. /// the <c>AudioCategroy</c> the audio item belongs to.
  191. /// </summary>
  192. public AudioCategory category
  193. {
  194. private set;
  195. get;
  196. }
  197.  
  198. void Awake()
  199. {
  200. _lastChosen = -1;
  201. }
  202.  
  203. /// <summary>
  204. /// Initializes the audio item for a certain category. (Internal use only, not required to call).
  205. /// </summary>
  206. internal void _Initialize( AudioCategory categ )
  207. {
  208. category = categ;
  209.  
  210. _NormalizeSubItems();
  211. }
  212.  
  213. private void _NormalizeSubItems()
  214. {
  215. float sum = 0.0f;
  216.  
  217. int subItemID = 0;
  218.  
  219. foreach ( AudioSubItem i in subItems )
  220. {
  221. i.item = this;
  222. if ( _IsValidSubItem( i ) )
  223. {
  224. sum += i.Probability;
  225. }
  226. i._subItemID = subItemID;
  227. subItemID++;
  228. }
  229.  
  230. if ( sum <= 0 )
  231. {
  232. return;
  233. }
  234.  
  235. // Compute normalized probabilities
  236.  
  237. float summedProb = 0;
  238.  
  239. foreach ( AudioSubItem i in subItems )
  240. {
  241. if ( _IsValidSubItem( i ) )
  242. {
  243. summedProb += i.Probability / sum;
  244.  
  245. i._SummedProbability = summedProb;
  246. }
  247. }
  248. }
  249.  
  250. private static bool _IsValidSubItem( AudioSubItem item )
  251. {
  252. switch ( item.SubItemType )
  253. {
  254. case AudioSubItemType.Clip:
  255. return item.Clip != null;
  256. case AudioSubItemType.Item:
  257. return item.ItemModeAudioID != null && item.ItemModeAudioID.Length > 0;
  258. }
  259. return false;
  260. }
  261. }
  262.  
  263. /// <summary>
  264. /// The type of an <see cref="AudioSubItem"/>
  265. /// </summary>
  266. public enum AudioSubItemType
  267. {
  268. /// <summary>The <see cref="AudioSubItem"/> plays an <see cref="UnityEngine.AudioClip"/></summary>
  269. Clip,
  270. /// <summary>The <see cref="AudioSubItem"/> plays an <see cref="AudioItem"/></summary>
  271. Item,
  272. }
  273.  
  274. /// <summary>
  275. /// An AudioSubItem represents a specific Unity audio clip.
  276. /// </summary>
  277. /// <remarks>
  278. /// Add your AudioSubItem to an AudioItem using the Unity inspector.
  279. /// </remarks>
  280. [System.Serializable]
  281. public class AudioSubItem
  282. {
  283. /// <summary>
  284. /// Specifies the type of this <see cref="AudioSubItem"/>
  285. /// </summary>
  286. public AudioSubItemType SubItemType = AudioSubItemType.Clip;
  287.  
  288. /// <summary>
  289. /// If multiple sub-items are defined within an audio item, the specific audio clip is chosen with a probability in proportion to the <c>Probability</c> value.
  290. /// </summary>
  291. public float Probability = 1.0f;
  292.  
  293. /// <summary>
  294. /// Specifies the <c>audioID</c> to be played in case of the <see cref="AudioSubItemType.Item"/> mode
  295. /// </summary>
  296. public string ItemModeAudioID;
  297.  
  298. /// <summary>
  299. /// Specifies the <see cref="UnityEngine.AudioClip"/> to be played in case of the <see cref="AudioSubItemType.Item"/> mode.
  300. /// </summary>
  301. public AudioClip Clip;
  302.  
  303. /// <summary>
  304. /// The volume applied to the audio sub-item.
  305. /// </summary>
  306. public float Volume = 1.0f;
  307.  
  308. /// <summary>
  309. /// Alters the pitch in units of semitones ( thus 12 = twice the speed)
  310. /// </summary>
  311. public float PitchShift = 0f;
  312.  
  313. /// <summary>
  314. /// Alters the pan: -1..left, +1..right
  315. /// </summary>
  316. public float Pan2D = 0;
  317.  
  318. /// <summary>
  319. /// Defers the playback of the audio sub-item for <c>Delay</c> seconds.
  320. /// </summary>
  321. public float Delay = 0;
  322.  
  323. /// <summary>
  324. /// Randomly shifts the pitch in units of semitones ( thus 12 = twice the speed)
  325. /// </summary>
  326. public float RandomPitch = 0;
  327.  
  328. /// <summary>
  329. /// Randomly shifts the volume +/- this value
  330. /// </summary>
  331. public float RandomVolume = 0;
  332.  
  333. /// <summary>
  334. /// Randomly adds a delay between 0 and RandomDelay
  335. /// </summary>
  336. public float RandomDelay = 0;
  337.  
  338. /// <summary>
  339. /// Ends playing the audio at this time (in seconds).
  340. /// </summary>
  341. /// <remarks>
  342. /// Can be used as a workaround for an unknown clip length (e.g. for tracker files)
  343. /// </remarks>
  344. public float ClipStopTime = 0;
  345.  
  346. /// <summary>
  347. /// Offsets the the audio clip start time (in seconds).
  348. /// </summary>
  349. /// <remarks>
  350. /// Does not work with looping.
  351. /// </remarks>
  352. public float ClipStartTime = 0;
  353.  
  354. /// <summary>
  355. /// Automatic fade-in in seconds
  356. /// </summary>
  357. public float FadeIn = 0;
  358.  
  359. /// <summary>
  360. /// Automatic fade-out in seconds
  361. /// </summary>
  362. public float FadeOut = 0;
  363.  
  364. /// <summary>
  365. /// Starts playing at a random position.
  366. /// </summary>
  367. /// <remarks>
  368. /// Useful for audio loops.
  369. /// </remarks>
  370. public bool RandomStartPosition = false;
  371.  
  372. private float _summedProbability = -1.0f; // -1 means not initialized or invalid
  373. internal int _subItemID = 0;
  374.  
  375. internal float _SummedProbability
  376. {
  377. get { return _summedProbability; }
  378. set { _summedProbability = value; }
  379. }
  380.  
  381. /// <summary>
  382. /// the <c>AudioItem</c> the sub-item belongs to.
  383. /// </summary>
  384. public AudioItem item
  385. {
  386. internal set;
  387. get;
  388. }
  389.  
  390. /// <summary>
  391. /// Returns the name of the audio clip for debugging.
  392. /// </summary>
  393. /// <returns>
  394. /// The debug output string.
  395. /// </returns>
  396. public override string ToString()
  397. {
  398. if ( SubItemType == AudioSubItemType.Clip )
  399. {
  400. return "CLIP: " + Clip.name;
  401. }
  402. else
  403. return "ITEM: " + ItemModeAudioID;
  404. }
  405.  
  406. }
Add Comment
Please, Sign In to add comment