audren_u.cpp 27 KB

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