AudioRenderSystem.cs 31 KB

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