audren_u.cpp 28 KB

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