SoundManager.SoundAgent.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421
  1. //------------------------------------------------------------
  2. // Game Framework
  3. // Copyright © 2013-2021 loyalsoft. All rights reserved.
  4. // Homepage: http://www.game7000.com/
  5. // Feedback: http://www.game7000.com/
  6. //------------------------------------------------------------
  7. using System;
  8. namespace GameFramework.Sound
  9. {
  10. internal sealed partial class SoundManager : GameFrameworkModule, ISoundManager
  11. {
  12. /// <summary>
  13. /// 声音代理。
  14. /// </summary>
  15. private sealed class SoundAgent : ISoundAgent
  16. {
  17. private readonly SoundGroup m_SoundGroup;
  18. private readonly ISoundHelper m_SoundHelper;
  19. private readonly ISoundAgentHelper m_SoundAgentHelper;
  20. private int m_SerialId;
  21. private object m_SoundAsset;
  22. private DateTime m_SetSoundAssetTime;
  23. private bool m_MuteInSoundGroup;
  24. private float m_VolumeInSoundGroup;
  25. /// <summary>
  26. /// 初始化声音代理的新实例。
  27. /// </summary>
  28. /// <param name="soundGroup">所在的声音组。</param>
  29. /// <param name="soundHelper">声音辅助器接口。</param>
  30. /// <param name="soundAgentHelper">声音代理辅助器接口。</param>
  31. public SoundAgent(SoundGroup soundGroup, ISoundHelper soundHelper, ISoundAgentHelper soundAgentHelper)
  32. {
  33. if (soundGroup == null)
  34. {
  35. throw new GameFrameworkException("Sound group is invalid.");
  36. }
  37. if (soundHelper == null)
  38. {
  39. throw new GameFrameworkException("Sound helper is invalid.");
  40. }
  41. if (soundAgentHelper == null)
  42. {
  43. throw new GameFrameworkException("Sound agent helper is invalid.");
  44. }
  45. m_SoundGroup = soundGroup;
  46. m_SoundHelper = soundHelper;
  47. m_SoundAgentHelper = soundAgentHelper;
  48. m_SoundAgentHelper.ResetSoundAgent += OnResetSoundAgent;
  49. m_SerialId = 0;
  50. m_SoundAsset = null;
  51. Reset();
  52. }
  53. /// <summary>
  54. /// 获取所在的声音组。
  55. /// </summary>
  56. public ISoundGroup SoundGroup
  57. {
  58. get
  59. {
  60. return m_SoundGroup;
  61. }
  62. }
  63. /// <summary>
  64. /// 获取或设置声音的序列编号。
  65. /// </summary>
  66. public int SerialId
  67. {
  68. get
  69. {
  70. return m_SerialId;
  71. }
  72. set
  73. {
  74. m_SerialId = value;
  75. }
  76. }
  77. /// <summary>
  78. /// 获取当前是否正在播放。
  79. /// </summary>
  80. public bool IsPlaying
  81. {
  82. get
  83. {
  84. return m_SoundAgentHelper.IsPlaying;
  85. }
  86. }
  87. /// <summary>
  88. /// 获取声音长度。
  89. /// </summary>
  90. public float Length
  91. {
  92. get
  93. {
  94. return m_SoundAgentHelper.Length;
  95. }
  96. }
  97. /// <summary>
  98. /// 获取或设置播放位置。
  99. /// </summary>
  100. public float Time
  101. {
  102. get
  103. {
  104. return m_SoundAgentHelper.Time;
  105. }
  106. set
  107. {
  108. m_SoundAgentHelper.Time = value;
  109. }
  110. }
  111. /// <summary>
  112. /// 获取是否静音。
  113. /// </summary>
  114. public bool Mute
  115. {
  116. get
  117. {
  118. return m_SoundAgentHelper.Mute;
  119. }
  120. }
  121. /// <summary>
  122. /// 获取或设置在声音组内是否静音。
  123. /// </summary>
  124. public bool MuteInSoundGroup
  125. {
  126. get
  127. {
  128. return m_MuteInSoundGroup;
  129. }
  130. set
  131. {
  132. m_MuteInSoundGroup = value;
  133. RefreshMute();
  134. }
  135. }
  136. /// <summary>
  137. /// 获取或设置是否循环播放。
  138. /// </summary>
  139. public bool Loop
  140. {
  141. get
  142. {
  143. return m_SoundAgentHelper.Loop;
  144. }
  145. set
  146. {
  147. m_SoundAgentHelper.Loop = value;
  148. }
  149. }
  150. /// <summary>
  151. /// 获取或设置声音优先级。
  152. /// </summary>
  153. public int Priority
  154. {
  155. get
  156. {
  157. return m_SoundAgentHelper.Priority;
  158. }
  159. set
  160. {
  161. m_SoundAgentHelper.Priority = value;
  162. }
  163. }
  164. /// <summary>
  165. /// 获取音量大小。
  166. /// </summary>
  167. public float Volume
  168. {
  169. get
  170. {
  171. return m_SoundAgentHelper.Volume;
  172. }
  173. }
  174. /// <summary>
  175. /// 获取或设置在声音组内音量大小。
  176. /// </summary>
  177. public float VolumeInSoundGroup
  178. {
  179. get
  180. {
  181. return m_VolumeInSoundGroup;
  182. }
  183. set
  184. {
  185. m_VolumeInSoundGroup = value;
  186. RefreshVolume();
  187. }
  188. }
  189. /// <summary>
  190. /// 获取或设置声音音调。
  191. /// </summary>
  192. public float Pitch
  193. {
  194. get
  195. {
  196. return m_SoundAgentHelper.Pitch;
  197. }
  198. set
  199. {
  200. m_SoundAgentHelper.Pitch = value;
  201. }
  202. }
  203. /// <summary>
  204. /// 获取或设置声音立体声声相。
  205. /// </summary>
  206. public float PanStereo
  207. {
  208. get
  209. {
  210. return m_SoundAgentHelper.PanStereo;
  211. }
  212. set
  213. {
  214. m_SoundAgentHelper.PanStereo = value;
  215. }
  216. }
  217. /// <summary>
  218. /// 获取或设置声音空间混合量。
  219. /// </summary>
  220. public float SpatialBlend
  221. {
  222. get
  223. {
  224. return m_SoundAgentHelper.SpatialBlend;
  225. }
  226. set
  227. {
  228. m_SoundAgentHelper.SpatialBlend = value;
  229. }
  230. }
  231. /// <summary>
  232. /// 获取或设置声音最大距离。
  233. /// </summary>
  234. public float MaxDistance
  235. {
  236. get
  237. {
  238. return m_SoundAgentHelper.MaxDistance;
  239. }
  240. set
  241. {
  242. m_SoundAgentHelper.MaxDistance = value;
  243. }
  244. }
  245. /// <summary>
  246. /// 获取或设置声音多普勒等级。
  247. /// </summary>
  248. public float DopplerLevel
  249. {
  250. get
  251. {
  252. return m_SoundAgentHelper.DopplerLevel;
  253. }
  254. set
  255. {
  256. m_SoundAgentHelper.DopplerLevel = value;
  257. }
  258. }
  259. /// <summary>
  260. /// 获取声音代理辅助器。
  261. /// </summary>
  262. public ISoundAgentHelper Helper
  263. {
  264. get
  265. {
  266. return m_SoundAgentHelper;
  267. }
  268. }
  269. /// <summary>
  270. /// 获取声音创建时间。
  271. /// </summary>
  272. internal DateTime SetSoundAssetTime
  273. {
  274. get
  275. {
  276. return m_SetSoundAssetTime;
  277. }
  278. }
  279. /// <summary>
  280. /// 播放声音。
  281. /// </summary>
  282. public void Play()
  283. {
  284. m_SoundAgentHelper.Play(Constant.DefaultFadeInSeconds);
  285. }
  286. /// <summary>
  287. /// 播放声音。
  288. /// </summary>
  289. /// <param name="fadeInSeconds">声音淡入时间,以秒为单位。</param>
  290. public void Play(float fadeInSeconds)
  291. {
  292. m_SoundAgentHelper.Play(fadeInSeconds);
  293. }
  294. /// <summary>
  295. /// 停止播放声音。
  296. /// </summary>
  297. public void Stop()
  298. {
  299. m_SoundAgentHelper.Stop(Constant.DefaultFadeOutSeconds);
  300. }
  301. /// <summary>
  302. /// 停止播放声音。
  303. /// </summary>
  304. /// <param name="fadeOutSeconds">声音淡出时间,以秒为单位。</param>
  305. public void Stop(float fadeOutSeconds)
  306. {
  307. m_SoundAgentHelper.Stop(fadeOutSeconds);
  308. }
  309. /// <summary>
  310. /// 暂停播放声音。
  311. /// </summary>
  312. public void Pause()
  313. {
  314. m_SoundAgentHelper.Pause(Constant.DefaultFadeOutSeconds);
  315. }
  316. /// <summary>
  317. /// 暂停播放声音。
  318. /// </summary>
  319. /// <param name="fadeOutSeconds">声音淡出时间,以秒为单位。</param>
  320. public void Pause(float fadeOutSeconds)
  321. {
  322. m_SoundAgentHelper.Pause(fadeOutSeconds);
  323. }
  324. /// <summary>
  325. /// 恢复播放声音。
  326. /// </summary>
  327. public void Resume()
  328. {
  329. m_SoundAgentHelper.Resume(Constant.DefaultFadeInSeconds);
  330. }
  331. /// <summary>
  332. /// 恢复播放声音。
  333. /// </summary>
  334. /// <param name="fadeInSeconds">声音淡入时间,以秒为单位。</param>
  335. public void Resume(float fadeInSeconds)
  336. {
  337. m_SoundAgentHelper.Resume(fadeInSeconds);
  338. }
  339. /// <summary>
  340. /// 重置声音代理。
  341. /// </summary>
  342. public void Reset()
  343. {
  344. if (m_SoundAsset != null)
  345. {
  346. m_SoundHelper.ReleaseSoundAsset(m_SoundAsset);
  347. m_SoundAsset = null;
  348. }
  349. m_SetSoundAssetTime = DateTime.MinValue;
  350. Time = Constant.DefaultTime;
  351. MuteInSoundGroup = Constant.DefaultMute;
  352. Loop = Constant.DefaultLoop;
  353. Priority = Constant.DefaultPriority;
  354. VolumeInSoundGroup = Constant.DefaultVolume;
  355. Pitch = Constant.DefaultPitch;
  356. PanStereo = Constant.DefaultPanStereo;
  357. SpatialBlend = Constant.DefaultSpatialBlend;
  358. MaxDistance = Constant.DefaultMaxDistance;
  359. DopplerLevel = Constant.DefaultDopplerLevel;
  360. m_SoundAgentHelper.Reset();
  361. }
  362. internal bool SetSoundAsset(object soundAsset)
  363. {
  364. Reset();
  365. m_SoundAsset = soundAsset;
  366. m_SetSoundAssetTime = DateTime.UtcNow;
  367. return m_SoundAgentHelper.SetSoundAsset(soundAsset);
  368. }
  369. internal void RefreshMute()
  370. {
  371. m_SoundAgentHelper.Mute = m_SoundGroup.Mute || m_MuteInSoundGroup;
  372. }
  373. internal void RefreshVolume()
  374. {
  375. m_SoundAgentHelper.Volume = m_SoundGroup.Volume * m_VolumeInSoundGroup;
  376. }
  377. private void OnResetSoundAgent(object sender, ResetSoundAgentEventArgs e)
  378. {
  379. Reset();
  380. }
  381. }
  382. }
  383. }