CommandGenerator.cs 45 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014
  1. using Ryujinx.Audio.Common;
  2. using Ryujinx.Audio.Renderer.Common;
  3. using Ryujinx.Audio.Renderer.Dsp.Command;
  4. using Ryujinx.Audio.Renderer.Dsp.State;
  5. using Ryujinx.Audio.Renderer.Parameter;
  6. using Ryujinx.Audio.Renderer.Server.Effect;
  7. using Ryujinx.Audio.Renderer.Server.Mix;
  8. using Ryujinx.Audio.Renderer.Server.Performance;
  9. using Ryujinx.Audio.Renderer.Server.Sink;
  10. using Ryujinx.Audio.Renderer.Server.Splitter;
  11. using Ryujinx.Audio.Renderer.Server.Voice;
  12. using Ryujinx.Audio.Renderer.Utils;
  13. using System;
  14. using System.Diagnostics;
  15. namespace Ryujinx.Audio.Renderer.Server
  16. {
  17. public class CommandGenerator
  18. {
  19. private CommandBuffer _commandBuffer;
  20. private RendererSystemContext _rendererContext;
  21. private VoiceContext _voiceContext;
  22. private MixContext _mixContext;
  23. private EffectContext _effectContext;
  24. private SinkContext _sinkContext;
  25. private SplitterContext _splitterContext;
  26. private PerformanceManager _performanceManager;
  27. public CommandGenerator(CommandBuffer commandBuffer, RendererSystemContext rendererContext, VoiceContext voiceContext, MixContext mixContext, EffectContext effectContext, SinkContext sinkContext, SplitterContext splitterContext, PerformanceManager performanceManager)
  28. {
  29. _commandBuffer = commandBuffer;
  30. _rendererContext = rendererContext;
  31. _voiceContext = voiceContext;
  32. _mixContext = mixContext;
  33. _effectContext = effectContext;
  34. _sinkContext = sinkContext;
  35. _splitterContext = splitterContext;
  36. _performanceManager = performanceManager;
  37. _commandBuffer.GenerateClearMixBuffer(Constants.InvalidNodeId);
  38. }
  39. private void GenerateDataSource(ref VoiceState voiceState, Memory<VoiceUpdateState> dspState, int channelIndex)
  40. {
  41. if (voiceState.MixId != Constants.UnusedMixId)
  42. {
  43. ref MixState mix = ref _mixContext.GetState(voiceState.MixId);
  44. _commandBuffer.GenerateDepopPrepare(dspState,
  45. _rendererContext.DepopBuffer,
  46. mix.BufferCount,
  47. mix.BufferOffset,
  48. voiceState.NodeId,
  49. voiceState.WasPlaying);
  50. }
  51. else if (voiceState.SplitterId != Constants.UnusedSplitterId)
  52. {
  53. int destinationId = 0;
  54. while (true)
  55. {
  56. Span<SplitterDestination> destinationSpan = _splitterContext.GetDestination((int)voiceState.SplitterId, destinationId++);
  57. if (destinationSpan.IsEmpty)
  58. {
  59. break;
  60. }
  61. ref SplitterDestination destination = ref destinationSpan[0];
  62. if (destination.IsConfigured())
  63. {
  64. int mixId = destination.DestinationId;
  65. if (mixId < _mixContext.GetCount() && mixId != Constants.UnusedSplitterIdInt)
  66. {
  67. ref MixState mix = ref _mixContext.GetState(mixId);
  68. _commandBuffer.GenerateDepopPrepare(dspState,
  69. _rendererContext.DepopBuffer,
  70. mix.BufferCount,
  71. mix.BufferOffset,
  72. voiceState.NodeId,
  73. voiceState.WasPlaying);
  74. destination.MarkAsNeedToUpdateInternalState();
  75. }
  76. }
  77. }
  78. }
  79. if (!voiceState.WasPlaying)
  80. {
  81. Debug.Assert(voiceState.SampleFormat != SampleFormat.Adpcm || channelIndex == 0);
  82. if (_rendererContext.BehaviourContext.IsWaveBufferVersion2Supported())
  83. {
  84. _commandBuffer.GenerateDataSourceVersion2(ref voiceState,
  85. dspState,
  86. (ushort)_rendererContext.MixBufferCount,
  87. (ushort)channelIndex,
  88. voiceState.NodeId);
  89. }
  90. else
  91. {
  92. switch (voiceState.SampleFormat)
  93. {
  94. case SampleFormat.PcmInt16:
  95. _commandBuffer.GeneratePcmInt16DataSourceVersion1(ref voiceState,
  96. dspState,
  97. (ushort)_rendererContext.MixBufferCount,
  98. (ushort)channelIndex,
  99. voiceState.NodeId);
  100. break;
  101. case SampleFormat.PcmFloat:
  102. _commandBuffer.GeneratePcmFloatDataSourceVersion1(ref voiceState,
  103. dspState,
  104. (ushort)_rendererContext.MixBufferCount,
  105. (ushort)channelIndex,
  106. voiceState.NodeId);
  107. break;
  108. case SampleFormat.Adpcm:
  109. _commandBuffer.GenerateAdpcmDataSourceVersion1(ref voiceState,
  110. dspState,
  111. (ushort)_rendererContext.MixBufferCount,
  112. voiceState.NodeId);
  113. break;
  114. default:
  115. throw new NotImplementedException($"Unsupported data source {voiceState.SampleFormat}");
  116. }
  117. }
  118. }
  119. }
  120. private void GenerateBiquadFilterForVoice(ref VoiceState voiceState, Memory<VoiceUpdateState> state, int baseIndex, int bufferOffset, int nodeId)
  121. {
  122. bool supportsOptimizedPath = _rendererContext.BehaviourContext.IsBiquadFilterGroupedOptimizationSupported();
  123. if (supportsOptimizedPath && voiceState.BiquadFilters[0].Enable && voiceState.BiquadFilters[1].Enable)
  124. {
  125. Memory<byte> biquadStateRawMemory = SpanMemoryManager<byte>.Cast(state).Slice(VoiceUpdateState.BiquadStateOffset, VoiceUpdateState.BiquadStateSize * Constants.VoiceBiquadFilterCount);
  126. Memory<BiquadFilterState> stateMemory = SpanMemoryManager<BiquadFilterState>.Cast(biquadStateRawMemory);
  127. _commandBuffer.GenerateGroupedBiquadFilter(baseIndex, voiceState.BiquadFilters.AsSpan(), stateMemory, bufferOffset, bufferOffset, voiceState.BiquadFilterNeedInitialization, nodeId);
  128. }
  129. else
  130. {
  131. for (int i = 0; i < voiceState.BiquadFilters.Length; i++)
  132. {
  133. ref BiquadFilterParameter filter = ref voiceState.BiquadFilters[i];
  134. if (filter.Enable)
  135. {
  136. Memory<byte> biquadStateRawMemory = SpanMemoryManager<byte>.Cast(state).Slice(VoiceUpdateState.BiquadStateOffset, VoiceUpdateState.BiquadStateSize * Constants.VoiceBiquadFilterCount);
  137. Memory<BiquadFilterState> stateMemory = SpanMemoryManager<BiquadFilterState>.Cast(biquadStateRawMemory);
  138. _commandBuffer.GenerateBiquadFilter(baseIndex,
  139. ref filter,
  140. stateMemory.Slice(i, 1),
  141. bufferOffset,
  142. bufferOffset,
  143. !voiceState.BiquadFilterNeedInitialization[i],
  144. nodeId);
  145. }
  146. }
  147. }
  148. }
  149. private void GenerateVoiceMix(Span<float> mixVolumes, Span<float> previousMixVolumes, Memory<VoiceUpdateState> state, uint bufferOffset, uint bufferCount, uint bufferIndex, int nodeId)
  150. {
  151. if (bufferCount > Constants.VoiceChannelCountMax)
  152. {
  153. _commandBuffer.GenerateMixRampGrouped(bufferCount,
  154. bufferIndex,
  155. bufferOffset,
  156. previousMixVolumes,
  157. mixVolumes,
  158. state,
  159. nodeId);
  160. }
  161. else
  162. {
  163. for (int i = 0; i < bufferCount; i++)
  164. {
  165. float previousMixVolume = previousMixVolumes[i];
  166. float mixVolume = mixVolumes[i];
  167. if (mixVolume != 0.0f || previousMixVolume != 0.0f)
  168. {
  169. _commandBuffer.GenerateMixRamp(previousMixVolume,
  170. mixVolume,
  171. bufferIndex,
  172. bufferOffset + (uint)i,
  173. i,
  174. state,
  175. nodeId);
  176. }
  177. }
  178. }
  179. }
  180. private void GenerateVoice(ref VoiceState voiceState)
  181. {
  182. int nodeId = voiceState.NodeId;
  183. uint channelsCount = voiceState.ChannelsCount;
  184. for (int channelIndex = 0; channelIndex < channelsCount; channelIndex++)
  185. {
  186. Memory<VoiceUpdateState> dspStateMemory = _voiceContext.GetUpdateStateForDsp(voiceState.ChannelResourceIds[channelIndex]);
  187. ref VoiceChannelResource channelResource = ref _voiceContext.GetChannelResource(voiceState.ChannelResourceIds[channelIndex]);
  188. PerformanceDetailType dataSourceDetailType = PerformanceDetailType.Adpcm;
  189. if (voiceState.SampleFormat == SampleFormat.PcmInt16)
  190. {
  191. dataSourceDetailType = PerformanceDetailType.PcmInt16;
  192. }
  193. else if (voiceState.SampleFormat == SampleFormat.PcmFloat)
  194. {
  195. dataSourceDetailType = PerformanceDetailType.PcmFloat;
  196. }
  197. bool performanceInitialized = false;
  198. PerformanceEntryAddresses performanceEntry = new PerformanceEntryAddresses();
  199. if (_performanceManager != null && _performanceManager.IsTargetNodeId(nodeId) && _performanceManager.GetNextEntry(out performanceEntry, dataSourceDetailType, PerformanceEntryType.Voice, nodeId))
  200. {
  201. performanceInitialized = true;
  202. GeneratePerformance(ref performanceEntry, PerformanceCommand.Type.Start, nodeId);
  203. }
  204. GenerateDataSource(ref voiceState, dspStateMemory, channelIndex);
  205. if (performanceInitialized)
  206. {
  207. GeneratePerformance(ref performanceEntry, PerformanceCommand.Type.End, nodeId);
  208. }
  209. if (voiceState.WasPlaying)
  210. {
  211. voiceState.PreviousVolume = 0.0f;
  212. }
  213. else if (voiceState.HasAnyDestination())
  214. {
  215. performanceInitialized = false;
  216. if (_performanceManager != null && _performanceManager.IsTargetNodeId(nodeId) && _performanceManager.GetNextEntry(out performanceEntry, PerformanceDetailType.BiquadFilter, PerformanceEntryType.Voice, nodeId))
  217. {
  218. performanceInitialized = true;
  219. GeneratePerformance(ref performanceEntry, PerformanceCommand.Type.Start, nodeId);
  220. }
  221. GenerateBiquadFilterForVoice(ref voiceState, dspStateMemory, (int)_rendererContext.MixBufferCount, channelIndex, nodeId);
  222. if (performanceInitialized)
  223. {
  224. GeneratePerformance(ref performanceEntry, PerformanceCommand.Type.End, nodeId);
  225. }
  226. performanceInitialized = false;
  227. if (_performanceManager != null && _performanceManager.IsTargetNodeId(nodeId) && _performanceManager.GetNextEntry(out performanceEntry, PerformanceDetailType.VolumeRamp, PerformanceEntryType.Voice, nodeId))
  228. {
  229. performanceInitialized = true;
  230. GeneratePerformance(ref performanceEntry, PerformanceCommand.Type.Start, nodeId);
  231. }
  232. _commandBuffer.GenerateVolumeRamp(voiceState.PreviousVolume,
  233. voiceState.Volume,
  234. _rendererContext.MixBufferCount + (uint)channelIndex,
  235. nodeId);
  236. if (performanceInitialized)
  237. {
  238. GeneratePerformance(ref performanceEntry, PerformanceCommand.Type.End, nodeId);
  239. }
  240. voiceState.PreviousVolume = voiceState.Volume;
  241. if (voiceState.MixId == Constants.UnusedMixId)
  242. {
  243. if (voiceState.SplitterId != Constants.UnusedSplitterId)
  244. {
  245. int destinationId = channelIndex;
  246. while (true)
  247. {
  248. Span<SplitterDestination> destinationSpan = _splitterContext.GetDestination((int)voiceState.SplitterId, destinationId);
  249. if (destinationSpan.IsEmpty)
  250. {
  251. break;
  252. }
  253. ref SplitterDestination destination = ref destinationSpan[0];
  254. destinationId += (int)channelsCount;
  255. if (destination.IsConfigured())
  256. {
  257. int mixId = destination.DestinationId;
  258. if (mixId < _mixContext.GetCount() && mixId != Constants.UnusedSplitterIdInt)
  259. {
  260. ref MixState mix = ref _mixContext.GetState(mixId);
  261. GenerateVoiceMix(destination.MixBufferVolume,
  262. destination.PreviousMixBufferVolume,
  263. dspStateMemory,
  264. mix.BufferOffset,
  265. mix.BufferCount,
  266. _rendererContext.MixBufferCount + (uint)channelIndex,
  267. nodeId);
  268. destination.MarkAsNeedToUpdateInternalState();
  269. }
  270. }
  271. }
  272. }
  273. }
  274. else
  275. {
  276. ref MixState mix = ref _mixContext.GetState(voiceState.MixId);
  277. performanceInitialized = false;
  278. if (_performanceManager != null && _performanceManager.IsTargetNodeId(nodeId) && _performanceManager.GetNextEntry(out performanceEntry, PerformanceDetailType.Mix, PerformanceEntryType.Voice, nodeId))
  279. {
  280. performanceInitialized = true;
  281. GeneratePerformance(ref performanceEntry, PerformanceCommand.Type.Start, nodeId);
  282. }
  283. GenerateVoiceMix(channelResource.Mix.AsSpan(),
  284. channelResource.PreviousMix.AsSpan(),
  285. dspStateMemory,
  286. mix.BufferOffset,
  287. mix.BufferCount,
  288. _rendererContext.MixBufferCount + (uint)channelIndex,
  289. nodeId);
  290. if (performanceInitialized)
  291. {
  292. GeneratePerformance(ref performanceEntry, PerformanceCommand.Type.End, nodeId);
  293. }
  294. channelResource.UpdateState();
  295. }
  296. for (int i = 0; i < voiceState.BiquadFilterNeedInitialization.Length; i++)
  297. {
  298. voiceState.BiquadFilterNeedInitialization[i] = voiceState.BiquadFilters[i].Enable;
  299. }
  300. }
  301. }
  302. }
  303. public void GenerateVoices()
  304. {
  305. for (int i = 0; i < _voiceContext.GetCount(); i++)
  306. {
  307. ref VoiceState sortedState = ref _voiceContext.GetSortedState(i);
  308. if (!sortedState.ShouldSkip() && sortedState.UpdateForCommandGeneration(_voiceContext))
  309. {
  310. int nodeId = sortedState.NodeId;
  311. PerformanceEntryAddresses performanceEntry = new PerformanceEntryAddresses();
  312. bool performanceInitialized = false;
  313. if (_performanceManager != null && _performanceManager.GetNextEntry(out performanceEntry, PerformanceEntryType.Voice, nodeId))
  314. {
  315. performanceInitialized = true;
  316. GeneratePerformance(ref performanceEntry, PerformanceCommand.Type.Start, nodeId);
  317. }
  318. GenerateVoice(ref sortedState);
  319. if (performanceInitialized)
  320. {
  321. GeneratePerformance(ref performanceEntry, PerformanceCommand.Type.End, nodeId);
  322. }
  323. }
  324. }
  325. _splitterContext.UpdateInternalState();
  326. }
  327. public void GeneratePerformance(ref PerformanceEntryAddresses performanceEntryAddresses, PerformanceCommand.Type type, int nodeId)
  328. {
  329. _commandBuffer.GeneratePerformance(ref performanceEntryAddresses, type, nodeId);
  330. }
  331. private void GenerateBufferMixerEffect(int bufferOffset, BufferMixEffect effect, int nodeId)
  332. {
  333. Debug.Assert(effect.Type == EffectType.BufferMix);
  334. if (effect.IsEnabled)
  335. {
  336. for (int i = 0; i < effect.Parameter.MixesCount; i++)
  337. {
  338. if (effect.Parameter.Volumes[i] != 0.0f)
  339. {
  340. _commandBuffer.GenerateMix((uint)bufferOffset + effect.Parameter.Input[i],
  341. (uint)bufferOffset + effect.Parameter.Output[i],
  342. nodeId,
  343. effect.Parameter.Volumes[i]);
  344. }
  345. }
  346. }
  347. }
  348. private void GenerateAuxEffect(uint bufferOffset, AuxiliaryBufferEffect effect, int nodeId)
  349. {
  350. Debug.Assert(effect.Type == EffectType.AuxiliaryBuffer);
  351. if (effect.IsEnabled)
  352. {
  353. effect.GetWorkBuffer(0);
  354. effect.GetWorkBuffer(1);
  355. }
  356. if (effect.State.SendBufferInfoBase != 0 && effect.State.ReturnBufferInfoBase != 0)
  357. {
  358. int i = 0;
  359. uint writeOffset = 0;
  360. for (uint channelIndex = effect.Parameter.ChannelCount; channelIndex != 0; channelIndex--)
  361. {
  362. uint newUpdateCount = writeOffset + _commandBuffer.CommandList.SampleCount;
  363. uint updateCount;
  364. if (channelIndex != 1)
  365. {
  366. updateCount = 0;
  367. }
  368. else
  369. {
  370. updateCount = newUpdateCount;
  371. }
  372. _commandBuffer.GenerateAuxEffect(bufferOffset,
  373. effect.Parameter.Input[i],
  374. effect.Parameter.Output[i],
  375. ref effect.State,
  376. effect.IsEnabled,
  377. effect.Parameter.BufferStorageSize,
  378. effect.State.SendBufferInfoBase,
  379. effect.State.ReturnBufferInfoBase,
  380. updateCount,
  381. writeOffset,
  382. nodeId);
  383. writeOffset = newUpdateCount;
  384. i++;
  385. }
  386. }
  387. }
  388. private void GenerateDelayEffect(uint bufferOffset, DelayEffect effect, int nodeId, bool newEffectChannelMappingSupported)
  389. {
  390. Debug.Assert(effect.Type == EffectType.Delay);
  391. ulong workBuffer = effect.GetWorkBuffer(-1);
  392. _commandBuffer.GenerateDelayEffect(bufferOffset, effect.Parameter, effect.State, effect.IsEnabled, workBuffer, nodeId, newEffectChannelMappingSupported);
  393. }
  394. private void GenerateReverbEffect(uint bufferOffset, ReverbEffect effect, int nodeId, bool isLongSizePreDelaySupported, bool newEffectChannelMappingSupported)
  395. {
  396. Debug.Assert(effect.Type == EffectType.Reverb);
  397. ulong workBuffer = effect.GetWorkBuffer(-1);
  398. _commandBuffer.GenerateReverbEffect(bufferOffset, effect.Parameter, effect.State, effect.IsEnabled, workBuffer, nodeId, isLongSizePreDelaySupported, newEffectChannelMappingSupported);
  399. }
  400. private void GenerateReverb3dEffect(uint bufferOffset, Reverb3dEffect effect, int nodeId, bool newEffectChannelMappingSupported)
  401. {
  402. Debug.Assert(effect.Type == EffectType.Reverb3d);
  403. ulong workBuffer = effect.GetWorkBuffer(-1);
  404. _commandBuffer.GenerateReverb3dEffect(bufferOffset, effect.Parameter, effect.State, effect.IsEnabled, workBuffer, nodeId, newEffectChannelMappingSupported);
  405. }
  406. private void GenerateBiquadFilterEffect(uint bufferOffset, BiquadFilterEffect effect, int nodeId)
  407. {
  408. Debug.Assert(effect.Type == EffectType.BiquadFilter);
  409. if (effect.IsEnabled)
  410. {
  411. bool needInitialization = effect.Parameter.Status == UsageState.Invalid ||
  412. (effect.Parameter.Status == UsageState.New && !_rendererContext.BehaviourContext.IsBiquadFilterEffectStateClearBugFixed());
  413. BiquadFilterParameter parameter = new BiquadFilterParameter();
  414. parameter.Enable = true;
  415. effect.Parameter.Denominator.AsSpan().CopyTo(parameter.Denominator.AsSpan());
  416. effect.Parameter.Numerator.AsSpan().CopyTo(parameter.Numerator.AsSpan());
  417. for (int i = 0; i < effect.Parameter.ChannelCount; i++)
  418. {
  419. _commandBuffer.GenerateBiquadFilter((int)bufferOffset, ref parameter, effect.State.Slice(i, 1),
  420. effect.Parameter.Input[i],
  421. effect.Parameter.Output[i],
  422. needInitialization,
  423. nodeId);
  424. }
  425. }
  426. else
  427. {
  428. for (int i = 0; i < effect.Parameter.ChannelCount; i++)
  429. {
  430. uint inputBufferIndex = bufferOffset + effect.Parameter.Input[i];
  431. uint outputBufferIndex = bufferOffset + effect.Parameter.Output[i];
  432. // If the input and output isn't the same, generate a command.
  433. if (inputBufferIndex != outputBufferIndex)
  434. {
  435. _commandBuffer.GenerateCopyMixBuffer(inputBufferIndex, outputBufferIndex, nodeId);
  436. }
  437. }
  438. }
  439. }
  440. private void GenerateLimiterEffect(uint bufferOffset, LimiterEffect effect, int nodeId, int effectId)
  441. {
  442. Debug.Assert(effect.Type == EffectType.Limiter);
  443. ulong workBuffer = effect.GetWorkBuffer(-1);
  444. if (_rendererContext.BehaviourContext.IsEffectInfoVersion2Supported())
  445. {
  446. Memory<EffectResultState> dspResultState;
  447. if (effect.Parameter.StatisticsEnabled)
  448. {
  449. dspResultState = _effectContext.GetDspStateMemory(effectId);
  450. }
  451. else
  452. {
  453. dspResultState = Memory<EffectResultState>.Empty;
  454. }
  455. _commandBuffer.GenerateLimiterEffectVersion2(bufferOffset, effect.Parameter, effect.State, dspResultState, effect.IsEnabled, workBuffer, nodeId);
  456. }
  457. else
  458. {
  459. _commandBuffer.GenerateLimiterEffectVersion1(bufferOffset, effect.Parameter, effect.State, effect.IsEnabled, workBuffer, nodeId);
  460. }
  461. }
  462. private void GenerateCaptureEffect(uint bufferOffset, CaptureBufferEffect effect, int nodeId)
  463. {
  464. Debug.Assert(effect.Type == EffectType.CaptureBuffer);
  465. if (effect.IsEnabled)
  466. {
  467. effect.GetWorkBuffer(0);
  468. }
  469. if (effect.State.SendBufferInfoBase != 0)
  470. {
  471. int i = 0;
  472. uint writeOffset = 0;
  473. for (uint channelIndex = effect.Parameter.ChannelCount; channelIndex != 0; channelIndex--)
  474. {
  475. uint newUpdateCount = writeOffset + _commandBuffer.CommandList.SampleCount;
  476. uint updateCount;
  477. if (channelIndex != 1)
  478. {
  479. updateCount = 0;
  480. }
  481. else
  482. {
  483. updateCount = newUpdateCount;
  484. }
  485. _commandBuffer.GenerateCaptureEffect(bufferOffset,
  486. effect.Parameter.Input[i],
  487. effect.State.SendBufferInfo,
  488. effect.IsEnabled,
  489. effect.Parameter.BufferStorageSize,
  490. effect.State.SendBufferInfoBase,
  491. updateCount,
  492. writeOffset,
  493. nodeId);
  494. writeOffset = newUpdateCount;
  495. i++;
  496. }
  497. }
  498. }
  499. private void GenerateEffect(ref MixState mix, int effectId, BaseEffect effect)
  500. {
  501. int nodeId = mix.NodeId;
  502. bool isFinalMix = mix.MixId == Constants.FinalMixId;
  503. PerformanceEntryAddresses performanceEntry = new PerformanceEntryAddresses();
  504. bool performanceInitialized = false;
  505. if (_performanceManager != null && _performanceManager.GetNextEntry(out performanceEntry, effect.GetPerformanceDetailType(),
  506. isFinalMix ? PerformanceEntryType.FinalMix : PerformanceEntryType.SubMix, nodeId))
  507. {
  508. performanceInitialized = true;
  509. GeneratePerformance(ref performanceEntry, PerformanceCommand.Type.Start, nodeId);
  510. }
  511. switch (effect.Type)
  512. {
  513. case EffectType.BufferMix:
  514. GenerateBufferMixerEffect((int)mix.BufferOffset, (BufferMixEffect)effect, nodeId);
  515. break;
  516. case EffectType.AuxiliaryBuffer:
  517. GenerateAuxEffect(mix.BufferOffset, (AuxiliaryBufferEffect)effect, nodeId);
  518. break;
  519. case EffectType.Delay:
  520. GenerateDelayEffect(mix.BufferOffset, (DelayEffect)effect, nodeId, _rendererContext.BehaviourContext.IsNewEffectChannelMappingSupported());
  521. break;
  522. case EffectType.Reverb:
  523. GenerateReverbEffect(mix.BufferOffset, (ReverbEffect)effect, nodeId, mix.IsLongSizePreDelaySupported, _rendererContext.BehaviourContext.IsNewEffectChannelMappingSupported());
  524. break;
  525. case EffectType.Reverb3d:
  526. GenerateReverb3dEffect(mix.BufferOffset, (Reverb3dEffect)effect, nodeId, _rendererContext.BehaviourContext.IsNewEffectChannelMappingSupported());
  527. break;
  528. case EffectType.BiquadFilter:
  529. GenerateBiquadFilterEffect(mix.BufferOffset, (BiquadFilterEffect)effect, nodeId);
  530. break;
  531. case EffectType.Limiter:
  532. GenerateLimiterEffect(mix.BufferOffset, (LimiterEffect)effect, nodeId, effectId);
  533. break;
  534. case EffectType.CaptureBuffer:
  535. GenerateCaptureEffect(mix.BufferOffset, (CaptureBufferEffect)effect, nodeId);
  536. break;
  537. default:
  538. throw new NotImplementedException($"Unsupported effect type {effect.Type}");
  539. }
  540. if (performanceInitialized)
  541. {
  542. GeneratePerformance(ref performanceEntry, PerformanceCommand.Type.End, nodeId);
  543. }
  544. effect.UpdateForCommandGeneration();
  545. }
  546. private void GenerateEffects(ref MixState mix)
  547. {
  548. ReadOnlySpan<int> effectProcessingOrderArray = mix.EffectProcessingOrderArray;
  549. Debug.Assert(_effectContext.GetCount() == 0 || !effectProcessingOrderArray.IsEmpty);
  550. for (int i = 0; i < _effectContext.GetCount(); i++)
  551. {
  552. int effectOrder = effectProcessingOrderArray[i];
  553. if (effectOrder == Constants.InvalidProcessingOrder)
  554. {
  555. break;
  556. }
  557. // BaseEffect is a class, we don't need to pass it by ref
  558. BaseEffect effect = _effectContext.GetEffect(effectOrder);
  559. Debug.Assert(effect.Type != EffectType.Invalid);
  560. Debug.Assert(effect.MixId == mix.MixId);
  561. if (!effect.ShouldSkip())
  562. {
  563. GenerateEffect(ref mix, effectOrder, effect);
  564. }
  565. }
  566. }
  567. private void GenerateMix(ref MixState mix)
  568. {
  569. if (mix.HasAnyDestination())
  570. {
  571. Debug.Assert(mix.DestinationMixId != Constants.UnusedMixId || mix.DestinationSplitterId != Constants.UnusedSplitterId);
  572. if (mix.DestinationMixId == Constants.UnusedMixId)
  573. {
  574. if (mix.DestinationSplitterId != Constants.UnusedSplitterId)
  575. {
  576. int destinationId = 0;
  577. while (true)
  578. {
  579. int destinationIndex = destinationId++;
  580. Span<SplitterDestination> destinationSpan = _splitterContext.GetDestination((int)mix.DestinationSplitterId, destinationIndex);
  581. if (destinationSpan.IsEmpty)
  582. {
  583. break;
  584. }
  585. ref SplitterDestination destination = ref destinationSpan[0];
  586. if (destination.IsConfigured())
  587. {
  588. int mixId = destination.DestinationId;
  589. if (mixId < _mixContext.GetCount() && mixId != Constants.UnusedSplitterIdInt)
  590. {
  591. ref MixState destinationMix = ref _mixContext.GetState(mixId);
  592. uint inputBufferIndex = mix.BufferOffset + ((uint)destinationIndex % mix.BufferCount);
  593. for (uint bufferDestinationIndex = 0; bufferDestinationIndex < destinationMix.BufferCount; bufferDestinationIndex++)
  594. {
  595. float volume = mix.Volume * destination.GetMixVolume((int)bufferDestinationIndex);
  596. if (volume != 0.0f)
  597. {
  598. _commandBuffer.GenerateMix(inputBufferIndex,
  599. destinationMix.BufferOffset + bufferDestinationIndex,
  600. mix.NodeId,
  601. volume);
  602. }
  603. }
  604. }
  605. }
  606. }
  607. }
  608. }
  609. else
  610. {
  611. ref MixState destinationMix = ref _mixContext.GetState(mix.DestinationMixId);
  612. for (uint bufferIndex = 0; bufferIndex < mix.BufferCount; bufferIndex++)
  613. {
  614. for (uint bufferDestinationIndex = 0; bufferDestinationIndex < destinationMix.BufferCount; bufferDestinationIndex++)
  615. {
  616. float volume = mix.Volume * mix.GetMixBufferVolume((int)bufferIndex, (int)bufferDestinationIndex);
  617. if (volume != 0.0f)
  618. {
  619. _commandBuffer.GenerateMix(mix.BufferOffset + bufferIndex,
  620. destinationMix.BufferOffset + bufferDestinationIndex,
  621. mix.NodeId,
  622. volume);
  623. }
  624. }
  625. }
  626. }
  627. }
  628. }
  629. private void GenerateSubMix(ref MixState subMix)
  630. {
  631. _commandBuffer.GenerateDepopForMixBuffersCommand(_rendererContext.DepopBuffer,
  632. subMix.BufferOffset,
  633. subMix.BufferCount,
  634. subMix.NodeId,
  635. subMix.SampleRate);
  636. GenerateEffects(ref subMix);
  637. PerformanceEntryAddresses performanceEntry = new PerformanceEntryAddresses();
  638. int nodeId = subMix.NodeId;
  639. bool performanceInitialized = false;
  640. if (_performanceManager != null && _performanceManager.IsTargetNodeId(nodeId) && _performanceManager.GetNextEntry(out performanceEntry, PerformanceDetailType.Mix, PerformanceEntryType.SubMix, nodeId))
  641. {
  642. performanceInitialized = true;
  643. GeneratePerformance(ref performanceEntry, PerformanceCommand.Type.Start, nodeId);
  644. }
  645. GenerateMix(ref subMix);
  646. if (performanceInitialized)
  647. {
  648. GeneratePerformance(ref performanceEntry, PerformanceCommand.Type.End, nodeId);
  649. }
  650. }
  651. public void GenerateSubMixes()
  652. {
  653. for (int id = 0; id < _mixContext.GetCount(); id++)
  654. {
  655. ref MixState sortedState = ref _mixContext.GetSortedState(id);
  656. if (sortedState.IsUsed && sortedState.MixId != Constants.FinalMixId)
  657. {
  658. int nodeId = sortedState.NodeId;
  659. PerformanceEntryAddresses performanceEntry = new PerformanceEntryAddresses();
  660. bool performanceInitialized = false;
  661. if (_performanceManager != null && _performanceManager.GetNextEntry(out performanceEntry, PerformanceEntryType.SubMix, nodeId))
  662. {
  663. performanceInitialized = true;
  664. GeneratePerformance(ref performanceEntry, PerformanceCommand.Type.Start, nodeId);
  665. }
  666. GenerateSubMix(ref sortedState);
  667. if (performanceInitialized)
  668. {
  669. GeneratePerformance(ref performanceEntry, PerformanceCommand.Type.End, nodeId);
  670. }
  671. }
  672. }
  673. }
  674. private void GenerateFinalMix()
  675. {
  676. ref MixState finalMix = ref _mixContext.GetFinalState();
  677. _commandBuffer.GenerateDepopForMixBuffersCommand(_rendererContext.DepopBuffer,
  678. finalMix.BufferOffset,
  679. finalMix.BufferCount,
  680. finalMix.NodeId,
  681. finalMix.SampleRate);
  682. GenerateEffects(ref finalMix);
  683. PerformanceEntryAddresses performanceEntry = new PerformanceEntryAddresses();
  684. int nodeId = finalMix.NodeId;
  685. bool performanceInitialized = false;
  686. if (_performanceManager != null && _performanceManager.IsTargetNodeId(nodeId) && _performanceManager.GetNextEntry(out performanceEntry, PerformanceDetailType.Mix, PerformanceEntryType.FinalMix, nodeId))
  687. {
  688. performanceInitialized = true;
  689. GeneratePerformance(ref performanceEntry, PerformanceCommand.Type.Start, nodeId);
  690. }
  691. // Only generate volume command if the volume isn't 100%.
  692. if (finalMix.Volume != 1.0f)
  693. {
  694. for (uint bufferIndex = 0; bufferIndex < finalMix.BufferCount; bufferIndex++)
  695. {
  696. bool performanceSubInitialized = false;
  697. if (_performanceManager != null && _performanceManager.IsTargetNodeId(nodeId) && _performanceManager.GetNextEntry(out performanceEntry, PerformanceDetailType.VolumeRamp, PerformanceEntryType.FinalMix, nodeId))
  698. {
  699. performanceSubInitialized = true;
  700. GeneratePerformance(ref performanceEntry, PerformanceCommand.Type.Start, nodeId);
  701. }
  702. _commandBuffer.GenerateVolume(finalMix.Volume,
  703. finalMix.BufferOffset + bufferIndex,
  704. nodeId);
  705. if (performanceSubInitialized)
  706. {
  707. GeneratePerformance(ref performanceEntry, PerformanceCommand.Type.End, nodeId);
  708. }
  709. }
  710. }
  711. if (performanceInitialized)
  712. {
  713. GeneratePerformance(ref performanceEntry, PerformanceCommand.Type.End, nodeId);
  714. }
  715. }
  716. public void GenerateFinalMixes()
  717. {
  718. int nodeId = _mixContext.GetFinalState().NodeId;
  719. PerformanceEntryAddresses performanceEntry = new PerformanceEntryAddresses();
  720. bool performanceInitialized = false;
  721. if (_performanceManager != null && _performanceManager.GetNextEntry(out performanceEntry, PerformanceEntryType.FinalMix, nodeId))
  722. {
  723. performanceInitialized = true;
  724. GeneratePerformance(ref performanceEntry, PerformanceCommand.Type.Start, nodeId);
  725. }
  726. GenerateFinalMix();
  727. if (performanceInitialized)
  728. {
  729. GeneratePerformance(ref performanceEntry, PerformanceCommand.Type.End, nodeId);
  730. }
  731. }
  732. private void GenerateCircularBuffer(CircularBufferSink sink, ref MixState finalMix)
  733. {
  734. _commandBuffer.GenerateCircularBuffer(finalMix.BufferOffset, sink, Constants.InvalidNodeId);
  735. }
  736. private void GenerateDevice(DeviceSink sink, ref MixState finalMix)
  737. {
  738. if (_commandBuffer.CommandList.SampleRate != 48000 && sink.UpsamplerState == null)
  739. {
  740. sink.UpsamplerState = _rendererContext.UpsamplerManager.Allocate();
  741. }
  742. bool useCustomDownMixingCommand = _rendererContext.ChannelCount == 2 && sink.Parameter.DownMixParameterEnabled;
  743. if (useCustomDownMixingCommand)
  744. {
  745. _commandBuffer.GenerateDownMixSurroundToStereo(finalMix.BufferOffset,
  746. sink.Parameter.Input.AsSpan(),
  747. sink.Parameter.Input.AsSpan(),
  748. sink.DownMixCoefficients,
  749. Constants.InvalidNodeId);
  750. }
  751. // NOTE: We do the downmixing at the DSP level as it's easier that way.
  752. else if (_rendererContext.ChannelCount == 2 && sink.Parameter.InputCount == 6)
  753. {
  754. _commandBuffer.GenerateDownMixSurroundToStereo(finalMix.BufferOffset,
  755. sink.Parameter.Input.AsSpan(),
  756. sink.Parameter.Input.AsSpan(),
  757. Constants.DefaultSurroundToStereoCoefficients,
  758. Constants.InvalidNodeId);
  759. }
  760. CommandList commandList = _commandBuffer.CommandList;
  761. if (sink.UpsamplerState != null)
  762. {
  763. _commandBuffer.GenerateUpsample(finalMix.BufferOffset,
  764. sink.UpsamplerState,
  765. sink.Parameter.InputCount,
  766. sink.Parameter.Input.AsSpan(),
  767. commandList.BufferCount,
  768. commandList.SampleCount,
  769. commandList.SampleRate,
  770. Constants.InvalidNodeId);
  771. }
  772. _commandBuffer.GenerateDeviceSink(finalMix.BufferOffset,
  773. sink,
  774. _rendererContext.SessionId,
  775. commandList.Buffers,
  776. Constants.InvalidNodeId);
  777. }
  778. private void GenerateSink(BaseSink sink, ref MixState finalMix)
  779. {
  780. bool performanceInitialized = false;
  781. PerformanceEntryAddresses performanceEntry = new PerformanceEntryAddresses();
  782. if (_performanceManager != null && _performanceManager.GetNextEntry(out performanceEntry, PerformanceEntryType.Sink, sink.NodeId))
  783. {
  784. performanceInitialized = true;
  785. GeneratePerformance(ref performanceEntry, PerformanceCommand.Type.Start, sink.NodeId);
  786. }
  787. if (!sink.ShouldSkip)
  788. {
  789. switch (sink.Type)
  790. {
  791. case SinkType.CircularBuffer:
  792. GenerateCircularBuffer((CircularBufferSink)sink, ref finalMix);
  793. break;
  794. case SinkType.Device:
  795. GenerateDevice((DeviceSink)sink, ref finalMix);
  796. break;
  797. default:
  798. throw new NotImplementedException($"Unsupported sink type {sink.Type}");
  799. }
  800. sink.UpdateForCommandGeneration();
  801. }
  802. if (performanceInitialized)
  803. {
  804. GeneratePerformance(ref performanceEntry, PerformanceCommand.Type.End, sink.NodeId);
  805. }
  806. }
  807. public void GenerateSinks()
  808. {
  809. ref MixState finalMix = ref _mixContext.GetFinalState();
  810. for (int i = 0; i < _sinkContext.GetCount(); i++)
  811. {
  812. // BaseSink is a class, we don't need to pass it by ref
  813. BaseSink sink = _sinkContext.GetSink(i);
  814. if (sink.IsUsed)
  815. {
  816. GenerateSink(sink, ref finalMix);
  817. }
  818. }
  819. }
  820. }
  821. }