voice_context.cpp 22 KB

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