voice_context.cpp 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579
  1. // SPDX-FileCopyrightText: Copyright 2020 yuzu Emulator Project
  2. // SPDX-License-Identifier: GPL-2.0-or-later
  3. #include <algorithm>
  4. #include "audio_core/behavior_info.h"
  5. #include "audio_core/voice_context.h"
  6. #include "core/memory.h"
  7. namespace AudioCore {
  8. ServerVoiceChannelResource::ServerVoiceChannelResource(s32 id_) : id(id_) {}
  9. ServerVoiceChannelResource::~ServerVoiceChannelResource() = default;
  10. bool ServerVoiceChannelResource::InUse() const {
  11. return in_use;
  12. }
  13. float ServerVoiceChannelResource::GetCurrentMixVolumeAt(std::size_t i) const {
  14. ASSERT(i < AudioCommon::MAX_MIX_BUFFERS);
  15. return mix_volume.at(i);
  16. }
  17. float ServerVoiceChannelResource::GetLastMixVolumeAt(std::size_t i) const {
  18. ASSERT(i < AudioCommon::MAX_MIX_BUFFERS);
  19. return last_mix_volume.at(i);
  20. }
  21. void ServerVoiceChannelResource::Update(VoiceChannelResource::InParams& in_params) {
  22. in_use = in_params.in_use;
  23. // Update our mix volumes only if it's in use
  24. if (in_params.in_use) {
  25. mix_volume = in_params.mix_volume;
  26. }
  27. }
  28. void ServerVoiceChannelResource::UpdateLastMixVolumes() {
  29. last_mix_volume = mix_volume;
  30. }
  31. const std::array<float, AudioCommon::MAX_MIX_BUFFERS>&
  32. ServerVoiceChannelResource::GetCurrentMixVolume() const {
  33. return mix_volume;
  34. }
  35. const std::array<float, AudioCommon::MAX_MIX_BUFFERS>&
  36. ServerVoiceChannelResource::GetLastMixVolume() const {
  37. return last_mix_volume;
  38. }
  39. ServerVoiceInfo::ServerVoiceInfo() {
  40. Initialize();
  41. }
  42. ServerVoiceInfo::~ServerVoiceInfo() = default;
  43. void ServerVoiceInfo::Initialize() {
  44. in_params.in_use = false;
  45. in_params.node_id = 0;
  46. in_params.id = 0;
  47. in_params.current_playstate = ServerPlayState::Stop;
  48. in_params.priority = 255;
  49. in_params.sample_rate = 0;
  50. in_params.sample_format = SampleFormat::Invalid;
  51. in_params.channel_count = 0;
  52. in_params.pitch = 0.0f;
  53. in_params.volume = 0.0f;
  54. in_params.last_volume = 0.0f;
  55. in_params.biquad_filter.fill({});
  56. in_params.wave_buffer_count = 0;
  57. in_params.wave_buffer_head = 0;
  58. in_params.mix_id = AudioCommon::NO_MIX;
  59. in_params.splitter_info_id = AudioCommon::NO_SPLITTER;
  60. in_params.additional_params_address = 0;
  61. in_params.additional_params_size = 0;
  62. in_params.is_new = false;
  63. out_params.played_sample_count = 0;
  64. out_params.wave_buffer_consumed = 0;
  65. in_params.voice_drop_flag = false;
  66. in_params.buffer_mapped = true;
  67. in_params.wave_buffer_flush_request_count = 0;
  68. in_params.was_biquad_filter_enabled.fill(false);
  69. for (auto& wave_buffer : in_params.wave_buffer) {
  70. wave_buffer.start_sample_offset = 0;
  71. wave_buffer.end_sample_offset = 0;
  72. wave_buffer.is_looping = false;
  73. wave_buffer.end_of_stream = false;
  74. wave_buffer.buffer_address = 0;
  75. wave_buffer.buffer_size = 0;
  76. wave_buffer.context_address = 0;
  77. wave_buffer.context_size = 0;
  78. wave_buffer.sent_to_dsp = true;
  79. }
  80. stored_samples.clear();
  81. }
  82. void ServerVoiceInfo::UpdateParameters(const VoiceInfo::InParams& voice_in,
  83. BehaviorInfo& behavior_info) {
  84. in_params.in_use = voice_in.is_in_use;
  85. in_params.id = voice_in.id;
  86. in_params.node_id = voice_in.node_id;
  87. in_params.last_playstate = in_params.current_playstate;
  88. switch (voice_in.play_state) {
  89. case PlayState::Paused:
  90. in_params.current_playstate = ServerPlayState::Paused;
  91. break;
  92. case PlayState::Stopped:
  93. if (in_params.current_playstate != ServerPlayState::Stop) {
  94. in_params.current_playstate = ServerPlayState::RequestStop;
  95. }
  96. break;
  97. case PlayState::Started:
  98. in_params.current_playstate = ServerPlayState::Play;
  99. break;
  100. default:
  101. UNREACHABLE_MSG("Unknown playstate {}", voice_in.play_state);
  102. break;
  103. }
  104. in_params.priority = voice_in.priority;
  105. in_params.sorting_order = voice_in.sorting_order;
  106. in_params.sample_rate = voice_in.sample_rate;
  107. in_params.sample_format = voice_in.sample_format;
  108. in_params.channel_count = voice_in.channel_count;
  109. in_params.pitch = voice_in.pitch;
  110. in_params.volume = voice_in.volume;
  111. in_params.biquad_filter = voice_in.biquad_filter;
  112. in_params.wave_buffer_count = voice_in.wave_buffer_count;
  113. in_params.wave_buffer_head = voice_in.wave_buffer_head;
  114. if (behavior_info.IsFlushVoiceWaveBuffersSupported()) {
  115. const auto in_request_count = in_params.wave_buffer_flush_request_count;
  116. const auto voice_request_count = voice_in.wave_buffer_flush_request_count;
  117. in_params.wave_buffer_flush_request_count =
  118. static_cast<u8>(in_request_count + voice_request_count);
  119. }
  120. in_params.mix_id = voice_in.mix_id;
  121. if (behavior_info.IsSplitterSupported()) {
  122. in_params.splitter_info_id = voice_in.splitter_info_id;
  123. } else {
  124. in_params.splitter_info_id = AudioCommon::NO_SPLITTER;
  125. }
  126. std::memcpy(in_params.voice_channel_resource_id.data(),
  127. voice_in.voice_channel_resource_ids.data(),
  128. sizeof(s32) * in_params.voice_channel_resource_id.size());
  129. if (behavior_info.IsVoicePlayedSampleCountResetAtLoopPointSupported()) {
  130. in_params.behavior_flags.is_played_samples_reset_at_loop_point =
  131. voice_in.behavior_flags.is_played_samples_reset_at_loop_point;
  132. } else {
  133. in_params.behavior_flags.is_played_samples_reset_at_loop_point.Assign(0);
  134. }
  135. if (behavior_info.IsVoicePitchAndSrcSkippedSupported()) {
  136. in_params.behavior_flags.is_pitch_and_src_skipped =
  137. voice_in.behavior_flags.is_pitch_and_src_skipped;
  138. } else {
  139. in_params.behavior_flags.is_pitch_and_src_skipped.Assign(0);
  140. }
  141. if (voice_in.is_voice_drop_flag_clear_requested) {
  142. in_params.voice_drop_flag = false;
  143. }
  144. if (in_params.additional_params_address != voice_in.additional_params_address ||
  145. in_params.additional_params_size != voice_in.additional_params_size) {
  146. in_params.additional_params_address = voice_in.additional_params_address;
  147. in_params.additional_params_size = voice_in.additional_params_size;
  148. // TODO(ogniK): Reattach buffer, do we actually need to? Maybe just signal to the DSP that
  149. // our context is new
  150. }
  151. }
  152. void ServerVoiceInfo::UpdateWaveBuffers(
  153. const VoiceInfo::InParams& voice_in,
  154. std::array<VoiceState*, AudioCommon::MAX_CHANNEL_COUNT>& voice_states,
  155. BehaviorInfo& behavior_info) {
  156. if (voice_in.is_new) {
  157. // Initialize our wave buffers
  158. for (auto& wave_buffer : in_params.wave_buffer) {
  159. wave_buffer.start_sample_offset = 0;
  160. wave_buffer.end_sample_offset = 0;
  161. wave_buffer.is_looping = false;
  162. wave_buffer.end_of_stream = false;
  163. wave_buffer.buffer_address = 0;
  164. wave_buffer.buffer_size = 0;
  165. wave_buffer.context_address = 0;
  166. wave_buffer.context_size = 0;
  167. wave_buffer.loop_start_sample = 0;
  168. wave_buffer.loop_end_sample = 0;
  169. wave_buffer.sent_to_dsp = true;
  170. }
  171. // Mark all our wave buffers as invalid
  172. for (std::size_t channel = 0; channel < static_cast<std::size_t>(in_params.channel_count);
  173. channel++) {
  174. for (std::size_t i = 0; i < AudioCommon::MAX_WAVE_BUFFERS; ++i) {
  175. voice_states[channel]->is_wave_buffer_valid[i] = false;
  176. }
  177. }
  178. }
  179. // Update our wave buffers
  180. for (std::size_t i = 0; i < AudioCommon::MAX_WAVE_BUFFERS; i++) {
  181. // Assume that we have at least 1 channel voice state
  182. const auto have_valid_wave_buffer = voice_states[0]->is_wave_buffer_valid[i];
  183. UpdateWaveBuffer(in_params.wave_buffer[i], voice_in.wave_buffer[i], in_params.sample_format,
  184. have_valid_wave_buffer, behavior_info);
  185. }
  186. }
  187. void ServerVoiceInfo::UpdateWaveBuffer(ServerWaveBuffer& out_wavebuffer,
  188. const WaveBuffer& in_wave_buffer, SampleFormat sample_format,
  189. bool is_buffer_valid,
  190. [[maybe_unused]] BehaviorInfo& behavior_info) {
  191. if (!is_buffer_valid && out_wavebuffer.sent_to_dsp && out_wavebuffer.buffer_address != 0) {
  192. out_wavebuffer.buffer_address = 0;
  193. out_wavebuffer.buffer_size = 0;
  194. }
  195. if (!in_wave_buffer.sent_to_server || !in_params.buffer_mapped) {
  196. // Validate sample offset sizings
  197. if (sample_format == SampleFormat::Pcm16) {
  198. const s64 buffer_size = static_cast<s64>(in_wave_buffer.buffer_size);
  199. const s64 start = sizeof(s16) * in_wave_buffer.start_sample_offset;
  200. const s64 end = sizeof(s16) * in_wave_buffer.end_sample_offset;
  201. if (0 > start || start > buffer_size || 0 > end || end > buffer_size) {
  202. // TODO(ogniK): Write error info
  203. LOG_ERROR(Audio,
  204. "PCM16 wavebuffer has an invalid size. Buffer has size 0x{:08X}, but "
  205. "offsets were "
  206. "{:08X} - 0x{:08X}",
  207. buffer_size, sizeof(s16) * in_wave_buffer.start_sample_offset,
  208. sizeof(s16) * in_wave_buffer.end_sample_offset);
  209. return;
  210. }
  211. } else if (sample_format == SampleFormat::Adpcm) {
  212. const s64 buffer_size = static_cast<s64>(in_wave_buffer.buffer_size);
  213. const s64 start_frames = in_wave_buffer.start_sample_offset / 14;
  214. const s64 start_extra = in_wave_buffer.start_sample_offset % 14 == 0
  215. ? 0
  216. : (in_wave_buffer.start_sample_offset % 14) / 2 + 1 +
  217. (in_wave_buffer.start_sample_offset % 2);
  218. const s64 start = start_frames * 8 + start_extra;
  219. const s64 end_frames = in_wave_buffer.end_sample_offset / 14;
  220. const s64 end_extra = in_wave_buffer.end_sample_offset % 14 == 0
  221. ? 0
  222. : (in_wave_buffer.end_sample_offset % 14) / 2 + 1 +
  223. (in_wave_buffer.end_sample_offset % 2);
  224. const s64 end = end_frames * 8 + end_extra;
  225. if (in_wave_buffer.start_sample_offset < 0 || start > buffer_size ||
  226. in_wave_buffer.end_sample_offset < 0 || end > buffer_size) {
  227. LOG_ERROR(Audio,
  228. "ADPMC wavebuffer has an invalid size. Buffer has size 0x{:08X}, but "
  229. "offsets were "
  230. "{:08X} - 0x{:08X}",
  231. in_wave_buffer.buffer_size, start, end);
  232. return;
  233. }
  234. }
  235. // TODO(ogniK): ADPCM Size error
  236. out_wavebuffer.sent_to_dsp = false;
  237. out_wavebuffer.start_sample_offset = in_wave_buffer.start_sample_offset;
  238. out_wavebuffer.end_sample_offset = in_wave_buffer.end_sample_offset;
  239. out_wavebuffer.is_looping = in_wave_buffer.is_looping;
  240. out_wavebuffer.end_of_stream = in_wave_buffer.end_of_stream;
  241. out_wavebuffer.buffer_address = in_wave_buffer.buffer_address;
  242. out_wavebuffer.buffer_size = in_wave_buffer.buffer_size;
  243. out_wavebuffer.context_address = in_wave_buffer.context_address;
  244. out_wavebuffer.context_size = in_wave_buffer.context_size;
  245. out_wavebuffer.loop_start_sample = in_wave_buffer.loop_start_sample;
  246. out_wavebuffer.loop_end_sample = in_wave_buffer.loop_end_sample;
  247. in_params.buffer_mapped =
  248. in_wave_buffer.buffer_address != 0 && in_wave_buffer.buffer_size != 0;
  249. // TODO(ogniK): Pool mapper attachment
  250. // TODO(ogniK): IsAdpcmLoopContextBugFixed
  251. if (sample_format == SampleFormat::Adpcm && in_wave_buffer.context_address != 0 &&
  252. in_wave_buffer.context_size != 0 && behavior_info.IsAdpcmLoopContextBugFixed()) {
  253. } else {
  254. out_wavebuffer.context_address = 0;
  255. out_wavebuffer.context_size = 0;
  256. }
  257. }
  258. }
  259. void ServerVoiceInfo::WriteOutStatus(
  260. VoiceInfo::OutParams& voice_out, VoiceInfo::InParams& voice_in,
  261. std::array<VoiceState*, AudioCommon::MAX_CHANNEL_COUNT>& voice_states) {
  262. if (voice_in.is_new || in_params.is_new) {
  263. in_params.is_new = true;
  264. voice_out.wave_buffer_consumed = 0;
  265. voice_out.played_sample_count = 0;
  266. voice_out.voice_dropped = false;
  267. } else {
  268. const auto& state = voice_states[0];
  269. voice_out.wave_buffer_consumed = state->wave_buffer_consumed;
  270. voice_out.played_sample_count = state->played_sample_count;
  271. voice_out.voice_dropped = state->voice_dropped;
  272. }
  273. }
  274. const ServerVoiceInfo::InParams& ServerVoiceInfo::GetInParams() const {
  275. return in_params;
  276. }
  277. ServerVoiceInfo::InParams& ServerVoiceInfo::GetInParams() {
  278. return in_params;
  279. }
  280. const ServerVoiceInfo::OutParams& ServerVoiceInfo::GetOutParams() const {
  281. return out_params;
  282. }
  283. ServerVoiceInfo::OutParams& ServerVoiceInfo::GetOutParams() {
  284. return out_params;
  285. }
  286. bool ServerVoiceInfo::ShouldSkip() const {
  287. // TODO(ogniK): Handle unmapped wave buffers or parameters
  288. return !in_params.in_use || in_params.wave_buffer_count == 0 || !in_params.buffer_mapped ||
  289. in_params.voice_drop_flag;
  290. }
  291. bool ServerVoiceInfo::UpdateForCommandGeneration(VoiceContext& voice_context) {
  292. std::array<VoiceState*, AudioCommon::MAX_CHANNEL_COUNT> dsp_voice_states{};
  293. if (in_params.is_new) {
  294. ResetResources(voice_context);
  295. in_params.last_volume = in_params.volume;
  296. in_params.is_new = false;
  297. }
  298. const s32 channel_count = in_params.channel_count;
  299. for (s32 i = 0; i < channel_count; i++) {
  300. const auto channel_resource = in_params.voice_channel_resource_id[i];
  301. dsp_voice_states[i] =
  302. &voice_context.GetDspSharedState(static_cast<std::size_t>(channel_resource));
  303. }
  304. return UpdateParametersForCommandGeneration(dsp_voice_states);
  305. }
  306. void ServerVoiceInfo::ResetResources(VoiceContext& voice_context) {
  307. const s32 channel_count = in_params.channel_count;
  308. for (s32 i = 0; i < channel_count; i++) {
  309. const auto channel_resource = in_params.voice_channel_resource_id[i];
  310. auto& dsp_state =
  311. voice_context.GetDspSharedState(static_cast<std::size_t>(channel_resource));
  312. dsp_state = {};
  313. voice_context.GetChannelResource(static_cast<std::size_t>(channel_resource))
  314. .UpdateLastMixVolumes();
  315. }
  316. }
  317. bool ServerVoiceInfo::UpdateParametersForCommandGeneration(
  318. std::array<VoiceState*, AudioCommon::MAX_CHANNEL_COUNT>& dsp_voice_states) {
  319. const s32 channel_count = in_params.channel_count;
  320. if (in_params.wave_buffer_flush_request_count > 0) {
  321. FlushWaveBuffers(in_params.wave_buffer_flush_request_count, dsp_voice_states,
  322. channel_count);
  323. in_params.wave_buffer_flush_request_count = 0;
  324. }
  325. switch (in_params.current_playstate) {
  326. case ServerPlayState::Play: {
  327. for (std::size_t i = 0; i < AudioCommon::MAX_WAVE_BUFFERS; i++) {
  328. if (!in_params.wave_buffer[i].sent_to_dsp) {
  329. for (s32 channel = 0; channel < channel_count; channel++) {
  330. dsp_voice_states[channel]->is_wave_buffer_valid[i] = true;
  331. }
  332. in_params.wave_buffer[i].sent_to_dsp = true;
  333. }
  334. }
  335. in_params.should_depop = false;
  336. return HasValidWaveBuffer(dsp_voice_states[0]);
  337. }
  338. case ServerPlayState::Paused:
  339. case ServerPlayState::Stop: {
  340. in_params.should_depop = in_params.last_playstate == ServerPlayState::Play;
  341. return in_params.should_depop;
  342. }
  343. case ServerPlayState::RequestStop: {
  344. for (std::size_t i = 0; i < AudioCommon::MAX_WAVE_BUFFERS; i++) {
  345. in_params.wave_buffer[i].sent_to_dsp = true;
  346. for (s32 channel = 0; channel < channel_count; channel++) {
  347. auto* dsp_state = dsp_voice_states[channel];
  348. if (dsp_state->is_wave_buffer_valid[i]) {
  349. dsp_state->wave_buffer_index =
  350. (dsp_state->wave_buffer_index + 1) % AudioCommon::MAX_WAVE_BUFFERS;
  351. dsp_state->wave_buffer_consumed++;
  352. }
  353. dsp_state->is_wave_buffer_valid[i] = false;
  354. }
  355. }
  356. for (s32 channel = 0; channel < channel_count; channel++) {
  357. auto* dsp_state = dsp_voice_states[channel];
  358. dsp_state->offset = 0;
  359. dsp_state->played_sample_count = 0;
  360. dsp_state->fraction = 0;
  361. dsp_state->sample_history.fill(0);
  362. dsp_state->context = {};
  363. }
  364. in_params.current_playstate = ServerPlayState::Stop;
  365. in_params.should_depop = in_params.last_playstate == ServerPlayState::Play;
  366. return in_params.should_depop;
  367. }
  368. default:
  369. UNREACHABLE_MSG("Invalid playstate {}", in_params.current_playstate);
  370. }
  371. return false;
  372. }
  373. void ServerVoiceInfo::FlushWaveBuffers(
  374. u8 flush_count, std::array<VoiceState*, AudioCommon::MAX_CHANNEL_COUNT>& dsp_voice_states,
  375. s32 channel_count) {
  376. auto wave_head = in_params.wave_buffer_head;
  377. for (u8 i = 0; i < flush_count; i++) {
  378. in_params.wave_buffer[wave_head].sent_to_dsp = true;
  379. for (s32 channel = 0; channel < channel_count; channel++) {
  380. auto* dsp_state = dsp_voice_states[channel];
  381. dsp_state->wave_buffer_consumed++;
  382. dsp_state->is_wave_buffer_valid[wave_head] = false;
  383. dsp_state->wave_buffer_index =
  384. (dsp_state->wave_buffer_index + 1) % AudioCommon::MAX_WAVE_BUFFERS;
  385. }
  386. wave_head = (wave_head + 1) % AudioCommon::MAX_WAVE_BUFFERS;
  387. }
  388. }
  389. bool ServerVoiceInfo::HasValidWaveBuffer(const VoiceState* state) const {
  390. const auto& valid_wb = state->is_wave_buffer_valid;
  391. return std::find(valid_wb.begin(), valid_wb.end(), true) != valid_wb.end();
  392. }
  393. void ServerVoiceInfo::SetWaveBufferCompleted(VoiceState& dsp_state,
  394. const ServerWaveBuffer& wave_buffer) {
  395. dsp_state.is_wave_buffer_valid[dsp_state.wave_buffer_index] = false;
  396. dsp_state.wave_buffer_consumed++;
  397. dsp_state.wave_buffer_index = (dsp_state.wave_buffer_index + 1) % AudioCommon::MAX_WAVE_BUFFERS;
  398. dsp_state.loop_count = 0;
  399. if (wave_buffer.end_of_stream) {
  400. dsp_state.played_sample_count = 0;
  401. }
  402. }
  403. VoiceContext::VoiceContext(std::size_t voice_count_) : voice_count{voice_count_} {
  404. for (std::size_t i = 0; i < voice_count; i++) {
  405. voice_channel_resources.emplace_back(static_cast<s32>(i));
  406. sorted_voice_info.push_back(&voice_info.emplace_back());
  407. voice_states.emplace_back();
  408. dsp_voice_states.emplace_back();
  409. }
  410. }
  411. VoiceContext::~VoiceContext() {
  412. sorted_voice_info.clear();
  413. }
  414. std::size_t VoiceContext::GetVoiceCount() const {
  415. return voice_count;
  416. }
  417. ServerVoiceChannelResource& VoiceContext::GetChannelResource(std::size_t i) {
  418. ASSERT(i < voice_count);
  419. return voice_channel_resources.at(i);
  420. }
  421. const ServerVoiceChannelResource& VoiceContext::GetChannelResource(std::size_t i) const {
  422. ASSERT(i < voice_count);
  423. return voice_channel_resources.at(i);
  424. }
  425. VoiceState& VoiceContext::GetState(std::size_t i) {
  426. ASSERT(i < voice_count);
  427. return voice_states.at(i);
  428. }
  429. const VoiceState& VoiceContext::GetState(std::size_t i) const {
  430. ASSERT(i < voice_count);
  431. return voice_states.at(i);
  432. }
  433. VoiceState& VoiceContext::GetDspSharedState(std::size_t i) {
  434. ASSERT(i < voice_count);
  435. return dsp_voice_states.at(i);
  436. }
  437. const VoiceState& VoiceContext::GetDspSharedState(std::size_t i) const {
  438. ASSERT(i < voice_count);
  439. return dsp_voice_states.at(i);
  440. }
  441. ServerVoiceInfo& VoiceContext::GetInfo(std::size_t i) {
  442. ASSERT(i < voice_count);
  443. return voice_info.at(i);
  444. }
  445. const ServerVoiceInfo& VoiceContext::GetInfo(std::size_t i) const {
  446. ASSERT(i < voice_count);
  447. return voice_info.at(i);
  448. }
  449. ServerVoiceInfo& VoiceContext::GetSortedInfo(std::size_t i) {
  450. ASSERT(i < voice_count);
  451. return *sorted_voice_info.at(i);
  452. }
  453. const ServerVoiceInfo& VoiceContext::GetSortedInfo(std::size_t i) const {
  454. ASSERT(i < voice_count);
  455. return *sorted_voice_info.at(i);
  456. }
  457. s32 VoiceContext::DecodePcm16(s32* output_buffer, ServerWaveBuffer* wave_buffer, s32 channel,
  458. s32 channel_count, s32 buffer_offset, s32 sample_count,
  459. Core::Memory::Memory& memory) {
  460. if (wave_buffer->buffer_address == 0) {
  461. return 0;
  462. }
  463. if (wave_buffer->buffer_size == 0) {
  464. return 0;
  465. }
  466. if (wave_buffer->end_sample_offset < wave_buffer->start_sample_offset) {
  467. return 0;
  468. }
  469. const auto samples_remaining =
  470. (wave_buffer->end_sample_offset - wave_buffer->start_sample_offset) - buffer_offset;
  471. const auto start_offset = (wave_buffer->start_sample_offset + buffer_offset) * channel_count;
  472. const auto buffer_pos = wave_buffer->buffer_address + start_offset;
  473. s16* buffer_data = reinterpret_cast<s16*>(memory.GetPointer(buffer_pos));
  474. const auto samples_processed = std::min(sample_count, samples_remaining);
  475. // Fast path
  476. if (channel_count == 1) {
  477. for (std::ptrdiff_t i = 0; i < samples_processed; i++) {
  478. output_buffer[i] = buffer_data[i];
  479. }
  480. } else {
  481. for (std::ptrdiff_t i = 0; i < samples_processed; i++) {
  482. output_buffer[i] = buffer_data[i * channel_count + channel];
  483. }
  484. }
  485. return samples_processed;
  486. }
  487. void VoiceContext::SortInfo() {
  488. for (std::size_t i = 0; i < voice_count; i++) {
  489. sorted_voice_info[i] = &voice_info[i];
  490. }
  491. std::sort(sorted_voice_info.begin(), sorted_voice_info.end(),
  492. [](const ServerVoiceInfo* lhs, const ServerVoiceInfo* rhs) {
  493. const auto& lhs_in = lhs->GetInParams();
  494. const auto& rhs_in = rhs->GetInParams();
  495. // Sort by priority
  496. if (lhs_in.priority != rhs_in.priority) {
  497. return lhs_in.priority > rhs_in.priority;
  498. } else {
  499. // If the priorities match, sort by sorting order
  500. return lhs_in.sorting_order > rhs_in.sorting_order;
  501. }
  502. });
  503. }
  504. void VoiceContext::UpdateStateByDspShared() {
  505. voice_states = dsp_voice_states;
  506. }
  507. } // namespace AudioCore