CommandProcessingTimeEstimatorVersion3.cs 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635
  1. //
  2. // Copyright (c) 2019-2020 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 System;
  20. using System.Diagnostics;
  21. using static Ryujinx.Audio.Renderer.Parameter.VoiceInParameter;
  22. namespace Ryujinx.Audio.Renderer.Server
  23. {
  24. /// <summary>
  25. /// <see cref="ICommandProcessingTimeEstimator"/> version 3. (added with REV8)
  26. /// </summary>
  27. public class CommandProcessingTimeEstimatorVersion3 : ICommandProcessingTimeEstimator
  28. {
  29. private uint _sampleCount;
  30. private uint _bufferCount;
  31. public CommandProcessingTimeEstimatorVersion3(uint sampleCount, uint bufferCount)
  32. {
  33. _sampleCount = sampleCount;
  34. _bufferCount = bufferCount;
  35. }
  36. public uint Estimate(PerformanceCommand command)
  37. {
  38. Debug.Assert(_sampleCount == 160 || _sampleCount == 240);
  39. if (_sampleCount == 160)
  40. {
  41. return (uint)498.17f;
  42. }
  43. return (uint)489.42f;
  44. }
  45. public uint Estimate(ClearMixBufferCommand command)
  46. {
  47. Debug.Assert(_sampleCount == 160 || _sampleCount == 240);
  48. float costPerBuffer = 440.68f;
  49. float baseCost = 0;
  50. if (_sampleCount == 160)
  51. {
  52. costPerBuffer = 266.65f;
  53. }
  54. return (uint)(baseCost + costPerBuffer * _bufferCount);
  55. }
  56. public uint Estimate(BiquadFilterCommand command)
  57. {
  58. Debug.Assert(_sampleCount == 160 || _sampleCount == 240);
  59. if (_sampleCount == 160)
  60. {
  61. return (uint)4173.2f;
  62. }
  63. return (uint)5585.1f;
  64. }
  65. public uint Estimate(MixRampGroupedCommand command)
  66. {
  67. float costPerSample = 6.4434f;
  68. Debug.Assert(_sampleCount == 160 || _sampleCount == 240);
  69. if (_sampleCount == 160)
  70. {
  71. costPerSample = 6.708f;
  72. }
  73. int volumeCount = 0;
  74. for (int i = 0; i < command.MixBufferCount; i++)
  75. {
  76. if (command.Volume0[i] != 0.0f || command.Volume1[i] != 0.0f)
  77. {
  78. volumeCount++;
  79. }
  80. }
  81. return (uint)(_sampleCount * costPerSample * volumeCount);
  82. }
  83. public uint Estimate(MixRampCommand command)
  84. {
  85. Debug.Assert(_sampleCount == 160 || _sampleCount == 240);
  86. if (_sampleCount == 160)
  87. {
  88. return (uint)1968.7f;
  89. }
  90. return (uint)2459.4f;
  91. }
  92. public uint Estimate(DepopPrepareCommand command)
  93. {
  94. return 0;
  95. }
  96. public uint Estimate(VolumeRampCommand command)
  97. {
  98. Debug.Assert(_sampleCount == 160 || _sampleCount == 240);
  99. if (_sampleCount == 160)
  100. {
  101. return (uint)1425.3f;
  102. }
  103. return (uint)1700.0f;
  104. }
  105. public uint Estimate(PcmInt16DataSourceCommandVersion1 command)
  106. {
  107. Debug.Assert(_sampleCount == 160 || _sampleCount == 240);
  108. float costPerSample = 710.143f;
  109. float baseCost = 7853.286f;
  110. if (_sampleCount == 160)
  111. {
  112. costPerSample = 427.52f;
  113. baseCost = 6329.442f;
  114. }
  115. return (uint)(baseCost + (costPerSample * (((command.SampleRate / 200.0f) / _sampleCount) * (command.Pitch * 0.000030518f))));
  116. }
  117. public uint Estimate(AdpcmDataSourceCommandVersion1 command)
  118. {
  119. Debug.Assert(_sampleCount == 160 || _sampleCount == 240);
  120. float costPerSample = 3564.1f;
  121. float baseCost = 9736.702f;
  122. if (_sampleCount == 160)
  123. {
  124. costPerSample = 2125.6f;
  125. baseCost = 7913.808f;
  126. }
  127. return (uint)(baseCost + (costPerSample * (((command.SampleRate / 200.0f) / _sampleCount) * (command.Pitch * 0.000030518f))));
  128. }
  129. public uint Estimate(DepopForMixBuffersCommand command)
  130. {
  131. Debug.Assert(_sampleCount == 160 || _sampleCount == 240);
  132. if (_sampleCount == 160)
  133. {
  134. return (uint)739.64f;
  135. }
  136. return (uint)910.97f;
  137. }
  138. public uint Estimate(CopyMixBufferCommand command)
  139. {
  140. Debug.Assert(_sampleCount == 160 || _sampleCount == 240);
  141. if (_sampleCount == 160)
  142. {
  143. return (uint)842.59f;
  144. }
  145. return (uint)986.72f;
  146. }
  147. public uint Estimate(MixCommand command)
  148. {
  149. Debug.Assert(_sampleCount == 160 || _sampleCount == 240);
  150. if (_sampleCount == 160)
  151. {
  152. return (uint)1402.8f;
  153. }
  154. return (uint)1853.2f;
  155. }
  156. public uint Estimate(DelayCommand command)
  157. {
  158. Debug.Assert(_sampleCount == 160 || _sampleCount == 240);
  159. if (_sampleCount == 160)
  160. {
  161. if (command.Enabled)
  162. {
  163. switch (command.Parameter.ChannelCount)
  164. {
  165. case 1:
  166. return (uint)8929.04f;
  167. case 2:
  168. return (uint)25500.75f;
  169. case 4:
  170. return (uint)47759.62f;
  171. case 6:
  172. return (uint)82203.07f;
  173. default:
  174. throw new NotImplementedException($"{command.Parameter.ChannelCount}");
  175. }
  176. }
  177. else
  178. {
  179. switch (command.Parameter.ChannelCount)
  180. {
  181. case 1:
  182. return (uint)1295.20f;
  183. case 2:
  184. return (uint)1213.60f;
  185. case 4:
  186. return (uint)942.03f;
  187. case 6:
  188. return (uint)1001.55f;
  189. default:
  190. throw new NotImplementedException($"{command.Parameter.ChannelCount}");
  191. }
  192. }
  193. }
  194. if (command.Enabled)
  195. {
  196. switch (command.Parameter.ChannelCount)
  197. {
  198. case 1:
  199. return (uint)11941.05f;
  200. case 2:
  201. return (uint)37197.37f;
  202. case 4:
  203. return (uint)69749.84f;
  204. case 6:
  205. return (uint)120042.40f;
  206. default:
  207. throw new NotImplementedException($"{command.Parameter.ChannelCount}");
  208. }
  209. }
  210. else
  211. {
  212. switch (command.Parameter.ChannelCount)
  213. {
  214. case 1:
  215. return (uint)997.67f;
  216. case 2:
  217. return (uint)977.63f;
  218. case 4:
  219. return (uint)792.30f;
  220. case 6:
  221. return (uint)875.43f;
  222. default:
  223. throw new NotImplementedException($"{command.Parameter.ChannelCount}");
  224. }
  225. }
  226. }
  227. public uint Estimate(ReverbCommand command)
  228. {
  229. Debug.Assert(_sampleCount == 160 || _sampleCount == 240);
  230. if (_sampleCount == 160)
  231. {
  232. if (command.Enabled)
  233. {
  234. switch (command.Parameter.ChannelCount)
  235. {
  236. case 1:
  237. return (uint)81475.05f;
  238. case 2:
  239. return (uint)84975.0f;
  240. case 4:
  241. return (uint)91625.15f;
  242. case 6:
  243. return (uint)95332.27f;
  244. default:
  245. throw new NotImplementedException($"{command.Parameter.ChannelCount}");
  246. }
  247. }
  248. else
  249. {
  250. switch (command.Parameter.ChannelCount)
  251. {
  252. case 1:
  253. return (uint)536.30f;
  254. case 2:
  255. return (uint)588.70f;
  256. case 4:
  257. return (uint)643.70f;
  258. case 6:
  259. return (uint)706.0f;
  260. default:
  261. throw new NotImplementedException($"{command.Parameter.ChannelCount}");
  262. }
  263. }
  264. }
  265. if (command.Enabled)
  266. {
  267. switch (command.Parameter.ChannelCount)
  268. {
  269. case 1:
  270. return (uint)120174.47f;
  271. case 2:
  272. return (uint)25262.22f;
  273. case 4:
  274. return (uint)135751.23f;
  275. case 6:
  276. return (uint)141129.23f;
  277. default:
  278. throw new NotImplementedException($"{command.Parameter.ChannelCount}");
  279. }
  280. }
  281. else
  282. {
  283. switch (command.Parameter.ChannelCount)
  284. {
  285. case 1:
  286. return (uint)617.64f;
  287. case 2:
  288. return (uint)659.54f;
  289. case 4:
  290. return (uint)711.43f;
  291. case 6:
  292. return (uint)778.07f;
  293. default:
  294. throw new NotImplementedException($"{command.Parameter.ChannelCount}");
  295. }
  296. }
  297. }
  298. public uint Estimate(Reverb3dCommand command)
  299. {
  300. Debug.Assert(_sampleCount == 160 || _sampleCount == 240);
  301. if (_sampleCount == 160)
  302. {
  303. if (command.Enabled)
  304. {
  305. switch (command.Parameter.ChannelCount)
  306. {
  307. case 1:
  308. return (uint)116754.0f;
  309. case 2:
  310. return (uint)125912.05f;
  311. case 4:
  312. return (uint)146336.03f;
  313. case 6:
  314. return (uint)165812.66f;
  315. default:
  316. throw new NotImplementedException($"{command.Parameter.ChannelCount}");
  317. }
  318. }
  319. else
  320. {
  321. switch (command.Parameter.ChannelCount)
  322. {
  323. case 1:
  324. return (uint)734.0f;
  325. case 2:
  326. return (uint)766.62f;
  327. case 4:
  328. return (uint)797.46f;
  329. case 6:
  330. return (uint)867.43f;
  331. default:
  332. throw new NotImplementedException($"{command.Parameter.ChannelCount}");
  333. }
  334. }
  335. }
  336. if (command.Enabled)
  337. {
  338. switch (command.Parameter.ChannelCount)
  339. {
  340. case 1:
  341. return (uint)170292.34f;
  342. case 2:
  343. return (uint)183875.63f;
  344. case 4:
  345. return (uint)214696.19f;
  346. case 6:
  347. return (uint)243846.77f;
  348. default:
  349. throw new NotImplementedException($"{command.Parameter.ChannelCount}");
  350. }
  351. }
  352. else
  353. {
  354. switch (command.Parameter.ChannelCount)
  355. {
  356. case 1:
  357. return (uint)508.47f;
  358. case 2:
  359. return (uint)582.45f;
  360. case 4:
  361. return (uint)626.42f;
  362. case 6:
  363. return (uint)682.47f;
  364. default:
  365. throw new NotImplementedException($"{command.Parameter.ChannelCount}");
  366. }
  367. }
  368. }
  369. public uint Estimate(AuxiliaryBufferCommand command)
  370. {
  371. Debug.Assert(_sampleCount == 160 || _sampleCount == 240);
  372. if (_sampleCount == 160)
  373. {
  374. if (command.Enabled)
  375. {
  376. return (uint)7182.14f;
  377. }
  378. return (uint)472.11f;
  379. }
  380. if (command.Enabled)
  381. {
  382. return (uint)9435.96f;
  383. }
  384. return (uint)462.62f;
  385. }
  386. public uint Estimate(VolumeCommand command)
  387. {
  388. Debug.Assert(_sampleCount == 160 || _sampleCount == 240);
  389. if (_sampleCount == 160)
  390. {
  391. return (uint)1311.1f;
  392. }
  393. return (uint)1713.6f;
  394. }
  395. public uint Estimate(CircularBufferSinkCommand command)
  396. {
  397. Debug.Assert(_sampleCount == 160 || _sampleCount == 240);
  398. float costPerBuffer = 770.26f;
  399. float baseCost = 0f;
  400. if (_sampleCount == 160)
  401. {
  402. costPerBuffer = 531.07f;
  403. }
  404. return (uint)(baseCost + costPerBuffer * command.InputCount);
  405. }
  406. public uint Estimate(DownMixSurroundToStereoCommand command)
  407. {
  408. Debug.Assert(_sampleCount == 160 || _sampleCount == 240);
  409. if (_sampleCount == 160)
  410. {
  411. return (uint)9949.7f;
  412. }
  413. return (uint)14679.0f;
  414. }
  415. public uint Estimate(UpsampleCommand command)
  416. {
  417. Debug.Assert(_sampleCount == 160 || _sampleCount == 240);
  418. if (_sampleCount == 160)
  419. {
  420. return (uint)312990.0f;
  421. }
  422. return (uint)0.0f;
  423. }
  424. public uint Estimate(DeviceSinkCommand command)
  425. {
  426. Debug.Assert(_sampleCount == 160 || _sampleCount == 240);
  427. Debug.Assert(command.InputCount == 2 || command.InputCount == 6);
  428. if (command.InputCount == 2)
  429. {
  430. if (_sampleCount == 160)
  431. {
  432. return (uint)8980.0f;
  433. }
  434. return (uint)9221.9f;
  435. }
  436. if (_sampleCount == 160)
  437. {
  438. return (uint)9177.9f;
  439. }
  440. return (uint)9725.9f;
  441. }
  442. public uint Estimate(PcmFloatDataSourceCommandVersion1 command)
  443. {
  444. Debug.Assert(_sampleCount == 160 || _sampleCount == 240);
  445. float costPerSample = 3490.9f;
  446. float baseCost = 10090.9f;
  447. if (_sampleCount == 160)
  448. {
  449. costPerSample = 2310.4f;
  450. baseCost = 7845.25f;
  451. }
  452. return (uint)(baseCost + (costPerSample * (((command.SampleRate / 200.0f) / _sampleCount) * (command.Pitch * 0.000030518f))));
  453. }
  454. public uint Estimate(DataSourceVersion2Command command)
  455. {
  456. Debug.Assert(_sampleCount == 160 || _sampleCount == 240);
  457. (float baseCost, float costPerSample) = GetCostByFormat(_sampleCount, command.SampleFormat, command.SrcQuality);
  458. return (uint)(baseCost + (costPerSample * (((command.SampleRate / 200.0f) / _sampleCount) * (command.Pitch * 0.000030518f) - 1.0f)));
  459. }
  460. private static (float, float) GetCostByFormat(uint sampleCount, SampleFormat format, SampleRateConversionQuality quality)
  461. {
  462. Debug.Assert(sampleCount == 160 || sampleCount == 240);
  463. switch (format)
  464. {
  465. case SampleFormat.PcmInt16:
  466. switch (quality)
  467. {
  468. case SampleRateConversionQuality.Default:
  469. if (sampleCount == 160)
  470. {
  471. return (6329.44f, 427.52f);
  472. }
  473. return (7853.28f, 710.14f);
  474. case SampleRateConversionQuality.High:
  475. if (sampleCount == 160)
  476. {
  477. return (8049.42f, 371.88f);
  478. }
  479. return (10138.84f, 610.49f);
  480. case SampleRateConversionQuality.Low:
  481. if (sampleCount == 160)
  482. {
  483. return (5062.66f, 423.43f);
  484. }
  485. return (5810.96f, 676.72f);
  486. default:
  487. throw new NotImplementedException($"{format} {quality}");
  488. }
  489. case SampleFormat.PcmFloat:
  490. switch (quality)
  491. {
  492. case SampleRateConversionQuality.Default:
  493. if (sampleCount == 160)
  494. {
  495. return (7845.25f, 2310.4f);
  496. }
  497. return (10090.9f, 3490.9f);
  498. case SampleRateConversionQuality.High:
  499. if (sampleCount == 160)
  500. {
  501. return (9446.36f, 2308.91f);
  502. }
  503. return (12520.85f, 3480.61f);
  504. case SampleRateConversionQuality.Low:
  505. if (sampleCount == 160)
  506. {
  507. return (9446.36f, 2308.91f);
  508. }
  509. return (12520.85f, 3480.61f);
  510. default:
  511. throw new NotImplementedException($"{format} {quality}");
  512. }
  513. case SampleFormat.Adpcm:
  514. switch (quality)
  515. {
  516. case SampleRateConversionQuality.Default:
  517. if (sampleCount == 160)
  518. {
  519. return (7913.81f, 1827.66f);
  520. }
  521. return (9736.70f, 2756.37f);
  522. case SampleRateConversionQuality.High:
  523. if (sampleCount == 160)
  524. {
  525. return (9607.81f, 1829.29f);
  526. }
  527. return (12154.38f, 2731.31f);
  528. case SampleRateConversionQuality.Low:
  529. if (sampleCount == 160)
  530. {
  531. return (6517.48f, 1824.61f);
  532. }
  533. return (7929.44f, 2732.15f);
  534. default:
  535. throw new NotImplementedException($"{format} {quality}");
  536. }
  537. default:
  538. throw new NotImplementedException($"{format}");
  539. }
  540. }
  541. }
  542. }