audren_u.cpp 28 KB

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