voice_context.cpp 19 KB

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