common.h 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. // Copyright 2020 yuzu Emulator Project
  2. // Licensed under GPLv2 or any later version
  3. // Refer to the license.txt file included.
  4. #pragma once
  5. #include "common/common_funcs.h"
  6. #include "common/common_types.h"
  7. #include "common/swap.h"
  8. #include "core/hle/result.h"
  9. namespace AudioCommon {
  10. namespace Audren {
  11. constexpr ResultCode ERR_INVALID_PARAMETERS{ErrorModule::Audio, 41};
  12. constexpr ResultCode ERR_SPLITTER_SORT_FAILED{ErrorModule::Audio, 43};
  13. } // namespace Audren
  14. constexpr u32_le CURRENT_PROCESS_REVISION = Common::MakeMagic('R', 'E', 'V', '9');
  15. constexpr std::size_t MAX_MIX_BUFFERS = 24;
  16. constexpr std::size_t MAX_BIQUAD_FILTERS = 2;
  17. constexpr std::size_t MAX_CHANNEL_COUNT = 6;
  18. constexpr std::size_t MAX_WAVE_BUFFERS = 4;
  19. constexpr std::size_t MAX_SAMPLE_HISTORY = 4;
  20. constexpr u32 STREAM_SAMPLE_RATE = 48000;
  21. constexpr u32 STREAM_NUM_CHANNELS = 2;
  22. constexpr s32 NO_SPLITTER = -1;
  23. constexpr s32 NO_MIX = 0x7fffffff;
  24. constexpr s32 NO_FINAL_MIX = std::numeric_limits<s32>::min();
  25. constexpr s32 FINAL_MIX = 0;
  26. constexpr s32 NO_EFFECT_ORDER = -1;
  27. constexpr std::size_t TEMP_MIX_BASE_SIZE = 0x3f00; // TODO(ogniK): Work out this constant
  28. // Any size checks seem to take the sample history into account
  29. // and our const ends up being 0x3f04, the 4 bytes are most
  30. // likely the sample history
  31. constexpr std::size_t TOTAL_TEMP_MIX_SIZE = TEMP_MIX_BASE_SIZE + AudioCommon::MAX_SAMPLE_HISTORY;
  32. constexpr f32 I3DL2REVERB_MAX_LEVEL = 5000.0f;
  33. constexpr f32 I3DL2REVERB_MIN_REFLECTION_DURATION = 0.02f;
  34. constexpr std::size_t I3DL2REVERB_TAPS = 20;
  35. constexpr std::size_t I3DL2REVERB_DELAY_LINE_COUNT = 4;
  36. using Fractional = s32;
  37. template <typename T>
  38. constexpr Fractional ToFractional(T x) {
  39. return static_cast<Fractional>(x * static_cast<T>(0x4000));
  40. }
  41. constexpr Fractional MultiplyFractional(Fractional lhs, Fractional rhs) {
  42. return static_cast<Fractional>(static_cast<s64>(lhs) * rhs >> 14);
  43. }
  44. constexpr s32 FractionalToFixed(Fractional x) {
  45. const auto s = x & (1 << 13);
  46. return static_cast<s32>(x >> 14) + s;
  47. }
  48. constexpr s32 CalculateDelaySamples(s32 sample_rate_khz, float time) {
  49. return FractionalToFixed(MultiplyFractional(ToFractional(sample_rate_khz), ToFractional(time)));
  50. }
  51. static constexpr u32 VersionFromRevision(u32_le rev) {
  52. // "REV7" -> 7
  53. return ((rev >> 24) & 0xff) - 0x30;
  54. }
  55. static constexpr bool IsRevisionSupported(u32 required, u32_le user_revision) {
  56. const auto base = VersionFromRevision(user_revision);
  57. return required <= base;
  58. }
  59. static constexpr bool IsValidRevision(u32_le revision) {
  60. const auto base = VersionFromRevision(revision);
  61. constexpr auto max_rev = VersionFromRevision(CURRENT_PROCESS_REVISION);
  62. return base <= max_rev;
  63. }
  64. static constexpr bool CanConsumeBuffer(std::size_t size, std::size_t offset, std::size_t required) {
  65. if (offset > size) {
  66. return false;
  67. }
  68. if (size < required) {
  69. return false;
  70. }
  71. if ((size - offset) < required) {
  72. return false;
  73. }
  74. return true;
  75. }
  76. struct UpdateDataSizes {
  77. u32_le behavior{};
  78. u32_le memory_pool{};
  79. u32_le voice{};
  80. u32_le voice_channel_resource{};
  81. u32_le effect{};
  82. u32_le mixer{};
  83. u32_le sink{};
  84. u32_le performance{};
  85. u32_le splitter{};
  86. u32_le render_info{};
  87. INSERT_PADDING_WORDS(4);
  88. };
  89. static_assert(sizeof(UpdateDataSizes) == 0x38, "UpdateDataSizes is an invalid size");
  90. struct UpdateDataHeader {
  91. u32_le revision{};
  92. UpdateDataSizes size{};
  93. u32_le total_size{};
  94. };
  95. static_assert(sizeof(UpdateDataHeader) == 0x40, "UpdateDataHeader is an invalid size");
  96. struct AudioRendererParameter {
  97. u32_le sample_rate;
  98. u32_le sample_count;
  99. u32_le mix_buffer_count;
  100. u32_le submix_count;
  101. u32_le voice_count;
  102. u32_le sink_count;
  103. u32_le effect_count;
  104. u32_le performance_frame_count;
  105. u8 is_voice_drop_enabled;
  106. u8 unknown_21;
  107. u8 unknown_22;
  108. u8 execution_mode;
  109. u32_le splitter_count;
  110. u32_le num_splitter_send_channels;
  111. u32_le unknown_30;
  112. u32_le revision;
  113. };
  114. static_assert(sizeof(AudioRendererParameter) == 52, "AudioRendererParameter is an invalid size");
  115. } // namespace AudioCommon