AudioRenderSystem.cs 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860
  1. //
  2. // Copyright (c) 2019-2021 Ryujinx
  3. //
  4. // This program is free software: you can redistribute it and/or modify
  5. // it under the terms of the GNU Lesser General Public License as published by
  6. // the Free Software Foundation, either version 3 of the License, or
  7. // (at your option) any later version.
  8. //
  9. // This program is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU Lesser General Public License for more details.
  13. //
  14. // You should have received a copy of the GNU Lesser General Public License
  15. // along with this program. If not, see <https://www.gnu.org/licenses/>.
  16. //
  17. using Ryujinx.Audio.Integration;
  18. using Ryujinx.Audio.Renderer.Common;
  19. using Ryujinx.Audio.Renderer.Dsp.Command;
  20. using Ryujinx.Audio.Renderer.Parameter;
  21. using Ryujinx.Audio.Renderer.Server.Effect;
  22. using Ryujinx.Audio.Renderer.Server.MemoryPool;
  23. using Ryujinx.Audio.Renderer.Server.Mix;
  24. using Ryujinx.Audio.Renderer.Server.Performance;
  25. using Ryujinx.Audio.Renderer.Server.Sink;
  26. using Ryujinx.Audio.Renderer.Server.Splitter;
  27. using Ryujinx.Audio.Renderer.Server.Types;
  28. using Ryujinx.Audio.Renderer.Server.Upsampler;
  29. using Ryujinx.Audio.Renderer.Server.Voice;
  30. using Ryujinx.Audio.Renderer.Utils;
  31. using Ryujinx.Common;
  32. using Ryujinx.Common.Logging;
  33. using Ryujinx.Memory;
  34. using System;
  35. using System.Buffers;
  36. using System.Diagnostics;
  37. using System.Threading;
  38. using CpuAddress = System.UInt64;
  39. namespace Ryujinx.Audio.Renderer.Server
  40. {
  41. public class AudioRenderSystem : IDisposable
  42. {
  43. private object _lock = new object();
  44. private AudioRendererExecutionMode _executionMode;
  45. private IWritableEvent _systemEvent;
  46. private ManualResetEvent _terminationEvent;
  47. private MemoryPoolState _dspMemoryPoolState;
  48. private VoiceContext _voiceContext;
  49. private MixContext _mixContext;
  50. private SinkContext _sinkContext;
  51. private SplitterContext _splitterContext;
  52. private EffectContext _effectContext;
  53. private PerformanceManager _performanceManager;
  54. private UpsamplerManager _upsamplerManager;
  55. private bool _isActive;
  56. private BehaviourContext _behaviourContext;
  57. private ulong _totalElapsedTicksUpdating;
  58. private ulong _totalElapsedTicks;
  59. private int _sessionId;
  60. private Memory<MemoryPoolState> _memoryPools;
  61. private uint _sampleRate;
  62. private uint _sampleCount;
  63. private uint _mixBufferCount;
  64. private uint _voiceChannelCountMax;
  65. private uint _upsamplerCount;
  66. private uint _memoryPoolCount;
  67. private uint _processHandle;
  68. private ulong _appletResourceId;
  69. private WritableRegion _workBufferRegion;
  70. private MemoryHandle _workBufferMemoryPin;
  71. private Memory<float> _mixBuffer;
  72. private Memory<float> _depopBuffer;
  73. private uint _renderingTimeLimitPercent;
  74. private bool _voiceDropEnabled;
  75. private uint _voiceDropCount;
  76. private bool _isDspRunningBehind;
  77. private ICommandProcessingTimeEstimator _commandProcessingTimeEstimator;
  78. private Memory<byte> _performanceBuffer;
  79. public IVirtualMemoryManager MemoryManager { get; private set; }
  80. private ulong _elapsedFrameCount;
  81. private ulong _renderingStartTick;
  82. private AudioRendererManager _manager;
  83. private int _disposeState;
  84. public AudioRenderSystem(AudioRendererManager manager, IWritableEvent systemEvent)
  85. {
  86. _manager = manager;
  87. _terminationEvent = new ManualResetEvent(false);
  88. _dspMemoryPoolState = MemoryPoolState.Create(MemoryPoolState.LocationType.Dsp);
  89. _voiceContext = new VoiceContext();
  90. _mixContext = new MixContext();
  91. _sinkContext = new SinkContext();
  92. _splitterContext = new SplitterContext();
  93. _effectContext = new EffectContext();
  94. _commandProcessingTimeEstimator = null;
  95. _systemEvent = systemEvent;
  96. _behaviourContext = new BehaviourContext();
  97. _totalElapsedTicksUpdating = 0;
  98. _sessionId = 0;
  99. }
  100. public ResultCode Initialize(ref AudioRendererConfiguration parameter, uint processHandle, CpuAddress workBuffer, ulong workBufferSize, int sessionId, ulong appletResourceId, IVirtualMemoryManager memoryManager)
  101. {
  102. if (!BehaviourContext.CheckValidRevision(parameter.Revision))
  103. {
  104. return ResultCode.OperationFailed;
  105. }
  106. if (GetWorkBufferSize(ref parameter) > workBufferSize)
  107. {
  108. return ResultCode.WorkBufferTooSmall;
  109. }
  110. Debug.Assert(parameter.RenderingDevice == AudioRendererRenderingDevice.Dsp && parameter.ExecutionMode == AudioRendererExecutionMode.Auto);
  111. Logger.Info?.Print(LogClass.AudioRenderer, $"Initializing with REV{BehaviourContext.GetRevisionNumber(parameter.Revision)}");
  112. _behaviourContext.SetUserRevision(parameter.Revision);
  113. _sampleRate = parameter.SampleRate;
  114. _sampleCount = parameter.SampleCount;
  115. _mixBufferCount = parameter.MixBufferCount;
  116. _voiceChannelCountMax = Constants.VoiceChannelCountMax;
  117. _upsamplerCount = parameter.SinkCount + parameter.SubMixBufferCount;
  118. _appletResourceId = appletResourceId;
  119. _memoryPoolCount = parameter.EffectCount + parameter.VoiceCount * Constants.VoiceWaveBufferCount;
  120. _executionMode = parameter.ExecutionMode;
  121. _sessionId = sessionId;
  122. MemoryManager = memoryManager;
  123. if (memoryManager is IRefCounted rc)
  124. {
  125. rc.IncrementReferenceCount();
  126. }
  127. WorkBufferAllocator workBufferAllocator;
  128. _workBufferRegion = MemoryManager.GetWritableRegion(workBuffer, (int)workBufferSize);
  129. _workBufferRegion.Memory.Span.Fill(0);
  130. _workBufferMemoryPin = _workBufferRegion.Memory.Pin();
  131. workBufferAllocator = new WorkBufferAllocator(_workBufferRegion.Memory);
  132. PoolMapper poolMapper = new PoolMapper(processHandle, false);
  133. poolMapper.InitializeSystemPool(ref _dspMemoryPoolState, workBuffer, workBufferSize);
  134. _mixBuffer = workBufferAllocator.Allocate<float>(_sampleCount * (_voiceChannelCountMax + _mixBufferCount), 0x10);
  135. if (_mixBuffer.IsEmpty)
  136. {
  137. return ResultCode.WorkBufferTooSmall;
  138. }
  139. Memory<float> upSamplerWorkBuffer = workBufferAllocator.Allocate<float>(Constants.TargetSampleCount * (_voiceChannelCountMax + _mixBufferCount) * _upsamplerCount, 0x10);
  140. if (upSamplerWorkBuffer.IsEmpty)
  141. {
  142. return ResultCode.WorkBufferTooSmall;
  143. }
  144. _depopBuffer = workBufferAllocator.Allocate<float>((ulong)BitUtils.AlignUp(parameter.MixBufferCount, Constants.BufferAlignment), Constants.BufferAlignment);
  145. if (_depopBuffer.IsEmpty)
  146. {
  147. return ResultCode.WorkBufferTooSmall;
  148. }
  149. // Invalidate DSP cache on what was currently allocated with workBuffer.
  150. AudioProcessorMemoryManager.InvalidateDspCache(_dspMemoryPoolState.Translate(workBuffer, workBufferAllocator.Offset), workBufferAllocator.Offset);
  151. Debug.Assert((workBufferAllocator.Offset % Constants.BufferAlignment) == 0);
  152. Memory<VoiceState> voices = workBufferAllocator.Allocate<VoiceState>(parameter.VoiceCount, VoiceState.Alignment);
  153. if (voices.IsEmpty)
  154. {
  155. return ResultCode.WorkBufferTooSmall;
  156. }
  157. foreach (ref VoiceState voice in voices.Span)
  158. {
  159. voice.Initialize();
  160. }
  161. // A pain to handle as we can't have VoiceState*, use indices to be a bit more safe
  162. Memory<int> sortedVoices = workBufferAllocator.Allocate<int>(parameter.VoiceCount, 0x10);
  163. if (sortedVoices.IsEmpty)
  164. {
  165. return ResultCode.WorkBufferTooSmall;
  166. }
  167. // Clear memory (use -1 as it's an invalid index)
  168. sortedVoices.Span.Fill(-1);
  169. Memory<VoiceChannelResource> voiceChannelResources = workBufferAllocator.Allocate<VoiceChannelResource>(parameter.VoiceCount, VoiceChannelResource.Alignment);
  170. if (voiceChannelResources.IsEmpty)
  171. {
  172. return ResultCode.WorkBufferTooSmall;
  173. }
  174. for (uint id = 0; id < voiceChannelResources.Length; id++)
  175. {
  176. ref VoiceChannelResource voiceChannelResource = ref voiceChannelResources.Span[(int)id];
  177. voiceChannelResource.Id = id;
  178. voiceChannelResource.IsUsed = false;
  179. }
  180. Memory<VoiceUpdateState> voiceUpdateStates = workBufferAllocator.Allocate<VoiceUpdateState>(parameter.VoiceCount, VoiceUpdateState.Align);
  181. if (voiceUpdateStates.IsEmpty)
  182. {
  183. return ResultCode.WorkBufferTooSmall;
  184. }
  185. uint mixesCount = parameter.SubMixBufferCount + 1;
  186. Memory<MixState> mixes = workBufferAllocator.Allocate<MixState>(mixesCount, MixState.Alignment);
  187. if (mixes.IsEmpty)
  188. {
  189. return ResultCode.WorkBufferTooSmall;
  190. }
  191. if (parameter.EffectCount == 0)
  192. {
  193. foreach (ref MixState mix in mixes.Span)
  194. {
  195. mix = new MixState(Memory<int>.Empty, ref _behaviourContext);
  196. }
  197. }
  198. else
  199. {
  200. Memory<int> effectProcessingOrderArray = workBufferAllocator.Allocate<int>(parameter.EffectCount * mixesCount, 0x10);
  201. foreach (ref MixState mix in mixes.Span)
  202. {
  203. mix = new MixState(effectProcessingOrderArray.Slice(0, (int)parameter.EffectCount), ref _behaviourContext);
  204. effectProcessingOrderArray = effectProcessingOrderArray.Slice((int)parameter.EffectCount);
  205. }
  206. }
  207. // Initialize the final mix id
  208. mixes.Span[0].MixId = Constants.FinalMixId;
  209. Memory<int> sortedMixesState = workBufferAllocator.Allocate<int>(mixesCount, 0x10);
  210. if (sortedMixesState.IsEmpty)
  211. {
  212. return ResultCode.WorkBufferTooSmall;
  213. }
  214. // Clear memory (use -1 as it's an invalid index)
  215. sortedMixesState.Span.Fill(-1);
  216. Memory<byte> nodeStatesWorkBuffer = Memory<byte>.Empty;
  217. Memory<byte> edgeMatrixWorkBuffer = Memory<byte>.Empty;
  218. if (_behaviourContext.IsSplitterSupported())
  219. {
  220. nodeStatesWorkBuffer = workBufferAllocator.Allocate((uint)NodeStates.GetWorkBufferSize((int)mixesCount), 1);
  221. edgeMatrixWorkBuffer = workBufferAllocator.Allocate((uint)EdgeMatrix.GetWorkBufferSize((int)mixesCount), 1);
  222. if (nodeStatesWorkBuffer.IsEmpty || edgeMatrixWorkBuffer.IsEmpty)
  223. {
  224. return ResultCode.WorkBufferTooSmall;
  225. }
  226. }
  227. _mixContext.Initialize(sortedMixesState, mixes, nodeStatesWorkBuffer, edgeMatrixWorkBuffer);
  228. _memoryPools = workBufferAllocator.Allocate<MemoryPoolState>(_memoryPoolCount, MemoryPoolState.Alignment);
  229. if (_memoryPools.IsEmpty)
  230. {
  231. return ResultCode.WorkBufferTooSmall;
  232. }
  233. foreach (ref MemoryPoolState state in _memoryPools.Span)
  234. {
  235. state = MemoryPoolState.Create(MemoryPoolState.LocationType.Cpu);
  236. }
  237. if (!_splitterContext.Initialize(ref _behaviourContext, ref parameter, workBufferAllocator))
  238. {
  239. return ResultCode.WorkBufferTooSmall;
  240. }
  241. _processHandle = processHandle;
  242. _upsamplerManager = new UpsamplerManager(upSamplerWorkBuffer, _upsamplerCount);
  243. _effectContext.Initialize(parameter.EffectCount, _behaviourContext.IsEffectInfoVersion2Supported() ? parameter.EffectCount : 0);
  244. _sinkContext.Initialize(parameter.SinkCount);
  245. Memory<VoiceUpdateState> voiceUpdateStatesDsp = workBufferAllocator.Allocate<VoiceUpdateState>(parameter.VoiceCount, VoiceUpdateState.Align);
  246. if (voiceUpdateStatesDsp.IsEmpty)
  247. {
  248. return ResultCode.WorkBufferTooSmall;
  249. }
  250. _voiceContext.Initialize(sortedVoices, voices, voiceChannelResources, voiceUpdateStates, voiceUpdateStatesDsp, parameter.VoiceCount);
  251. if (parameter.PerformanceMetricFramesCount > 0)
  252. {
  253. ulong performanceBufferSize = PerformanceManager.GetRequiredBufferSizeForPerformanceMetricsPerFrame(ref parameter, ref _behaviourContext) * (parameter.PerformanceMetricFramesCount + 1) + 0xC;
  254. _performanceBuffer = workBufferAllocator.Allocate(performanceBufferSize, Constants.BufferAlignment);
  255. if (_performanceBuffer.IsEmpty)
  256. {
  257. return ResultCode.WorkBufferTooSmall;
  258. }
  259. _performanceManager = PerformanceManager.Create(_performanceBuffer, ref parameter, _behaviourContext);
  260. }
  261. else
  262. {
  263. _performanceManager = null;
  264. }
  265. _totalElapsedTicksUpdating = 0;
  266. _totalElapsedTicks = 0;
  267. _renderingTimeLimitPercent = 100;
  268. _voiceDropEnabled = parameter.VoiceDropEnabled && _executionMode == AudioRendererExecutionMode.Auto;
  269. AudioProcessorMemoryManager.InvalidateDataCache(workBuffer, workBufferSize);
  270. _processHandle = processHandle;
  271. _elapsedFrameCount = 0;
  272. switch (_behaviourContext.GetCommandProcessingTimeEstimatorVersion())
  273. {
  274. case 1:
  275. _commandProcessingTimeEstimator = new CommandProcessingTimeEstimatorVersion1(_sampleCount, _mixBufferCount);
  276. break;
  277. case 2:
  278. _commandProcessingTimeEstimator = new CommandProcessingTimeEstimatorVersion2(_sampleCount, _mixBufferCount);
  279. break;
  280. case 3:
  281. _commandProcessingTimeEstimator = new CommandProcessingTimeEstimatorVersion3(_sampleCount, _mixBufferCount);
  282. break;
  283. default:
  284. throw new NotImplementedException($"Unsupported processing time estimator version {_behaviourContext.GetCommandProcessingTimeEstimatorVersion()}.");
  285. }
  286. return ResultCode.Success;
  287. }
  288. public void Start()
  289. {
  290. Logger.Info?.Print(LogClass.AudioRenderer, $"Starting renderer id {_sessionId}");
  291. lock (_lock)
  292. {
  293. _elapsedFrameCount = 0;
  294. _isActive = true;
  295. }
  296. }
  297. public void Stop()
  298. {
  299. Logger.Info?.Print(LogClass.AudioRenderer, $"Stopping renderer id {_sessionId}");
  300. lock (_lock)
  301. {
  302. _isActive = false;
  303. }
  304. if (_executionMode == AudioRendererExecutionMode.Auto)
  305. {
  306. _terminationEvent.WaitOne();
  307. }
  308. Logger.Info?.Print(LogClass.AudioRenderer, $"Stopped renderer id {_sessionId}");
  309. }
  310. public ResultCode Update(Memory<byte> output, Memory<byte> performanceOutput, ReadOnlyMemory<byte> input)
  311. {
  312. lock (_lock)
  313. {
  314. ulong updateStartTicks = GetSystemTicks();
  315. output.Span.Fill(0);
  316. StateUpdater stateUpdater = new StateUpdater(input, output, _processHandle, _behaviourContext);
  317. ResultCode result;
  318. result = stateUpdater.UpdateBehaviourContext();
  319. if (result != ResultCode.Success)
  320. {
  321. return result;
  322. }
  323. result = stateUpdater.UpdateMemoryPools(_memoryPools.Span);
  324. if (result != ResultCode.Success)
  325. {
  326. return result;
  327. }
  328. result = stateUpdater.UpdateVoiceChannelResources(_voiceContext);
  329. if (result != ResultCode.Success)
  330. {
  331. return result;
  332. }
  333. result = stateUpdater.UpdateVoices(_voiceContext, _memoryPools);
  334. if (result != ResultCode.Success)
  335. {
  336. return result;
  337. }
  338. result = stateUpdater.UpdateEffects(_effectContext, _isActive, _memoryPools);
  339. if (result != ResultCode.Success)
  340. {
  341. return result;
  342. }
  343. if (_behaviourContext.IsSplitterSupported())
  344. {
  345. result = stateUpdater.UpdateSplitter(_splitterContext);
  346. if (result != ResultCode.Success)
  347. {
  348. return result;
  349. }
  350. }
  351. result = stateUpdater.UpdateMixes(_mixContext, GetMixBufferCount(), _effectContext, _splitterContext);
  352. if (result != ResultCode.Success)
  353. {
  354. return result;
  355. }
  356. result = stateUpdater.UpdateSinks(_sinkContext, _memoryPools);
  357. if (result != ResultCode.Success)
  358. {
  359. return result;
  360. }
  361. result = stateUpdater.UpdatePerformanceBuffer(_performanceManager, performanceOutput.Span);
  362. if (result != ResultCode.Success)
  363. {
  364. return result;
  365. }
  366. result = stateUpdater.UpdateErrorInfo();
  367. if (result != ResultCode.Success)
  368. {
  369. return result;
  370. }
  371. if (_behaviourContext.IsElapsedFrameCountSupported())
  372. {
  373. result = stateUpdater.UpdateRendererInfo(_elapsedFrameCount);
  374. if (result != ResultCode.Success)
  375. {
  376. return result;
  377. }
  378. }
  379. result = stateUpdater.CheckConsumedSize();
  380. if (result != ResultCode.Success)
  381. {
  382. return result;
  383. }
  384. _systemEvent.Clear();
  385. ulong updateEndTicks = GetSystemTicks();
  386. _totalElapsedTicksUpdating += (updateEndTicks - updateStartTicks);
  387. return result;
  388. }
  389. }
  390. private ulong GetSystemTicks()
  391. {
  392. double ticks = ARMeilleure.State.ExecutionContext.ElapsedTicks * ARMeilleure.State.ExecutionContext.TickFrequency;
  393. return (ulong)(ticks * Constants.TargetTimerFrequency);
  394. }
  395. private uint ComputeVoiceDrop(CommandBuffer commandBuffer, long voicesEstimatedTime, long deltaTimeDsp)
  396. {
  397. int i;
  398. for (i = 0; i < commandBuffer.CommandList.Commands.Count; i++)
  399. {
  400. ICommand command = commandBuffer.CommandList.Commands[i];
  401. CommandType commandType = command.CommandType;
  402. if (commandType == CommandType.AdpcmDataSourceVersion1 ||
  403. commandType == CommandType.AdpcmDataSourceVersion2 ||
  404. commandType == CommandType.PcmInt16DataSourceVersion1 ||
  405. commandType == CommandType.PcmInt16DataSourceVersion2 ||
  406. commandType == CommandType.PcmFloatDataSourceVersion1 ||
  407. commandType == CommandType.PcmFloatDataSourceVersion2 ||
  408. commandType == CommandType.Performance)
  409. {
  410. break;
  411. }
  412. }
  413. uint voiceDropped = 0;
  414. for (; i < commandBuffer.CommandList.Commands.Count; i++)
  415. {
  416. ICommand targetCommand = commandBuffer.CommandList.Commands[i];
  417. int targetNodeId = targetCommand.NodeId;
  418. if (voicesEstimatedTime <= deltaTimeDsp || NodeIdHelper.GetType(targetNodeId) != NodeIdType.Voice)
  419. {
  420. break;
  421. }
  422. ref VoiceState voice = ref _voiceContext.GetState(NodeIdHelper.GetBase(targetNodeId));
  423. if (voice.Priority == Constants.VoiceHighestPriority)
  424. {
  425. break;
  426. }
  427. // We can safely drop this voice, disable all associated commands while activating depop preparation commands.
  428. voiceDropped++;
  429. voice.VoiceDropFlag = true;
  430. Logger.Warning?.Print(LogClass.AudioRenderer, $"Dropping voice {voice.NodeId}");
  431. for (; i < commandBuffer.CommandList.Commands.Count; i++)
  432. {
  433. ICommand command = commandBuffer.CommandList.Commands[i];
  434. if (command.NodeId != targetNodeId)
  435. {
  436. break;
  437. }
  438. if (command.CommandType == CommandType.DepopPrepare)
  439. {
  440. command.Enabled = true;
  441. }
  442. else if (command.CommandType == CommandType.Performance || !command.Enabled)
  443. {
  444. continue;
  445. }
  446. else
  447. {
  448. command.Enabled = false;
  449. voicesEstimatedTime -= (long)command.EstimatedProcessingTime;
  450. }
  451. }
  452. }
  453. return voiceDropped;
  454. }
  455. private void GenerateCommandList(out CommandList commandList)
  456. {
  457. Debug.Assert(_executionMode == AudioRendererExecutionMode.Auto);
  458. PoolMapper.ClearUsageState(_memoryPools);
  459. ulong startTicks = GetSystemTicks();
  460. commandList = new CommandList(this);
  461. if (_performanceManager != null)
  462. {
  463. _performanceManager.TapFrame(_isDspRunningBehind, _voiceDropCount, _renderingStartTick);
  464. _isDspRunningBehind = false;
  465. _voiceDropCount = 0;
  466. _renderingStartTick = 0;
  467. }
  468. CommandBuffer commandBuffer = new CommandBuffer(commandList, _commandProcessingTimeEstimator);
  469. CommandGenerator commandGenerator = new CommandGenerator(commandBuffer, GetContext(), _voiceContext, _mixContext, _effectContext, _sinkContext, _splitterContext, _performanceManager);
  470. _voiceContext.Sort();
  471. commandGenerator.GenerateVoices();
  472. long voicesEstimatedTime = (long)commandBuffer.EstimatedProcessingTime;
  473. commandGenerator.GenerateSubMixes();
  474. commandGenerator.GenerateFinalMixes();
  475. commandGenerator.GenerateSinks();
  476. long totalEstimatedTime = (long)commandBuffer.EstimatedProcessingTime;
  477. if (_voiceDropEnabled)
  478. {
  479. long maxDspTime = GetMaxAllocatedTimeForDsp();
  480. long restEstimateTime = totalEstimatedTime - voicesEstimatedTime;
  481. long deltaTimeDsp = Math.Max(maxDspTime - restEstimateTime, 0);
  482. _voiceDropCount = ComputeVoiceDrop(commandBuffer, voicesEstimatedTime, deltaTimeDsp);
  483. }
  484. _voiceContext.UpdateForCommandGeneration();
  485. if (_behaviourContext.IsEffectInfoVersion2Supported())
  486. {
  487. _effectContext.UpdateResultStateForCommandGeneration();
  488. }
  489. ulong endTicks = GetSystemTicks();
  490. _totalElapsedTicks = endTicks - startTicks;
  491. _renderingStartTick = GetSystemTicks();
  492. _elapsedFrameCount++;
  493. }
  494. private int GetMaxAllocatedTimeForDsp()
  495. {
  496. return (int)(Constants.AudioProcessorMaxUpdateTimePerSessions * _behaviourContext.GetAudioRendererProcessingTimeLimit() * (GetRenderingTimeLimit() / 100.0f));
  497. }
  498. public void SendCommands()
  499. {
  500. lock (_lock)
  501. {
  502. if (_isActive)
  503. {
  504. _terminationEvent.Reset();
  505. GenerateCommandList(out CommandList commands);
  506. _manager.Processor.Send(_sessionId,
  507. commands,
  508. GetMaxAllocatedTimeForDsp(),
  509. _appletResourceId);
  510. _systemEvent.Signal();
  511. }
  512. else
  513. {
  514. _terminationEvent.Set();
  515. }
  516. }
  517. }
  518. public uint GetMixBufferCount()
  519. {
  520. return _mixBufferCount;
  521. }
  522. public void SetRenderingTimeLimitPercent(uint percent)
  523. {
  524. Debug.Assert(percent <= 100);
  525. _renderingTimeLimitPercent = percent;
  526. }
  527. public uint GetRenderingTimeLimit()
  528. {
  529. return _renderingTimeLimitPercent;
  530. }
  531. public Memory<float> GetMixBuffer()
  532. {
  533. return _mixBuffer;
  534. }
  535. public uint GetSampleCount()
  536. {
  537. return _sampleCount;
  538. }
  539. public uint GetSampleRate()
  540. {
  541. return _sampleRate;
  542. }
  543. public uint GetVoiceChannelCountMax()
  544. {
  545. return _voiceChannelCountMax;
  546. }
  547. public bool IsActive()
  548. {
  549. return _isActive;
  550. }
  551. private RendererSystemContext GetContext()
  552. {
  553. return new RendererSystemContext
  554. {
  555. ChannelCount = _manager.Processor.OutputDevices[_sessionId].GetChannelCount(),
  556. BehaviourContext = _behaviourContext,
  557. DepopBuffer = _depopBuffer,
  558. MixBufferCount = GetMixBufferCount(),
  559. SessionId = _sessionId,
  560. UpsamplerManager = _upsamplerManager
  561. };
  562. }
  563. public int GetSessionId()
  564. {
  565. return _sessionId;
  566. }
  567. public static ulong GetWorkBufferSize(ref AudioRendererConfiguration parameter)
  568. {
  569. BehaviourContext behaviourContext = new BehaviourContext();
  570. behaviourContext.SetUserRevision(parameter.Revision);
  571. uint mixesCount = parameter.SubMixBufferCount + 1;
  572. uint memoryPoolCount = parameter.EffectCount + parameter.VoiceCount * Constants.VoiceWaveBufferCount;
  573. ulong size = 0;
  574. // Mix Buffers
  575. size = WorkBufferAllocator.GetTargetSize<float>(size, parameter.SampleCount * (Constants.VoiceChannelCountMax + parameter.MixBufferCount), 0x10);
  576. // Upsampler workbuffer
  577. size = WorkBufferAllocator.GetTargetSize<float>(size, Constants.TargetSampleCount * (Constants.VoiceChannelCountMax + parameter.MixBufferCount) * (parameter.SinkCount + parameter.SubMixBufferCount), 0x10);
  578. // Depop buffer
  579. size = WorkBufferAllocator.GetTargetSize<float>(size, (ulong)BitUtils.AlignUp(parameter.MixBufferCount, Constants.BufferAlignment), Constants.BufferAlignment);
  580. // Voice
  581. size = WorkBufferAllocator.GetTargetSize<VoiceState>(size, parameter.VoiceCount, VoiceState.Alignment);
  582. size = WorkBufferAllocator.GetTargetSize<int>(size, parameter.VoiceCount, 0x10);
  583. size = WorkBufferAllocator.GetTargetSize<VoiceChannelResource>(size, parameter.VoiceCount, VoiceChannelResource.Alignment);
  584. size = WorkBufferAllocator.GetTargetSize<VoiceUpdateState>(size, parameter.VoiceCount, VoiceUpdateState.Align);
  585. // Mix
  586. size = WorkBufferAllocator.GetTargetSize<MixState>(size, mixesCount, MixState.Alignment);
  587. size = WorkBufferAllocator.GetTargetSize<int>(size, parameter.EffectCount * mixesCount, 0x10);
  588. size = WorkBufferAllocator.GetTargetSize<int>(size, mixesCount, 0x10);
  589. if (behaviourContext.IsSplitterSupported())
  590. {
  591. size += (ulong)BitUtils.AlignUp(NodeStates.GetWorkBufferSize((int)mixesCount) + EdgeMatrix.GetWorkBufferSize((int)mixesCount), 0x10);
  592. }
  593. // Memory Pool
  594. size = WorkBufferAllocator.GetTargetSize<MemoryPoolState>(size, memoryPoolCount, MemoryPoolState.Alignment);
  595. // Splitter
  596. size = SplitterContext.GetWorkBufferSize(size, ref behaviourContext, ref parameter);
  597. // DSP Voice
  598. size = WorkBufferAllocator.GetTargetSize<VoiceUpdateState>(size, parameter.VoiceCount, VoiceUpdateState.Align);
  599. // Performance
  600. if (parameter.PerformanceMetricFramesCount > 0)
  601. {
  602. ulong performanceMetricsPerFramesSize = PerformanceManager.GetRequiredBufferSizeForPerformanceMetricsPerFrame(ref parameter, ref behaviourContext) * (parameter.PerformanceMetricFramesCount + 1) + 0xC;
  603. size += BitUtils.AlignUp(performanceMetricsPerFramesSize, Constants.PerformanceMetricsPerFramesSizeAlignment);
  604. }
  605. return BitUtils.AlignUp(size, Constants.WorkBufferAlignment);
  606. }
  607. public ResultCode QuerySystemEvent(out IWritableEvent systemEvent)
  608. {
  609. systemEvent = default;
  610. if (_executionMode == AudioRendererExecutionMode.Manual)
  611. {
  612. return ResultCode.UnsupportedOperation;
  613. }
  614. systemEvent = _systemEvent;
  615. return ResultCode.Success;
  616. }
  617. public void Dispose()
  618. {
  619. if (Interlocked.CompareExchange(ref _disposeState, 1, 0) == 0)
  620. {
  621. Dispose(true);
  622. }
  623. }
  624. protected virtual void Dispose(bool disposing)
  625. {
  626. if (disposing)
  627. {
  628. if (_isActive)
  629. {
  630. Stop();
  631. }
  632. PoolMapper mapper = new PoolMapper(_processHandle, false);
  633. mapper.Unmap(ref _dspMemoryPoolState);
  634. PoolMapper.ClearUsageState(_memoryPools);
  635. for (int i = 0; i < _memoryPoolCount; i++)
  636. {
  637. ref MemoryPoolState memoryPool = ref _memoryPools.Span[i];
  638. if (memoryPool.IsMapped())
  639. {
  640. mapper.Unmap(ref memoryPool);
  641. }
  642. }
  643. _manager.Unregister(this);
  644. _terminationEvent.Dispose();
  645. _workBufferMemoryPin.Dispose();
  646. _workBufferRegion.Dispose();
  647. if (MemoryManager is IRefCounted rc)
  648. {
  649. rc.DecrementReferenceCount();
  650. MemoryManager = null;
  651. }
  652. }
  653. }
  654. }
  655. }