voice_context.cpp 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530
  1. // Copyright 2020 yuzu Emulator Project
  2. // Licensed under GPLv2 or any later version
  3. // Refer to the license.txt file included.
  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_bufffer_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 = false;
  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_bufffer_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.sent_to_dsp = true;
  168. }
  169. // Mark all our wave buffers as invalid
  170. for (std::size_t channel = 0; channel < static_cast<std::size_t>(in_params.channel_count);
  171. channel++) {
  172. for (auto& is_valid : voice_states[channel]->is_wave_buffer_valid) {
  173. is_valid = false;
  174. }
  175. }
  176. }
  177. // Update our wave buffers
  178. for (std::size_t i = 0; i < AudioCommon::MAX_WAVE_BUFFERS; i++) {
  179. // Assume that we have at least 1 channel voice state
  180. const auto have_valid_wave_buffer = voice_states[0]->is_wave_buffer_valid[i];
  181. UpdateWaveBuffer(in_params.wave_buffer[i], voice_in.wave_buffer[i], in_params.sample_format,
  182. have_valid_wave_buffer, behavior_info);
  183. }
  184. }
  185. void ServerVoiceInfo::UpdateWaveBuffer(ServerWaveBuffer& out_wavebuffer,
  186. const WaveBuffer& in_wave_buffer, SampleFormat sample_format,
  187. bool is_buffer_valid,
  188. [[maybe_unused]] BehaviorInfo& behavior_info) {
  189. if (!is_buffer_valid && out_wavebuffer.sent_to_dsp) {
  190. out_wavebuffer.buffer_address = 0;
  191. out_wavebuffer.buffer_size = 0;
  192. }
  193. if (!in_wave_buffer.sent_to_server || !in_params.buffer_mapped) {
  194. // Validate sample offset sizings
  195. if (sample_format == SampleFormat::Pcm16) {
  196. const auto buffer_size = in_wave_buffer.buffer_size;
  197. if (in_wave_buffer.start_sample_offset < 0 || in_wave_buffer.end_sample_offset < 0 ||
  198. (buffer_size < (sizeof(s16) * in_wave_buffer.start_sample_offset)) ||
  199. (buffer_size < (sizeof(s16) * in_wave_buffer.end_sample_offset))) {
  200. // TODO(ogniK): Write error info
  201. return;
  202. }
  203. }
  204. // TODO(ogniK): ADPCM Size error
  205. out_wavebuffer.sent_to_dsp = false;
  206. out_wavebuffer.start_sample_offset = in_wave_buffer.start_sample_offset;
  207. out_wavebuffer.end_sample_offset = in_wave_buffer.end_sample_offset;
  208. out_wavebuffer.is_looping = in_wave_buffer.is_looping;
  209. out_wavebuffer.end_of_stream = in_wave_buffer.end_of_stream;
  210. out_wavebuffer.buffer_address = in_wave_buffer.buffer_address;
  211. out_wavebuffer.buffer_size = in_wave_buffer.buffer_size;
  212. out_wavebuffer.context_address = in_wave_buffer.context_address;
  213. out_wavebuffer.context_size = in_wave_buffer.context_size;
  214. in_params.buffer_mapped =
  215. in_wave_buffer.buffer_address != 0 && in_wave_buffer.buffer_size != 0;
  216. // TODO(ogniK): Pool mapper attachment
  217. // TODO(ogniK): IsAdpcmLoopContextBugFixed
  218. }
  219. }
  220. void ServerVoiceInfo::WriteOutStatus(
  221. VoiceInfo::OutParams& voice_out, VoiceInfo::InParams& voice_in,
  222. std::array<VoiceState*, AudioCommon::MAX_CHANNEL_COUNT>& voice_states) {
  223. if (voice_in.is_new) {
  224. in_params.is_new = true;
  225. voice_out.wave_buffer_consumed = 0;
  226. voice_out.played_sample_count = 0;
  227. voice_out.voice_dropped = false;
  228. } else if (!in_params.is_new) {
  229. voice_out.wave_buffer_consumed = voice_states[0]->wave_buffer_consumed;
  230. voice_out.played_sample_count = voice_states[0]->played_sample_count;
  231. voice_out.voice_dropped = in_params.voice_drop_flag;
  232. } else {
  233. voice_out.wave_buffer_consumed = 0;
  234. voice_out.played_sample_count = 0;
  235. voice_out.voice_dropped = false;
  236. }
  237. }
  238. const ServerVoiceInfo::InParams& ServerVoiceInfo::GetInParams() const {
  239. return in_params;
  240. }
  241. ServerVoiceInfo::InParams& ServerVoiceInfo::GetInParams() {
  242. return in_params;
  243. }
  244. const ServerVoiceInfo::OutParams& ServerVoiceInfo::GetOutParams() const {
  245. return out_params;
  246. }
  247. ServerVoiceInfo::OutParams& ServerVoiceInfo::GetOutParams() {
  248. return out_params;
  249. }
  250. bool ServerVoiceInfo::ShouldSkip() const {
  251. // TODO(ogniK): Handle unmapped wave buffers or parameters
  252. return !in_params.in_use || (in_params.wave_buffer_count == 0) || in_params.voice_drop_flag;
  253. }
  254. bool ServerVoiceInfo::UpdateForCommandGeneration(VoiceContext& voice_context) {
  255. std::array<VoiceState*, AudioCommon::MAX_CHANNEL_COUNT> dsp_voice_states{};
  256. if (in_params.is_new) {
  257. ResetResources(voice_context);
  258. in_params.last_volume = in_params.volume;
  259. in_params.is_new = false;
  260. }
  261. const s32 channel_count = in_params.channel_count;
  262. for (s32 i = 0; i < channel_count; i++) {
  263. const auto channel_resource = in_params.voice_channel_resource_id[i];
  264. dsp_voice_states[i] =
  265. &voice_context.GetDspSharedState(static_cast<std::size_t>(channel_resource));
  266. }
  267. return UpdateParametersForCommandGeneration(dsp_voice_states);
  268. }
  269. void ServerVoiceInfo::ResetResources(VoiceContext& voice_context) {
  270. const s32 channel_count = in_params.channel_count;
  271. for (s32 i = 0; i < channel_count; i++) {
  272. const auto channel_resource = in_params.voice_channel_resource_id[i];
  273. auto& dsp_state =
  274. voice_context.GetDspSharedState(static_cast<std::size_t>(channel_resource));
  275. dsp_state = {};
  276. voice_context.GetChannelResource(static_cast<std::size_t>(channel_resource))
  277. .UpdateLastMixVolumes();
  278. }
  279. }
  280. bool ServerVoiceInfo::UpdateParametersForCommandGeneration(
  281. std::array<VoiceState*, AudioCommon::MAX_CHANNEL_COUNT>& dsp_voice_states) {
  282. const s32 channel_count = in_params.channel_count;
  283. if (in_params.wave_buffer_flush_request_count > 0) {
  284. FlushWaveBuffers(in_params.wave_buffer_flush_request_count, dsp_voice_states,
  285. channel_count);
  286. in_params.wave_buffer_flush_request_count = 0;
  287. }
  288. switch (in_params.current_playstate) {
  289. case ServerPlayState::Play: {
  290. for (std::size_t i = 0; i < AudioCommon::MAX_WAVE_BUFFERS; i++) {
  291. if (!in_params.wave_buffer[i].sent_to_dsp) {
  292. for (s32 channel = 0; channel < channel_count; channel++) {
  293. dsp_voice_states[channel]->is_wave_buffer_valid[i] = true;
  294. }
  295. in_params.wave_buffer[i].sent_to_dsp = true;
  296. }
  297. }
  298. in_params.should_depop = false;
  299. return HasValidWaveBuffer(dsp_voice_states[0]);
  300. }
  301. case ServerPlayState::Paused:
  302. case ServerPlayState::Stop: {
  303. in_params.should_depop = in_params.last_playstate == ServerPlayState::Play;
  304. return in_params.should_depop;
  305. }
  306. case ServerPlayState::RequestStop: {
  307. for (std::size_t i = 0; i < AudioCommon::MAX_WAVE_BUFFERS; i++) {
  308. in_params.wave_buffer[i].sent_to_dsp = true;
  309. for (s32 channel = 0; channel < channel_count; channel++) {
  310. auto* dsp_state = dsp_voice_states[channel];
  311. if (dsp_state->is_wave_buffer_valid[i]) {
  312. dsp_state->wave_buffer_index =
  313. (dsp_state->wave_buffer_index + 1) % AudioCommon::MAX_WAVE_BUFFERS;
  314. dsp_state->wave_buffer_consumed++;
  315. }
  316. dsp_state->is_wave_buffer_valid[i] = false;
  317. }
  318. }
  319. for (s32 channel = 0; channel < channel_count; channel++) {
  320. auto* dsp_state = dsp_voice_states[channel];
  321. dsp_state->offset = 0;
  322. dsp_state->played_sample_count = 0;
  323. dsp_state->fraction = 0;
  324. dsp_state->sample_history.fill(0);
  325. dsp_state->context = {};
  326. }
  327. in_params.current_playstate = ServerPlayState::Stop;
  328. in_params.should_depop = in_params.last_playstate == ServerPlayState::Play;
  329. return in_params.should_depop;
  330. }
  331. default:
  332. UNREACHABLE_MSG("Invalid playstate {}", in_params.current_playstate);
  333. }
  334. return false;
  335. }
  336. void ServerVoiceInfo::FlushWaveBuffers(
  337. u8 flush_count, std::array<VoiceState*, AudioCommon::MAX_CHANNEL_COUNT>& dsp_voice_states,
  338. s32 channel_count) {
  339. auto wave_head = in_params.wave_bufffer_head;
  340. for (u8 i = 0; i < flush_count; i++) {
  341. in_params.wave_buffer[wave_head].sent_to_dsp = true;
  342. for (s32 channel = 0; channel < channel_count; channel++) {
  343. auto* dsp_state = dsp_voice_states[channel];
  344. dsp_state->wave_buffer_consumed++;
  345. dsp_state->is_wave_buffer_valid[wave_head] = false;
  346. dsp_state->wave_buffer_index =
  347. (dsp_state->wave_buffer_index + 1) % AudioCommon::MAX_WAVE_BUFFERS;
  348. }
  349. wave_head = (wave_head + 1) % AudioCommon::MAX_WAVE_BUFFERS;
  350. }
  351. }
  352. bool ServerVoiceInfo::HasValidWaveBuffer(const VoiceState* state) const {
  353. const auto& valid_wb = state->is_wave_buffer_valid;
  354. return std::find(valid_wb.begin(), valid_wb.end(), true) != valid_wb.end();
  355. }
  356. VoiceContext::VoiceContext(std::size_t voice_count_) : voice_count{voice_count_} {
  357. for (std::size_t i = 0; i < voice_count; i++) {
  358. voice_channel_resources.emplace_back(static_cast<s32>(i));
  359. sorted_voice_info.push_back(&voice_info.emplace_back());
  360. voice_states.emplace_back();
  361. dsp_voice_states.emplace_back();
  362. }
  363. }
  364. VoiceContext::~VoiceContext() {
  365. sorted_voice_info.clear();
  366. }
  367. std::size_t VoiceContext::GetVoiceCount() const {
  368. return voice_count;
  369. }
  370. ServerVoiceChannelResource& VoiceContext::GetChannelResource(std::size_t i) {
  371. ASSERT(i < voice_count);
  372. return voice_channel_resources.at(i);
  373. }
  374. const ServerVoiceChannelResource& VoiceContext::GetChannelResource(std::size_t i) const {
  375. ASSERT(i < voice_count);
  376. return voice_channel_resources.at(i);
  377. }
  378. VoiceState& VoiceContext::GetState(std::size_t i) {
  379. ASSERT(i < voice_count);
  380. return voice_states.at(i);
  381. }
  382. const VoiceState& VoiceContext::GetState(std::size_t i) const {
  383. ASSERT(i < voice_count);
  384. return voice_states.at(i);
  385. }
  386. VoiceState& VoiceContext::GetDspSharedState(std::size_t i) {
  387. ASSERT(i < voice_count);
  388. return dsp_voice_states.at(i);
  389. }
  390. const VoiceState& VoiceContext::GetDspSharedState(std::size_t i) const {
  391. ASSERT(i < voice_count);
  392. return dsp_voice_states.at(i);
  393. }
  394. ServerVoiceInfo& VoiceContext::GetInfo(std::size_t i) {
  395. ASSERT(i < voice_count);
  396. return voice_info.at(i);
  397. }
  398. const ServerVoiceInfo& VoiceContext::GetInfo(std::size_t i) const {
  399. ASSERT(i < voice_count);
  400. return voice_info.at(i);
  401. }
  402. ServerVoiceInfo& VoiceContext::GetSortedInfo(std::size_t i) {
  403. ASSERT(i < voice_count);
  404. return *sorted_voice_info.at(i);
  405. }
  406. const ServerVoiceInfo& VoiceContext::GetSortedInfo(std::size_t i) const {
  407. ASSERT(i < voice_count);
  408. return *sorted_voice_info.at(i);
  409. }
  410. s32 VoiceContext::DecodePcm16(s32* output_buffer, ServerWaveBuffer* wave_buffer, s32 channel,
  411. s32 channel_count, s32 buffer_offset, s32 sample_count,
  412. Core::Memory::Memory& memory) {
  413. if (wave_buffer->buffer_address == 0) {
  414. return 0;
  415. }
  416. if (wave_buffer->buffer_size == 0) {
  417. return 0;
  418. }
  419. if (wave_buffer->end_sample_offset < wave_buffer->start_sample_offset) {
  420. return 0;
  421. }
  422. const auto samples_remaining =
  423. (wave_buffer->end_sample_offset - wave_buffer->start_sample_offset) - buffer_offset;
  424. const auto start_offset = (wave_buffer->start_sample_offset + buffer_offset) * channel_count;
  425. const auto buffer_pos = wave_buffer->buffer_address + start_offset;
  426. s16* buffer_data = reinterpret_cast<s16*>(memory.GetPointer(buffer_pos));
  427. const auto samples_processed = std::min(sample_count, samples_remaining);
  428. // Fast path
  429. if (channel_count == 1) {
  430. for (std::ptrdiff_t i = 0; i < samples_processed; i++) {
  431. output_buffer[i] = buffer_data[i];
  432. }
  433. } else {
  434. for (std::ptrdiff_t i = 0; i < samples_processed; i++) {
  435. output_buffer[i] = buffer_data[i * channel_count + channel];
  436. }
  437. }
  438. return samples_processed;
  439. }
  440. void VoiceContext::SortInfo() {
  441. for (std::size_t i = 0; i < voice_count; i++) {
  442. sorted_voice_info[i] = &voice_info[i];
  443. }
  444. std::sort(sorted_voice_info.begin(), sorted_voice_info.end(),
  445. [](const ServerVoiceInfo* lhs, const ServerVoiceInfo* rhs) {
  446. const auto& lhs_in = lhs->GetInParams();
  447. const auto& rhs_in = rhs->GetInParams();
  448. // Sort by priority
  449. if (lhs_in.priority != rhs_in.priority) {
  450. return lhs_in.priority > rhs_in.priority;
  451. } else {
  452. // If the priorities match, sort by sorting order
  453. return lhs_in.sorting_order > rhs_in.sorting_order;
  454. }
  455. });
  456. }
  457. void VoiceContext::UpdateStateByDspShared() {
  458. voice_states = dsp_voice_states;
  459. }
  460. } // namespace AudioCore