AudioRenderSystem.cs 30 KB

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