audren_u.cpp 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709
  1. // Copyright 2018 yuzu emulator team
  2. // Licensed under GPLv2 or any later version
  3. // Refer to the license.txt file included.
  4. #include <algorithm>
  5. #include <array>
  6. #include <memory>
  7. #include <string_view>
  8. #include "audio_core/audio_renderer.h"
  9. #include "common/alignment.h"
  10. #include "common/bit_util.h"
  11. #include "common/common_funcs.h"
  12. #include "common/logging/log.h"
  13. #include "common/string_util.h"
  14. #include "core/core.h"
  15. #include "core/hle/ipc_helpers.h"
  16. #include "core/hle/kernel/hle_ipc.h"
  17. #include "core/hle/kernel/k_event.h"
  18. #include "core/hle/kernel/k_readable_event.h"
  19. #include "core/hle/kernel/k_writable_event.h"
  20. #include "core/hle/kernel/kernel.h"
  21. #include "core/hle/service/audio/audren_u.h"
  22. #include "core/hle/service/audio/errors.h"
  23. namespace Service::Audio {
  24. class IAudioRenderer final : public ServiceFramework<IAudioRenderer> {
  25. public:
  26. explicit IAudioRenderer(Core::System& system_,
  27. const AudioCommon::AudioRendererParameter& audren_params,
  28. const std::size_t instance_number)
  29. : ServiceFramework{system_, "IAudioRenderer"}, system_event{system.Kernel()} {
  30. // clang-format off
  31. static const FunctionInfo functions[] = {
  32. {0, &IAudioRenderer::GetSampleRate, "GetSampleRate"},
  33. {1, &IAudioRenderer::GetSampleCount, "GetSampleCount"},
  34. {2, &IAudioRenderer::GetMixBufferCount, "GetMixBufferCount"},
  35. {3, &IAudioRenderer::GetState, "GetState"},
  36. {4, &IAudioRenderer::RequestUpdateImpl, "RequestUpdate"},
  37. {5, &IAudioRenderer::Start, "Start"},
  38. {6, &IAudioRenderer::Stop, "Stop"},
  39. {7, &IAudioRenderer::QuerySystemEvent, "QuerySystemEvent"},
  40. {8, &IAudioRenderer::SetRenderingTimeLimit, "SetRenderingTimeLimit"},
  41. {9, &IAudioRenderer::GetRenderingTimeLimit, "GetRenderingTimeLimit"},
  42. {10, &IAudioRenderer::RequestUpdateImpl, "RequestUpdateAuto"},
  43. {11, &IAudioRenderer::ExecuteAudioRendererRendering, "ExecuteAudioRendererRendering"},
  44. };
  45. // clang-format on
  46. RegisterHandlers(functions);
  47. Kernel::KAutoObject::Create(std::addressof(system_event));
  48. system_event.Initialize("IAudioRenderer:SystemEvent");
  49. renderer = std::make_unique<AudioCore::AudioRenderer>(
  50. system.CoreTiming(), system.Memory(), audren_params,
  51. [this]() {
  52. const auto guard = LockService();
  53. system_event.GetWritableEvent().Signal();
  54. },
  55. instance_number);
  56. }
  57. private:
  58. void GetSampleRate(Kernel::HLERequestContext& ctx) {
  59. LOG_DEBUG(Service_Audio, "called");
  60. IPC::ResponseBuilder rb{ctx, 3};
  61. rb.Push(ResultSuccess);
  62. rb.Push<u32>(renderer->GetSampleRate());
  63. }
  64. void GetSampleCount(Kernel::HLERequestContext& ctx) {
  65. LOG_DEBUG(Service_Audio, "called");
  66. IPC::ResponseBuilder rb{ctx, 3};
  67. rb.Push(ResultSuccess);
  68. rb.Push<u32>(renderer->GetSampleCount());
  69. }
  70. void GetState(Kernel::HLERequestContext& ctx) {
  71. LOG_DEBUG(Service_Audio, "called");
  72. IPC::ResponseBuilder rb{ctx, 3};
  73. rb.Push(ResultSuccess);
  74. rb.Push<u32>(static_cast<u32>(renderer->GetStreamState()));
  75. }
  76. void GetMixBufferCount(Kernel::HLERequestContext& ctx) {
  77. LOG_DEBUG(Service_Audio, "called");
  78. IPC::ResponseBuilder rb{ctx, 3};
  79. rb.Push(ResultSuccess);
  80. rb.Push<u32>(renderer->GetMixBufferCount());
  81. }
  82. void RequestUpdateImpl(Kernel::HLERequestContext& ctx) {
  83. LOG_DEBUG(Service_Audio, "(STUBBED) called");
  84. std::vector<u8> output_params(ctx.GetWriteBufferSize(), 0);
  85. auto result = renderer->UpdateAudioRenderer(ctx.ReadBuffer(), output_params);
  86. if (result.IsSuccess()) {
  87. ctx.WriteBuffer(output_params);
  88. }
  89. IPC::ResponseBuilder rb{ctx, 2};
  90. rb.Push(result);
  91. }
  92. void Start(Kernel::HLERequestContext& ctx) {
  93. LOG_WARNING(Service_Audio, "(STUBBED) called");
  94. const auto result = renderer->Start();
  95. IPC::ResponseBuilder rb{ctx, 2};
  96. rb.Push(result);
  97. }
  98. void Stop(Kernel::HLERequestContext& ctx) {
  99. LOG_WARNING(Service_Audio, "(STUBBED) called");
  100. const auto result = renderer->Stop();
  101. IPC::ResponseBuilder rb{ctx, 2};
  102. rb.Push(result);
  103. }
  104. void QuerySystemEvent(Kernel::HLERequestContext& ctx) {
  105. LOG_WARNING(Service_Audio, "(STUBBED) called");
  106. IPC::ResponseBuilder rb{ctx, 2, 1};
  107. rb.Push(ResultSuccess);
  108. rb.PushCopyObjects(system_event.GetReadableEvent());
  109. }
  110. void SetRenderingTimeLimit(Kernel::HLERequestContext& ctx) {
  111. IPC::RequestParser rp{ctx};
  112. rendering_time_limit_percent = rp.Pop<u32>();
  113. LOG_DEBUG(Service_Audio, "called. rendering_time_limit_percent={}",
  114. rendering_time_limit_percent);
  115. ASSERT(rendering_time_limit_percent <= 100);
  116. IPC::ResponseBuilder rb{ctx, 2};
  117. rb.Push(ResultSuccess);
  118. }
  119. void GetRenderingTimeLimit(Kernel::HLERequestContext& ctx) {
  120. LOG_DEBUG(Service_Audio, "called");
  121. IPC::ResponseBuilder rb{ctx, 3};
  122. rb.Push(ResultSuccess);
  123. rb.Push(rendering_time_limit_percent);
  124. }
  125. void ExecuteAudioRendererRendering(Kernel::HLERequestContext& ctx) {
  126. LOG_DEBUG(Service_Audio, "called");
  127. // This service command currently only reports an unsupported operation
  128. // error code, or aborts. Given that, we just always return an error
  129. // code in this case.
  130. IPC::ResponseBuilder rb{ctx, 2};
  131. rb.Push(ERR_NOT_SUPPORTED);
  132. }
  133. Kernel::KEvent system_event;
  134. std::unique_ptr<AudioCore::AudioRenderer> renderer;
  135. u32 rendering_time_limit_percent = 100;
  136. };
  137. class IAudioDevice final : public ServiceFramework<IAudioDevice> {
  138. public:
  139. explicit IAudioDevice(Core::System& system_, Kernel::KEvent& buffer_event_, u32_le revision_)
  140. : ServiceFramework{system_, "IAudioDevice"}, buffer_event{buffer_event_}, revision{
  141. revision_} {
  142. static const FunctionInfo functions[] = {
  143. {0, &IAudioDevice::ListAudioDeviceName, "ListAudioDeviceName"},
  144. {1, &IAudioDevice::SetAudioDeviceOutputVolume, "SetAudioDeviceOutputVolume"},
  145. {2, &IAudioDevice::GetAudioDeviceOutputVolume, "GetAudioDeviceOutputVolume"},
  146. {3, &IAudioDevice::GetActiveAudioDeviceName, "GetActiveAudioDeviceName"},
  147. {4, &IAudioDevice::QueryAudioDeviceSystemEvent, "QueryAudioDeviceSystemEvent"},
  148. {5, &IAudioDevice::GetActiveChannelCount, "GetActiveChannelCount"},
  149. {6, &IAudioDevice::ListAudioDeviceName, "ListAudioDeviceNameAuto"},
  150. {7, &IAudioDevice::SetAudioDeviceOutputVolume, "SetAudioDeviceOutputVolumeAuto"},
  151. {8, &IAudioDevice::GetAudioDeviceOutputVolume, "GetAudioDeviceOutputVolumeAuto"},
  152. {10, &IAudioDevice::GetActiveAudioDeviceName, "GetActiveAudioDeviceNameAuto"},
  153. {11, &IAudioDevice::QueryAudioDeviceInputEvent, "QueryAudioDeviceInputEvent"},
  154. {12, &IAudioDevice::QueryAudioDeviceOutputEvent, "QueryAudioDeviceOutputEvent"},
  155. {13, nullptr, "GetActiveAudioOutputDeviceName"},
  156. {14, nullptr, "ListAudioOutputDeviceName"},
  157. };
  158. RegisterHandlers(functions);
  159. }
  160. private:
  161. using AudioDeviceName = std::array<char, 256>;
  162. static constexpr std::array<std::string_view, 4> audio_device_names{{
  163. "AudioStereoJackOutput",
  164. "AudioBuiltInSpeakerOutput",
  165. "AudioTvOutput",
  166. "AudioUsbDeviceOutput",
  167. }};
  168. enum class DeviceType {
  169. AHUBHeadphones,
  170. AHUBSpeakers,
  171. HDA,
  172. USBOutput,
  173. };
  174. void ListAudioDeviceName(Kernel::HLERequestContext& ctx) {
  175. LOG_DEBUG(Service_Audio, "called");
  176. const bool usb_output_supported =
  177. IsFeatureSupported(AudioFeatures::AudioUSBDeviceOutput, revision);
  178. const std::size_t count = ctx.GetWriteBufferSize() / sizeof(AudioDeviceName);
  179. std::vector<AudioDeviceName> name_buffer;
  180. name_buffer.reserve(audio_device_names.size());
  181. for (std::size_t i = 0; i < count && i < audio_device_names.size(); i++) {
  182. const auto type = static_cast<DeviceType>(i);
  183. if (!usb_output_supported && type == DeviceType::USBOutput) {
  184. continue;
  185. }
  186. const auto& device_name = audio_device_names[i];
  187. auto& entry = name_buffer.emplace_back();
  188. device_name.copy(entry.data(), device_name.size());
  189. }
  190. ctx.WriteBuffer(name_buffer);
  191. IPC::ResponseBuilder rb{ctx, 3};
  192. rb.Push(ResultSuccess);
  193. rb.Push(static_cast<u32>(name_buffer.size()));
  194. }
  195. void SetAudioDeviceOutputVolume(Kernel::HLERequestContext& ctx) {
  196. IPC::RequestParser rp{ctx};
  197. const f32 volume = rp.Pop<f32>();
  198. const auto device_name_buffer = ctx.ReadBuffer();
  199. const std::string name = Common::StringFromBuffer(device_name_buffer);
  200. LOG_WARNING(Service_Audio, "(STUBBED) called. name={}, volume={}", name, volume);
  201. IPC::ResponseBuilder rb{ctx, 2};
  202. rb.Push(ResultSuccess);
  203. }
  204. void GetAudioDeviceOutputVolume(Kernel::HLERequestContext& ctx) {
  205. const auto device_name_buffer = ctx.ReadBuffer();
  206. const std::string name = Common::StringFromBuffer(device_name_buffer);
  207. LOG_WARNING(Service_Audio, "(STUBBED) called. name={}", name);
  208. IPC::ResponseBuilder rb{ctx, 3};
  209. rb.Push(ResultSuccess);
  210. rb.Push(1.0f);
  211. }
  212. void GetActiveAudioDeviceName(Kernel::HLERequestContext& ctx) {
  213. LOG_WARNING(Service_Audio, "(STUBBED) called");
  214. // Currently set to always be TV audio output.
  215. const auto& device_name = audio_device_names[2];
  216. AudioDeviceName out_device_name{};
  217. device_name.copy(out_device_name.data(), device_name.size());
  218. ctx.WriteBuffer(out_device_name);
  219. IPC::ResponseBuilder rb{ctx, 2};
  220. rb.Push(ResultSuccess);
  221. }
  222. void QueryAudioDeviceSystemEvent(Kernel::HLERequestContext& ctx) {
  223. LOG_WARNING(Service_Audio, "(STUBBED) called");
  224. buffer_event.GetWritableEvent().Signal();
  225. IPC::ResponseBuilder rb{ctx, 2, 1};
  226. rb.Push(ResultSuccess);
  227. rb.PushCopyObjects(buffer_event.GetReadableEvent());
  228. }
  229. void GetActiveChannelCount(Kernel::HLERequestContext& ctx) {
  230. LOG_WARNING(Service_Audio, "(STUBBED) called");
  231. IPC::ResponseBuilder rb{ctx, 3};
  232. rb.Push(ResultSuccess);
  233. rb.Push<u32>(2);
  234. }
  235. // Should be similar to QueryAudioDeviceOutputEvent
  236. void QueryAudioDeviceInputEvent(Kernel::HLERequestContext& ctx) {
  237. LOG_WARNING(Service_Audio, "(STUBBED) called");
  238. IPC::ResponseBuilder rb{ctx, 2, 1};
  239. rb.Push(ResultSuccess);
  240. rb.PushCopyObjects(buffer_event.GetReadableEvent());
  241. }
  242. void QueryAudioDeviceOutputEvent(Kernel::HLERequestContext& ctx) {
  243. LOG_DEBUG(Service_Audio, "called");
  244. IPC::ResponseBuilder rb{ctx, 2, 1};
  245. rb.Push(ResultSuccess);
  246. rb.PushCopyObjects(buffer_event.GetReadableEvent());
  247. }
  248. Kernel::KEvent& buffer_event;
  249. u32_le revision = 0;
  250. };
  251. AudRenU::AudRenU(Core::System& system_)
  252. : ServiceFramework{system_, "audren:u"}, buffer_event{system.Kernel()} {
  253. // clang-format off
  254. static const FunctionInfo functions[] = {
  255. {0, &AudRenU::OpenAudioRenderer, "OpenAudioRenderer"},
  256. {1, &AudRenU::GetAudioRendererWorkBufferSize, "GetWorkBufferSize"},
  257. {2, &AudRenU::GetAudioDeviceService, "GetAudioDeviceService"},
  258. {3, &AudRenU::OpenAudioRendererForManualExecution, "OpenAudioRendererForManualExecution"},
  259. {4, &AudRenU::GetAudioDeviceServiceWithRevisionInfo, "GetAudioDeviceServiceWithRevisionInfo"},
  260. };
  261. // clang-format on
  262. RegisterHandlers(functions);
  263. Kernel::KAutoObject::Create(std::addressof(buffer_event));
  264. buffer_event.Initialize("IAudioOutBufferReleasedEvent");
  265. }
  266. AudRenU::~AudRenU() = default;
  267. void AudRenU::OpenAudioRenderer(Kernel::HLERequestContext& ctx) {
  268. LOG_DEBUG(Service_Audio, "called");
  269. OpenAudioRendererImpl(ctx);
  270. }
  271. static u64 CalculateNumPerformanceEntries(const AudioCommon::AudioRendererParameter& params) {
  272. // +1 represents the final mix.
  273. return u64{params.effect_count} + params.submix_count + params.sink_count + params.voice_count +
  274. 1;
  275. }
  276. void AudRenU::GetAudioRendererWorkBufferSize(Kernel::HLERequestContext& ctx) {
  277. LOG_DEBUG(Service_Audio, "called");
  278. // Several calculations below align the sizes being calculated
  279. // onto a 64 byte boundary.
  280. static constexpr u64 buffer_alignment_size = 64;
  281. // Some calculations that calculate portions of the buffer
  282. // that will contain information, on the other hand, align
  283. // the result of some of their calcularions on a 16 byte boundary.
  284. static constexpr u64 info_field_alignment_size = 16;
  285. // Maximum detail entries that may exist at one time for performance
  286. // frame statistics.
  287. static constexpr u64 max_perf_detail_entries = 100;
  288. // Size of the data structure representing the bulk of the voice-related state.
  289. static constexpr u64 voice_state_size_bytes = 0x100;
  290. // Size of the upsampler manager data structure
  291. constexpr u64 upsampler_manager_size = 0x48;
  292. // Calculates the part of the size that relates to mix buffers.
  293. const auto calculate_mix_buffer_sizes = [](const AudioCommon::AudioRendererParameter& params) {
  294. // As of 8.0.0 this is the maximum on voice channels.
  295. constexpr u64 max_voice_channels = 6;
  296. // The service expects the sample_count member of the parameters to either be
  297. // a value of 160 or 240, so the maximum sample count is assumed in order
  298. // to adequately handle all values at runtime.
  299. constexpr u64 default_max_sample_count = 240;
  300. const u64 total_mix_buffers = params.mix_buffer_count + max_voice_channels;
  301. u64 size = 0;
  302. size += total_mix_buffers * (sizeof(s32) * params.sample_count);
  303. size += total_mix_buffers * (sizeof(s32) * default_max_sample_count);
  304. size += u64{params.submix_count} + params.sink_count;
  305. size = Common::AlignUp(size, buffer_alignment_size);
  306. size += Common::AlignUp(params.unknown_30, buffer_alignment_size);
  307. size += Common::AlignUp(sizeof(s32) * params.mix_buffer_count, buffer_alignment_size);
  308. return size;
  309. };
  310. // Calculates the portion of the size related to the mix data (and the sorting thereof).
  311. const auto calculate_mix_info_size = [](const AudioCommon::AudioRendererParameter& params) {
  312. // The size of the mixing info data structure.
  313. constexpr u64 mix_info_size = 0x940;
  314. // Consists of total submixes with the final mix included.
  315. const u64 total_mix_count = u64{params.submix_count} + 1;
  316. // The total number of effects that may be available to the audio renderer at any time.
  317. constexpr u64 max_effects = 256;
  318. // Calculates the part of the size related to the audio node state.
  319. // This will only be used if the audio revision supports the splitter.
  320. const auto calculate_node_state_size = [](std::size_t num_nodes) {
  321. // Internally within a nodestate, it appears to use a data structure
  322. // similar to a std::bitset<64> twice.
  323. constexpr u64 bit_size = Common::BitSize<u64>();
  324. constexpr u64 num_bitsets = 2;
  325. // Node state instances have three states internally for performing
  326. // depth-first searches of nodes. Initialized, Found, and Done Sorting.
  327. constexpr u64 num_states = 3;
  328. u64 size = 0;
  329. size += (num_nodes * num_nodes) * sizeof(s32);
  330. size += num_states * (num_nodes * sizeof(s32));
  331. size += num_bitsets * (Common::AlignUp(num_nodes, bit_size) / Common::BitSize<u8>());
  332. return size;
  333. };
  334. // Calculates the part of the size related to the adjacency (aka edge) matrix.
  335. const auto calculate_edge_matrix_size = [](std::size_t num_nodes) {
  336. return (num_nodes * num_nodes) * sizeof(s32);
  337. };
  338. u64 size = 0;
  339. size += Common::AlignUp(sizeof(void*) * total_mix_count, info_field_alignment_size);
  340. size += Common::AlignUp(mix_info_size * total_mix_count, info_field_alignment_size);
  341. size += Common::AlignUp(sizeof(s32) * max_effects * params.submix_count,
  342. info_field_alignment_size);
  343. if (IsFeatureSupported(AudioFeatures::Splitter, params.revision)) {
  344. size += Common::AlignUp(calculate_node_state_size(total_mix_count) +
  345. calculate_edge_matrix_size(total_mix_count),
  346. info_field_alignment_size);
  347. }
  348. return size;
  349. };
  350. // Calculates the part of the size related to voice channel info.
  351. const auto calculate_voice_info_size = [](const AudioCommon::AudioRendererParameter& params) {
  352. constexpr u64 voice_info_size = 0x220;
  353. constexpr u64 voice_resource_size = 0xD0;
  354. u64 size = 0;
  355. size += Common::AlignUp(sizeof(void*) * params.voice_count, info_field_alignment_size);
  356. size += Common::AlignUp(voice_info_size * params.voice_count, info_field_alignment_size);
  357. size +=
  358. Common::AlignUp(voice_resource_size * params.voice_count, info_field_alignment_size);
  359. size +=
  360. Common::AlignUp(voice_state_size_bytes * params.voice_count, info_field_alignment_size);
  361. return size;
  362. };
  363. // Calculates the part of the size related to memory pools.
  364. const auto calculate_memory_pools_size = [](const AudioCommon::AudioRendererParameter& params) {
  365. const u64 num_memory_pools = sizeof(s32) * (u64{params.effect_count} + params.voice_count);
  366. const u64 memory_pool_info_size = 0x20;
  367. return Common::AlignUp(num_memory_pools * memory_pool_info_size, info_field_alignment_size);
  368. };
  369. // Calculates the part of the size related to the splitter context.
  370. const auto calculate_splitter_context_size =
  371. [](const AudioCommon::AudioRendererParameter& params) -> u64 {
  372. if (!IsFeatureSupported(AudioFeatures::Splitter, params.revision)) {
  373. return 0;
  374. }
  375. constexpr u64 splitter_info_size = 0x20;
  376. constexpr u64 splitter_destination_data_size = 0xE0;
  377. u64 size = 0;
  378. size += params.num_splitter_send_channels;
  379. size +=
  380. Common::AlignUp(splitter_info_size * params.splitter_count, info_field_alignment_size);
  381. size += Common::AlignUp(splitter_destination_data_size * params.num_splitter_send_channels,
  382. info_field_alignment_size);
  383. return size;
  384. };
  385. // Calculates the part of the size related to the upsampler info.
  386. const auto calculate_upsampler_info_size =
  387. [](const AudioCommon::AudioRendererParameter& params) {
  388. constexpr u64 upsampler_info_size = 0x280;
  389. // Yes, using the buffer size over info alignment size is intentional here.
  390. return Common::AlignUp(upsampler_info_size *
  391. (u64{params.submix_count} + params.sink_count),
  392. buffer_alignment_size);
  393. };
  394. // Calculates the part of the size related to effect info.
  395. const auto calculate_effect_info_size = [](const AudioCommon::AudioRendererParameter& params) {
  396. constexpr u64 effect_info_size = 0x2B0;
  397. return Common::AlignUp(effect_info_size * params.effect_count, info_field_alignment_size);
  398. };
  399. // Calculates the part of the size related to audio sink info.
  400. const auto calculate_sink_info_size = [](const AudioCommon::AudioRendererParameter& params) {
  401. const u64 sink_info_size = 0x170;
  402. return Common::AlignUp(sink_info_size * params.sink_count, info_field_alignment_size);
  403. };
  404. // Calculates the part of the size related to voice state info.
  405. const auto calculate_voice_state_size = [](const AudioCommon::AudioRendererParameter& params) {
  406. const u64 voice_state_size = 0x100;
  407. const u64 additional_size = buffer_alignment_size - 1;
  408. return Common::AlignUp(voice_state_size * params.voice_count + additional_size,
  409. info_field_alignment_size);
  410. };
  411. // Calculates the part of the size related to performance statistics.
  412. const auto calculate_perf_size = [](const AudioCommon::AudioRendererParameter& params) {
  413. // Extra size value appended to the end of the calculation.
  414. constexpr u64 appended = 128;
  415. // Whether or not we assume the newer version of performance metrics data structures.
  416. const bool is_v2 =
  417. IsFeatureSupported(AudioFeatures::PerformanceMetricsVersion2, params.revision);
  418. // Data structure sizes
  419. constexpr u64 perf_statistics_size = 0x0C;
  420. const u64 header_size = is_v2 ? 0x30 : 0x18;
  421. const u64 entry_size = is_v2 ? 0x18 : 0x10;
  422. const u64 detail_size = is_v2 ? 0x18 : 0x10;
  423. const u64 entry_count = CalculateNumPerformanceEntries(params);
  424. const u64 size_per_frame =
  425. header_size + (entry_size * entry_count) + (detail_size * max_perf_detail_entries);
  426. u64 size = 0;
  427. size += Common::AlignUp(size_per_frame * params.performance_frame_count + 1,
  428. buffer_alignment_size);
  429. size += Common::AlignUp(perf_statistics_size, buffer_alignment_size);
  430. size += appended;
  431. return size;
  432. };
  433. // Calculates the part of the size that relates to the audio command buffer.
  434. const auto calculate_command_buffer_size =
  435. [](const AudioCommon::AudioRendererParameter& params) {
  436. constexpr u64 alignment = (buffer_alignment_size - 1) * 2;
  437. if (!IsFeatureSupported(AudioFeatures::VariadicCommandBuffer, params.revision)) {
  438. constexpr u64 command_buffer_size = 0x18000;
  439. return command_buffer_size + alignment;
  440. }
  441. // When the variadic command buffer is supported, this means
  442. // the command generator for the audio renderer can issue commands
  443. // that are (as one would expect), variable in size. So what we need to do
  444. // is determine the maximum possible size for a few command data structures
  445. // then multiply them by the amount of present commands indicated by the given
  446. // respective audio parameters.
  447. constexpr u64 max_biquad_filters = 2;
  448. constexpr u64 max_mix_buffers = 24;
  449. constexpr u64 biquad_filter_command_size = 0x2C;
  450. constexpr u64 depop_mix_command_size = 0x24;
  451. constexpr u64 depop_setup_command_size = 0x50;
  452. constexpr u64 effect_command_max_size = 0x540;
  453. constexpr u64 mix_command_size = 0x1C;
  454. constexpr u64 mix_ramp_command_size = 0x24;
  455. constexpr u64 mix_ramp_grouped_command_size = 0x13C;
  456. constexpr u64 perf_command_size = 0x28;
  457. constexpr u64 sink_command_size = 0x130;
  458. constexpr u64 submix_command_max_size =
  459. depop_mix_command_size + (mix_command_size * max_mix_buffers) * max_mix_buffers;
  460. constexpr u64 volume_command_size = 0x1C;
  461. constexpr u64 volume_ramp_command_size = 0x20;
  462. constexpr u64 voice_biquad_filter_command_size =
  463. biquad_filter_command_size * max_biquad_filters;
  464. constexpr u64 voice_data_command_size = 0x9C;
  465. const u64 voice_command_max_size =
  466. (params.splitter_count * depop_setup_command_size) +
  467. (voice_data_command_size + voice_biquad_filter_command_size +
  468. volume_ramp_command_size + mix_ramp_grouped_command_size);
  469. // Now calculate the individual elements that comprise the size and add them together.
  470. const u64 effect_commands_size = params.effect_count * effect_command_max_size;
  471. const u64 final_mix_commands_size =
  472. depop_mix_command_size + volume_command_size * max_mix_buffers;
  473. const u64 perf_commands_size =
  474. perf_command_size *
  475. (CalculateNumPerformanceEntries(params) + max_perf_detail_entries);
  476. const u64 sink_commands_size = params.sink_count * sink_command_size;
  477. const u64 splitter_commands_size =
  478. params.num_splitter_send_channels * max_mix_buffers * mix_ramp_command_size;
  479. const u64 submix_commands_size = params.submix_count * submix_command_max_size;
  480. const u64 voice_commands_size = params.voice_count * voice_command_max_size;
  481. return effect_commands_size + final_mix_commands_size + perf_commands_size +
  482. sink_commands_size + splitter_commands_size + submix_commands_size +
  483. voice_commands_size + alignment;
  484. };
  485. IPC::RequestParser rp{ctx};
  486. const auto params = rp.PopRaw<AudioCommon::AudioRendererParameter>();
  487. u64 size = 0;
  488. size += calculate_mix_buffer_sizes(params);
  489. size += calculate_mix_info_size(params);
  490. size += calculate_voice_info_size(params);
  491. size += upsampler_manager_size;
  492. size += calculate_memory_pools_size(params);
  493. size += calculate_splitter_context_size(params);
  494. size = Common::AlignUp(size, buffer_alignment_size);
  495. size += calculate_upsampler_info_size(params);
  496. size += calculate_effect_info_size(params);
  497. size += calculate_sink_info_size(params);
  498. size += calculate_voice_state_size(params);
  499. size += calculate_perf_size(params);
  500. size += calculate_command_buffer_size(params);
  501. // finally, 4KB page align the size, and we're done.
  502. size = Common::AlignUp(size, 4096);
  503. IPC::ResponseBuilder rb{ctx, 4};
  504. rb.Push(ResultSuccess);
  505. rb.Push<u64>(size);
  506. LOG_DEBUG(Service_Audio, "buffer_size=0x{:X}", size);
  507. }
  508. void AudRenU::GetAudioDeviceService(Kernel::HLERequestContext& ctx) {
  509. IPC::RequestParser rp{ctx};
  510. const u64 aruid = rp.Pop<u64>();
  511. LOG_DEBUG(Service_Audio, "called. aruid={:016X}", aruid);
  512. // Revisionless variant of GetAudioDeviceServiceWithRevisionInfo that
  513. // always assumes the initial release revision (REV1).
  514. IPC::ResponseBuilder rb{ctx, 2, 0, 1};
  515. rb.Push(ResultSuccess);
  516. rb.PushIpcInterface<IAudioDevice>(system, buffer_event, Common::MakeMagic('R', 'E', 'V', '1'));
  517. }
  518. void AudRenU::OpenAudioRendererForManualExecution(Kernel::HLERequestContext& ctx) {
  519. LOG_DEBUG(Service_Audio, "called");
  520. OpenAudioRendererImpl(ctx);
  521. }
  522. void AudRenU::GetAudioDeviceServiceWithRevisionInfo(Kernel::HLERequestContext& ctx) {
  523. struct Parameters {
  524. u32 revision;
  525. u64 aruid;
  526. };
  527. IPC::RequestParser rp{ctx};
  528. const auto [revision, aruid] = rp.PopRaw<Parameters>();
  529. LOG_DEBUG(Service_Audio, "called. revision={:08X}, aruid={:016X}", revision, aruid);
  530. IPC::ResponseBuilder rb{ctx, 2, 0, 1};
  531. rb.Push(ResultSuccess);
  532. rb.PushIpcInterface<IAudioDevice>(system, buffer_event, revision);
  533. }
  534. void AudRenU::OpenAudioRendererImpl(Kernel::HLERequestContext& ctx) {
  535. IPC::RequestParser rp{ctx};
  536. const auto params = rp.PopRaw<AudioCommon::AudioRendererParameter>();
  537. IPC::ResponseBuilder rb{ctx, 2, 0, 1};
  538. rb.Push(ResultSuccess);
  539. rb.PushIpcInterface<IAudioRenderer>(system, params, audren_instance_count++);
  540. }
  541. bool IsFeatureSupported(AudioFeatures feature, u32_le revision) {
  542. // Byte swap
  543. const u32_be version_num = revision - Common::MakeMagic('R', 'E', 'V', '0');
  544. switch (feature) {
  545. case AudioFeatures::AudioUSBDeviceOutput:
  546. return version_num >= 4U;
  547. case AudioFeatures::Splitter:
  548. return version_num >= 2U;
  549. case AudioFeatures::PerformanceMetricsVersion2:
  550. case AudioFeatures::VariadicCommandBuffer:
  551. return version_num >= 5U;
  552. default:
  553. return false;
  554. }
  555. }
  556. } // namespace Service::Audio