BaseEffect.cs 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  1. using Ryujinx.Audio.Renderer.Common;
  2. using Ryujinx.Audio.Renderer.Parameter;
  3. using Ryujinx.Audio.Renderer.Server.MemoryPool;
  4. using Ryujinx.Audio.Renderer.Utils;
  5. using System;
  6. using System.Diagnostics;
  7. using static Ryujinx.Audio.Renderer.Common.BehaviourParameter;
  8. using DspAddress = System.UInt64;
  9. namespace Ryujinx.Audio.Renderer.Server.Effect
  10. {
  11. /// <summary>
  12. /// Base class used as a server state for an effect.
  13. /// </summary>
  14. public class BaseEffect
  15. {
  16. /// <summary>
  17. /// The <see cref="EffectType"/> of the effect.
  18. /// </summary>
  19. public EffectType Type;
  20. /// <summary>
  21. /// Set to true if the effect must be active.
  22. /// </summary>
  23. public bool IsEnabled;
  24. /// <summary>
  25. /// Set to true if the internal effect work buffers used wasn't mapped.
  26. /// </summary>
  27. public bool BufferUnmapped;
  28. /// <summary>
  29. /// The current state of the effect.
  30. /// </summary>
  31. public UsageState UsageState;
  32. /// <summary>
  33. /// The target mix id of the effect.
  34. /// </summary>
  35. public int MixId;
  36. /// <summary>
  37. /// Position of the effect while processing effects.
  38. /// </summary>
  39. public uint ProcessingOrder;
  40. /// <summary>
  41. /// Array of all the work buffer used by the effect.
  42. /// </summary>
  43. protected AddressInfo[] WorkBuffers;
  44. /// <summary>
  45. /// Create a new <see cref="BaseEffect"/>.
  46. /// </summary>
  47. public BaseEffect()
  48. {
  49. Type = TargetEffectType;
  50. UsageState = UsageState.Invalid;
  51. IsEnabled = false;
  52. BufferUnmapped = false;
  53. MixId = Constants.UnusedMixId;
  54. ProcessingOrder = uint.MaxValue;
  55. WorkBuffers = new AddressInfo[2];
  56. foreach (ref AddressInfo info in WorkBuffers.AsSpan())
  57. {
  58. info = AddressInfo.Create();
  59. }
  60. }
  61. /// <summary>
  62. /// The target <see cref="EffectType"/> handled by this <see cref="BaseEffect"/>.
  63. /// </summary>
  64. public virtual EffectType TargetEffectType => EffectType.Invalid;
  65. /// <summary>
  66. /// Check if the <see cref="EffectType"/> sent by the user match the internal <see cref="EffectType"/>.
  67. /// </summary>
  68. /// <param name="parameter">The user parameter.</param>
  69. /// <returns>Returns true if the <see cref="EffectType"/> sent by the user matches the internal <see cref="EffectType"/>.</returns>
  70. public bool IsTypeValid<T>(ref T parameter) where T : unmanaged, IEffectInParameter
  71. {
  72. return parameter.Type == TargetEffectType;
  73. }
  74. /// <summary>
  75. /// Update the usage state during command generation.
  76. /// </summary>
  77. protected void UpdateUsageStateForCommandGeneration()
  78. {
  79. UsageState = IsEnabled ? UsageState.Enabled : UsageState.Disabled;
  80. }
  81. /// <summary>
  82. /// Update the internal common parameters from a user parameter.
  83. /// </summary>
  84. /// <param name="parameter">The user parameter.</param>
  85. protected void UpdateParameterBase<T>(ref T parameter) where T : unmanaged, IEffectInParameter
  86. {
  87. MixId = parameter.MixId;
  88. ProcessingOrder = parameter.ProcessingOrder;
  89. }
  90. /// <summary>
  91. /// Force unmap all the work buffers.
  92. /// </summary>
  93. /// <param name="mapper">The mapper to use.</param>
  94. public void ForceUnmapBuffers(PoolMapper mapper)
  95. {
  96. foreach (ref AddressInfo info in WorkBuffers.AsSpan())
  97. {
  98. if (info.GetReference(false) != 0)
  99. {
  100. mapper.ForceUnmap(ref info);
  101. }
  102. }
  103. }
  104. /// <summary>
  105. /// Check if the effect needs to be skipped.
  106. /// </summary>
  107. /// <returns>Returns true if the effect needs to be skipped.</returns>
  108. public bool ShouldSkip()
  109. {
  110. return BufferUnmapped;
  111. }
  112. /// <summary>
  113. /// Update the <see cref="BaseEffect"/> state during command generation.
  114. /// </summary>
  115. public virtual void UpdateForCommandGeneration()
  116. {
  117. Debug.Assert(Type == TargetEffectType);
  118. }
  119. /// <summary>
  120. /// Initialize the given <paramref name="state"/> result state.
  121. /// </summary>
  122. /// <param name="state">The state to initalize</param>
  123. public virtual void InitializeResultState(ref EffectResultState state) { }
  124. /// <summary>
  125. /// Update the <paramref name="destState"/> result state with <paramref name="srcState"/>.
  126. /// </summary>
  127. /// <param name="destState">The destination result state</param>
  128. /// <param name="srcState">The source result state</param>
  129. public virtual void UpdateResultState(ref EffectResultState destState, ref EffectResultState srcState) { }
  130. /// <summary>
  131. /// Update the internal state from a user version 1 parameter.
  132. /// </summary>
  133. /// <param name="updateErrorInfo">The possible <see cref="ErrorInfo"/> that was generated.</param>
  134. /// <param name="parameter">The user parameter.</param>
  135. /// <param name="mapper">The mapper to use.</param>
  136. public virtual void Update(out ErrorInfo updateErrorInfo, ref EffectInParameterVersion1 parameter, PoolMapper mapper)
  137. {
  138. Debug.Assert(IsTypeValid(ref parameter));
  139. updateErrorInfo = new ErrorInfo();
  140. }
  141. /// <summary>
  142. /// Update the internal state from a user version 2 parameter.
  143. /// </summary>
  144. /// <param name="updateErrorInfo">The possible <see cref="ErrorInfo"/> that was generated.</param>
  145. /// <param name="parameter">The user parameter.</param>
  146. /// <param name="mapper">The mapper to use.</param>
  147. public virtual void Update(out ErrorInfo updateErrorInfo, ref EffectInParameterVersion2 parameter, PoolMapper mapper)
  148. {
  149. Debug.Assert(IsTypeValid(ref parameter));
  150. updateErrorInfo = new ErrorInfo();
  151. }
  152. /// <summary>
  153. /// Get the work buffer DSP address at the given index.
  154. /// </summary>
  155. /// <param name="index">The index of the work buffer</param>
  156. /// <returns>The work buffer DSP address at the given index.</returns>
  157. public virtual DspAddress GetWorkBuffer(int index)
  158. {
  159. throw new InvalidOperationException();
  160. }
  161. /// <summary>
  162. /// Get the first work buffer DSP address.
  163. /// </summary>
  164. /// <returns>The first work buffer DSP address.</returns>
  165. protected DspAddress GetSingleBuffer()
  166. {
  167. if (IsEnabled)
  168. {
  169. return WorkBuffers[0].GetReference(true);
  170. }
  171. if (UsageState != UsageState.Disabled)
  172. {
  173. DspAddress address = WorkBuffers[0].GetReference(false);
  174. ulong size = WorkBuffers[0].Size;
  175. if (address != 0 && size != 0)
  176. {
  177. AudioProcessorMemoryManager.InvalidateDataCache(address, size);
  178. }
  179. }
  180. return 0;
  181. }
  182. /// <summary>
  183. /// Store the output status to the given user output.
  184. /// </summary>
  185. /// <param name="outStatus">The given user output.</param>
  186. /// <param name="isAudioRendererActive">If set to true, the <see cref="AudioRenderSystem"/> is active.</param>
  187. public void StoreStatus<T>(ref T outStatus, bool isAudioRendererActive) where T : unmanaged, IEffectOutStatus
  188. {
  189. if (isAudioRendererActive)
  190. {
  191. if (UsageState == UsageState.Disabled)
  192. {
  193. outStatus.State = EffectState.Disabled;
  194. }
  195. else
  196. {
  197. outStatus.State = EffectState.Enabled;
  198. }
  199. }
  200. else if (UsageState == UsageState.New)
  201. {
  202. outStatus.State = EffectState.Enabled;
  203. }
  204. else
  205. {
  206. outStatus.State = EffectState.Disabled;
  207. }
  208. }
  209. /// <summary>
  210. /// Get the <see cref="PerformanceDetailType"/> associated to the <see cref="Type"/> of this effect.
  211. /// </summary>
  212. /// <returns>The <see cref="PerformanceDetailType"/> associated to the <see cref="Type"/> of this effect.</returns>
  213. public PerformanceDetailType GetPerformanceDetailType()
  214. {
  215. switch (Type)
  216. {
  217. case EffectType.BiquadFilter:
  218. return PerformanceDetailType.BiquadFilter;
  219. case EffectType.AuxiliaryBuffer:
  220. return PerformanceDetailType.Aux;
  221. case EffectType.Delay:
  222. return PerformanceDetailType.Delay;
  223. case EffectType.Reverb:
  224. return PerformanceDetailType.Reverb;
  225. case EffectType.Reverb3d:
  226. return PerformanceDetailType.Reverb3d;
  227. case EffectType.BufferMix:
  228. return PerformanceDetailType.Mix;
  229. case EffectType.Limiter:
  230. return PerformanceDetailType.Limiter;
  231. case EffectType.CaptureBuffer:
  232. return PerformanceDetailType.CaptureBuffer;
  233. default:
  234. throw new NotImplementedException($"{Type}");
  235. }
  236. }
  237. }
  238. }