compressor.cpp 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. #include "config.h"
  2. #include "AL/al.h"
  3. #include "AL/efx.h"
  4. #include "alc/effects/base.h"
  5. #include "effects.h"
  6. #ifdef ALSOFT_EAX
  7. #include "alnumeric.h"
  8. #include "al/eax/effect.h"
  9. #include "al/eax/exception.h"
  10. #include "al/eax/utils.h"
  11. #endif // ALSOFT_EAX
  12. namespace {
  13. constexpr EffectProps genDefaultProps() noexcept
  14. {
  15. CompressorProps props{};
  16. props.OnOff = AL_COMPRESSOR_DEFAULT_ONOFF;
  17. return props;
  18. }
  19. } // namespace
  20. const EffectProps CompressorEffectProps{genDefaultProps()};
  21. void EffectHandler::SetParami(CompressorProps &props, ALenum param, int val)
  22. {
  23. switch(param)
  24. {
  25. case AL_COMPRESSOR_ONOFF:
  26. if(!(val >= AL_COMPRESSOR_MIN_ONOFF && val <= AL_COMPRESSOR_MAX_ONOFF))
  27. throw effect_exception{AL_INVALID_VALUE, "Compressor state out of range"};
  28. props.OnOff = (val != AL_FALSE);
  29. break;
  30. default:
  31. throw effect_exception{AL_INVALID_ENUM, "Invalid compressor integer property 0x%04x",
  32. param};
  33. }
  34. }
  35. void EffectHandler::SetParamiv(CompressorProps &props, ALenum param, const int *vals)
  36. { SetParami(props, param, *vals); }
  37. void EffectHandler::SetParamf(CompressorProps&, ALenum param, float)
  38. { throw effect_exception{AL_INVALID_ENUM, "Invalid compressor float property 0x%04x", param}; }
  39. void EffectHandler::SetParamfv(CompressorProps&, ALenum param, const float*)
  40. {
  41. throw effect_exception{AL_INVALID_ENUM, "Invalid compressor float-vector property 0x%04x",
  42. param};
  43. }
  44. void EffectHandler::GetParami(const CompressorProps &props, ALenum param, int *val)
  45. {
  46. switch(param)
  47. {
  48. case AL_COMPRESSOR_ONOFF:
  49. *val = props.OnOff;
  50. break;
  51. default:
  52. throw effect_exception{AL_INVALID_ENUM, "Invalid compressor integer property 0x%04x",
  53. param};
  54. }
  55. }
  56. void EffectHandler::GetParamiv(const CompressorProps &props, ALenum param, int *vals)
  57. { GetParami(props, param, vals); }
  58. void EffectHandler::GetParamf(const CompressorProps&, ALenum param, float*)
  59. { throw effect_exception{AL_INVALID_ENUM, "Invalid compressor float property 0x%04x", param}; }
  60. void EffectHandler::GetParamfv(const CompressorProps&, ALenum param, float*)
  61. {
  62. throw effect_exception{AL_INVALID_ENUM, "Invalid compressor float-vector property 0x%04x",
  63. param};
  64. }
  65. #ifdef ALSOFT_EAX
  66. namespace {
  67. using CompressorCommitter = EaxCommitter<EaxCompressorCommitter>;
  68. struct OnOffValidator {
  69. void operator()(unsigned long ulOnOff) const
  70. {
  71. eax_validate_range<CompressorCommitter::Exception>(
  72. "On-Off",
  73. ulOnOff,
  74. EAXAGCCOMPRESSOR_MINONOFF,
  75. EAXAGCCOMPRESSOR_MAXONOFF);
  76. }
  77. }; // OnOffValidator
  78. struct AllValidator {
  79. void operator()(const EAXAGCCOMPRESSORPROPERTIES& all) const
  80. {
  81. OnOffValidator{}(all.ulOnOff);
  82. }
  83. }; // AllValidator
  84. } // namespace
  85. template<>
  86. struct CompressorCommitter::Exception : public EaxException
  87. {
  88. explicit Exception(const char *message) : EaxException{"EAX_CHORUS_EFFECT", message}
  89. { }
  90. };
  91. template<>
  92. [[noreturn]] void CompressorCommitter::fail(const char *message)
  93. {
  94. throw Exception{message};
  95. }
  96. bool EaxCompressorCommitter::commit(const EAXAGCCOMPRESSORPROPERTIES &props)
  97. {
  98. if(auto *cur = std::get_if<EAXAGCCOMPRESSORPROPERTIES>(&mEaxProps); cur && *cur == props)
  99. return false;
  100. mEaxProps = props;
  101. mAlProps = CompressorProps{props.ulOnOff != 0};
  102. return true;
  103. }
  104. void EaxCompressorCommitter::SetDefaults(EaxEffectProps &props)
  105. {
  106. props = EAXAGCCOMPRESSORPROPERTIES{EAXAGCCOMPRESSOR_DEFAULTONOFF};
  107. }
  108. void EaxCompressorCommitter::Get(const EaxCall &call, const EAXAGCCOMPRESSORPROPERTIES &props)
  109. {
  110. switch(call.get_property_id())
  111. {
  112. case EAXAGCCOMPRESSOR_NONE: break;
  113. case EAXAGCCOMPRESSOR_ALLPARAMETERS: call.set_value<Exception>(props); break;
  114. case EAXAGCCOMPRESSOR_ONOFF: call.set_value<Exception>(props.ulOnOff); break;
  115. default: fail_unknown_property_id();
  116. }
  117. }
  118. void EaxCompressorCommitter::Set(const EaxCall &call, EAXAGCCOMPRESSORPROPERTIES &props)
  119. {
  120. switch(call.get_property_id())
  121. {
  122. case EAXAGCCOMPRESSOR_NONE: break;
  123. case EAXAGCCOMPRESSOR_ALLPARAMETERS: defer<AllValidator>(call, props); break;
  124. case EAXAGCCOMPRESSOR_ONOFF: defer<OnOffValidator>(call, props.ulOnOff); break;
  125. default: fail_unknown_property_id();
  126. }
  127. }
  128. #endif // ALSOFT_EAX