AudioRenderSystem.cs 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868
  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 void Disable()
  311. {
  312. lock (_lock)
  313. {
  314. _isActive = false;
  315. }
  316. }
  317. public ResultCode Update(Memory<byte> output, Memory<byte> performanceOutput, ReadOnlyMemory<byte> input)
  318. {
  319. lock (_lock)
  320. {
  321. ulong updateStartTicks = GetSystemTicks();
  322. output.Span.Fill(0);
  323. StateUpdater stateUpdater = new StateUpdater(input, output, _processHandle, _behaviourContext);
  324. ResultCode result;
  325. result = stateUpdater.UpdateBehaviourContext();
  326. if (result != ResultCode.Success)
  327. {
  328. return result;
  329. }
  330. result = stateUpdater.UpdateMemoryPools(_memoryPools.Span);
  331. if (result != ResultCode.Success)
  332. {
  333. return result;
  334. }
  335. result = stateUpdater.UpdateVoiceChannelResources(_voiceContext);
  336. if (result != ResultCode.Success)
  337. {
  338. return result;
  339. }
  340. result = stateUpdater.UpdateVoices(_voiceContext, _memoryPools);
  341. if (result != ResultCode.Success)
  342. {
  343. return result;
  344. }
  345. result = stateUpdater.UpdateEffects(_effectContext, _isActive, _memoryPools);
  346. if (result != ResultCode.Success)
  347. {
  348. return result;
  349. }
  350. if (_behaviourContext.IsSplitterSupported())
  351. {
  352. result = stateUpdater.UpdateSplitter(_splitterContext);
  353. if (result != ResultCode.Success)
  354. {
  355. return result;
  356. }
  357. }
  358. result = stateUpdater.UpdateMixes(_mixContext, GetMixBufferCount(), _effectContext, _splitterContext);
  359. if (result != ResultCode.Success)
  360. {
  361. return result;
  362. }
  363. result = stateUpdater.UpdateSinks(_sinkContext, _memoryPools);
  364. if (result != ResultCode.Success)
  365. {
  366. return result;
  367. }
  368. result = stateUpdater.UpdatePerformanceBuffer(_performanceManager, performanceOutput.Span);
  369. if (result != ResultCode.Success)
  370. {
  371. return result;
  372. }
  373. result = stateUpdater.UpdateErrorInfo();
  374. if (result != ResultCode.Success)
  375. {
  376. return result;
  377. }
  378. if (_behaviourContext.IsElapsedFrameCountSupported())
  379. {
  380. result = stateUpdater.UpdateRendererInfo(_elapsedFrameCount);
  381. if (result != ResultCode.Success)
  382. {
  383. return result;
  384. }
  385. }
  386. result = stateUpdater.CheckConsumedSize();
  387. if (result != ResultCode.Success)
  388. {
  389. return result;
  390. }
  391. _systemEvent.Clear();
  392. ulong updateEndTicks = GetSystemTicks();
  393. _totalElapsedTicksUpdating += (updateEndTicks - updateStartTicks);
  394. return result;
  395. }
  396. }
  397. private ulong GetSystemTicks()
  398. {
  399. double ticks = ARMeilleure.State.ExecutionContext.ElapsedTicks * ARMeilleure.State.ExecutionContext.TickFrequency;
  400. return (ulong)(ticks * Constants.TargetTimerFrequency);
  401. }
  402. private uint ComputeVoiceDrop(CommandBuffer commandBuffer, long voicesEstimatedTime, long deltaTimeDsp)
  403. {
  404. int i;
  405. for (i = 0; i < commandBuffer.CommandList.Commands.Count; i++)
  406. {
  407. ICommand command = commandBuffer.CommandList.Commands[i];
  408. CommandType commandType = command.CommandType;
  409. if (commandType == CommandType.AdpcmDataSourceVersion1 ||
  410. commandType == CommandType.AdpcmDataSourceVersion2 ||
  411. commandType == CommandType.PcmInt16DataSourceVersion1 ||
  412. commandType == CommandType.PcmInt16DataSourceVersion2 ||
  413. commandType == CommandType.PcmFloatDataSourceVersion1 ||
  414. commandType == CommandType.PcmFloatDataSourceVersion2 ||
  415. commandType == CommandType.Performance)
  416. {
  417. break;
  418. }
  419. }
  420. uint voiceDropped = 0;
  421. for (; i < commandBuffer.CommandList.Commands.Count; i++)
  422. {
  423. ICommand targetCommand = commandBuffer.CommandList.Commands[i];
  424. int targetNodeId = targetCommand.NodeId;
  425. if (voicesEstimatedTime <= deltaTimeDsp || NodeIdHelper.GetType(targetNodeId) != NodeIdType.Voice)
  426. {
  427. break;
  428. }
  429. ref VoiceState voice = ref _voiceContext.GetState(NodeIdHelper.GetBase(targetNodeId));
  430. if (voice.Priority == Constants.VoiceHighestPriority)
  431. {
  432. break;
  433. }
  434. // We can safely drop this voice, disable all associated commands while activating depop preparation commands.
  435. voiceDropped++;
  436. voice.VoiceDropFlag = true;
  437. Logger.Warning?.Print(LogClass.AudioRenderer, $"Dropping voice {voice.NodeId}");
  438. for (; i < commandBuffer.CommandList.Commands.Count; i++)
  439. {
  440. ICommand command = commandBuffer.CommandList.Commands[i];
  441. if (command.NodeId != targetNodeId)
  442. {
  443. break;
  444. }
  445. if (command.CommandType == CommandType.DepopPrepare)
  446. {
  447. command.Enabled = true;
  448. }
  449. else if (command.CommandType == CommandType.Performance || !command.Enabled)
  450. {
  451. continue;
  452. }
  453. else
  454. {
  455. command.Enabled = false;
  456. voicesEstimatedTime -= (long)command.EstimatedProcessingTime;
  457. }
  458. }
  459. }
  460. return voiceDropped;
  461. }
  462. private void GenerateCommandList(out CommandList commandList)
  463. {
  464. Debug.Assert(_executionMode == AudioRendererExecutionMode.Auto);
  465. PoolMapper.ClearUsageState(_memoryPools);
  466. ulong startTicks = GetSystemTicks();
  467. commandList = new CommandList(this);
  468. if (_performanceManager != null)
  469. {
  470. _performanceManager.TapFrame(_isDspRunningBehind, _voiceDropCount, _renderingStartTick);
  471. _isDspRunningBehind = false;
  472. _voiceDropCount = 0;
  473. _renderingStartTick = 0;
  474. }
  475. CommandBuffer commandBuffer = new CommandBuffer(commandList, _commandProcessingTimeEstimator);
  476. CommandGenerator commandGenerator = new CommandGenerator(commandBuffer, GetContext(), _voiceContext, _mixContext, _effectContext, _sinkContext, _splitterContext, _performanceManager);
  477. _voiceContext.Sort();
  478. commandGenerator.GenerateVoices();
  479. long voicesEstimatedTime = (long)commandBuffer.EstimatedProcessingTime;
  480. commandGenerator.GenerateSubMixes();
  481. commandGenerator.GenerateFinalMixes();
  482. commandGenerator.GenerateSinks();
  483. long totalEstimatedTime = (long)commandBuffer.EstimatedProcessingTime;
  484. if (_voiceDropEnabled)
  485. {
  486. long maxDspTime = GetMaxAllocatedTimeForDsp();
  487. long restEstimateTime = totalEstimatedTime - voicesEstimatedTime;
  488. long deltaTimeDsp = Math.Max(maxDspTime - restEstimateTime, 0);
  489. _voiceDropCount = ComputeVoiceDrop(commandBuffer, voicesEstimatedTime, deltaTimeDsp);
  490. }
  491. _voiceContext.UpdateForCommandGeneration();
  492. if (_behaviourContext.IsEffectInfoVersion2Supported())
  493. {
  494. _effectContext.UpdateResultStateForCommandGeneration();
  495. }
  496. ulong endTicks = GetSystemTicks();
  497. _totalElapsedTicks = endTicks - startTicks;
  498. _renderingStartTick = GetSystemTicks();
  499. _elapsedFrameCount++;
  500. }
  501. private int GetMaxAllocatedTimeForDsp()
  502. {
  503. return (int)(Constants.AudioProcessorMaxUpdateTimePerSessions * _behaviourContext.GetAudioRendererProcessingTimeLimit() * (GetRenderingTimeLimit() / 100.0f));
  504. }
  505. public void SendCommands()
  506. {
  507. lock (_lock)
  508. {
  509. if (_isActive)
  510. {
  511. _terminationEvent.Reset();
  512. GenerateCommandList(out CommandList commands);
  513. _manager.Processor.Send(_sessionId,
  514. commands,
  515. GetMaxAllocatedTimeForDsp(),
  516. _appletResourceId);
  517. _systemEvent.Signal();
  518. }
  519. else
  520. {
  521. _terminationEvent.Set();
  522. }
  523. }
  524. }
  525. public uint GetMixBufferCount()
  526. {
  527. return _mixBufferCount;
  528. }
  529. public void SetRenderingTimeLimitPercent(uint percent)
  530. {
  531. Debug.Assert(percent <= 100);
  532. _renderingTimeLimitPercent = percent;
  533. }
  534. public uint GetRenderingTimeLimit()
  535. {
  536. return _renderingTimeLimitPercent;
  537. }
  538. public Memory<float> GetMixBuffer()
  539. {
  540. return _mixBuffer;
  541. }
  542. public uint GetSampleCount()
  543. {
  544. return _sampleCount;
  545. }
  546. public uint GetSampleRate()
  547. {
  548. return _sampleRate;
  549. }
  550. public uint GetVoiceChannelCountMax()
  551. {
  552. return _voiceChannelCountMax;
  553. }
  554. public bool IsActive()
  555. {
  556. return _isActive;
  557. }
  558. private RendererSystemContext GetContext()
  559. {
  560. return new RendererSystemContext
  561. {
  562. ChannelCount = _manager.Processor.OutputDevices[_sessionId].GetChannelCount(),
  563. BehaviourContext = _behaviourContext,
  564. DepopBuffer = _depopBuffer,
  565. MixBufferCount = GetMixBufferCount(),
  566. SessionId = _sessionId,
  567. UpsamplerManager = _upsamplerManager
  568. };
  569. }
  570. public int GetSessionId()
  571. {
  572. return _sessionId;
  573. }
  574. public static ulong GetWorkBufferSize(ref AudioRendererConfiguration parameter)
  575. {
  576. BehaviourContext behaviourContext = new BehaviourContext();
  577. behaviourContext.SetUserRevision(parameter.Revision);
  578. uint mixesCount = parameter.SubMixBufferCount + 1;
  579. uint memoryPoolCount = parameter.EffectCount + parameter.VoiceCount * Constants.VoiceWaveBufferCount;
  580. ulong size = 0;
  581. // Mix Buffers
  582. size = WorkBufferAllocator.GetTargetSize<float>(size, parameter.SampleCount * (Constants.VoiceChannelCountMax + parameter.MixBufferCount), 0x10);
  583. // Upsampler workbuffer
  584. size = WorkBufferAllocator.GetTargetSize<float>(size, Constants.TargetSampleCount * (Constants.VoiceChannelCountMax + parameter.MixBufferCount) * (parameter.SinkCount + parameter.SubMixBufferCount), 0x10);
  585. // Depop buffer
  586. size = WorkBufferAllocator.GetTargetSize<float>(size, (ulong)BitUtils.AlignUp(parameter.MixBufferCount, Constants.BufferAlignment), Constants.BufferAlignment);
  587. // Voice
  588. size = WorkBufferAllocator.GetTargetSize<VoiceState>(size, parameter.VoiceCount, VoiceState.Alignment);
  589. size = WorkBufferAllocator.GetTargetSize<int>(size, parameter.VoiceCount, 0x10);
  590. size = WorkBufferAllocator.GetTargetSize<VoiceChannelResource>(size, parameter.VoiceCount, VoiceChannelResource.Alignment);
  591. size = WorkBufferAllocator.GetTargetSize<VoiceUpdateState>(size, parameter.VoiceCount, VoiceUpdateState.Align);
  592. // Mix
  593. size = WorkBufferAllocator.GetTargetSize<MixState>(size, mixesCount, MixState.Alignment);
  594. size = WorkBufferAllocator.GetTargetSize<int>(size, parameter.EffectCount * mixesCount, 0x10);
  595. size = WorkBufferAllocator.GetTargetSize<int>(size, mixesCount, 0x10);
  596. if (behaviourContext.IsSplitterSupported())
  597. {
  598. size += (ulong)BitUtils.AlignUp(NodeStates.GetWorkBufferSize((int)mixesCount) + EdgeMatrix.GetWorkBufferSize((int)mixesCount), 0x10);
  599. }
  600. // Memory Pool
  601. size = WorkBufferAllocator.GetTargetSize<MemoryPoolState>(size, memoryPoolCount, MemoryPoolState.Alignment);
  602. // Splitter
  603. size = SplitterContext.GetWorkBufferSize(size, ref behaviourContext, ref parameter);
  604. // DSP Voice
  605. size = WorkBufferAllocator.GetTargetSize<VoiceUpdateState>(size, parameter.VoiceCount, VoiceUpdateState.Align);
  606. // Performance
  607. if (parameter.PerformanceMetricFramesCount > 0)
  608. {
  609. ulong performanceMetricsPerFramesSize = PerformanceManager.GetRequiredBufferSizeForPerformanceMetricsPerFrame(ref parameter, ref behaviourContext) * (parameter.PerformanceMetricFramesCount + 1) + 0xC;
  610. size += BitUtils.AlignUp(performanceMetricsPerFramesSize, Constants.PerformanceMetricsPerFramesSizeAlignment);
  611. }
  612. return BitUtils.AlignUp(size, Constants.WorkBufferAlignment);
  613. }
  614. public ResultCode QuerySystemEvent(out IWritableEvent systemEvent)
  615. {
  616. systemEvent = default;
  617. if (_executionMode == AudioRendererExecutionMode.Manual)
  618. {
  619. return ResultCode.UnsupportedOperation;
  620. }
  621. systemEvent = _systemEvent;
  622. return ResultCode.Success;
  623. }
  624. public void Dispose()
  625. {
  626. if (Interlocked.CompareExchange(ref _disposeState, 1, 0) == 0)
  627. {
  628. Dispose(true);
  629. }
  630. }
  631. protected virtual void Dispose(bool disposing)
  632. {
  633. if (disposing)
  634. {
  635. if (_isActive)
  636. {
  637. Stop();
  638. }
  639. PoolMapper mapper = new PoolMapper(_processHandle, false);
  640. mapper.Unmap(ref _dspMemoryPoolState);
  641. PoolMapper.ClearUsageState(_memoryPools);
  642. for (int i = 0; i < _memoryPoolCount; i++)
  643. {
  644. ref MemoryPoolState memoryPool = ref _memoryPools.Span[i];
  645. if (memoryPool.IsMapped())
  646. {
  647. mapper.Unmap(ref memoryPool);
  648. }
  649. }
  650. _manager.Unregister(this);
  651. _terminationEvent.Dispose();
  652. _workBufferMemoryPin.Dispose();
  653. _workBufferRegion.Dispose();
  654. if (MemoryManager is IRefCounted rc)
  655. {
  656. rc.DecrementReferenceCount();
  657. MemoryManager = null;
  658. }
  659. }
  660. }
  661. }
  662. }