source.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339
  1. // Copyright 2016 Citra Emulator Project
  2. // Licensed under GPLv2 or any later version
  3. // Refer to the license.txt file included.
  4. #include <algorithm>
  5. #include <array>
  6. #include "audio_core/codec.h"
  7. #include "audio_core/hle/common.h"
  8. #include "audio_core/hle/source.h"
  9. #include "audio_core/interpolate.h"
  10. #include "common/assert.h"
  11. #include "common/logging/log.h"
  12. #include "core/memory.h"
  13. namespace DSP {
  14. namespace HLE {
  15. SourceStatus::Status Source::Tick(SourceConfiguration::Configuration& config,
  16. const s16_le (&adpcm_coeffs)[16]) {
  17. ParseConfig(config, adpcm_coeffs);
  18. if (state.enabled) {
  19. GenerateFrame();
  20. }
  21. return GetCurrentStatus();
  22. }
  23. void Source::MixInto(QuadFrame32& dest, size_t intermediate_mix_id) const {
  24. if (!state.enabled)
  25. return;
  26. const std::array<float, 4>& gains = state.gain.at(intermediate_mix_id);
  27. for (size_t samplei = 0; samplei < samples_per_frame; samplei++) {
  28. // Conversion from stereo (current_frame) to quadraphonic (dest) occurs here.
  29. dest[samplei][0] += static_cast<s32>(gains[0] * current_frame[samplei][0]);
  30. dest[samplei][1] += static_cast<s32>(gains[1] * current_frame[samplei][1]);
  31. dest[samplei][2] += static_cast<s32>(gains[2] * current_frame[samplei][0]);
  32. dest[samplei][3] += static_cast<s32>(gains[3] * current_frame[samplei][1]);
  33. }
  34. }
  35. void Source::Reset() {
  36. current_frame.fill({});
  37. state = {};
  38. }
  39. void Source::ParseConfig(SourceConfiguration::Configuration& config,
  40. const s16_le (&adpcm_coeffs)[16]) {
  41. if (!config.dirty_raw) {
  42. return;
  43. }
  44. if (config.reset_flag) {
  45. config.reset_flag.Assign(0);
  46. Reset();
  47. LOG_TRACE(Audio_DSP, "source_id=%zu reset", source_id);
  48. }
  49. if (config.partial_reset_flag) {
  50. config.partial_reset_flag.Assign(0);
  51. state.input_queue = std::priority_queue<Buffer, std::vector<Buffer>, BufferOrder>{};
  52. LOG_TRACE(Audio_DSP, "source_id=%zu partial_reset", source_id);
  53. }
  54. if (config.enable_dirty) {
  55. config.enable_dirty.Assign(0);
  56. state.enabled = config.enable != 0;
  57. LOG_TRACE(Audio_DSP, "source_id=%zu enable=%d", source_id, state.enabled);
  58. }
  59. if (config.sync_dirty) {
  60. config.sync_dirty.Assign(0);
  61. state.sync = config.sync;
  62. LOG_TRACE(Audio_DSP, "source_id=%zu sync=%u", source_id, state.sync);
  63. }
  64. if (config.rate_multiplier_dirty) {
  65. config.rate_multiplier_dirty.Assign(0);
  66. state.rate_multiplier = config.rate_multiplier;
  67. LOG_TRACE(Audio_DSP, "source_id=%zu rate=%f", source_id, state.rate_multiplier);
  68. if (state.rate_multiplier <= 0) {
  69. LOG_ERROR(Audio_DSP, "Was given an invalid rate multiplier: source_id=%zu rate=%f",
  70. source_id, state.rate_multiplier);
  71. state.rate_multiplier = 1.0f;
  72. // Note: Actual firmware starts producing garbage if this occurs.
  73. }
  74. }
  75. if (config.adpcm_coefficients_dirty) {
  76. config.adpcm_coefficients_dirty.Assign(0);
  77. std::transform(adpcm_coeffs, adpcm_coeffs + state.adpcm_coeffs.size(),
  78. state.adpcm_coeffs.begin(),
  79. [](const auto& coeff) { return static_cast<s16>(coeff); });
  80. LOG_TRACE(Audio_DSP, "source_id=%zu adpcm update", source_id);
  81. }
  82. if (config.gain_0_dirty) {
  83. config.gain_0_dirty.Assign(0);
  84. std::transform(config.gain[0], config.gain[0] + state.gain[0].size(), state.gain[0].begin(),
  85. [](const auto& coeff) { return static_cast<float>(coeff); });
  86. LOG_TRACE(Audio_DSP, "source_id=%zu gain 0 update", source_id);
  87. }
  88. if (config.gain_1_dirty) {
  89. config.gain_1_dirty.Assign(0);
  90. std::transform(config.gain[1], config.gain[1] + state.gain[1].size(), state.gain[1].begin(),
  91. [](const auto& coeff) { return static_cast<float>(coeff); });
  92. LOG_TRACE(Audio_DSP, "source_id=%zu gain 1 update", source_id);
  93. }
  94. if (config.gain_2_dirty) {
  95. config.gain_2_dirty.Assign(0);
  96. std::transform(config.gain[2], config.gain[2] + state.gain[2].size(), state.gain[2].begin(),
  97. [](const auto& coeff) { return static_cast<float>(coeff); });
  98. LOG_TRACE(Audio_DSP, "source_id=%zu gain 2 update", source_id);
  99. }
  100. if (config.filters_enabled_dirty) {
  101. config.filters_enabled_dirty.Assign(0);
  102. state.filters.Enable(config.simple_filter_enabled.ToBool(),
  103. config.biquad_filter_enabled.ToBool());
  104. LOG_TRACE(Audio_DSP, "source_id=%zu enable_simple=%hu enable_biquad=%hu", source_id,
  105. config.simple_filter_enabled.Value(), config.biquad_filter_enabled.Value());
  106. }
  107. if (config.simple_filter_dirty) {
  108. config.simple_filter_dirty.Assign(0);
  109. state.filters.Configure(config.simple_filter);
  110. LOG_TRACE(Audio_DSP, "source_id=%zu simple filter update", source_id);
  111. }
  112. if (config.biquad_filter_dirty) {
  113. config.biquad_filter_dirty.Assign(0);
  114. state.filters.Configure(config.biquad_filter);
  115. LOG_TRACE(Audio_DSP, "source_id=%zu biquad filter update", source_id);
  116. }
  117. if (config.interpolation_dirty) {
  118. config.interpolation_dirty.Assign(0);
  119. state.interpolation_mode = config.interpolation_mode;
  120. LOG_TRACE(Audio_DSP, "source_id=%zu interpolation_mode=%zu", source_id,
  121. static_cast<size_t>(state.interpolation_mode));
  122. }
  123. if (config.format_dirty || config.embedded_buffer_dirty) {
  124. config.format_dirty.Assign(0);
  125. state.format = config.format;
  126. LOG_TRACE(Audio_DSP, "source_id=%zu format=%zu", source_id,
  127. static_cast<size_t>(state.format));
  128. }
  129. if (config.mono_or_stereo_dirty || config.embedded_buffer_dirty) {
  130. config.mono_or_stereo_dirty.Assign(0);
  131. state.mono_or_stereo = config.mono_or_stereo;
  132. LOG_TRACE(Audio_DSP, "source_id=%zu mono_or_stereo=%zu", source_id,
  133. static_cast<size_t>(state.mono_or_stereo));
  134. }
  135. if (config.embedded_buffer_dirty) {
  136. config.embedded_buffer_dirty.Assign(0);
  137. state.input_queue.emplace(Buffer{
  138. config.physical_address,
  139. config.length,
  140. static_cast<u8>(config.adpcm_ps),
  141. {config.adpcm_yn[0], config.adpcm_yn[1]},
  142. config.adpcm_dirty.ToBool(),
  143. config.is_looping.ToBool(),
  144. config.buffer_id,
  145. state.mono_or_stereo,
  146. state.format,
  147. false,
  148. });
  149. LOG_TRACE(Audio_DSP, "enqueuing embedded addr=0x%08x len=%u id=%hu",
  150. config.physical_address, config.length, config.buffer_id);
  151. }
  152. if (config.buffer_queue_dirty) {
  153. config.buffer_queue_dirty.Assign(0);
  154. for (size_t i = 0; i < 4; i++) {
  155. if (config.buffers_dirty & (1 << i)) {
  156. const auto& b = config.buffers[i];
  157. state.input_queue.emplace(Buffer{
  158. b.physical_address,
  159. b.length,
  160. static_cast<u8>(b.adpcm_ps),
  161. {b.adpcm_yn[0], b.adpcm_yn[1]},
  162. b.adpcm_dirty != 0,
  163. b.is_looping != 0,
  164. b.buffer_id,
  165. state.mono_or_stereo,
  166. state.format,
  167. true,
  168. });
  169. LOG_TRACE(Audio_DSP, "enqueuing queued %zu addr=0x%08x len=%u id=%hu", i,
  170. b.physical_address, b.length, b.buffer_id);
  171. }
  172. }
  173. config.buffers_dirty = 0;
  174. }
  175. if (config.dirty_raw) {
  176. LOG_DEBUG(Audio_DSP, "source_id=%zu remaining_dirty=%x", source_id, config.dirty_raw);
  177. }
  178. config.dirty_raw = 0;
  179. }
  180. void Source::GenerateFrame() {
  181. current_frame.fill({});
  182. if (state.current_buffer.empty() && !DequeueBuffer()) {
  183. state.enabled = false;
  184. state.buffer_update = true;
  185. state.current_buffer_id = 0;
  186. return;
  187. }
  188. size_t frame_position = 0;
  189. state.current_sample_number = state.next_sample_number;
  190. while (frame_position < current_frame.size()) {
  191. if (state.current_buffer.empty() && !DequeueBuffer()) {
  192. break;
  193. }
  194. const size_t size_to_copy =
  195. std::min(state.current_buffer.size(), current_frame.size() - frame_position);
  196. std::copy(state.current_buffer.begin(), state.current_buffer.begin() + size_to_copy,
  197. current_frame.begin() + frame_position);
  198. state.current_buffer.erase(state.current_buffer.begin(),
  199. state.current_buffer.begin() + size_to_copy);
  200. frame_position += size_to_copy;
  201. state.next_sample_number += static_cast<u32>(size_to_copy);
  202. }
  203. state.filters.ProcessFrame(current_frame);
  204. }
  205. bool Source::DequeueBuffer() {
  206. ASSERT_MSG(state.current_buffer.empty(),
  207. "Shouldn't dequeue; we still have data in current_buffer");
  208. if (state.input_queue.empty())
  209. return false;
  210. const Buffer buf = state.input_queue.top();
  211. state.input_queue.pop();
  212. if (buf.adpcm_dirty) {
  213. state.adpcm_state.yn1 = buf.adpcm_yn[0];
  214. state.adpcm_state.yn2 = buf.adpcm_yn[1];
  215. }
  216. if (buf.is_looping) {
  217. LOG_ERROR(Audio_DSP, "Looped buffers are unimplemented at the moment");
  218. }
  219. const u8* const memory = Memory::GetPhysicalPointer(buf.physical_address);
  220. if (memory) {
  221. const unsigned num_channels = buf.mono_or_stereo == MonoOrStereo::Stereo ? 2 : 1;
  222. switch (buf.format) {
  223. case Format::PCM8:
  224. state.current_buffer = Codec::DecodePCM8(num_channels, memory, buf.length);
  225. break;
  226. case Format::PCM16:
  227. state.current_buffer = Codec::DecodePCM16(num_channels, memory, buf.length);
  228. break;
  229. case Format::ADPCM:
  230. DEBUG_ASSERT(num_channels == 1);
  231. state.current_buffer =
  232. Codec::DecodeADPCM(memory, buf.length, state.adpcm_coeffs, state.adpcm_state);
  233. break;
  234. default:
  235. UNIMPLEMENTED();
  236. break;
  237. }
  238. } else {
  239. LOG_WARNING(Audio_DSP,
  240. "source_id=%zu buffer_id=%hu length=%u: Invalid physical address 0x%08X",
  241. source_id, buf.buffer_id, buf.length, buf.physical_address);
  242. state.current_buffer.clear();
  243. return true;
  244. }
  245. switch (state.interpolation_mode) {
  246. case InterpolationMode::None:
  247. state.current_buffer =
  248. AudioInterp::None(state.interp_state, state.current_buffer, state.rate_multiplier);
  249. break;
  250. case InterpolationMode::Linear:
  251. state.current_buffer =
  252. AudioInterp::Linear(state.interp_state, state.current_buffer, state.rate_multiplier);
  253. break;
  254. case InterpolationMode::Polyphase:
  255. // TODO(merry): Implement polyphase interpolation
  256. state.current_buffer =
  257. AudioInterp::Linear(state.interp_state, state.current_buffer, state.rate_multiplier);
  258. break;
  259. default:
  260. UNIMPLEMENTED();
  261. break;
  262. }
  263. state.current_sample_number = 0;
  264. state.next_sample_number = 0;
  265. state.current_buffer_id = buf.buffer_id;
  266. state.buffer_update = buf.from_queue;
  267. LOG_TRACE(Audio_DSP, "source_id=%zu buffer_id=%hu from_queue=%s current_buffer.size()=%zu",
  268. source_id, buf.buffer_id, buf.from_queue ? "true" : "false",
  269. state.current_buffer.size());
  270. return true;
  271. }
  272. SourceStatus::Status Source::GetCurrentStatus() {
  273. SourceStatus::Status ret;
  274. // Applications depend on the correct emulation of
  275. // current_buffer_id_dirty and current_buffer_id to synchronise
  276. // audio with video.
  277. ret.is_enabled = state.enabled;
  278. ret.current_buffer_id_dirty = state.buffer_update ? 1 : 0;
  279. state.buffer_update = false;
  280. ret.current_buffer_id = state.current_buffer_id;
  281. ret.buffer_position = state.current_sample_number;
  282. ret.sync = state.sync;
  283. return ret;
  284. }
  285. } // namespace HLE
  286. } // namespace DSP