CommandProcessingTimeEstimatorVersion2.cs 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544
  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.Dsp.Command;
  18. using System;
  19. using System.Diagnostics;
  20. namespace Ryujinx.Audio.Renderer.Server
  21. {
  22. /// <summary>
  23. /// <see cref="ICommandProcessingTimeEstimator"/> version 2. (added with REV5)
  24. /// </summary>
  25. public class CommandProcessingTimeEstimatorVersion2 : ICommandProcessingTimeEstimator
  26. {
  27. private uint _sampleCount;
  28. private uint _bufferCount;
  29. public CommandProcessingTimeEstimatorVersion2(uint sampleCount, uint bufferCount)
  30. {
  31. _sampleCount = sampleCount;
  32. _bufferCount = bufferCount;
  33. }
  34. public uint Estimate(PerformanceCommand command)
  35. {
  36. Debug.Assert(_sampleCount == 160 || _sampleCount == 240);
  37. if (_sampleCount == 160)
  38. {
  39. return (uint)489.35f;
  40. }
  41. return (uint)491.18f;
  42. }
  43. public uint Estimate(ClearMixBufferCommand command)
  44. {
  45. Debug.Assert(_sampleCount == 160 || _sampleCount == 240);
  46. float costPerBuffer = 668.8f;
  47. float baseCost = 193.2f;
  48. if (_sampleCount == 160)
  49. {
  50. costPerBuffer = 260.4f;
  51. baseCost = 139.65f;
  52. }
  53. return (uint)(baseCost + costPerBuffer * _bufferCount);
  54. }
  55. public uint Estimate(BiquadFilterCommand command)
  56. {
  57. Debug.Assert(_sampleCount == 160 || _sampleCount == 240);
  58. if (_sampleCount == 160)
  59. {
  60. return (uint)4813.2f;
  61. }
  62. return (uint)6915.4f;
  63. }
  64. public uint Estimate(MixRampGroupedCommand command)
  65. {
  66. const float costPerSample = 7.245f;
  67. Debug.Assert(_sampleCount == 160 || _sampleCount == 240);
  68. int volumeCount = 0;
  69. for (int i = 0; i < command.MixBufferCount; i++)
  70. {
  71. if (command.Volume0[i] != 0.0f || command.Volume1[i] != 0.0f)
  72. {
  73. volumeCount++;
  74. }
  75. }
  76. return (uint)(_sampleCount * costPerSample * volumeCount);
  77. }
  78. public uint Estimate(MixRampCommand command)
  79. {
  80. Debug.Assert(_sampleCount == 160 || _sampleCount == 240);
  81. if (_sampleCount == 160)
  82. {
  83. return (uint)1859.0f;
  84. }
  85. return (uint)2286.1f;
  86. }
  87. public uint Estimate(DepopPrepareCommand command)
  88. {
  89. Debug.Assert(_sampleCount == 160 || _sampleCount == 240);
  90. if (_sampleCount == 160)
  91. {
  92. return (uint)306.62f;
  93. }
  94. return (uint)293.22f;
  95. }
  96. public uint Estimate(VolumeRampCommand command)
  97. {
  98. Debug.Assert(_sampleCount == 160 || _sampleCount == 240);
  99. if (_sampleCount == 160)
  100. {
  101. return (uint)1403.9f;
  102. }
  103. return (uint)1884.3f;
  104. }
  105. public uint Estimate(PcmInt16DataSourceCommandVersion1 command)
  106. {
  107. Debug.Assert(_sampleCount == 160 || _sampleCount == 240);
  108. float costPerSample = 1195.5f;
  109. float baseCost = 7797.0f;
  110. if (_sampleCount == 160)
  111. {
  112. costPerSample = 749.27f;
  113. baseCost = 6138.9f;
  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 = 6225.5f;
  122. if (_sampleCount == 160)
  123. {
  124. costPerSample = 2125.6f;
  125. baseCost = 9039.5f;
  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)762.96f;
  135. }
  136. return (uint)726.96f;
  137. }
  138. public uint Estimate(CopyMixBufferCommand command)
  139. {
  140. Debug.Assert(_sampleCount == 160 || _sampleCount == 240);
  141. if (_sampleCount == 160)
  142. {
  143. return (uint)836.32f;
  144. }
  145. return (uint)1000.9f;
  146. }
  147. public uint Estimate(MixCommand command)
  148. {
  149. Debug.Assert(_sampleCount == 160 || _sampleCount == 240);
  150. if (_sampleCount == 160)
  151. {
  152. return (uint)1342.2f;
  153. }
  154. return (uint)1833.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)41636.0f;
  167. case 2:
  168. return (uint)97861.0f;
  169. case 4:
  170. return (uint)192520.0f;
  171. case 6:
  172. return (uint)301760.0f;
  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)578.53f;
  183. case 2:
  184. return (uint)663.06f;
  185. case 4:
  186. return (uint)703.98f;
  187. case 6:
  188. return (uint)760.03f;
  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)8770.3f;
  200. case 2:
  201. return (uint)25741.0f;
  202. case 4:
  203. return (uint)47551.0f;
  204. case 6:
  205. return (uint)81629.0f;
  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)521.28f;
  216. case 2:
  217. return (uint)585.4f;
  218. case 4:
  219. return (uint)629.88f;
  220. case 6:
  221. return (uint)713.57f;
  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)97192.0f;
  238. case 2:
  239. return (uint)103280.0f;
  240. case 4:
  241. return (uint)109580.0f;
  242. case 6:
  243. return (uint)115070.0f;
  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)492.01f;
  254. case 2:
  255. return (uint)554.46f;
  256. case 4:
  257. return (uint)595.86f;
  258. case 6:
  259. return (uint)656.62f;
  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)136460.0f;
  271. case 2:
  272. return (uint)145750.0f;
  273. case 4:
  274. return (uint)154800.0f;
  275. case 6:
  276. return (uint)161970.0f;
  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)495.79f;
  287. case 2:
  288. return (uint)527.16f;
  289. case 4:
  290. return (uint)598.75f;
  291. case 6:
  292. return (uint)666.03f;
  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)138840.0f;
  309. case 2:
  310. return (uint)135430.0f;
  311. case 4:
  312. return (uint)199180.0f;
  313. case 6:
  314. return (uint)247350.0f;
  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)718.7f;
  325. case 2:
  326. return (uint)751.3f;
  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)199950.0f;
  342. case 2:
  343. return (uint)195200.0f;
  344. case 4:
  345. return (uint)290580.0f;
  346. case 6:
  347. return (uint)363490.0f;
  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)534.24f;
  358. case 2:
  359. return (uint)570.87f;
  360. case 4:
  361. return (uint)660.93f;
  362. case 6:
  363. return (uint)694.6f;
  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)7177.9f;
  377. }
  378. return (uint)489.16f;
  379. }
  380. if (command.Enabled)
  381. {
  382. return (uint)9499.8f;
  383. }
  384. return (uint)485.56f;
  385. }
  386. public uint Estimate(VolumeCommand command)
  387. {
  388. Debug.Assert(_sampleCount == 160 || _sampleCount == 240);
  389. if (_sampleCount == 160)
  390. {
  391. return (uint)1280.3f;
  392. }
  393. return (uint)1737.8f;
  394. }
  395. public uint Estimate(CircularBufferSinkCommand command)
  396. {
  397. Debug.Assert(_sampleCount == 160 || _sampleCount == 240);
  398. float costPerBuffer = 1726.0f;
  399. float baseCost = 1369.7f;
  400. if (_sampleCount == 160)
  401. {
  402. costPerBuffer = 853.63f;
  403. baseCost = 1284.5f;
  404. }
  405. return (uint)(baseCost + costPerBuffer * command.InputCount);
  406. }
  407. public uint Estimate(DownMixSurroundToStereoCommand command)
  408. {
  409. Debug.Assert(_sampleCount == 160 || _sampleCount == 240);
  410. if (_sampleCount == 160)
  411. {
  412. return (uint)10009.0f;
  413. }
  414. return (uint)14577.0f;
  415. }
  416. public uint Estimate(UpsampleCommand command)
  417. {
  418. Debug.Assert(_sampleCount == 160 || _sampleCount == 240);
  419. if (_sampleCount == 160)
  420. {
  421. return (uint)292000.0f;
  422. }
  423. return (uint)0.0f;
  424. }
  425. public uint Estimate(DeviceSinkCommand command)
  426. {
  427. Debug.Assert(_sampleCount == 160 || _sampleCount == 240);
  428. Debug.Assert(command.InputCount == 2 || command.InputCount == 6);
  429. if (command.InputCount == 2)
  430. {
  431. if (_sampleCount == 160)
  432. {
  433. return (uint)9261.5f;
  434. }
  435. return (uint)9336.1f;
  436. }
  437. if (_sampleCount == 160)
  438. {
  439. return (uint)9111.8f;
  440. }
  441. return (uint)9566.7f;
  442. }
  443. public uint Estimate(PcmFloatDataSourceCommandVersion1 command)
  444. {
  445. // NOTE: This was added between REV7 and REV8 and for some reasons the estimator v2 was changed...
  446. Debug.Assert(_sampleCount == 160 || _sampleCount == 240);
  447. float costPerSample = 3490.9f;
  448. float baseCost = 10091.0f;
  449. if (_sampleCount == 160)
  450. {
  451. costPerSample = 2310.4f;
  452. baseCost = 7845.3f;
  453. }
  454. return (uint)(baseCost + (costPerSample * (((command.SampleRate / 200.0f) / _sampleCount) * (command.Pitch * 0.000030518f))));
  455. }
  456. public uint Estimate(DataSourceVersion2Command command)
  457. {
  458. return 0;
  459. }
  460. }
  461. }