🛠️🐜 Antkeeper superbuild with dependencies included https://antkeeper.com
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

316 lines
9.9 KiB

  1. #include "config.h"
  2. #include <stdexcept>
  3. #include "AL/al.h"
  4. #include "AL/efx.h"
  5. #include "alc/effects/base.h"
  6. #include "aloptional.h"
  7. #include "effects.h"
  8. #ifdef ALSOFT_EAX
  9. #include <cassert>
  10. #include "alnumeric.h"
  11. #include "al/eax/exception.h"
  12. #include "al/eax/utils.h"
  13. #endif // ALSOFT_EAX
  14. namespace {
  15. al::optional<FShifterDirection> DirectionFromEmum(ALenum value)
  16. {
  17. switch(value)
  18. {
  19. case AL_FREQUENCY_SHIFTER_DIRECTION_DOWN: return al::make_optional(FShifterDirection::Down);
  20. case AL_FREQUENCY_SHIFTER_DIRECTION_UP: return al::make_optional(FShifterDirection::Up);
  21. case AL_FREQUENCY_SHIFTER_DIRECTION_OFF: return al::make_optional(FShifterDirection::Off);
  22. }
  23. return al::nullopt;
  24. }
  25. ALenum EnumFromDirection(FShifterDirection dir)
  26. {
  27. switch(dir)
  28. {
  29. case FShifterDirection::Down: return AL_FREQUENCY_SHIFTER_DIRECTION_DOWN;
  30. case FShifterDirection::Up: return AL_FREQUENCY_SHIFTER_DIRECTION_UP;
  31. case FShifterDirection::Off: return AL_FREQUENCY_SHIFTER_DIRECTION_OFF;
  32. }
  33. throw std::runtime_error{"Invalid direction: "+std::to_string(static_cast<int>(dir))};
  34. }
  35. void Fshifter_setParamf(EffectProps *props, ALenum param, float val)
  36. {
  37. switch(param)
  38. {
  39. case AL_FREQUENCY_SHIFTER_FREQUENCY:
  40. if(!(val >= AL_FREQUENCY_SHIFTER_MIN_FREQUENCY && val <= AL_FREQUENCY_SHIFTER_MAX_FREQUENCY))
  41. throw effect_exception{AL_INVALID_VALUE, "Frequency shifter frequency out of range"};
  42. props->Fshifter.Frequency = val;
  43. break;
  44. default:
  45. throw effect_exception{AL_INVALID_ENUM, "Invalid frequency shifter float property 0x%04x",
  46. param};
  47. }
  48. }
  49. void Fshifter_setParamfv(EffectProps *props, ALenum param, const float *vals)
  50. { Fshifter_setParamf(props, param, vals[0]); }
  51. void Fshifter_setParami(EffectProps *props, ALenum param, int val)
  52. {
  53. switch(param)
  54. {
  55. case AL_FREQUENCY_SHIFTER_LEFT_DIRECTION:
  56. if(auto diropt = DirectionFromEmum(val))
  57. props->Fshifter.LeftDirection = *diropt;
  58. else
  59. throw effect_exception{AL_INVALID_VALUE,
  60. "Unsupported frequency shifter left direction: 0x%04x", val};
  61. break;
  62. case AL_FREQUENCY_SHIFTER_RIGHT_DIRECTION:
  63. if(auto diropt = DirectionFromEmum(val))
  64. props->Fshifter.RightDirection = *diropt;
  65. else
  66. throw effect_exception{AL_INVALID_VALUE,
  67. "Unsupported frequency shifter right direction: 0x%04x", val};
  68. break;
  69. default:
  70. throw effect_exception{AL_INVALID_ENUM,
  71. "Invalid frequency shifter integer property 0x%04x", param};
  72. }
  73. }
  74. void Fshifter_setParamiv(EffectProps *props, ALenum param, const int *vals)
  75. { Fshifter_setParami(props, param, vals[0]); }
  76. void Fshifter_getParami(const EffectProps *props, ALenum param, int *val)
  77. {
  78. switch(param)
  79. {
  80. case AL_FREQUENCY_SHIFTER_LEFT_DIRECTION:
  81. *val = EnumFromDirection(props->Fshifter.LeftDirection);
  82. break;
  83. case AL_FREQUENCY_SHIFTER_RIGHT_DIRECTION:
  84. *val = EnumFromDirection(props->Fshifter.RightDirection);
  85. break;
  86. default:
  87. throw effect_exception{AL_INVALID_ENUM,
  88. "Invalid frequency shifter integer property 0x%04x", param};
  89. }
  90. }
  91. void Fshifter_getParamiv(const EffectProps *props, ALenum param, int *vals)
  92. { Fshifter_getParami(props, param, vals); }
  93. void Fshifter_getParamf(const EffectProps *props, ALenum param, float *val)
  94. {
  95. switch(param)
  96. {
  97. case AL_FREQUENCY_SHIFTER_FREQUENCY:
  98. *val = props->Fshifter.Frequency;
  99. break;
  100. default:
  101. throw effect_exception{AL_INVALID_ENUM, "Invalid frequency shifter float property 0x%04x",
  102. param};
  103. }
  104. }
  105. void Fshifter_getParamfv(const EffectProps *props, ALenum param, float *vals)
  106. { Fshifter_getParamf(props, param, vals); }
  107. EffectProps genDefaultProps() noexcept
  108. {
  109. EffectProps props{};
  110. props.Fshifter.Frequency = AL_FREQUENCY_SHIFTER_DEFAULT_FREQUENCY;
  111. props.Fshifter.LeftDirection = *DirectionFromEmum(AL_FREQUENCY_SHIFTER_DEFAULT_LEFT_DIRECTION);
  112. props.Fshifter.RightDirection = *DirectionFromEmum(AL_FREQUENCY_SHIFTER_DEFAULT_RIGHT_DIRECTION);
  113. return props;
  114. }
  115. } // namespace
  116. DEFINE_ALEFFECT_VTABLE(Fshifter);
  117. const EffectProps FshifterEffectProps{genDefaultProps()};
  118. #ifdef ALSOFT_EAX
  119. namespace {
  120. class EaxFrequencyShifterEffectException : public EaxException
  121. {
  122. public:
  123. explicit EaxFrequencyShifterEffectException(const char* message)
  124. : EaxException{"EAX_FREQUENCY_SHIFTER_EFFECT", message}
  125. {}
  126. }; // EaxFrequencyShifterEffectException
  127. class EaxFrequencyShifterEffect final : public EaxEffect4<EaxFrequencyShifterEffectException, EAXFREQUENCYSHIFTERPROPERTIES> {
  128. public:
  129. EaxFrequencyShifterEffect(const EaxCall& call);
  130. private:
  131. struct FrequencyValidator {
  132. void operator()(float flFrequency) const
  133. {
  134. eax_validate_range<Exception>(
  135. "Frequency",
  136. flFrequency,
  137. EAXFREQUENCYSHIFTER_MINFREQUENCY,
  138. EAXFREQUENCYSHIFTER_MAXFREQUENCY);
  139. }
  140. }; // FrequencyValidator
  141. struct LeftDirectionValidator {
  142. void operator()(unsigned long ulLeftDirection) const
  143. {
  144. eax_validate_range<Exception>(
  145. "Left Direction",
  146. ulLeftDirection,
  147. EAXFREQUENCYSHIFTER_MINLEFTDIRECTION,
  148. EAXFREQUENCYSHIFTER_MAXLEFTDIRECTION);
  149. }
  150. }; // LeftDirectionValidator
  151. struct RightDirectionValidator {
  152. void operator()(unsigned long ulRightDirection) const
  153. {
  154. eax_validate_range<Exception>(
  155. "Right Direction",
  156. ulRightDirection,
  157. EAXFREQUENCYSHIFTER_MINRIGHTDIRECTION,
  158. EAXFREQUENCYSHIFTER_MAXRIGHTDIRECTION);
  159. }
  160. }; // RightDirectionValidator
  161. struct AllValidator {
  162. void operator()(const Props& all) const
  163. {
  164. FrequencyValidator{}(all.flFrequency);
  165. LeftDirectionValidator{}(all.ulLeftDirection);
  166. RightDirectionValidator{}(all.ulRightDirection);
  167. }
  168. }; // AllValidator
  169. void set_defaults(Props& props) override;
  170. void set_efx_frequency() noexcept;
  171. void set_efx_left_direction();
  172. void set_efx_right_direction();
  173. void set_efx_defaults() override;
  174. void get(const EaxCall& call, const Props& props) override;
  175. void set(const EaxCall& call, Props& props) override;
  176. bool commit_props(const Props& props) override;
  177. }; // EaxFrequencyShifterEffect
  178. EaxFrequencyShifterEffect::EaxFrequencyShifterEffect(const EaxCall& call)
  179. : EaxEffect4{AL_EFFECT_FREQUENCY_SHIFTER, call}
  180. {}
  181. void EaxFrequencyShifterEffect::set_defaults(Props& props)
  182. {
  183. props.flFrequency = EAXFREQUENCYSHIFTER_DEFAULTFREQUENCY;
  184. props.ulLeftDirection = EAXFREQUENCYSHIFTER_DEFAULTLEFTDIRECTION;
  185. props.ulRightDirection = EAXFREQUENCYSHIFTER_DEFAULTRIGHTDIRECTION;
  186. }
  187. void EaxFrequencyShifterEffect::set_efx_frequency() noexcept
  188. {
  189. al_effect_props_.Fshifter.Frequency = clamp(
  190. props_.flFrequency,
  191. AL_FREQUENCY_SHIFTER_MIN_FREQUENCY,
  192. AL_FREQUENCY_SHIFTER_MAX_FREQUENCY);
  193. }
  194. void EaxFrequencyShifterEffect::set_efx_left_direction()
  195. {
  196. const auto left_direction = clamp(
  197. static_cast<ALint>(props_.ulLeftDirection),
  198. AL_FREQUENCY_SHIFTER_MIN_LEFT_DIRECTION,
  199. AL_FREQUENCY_SHIFTER_MAX_LEFT_DIRECTION);
  200. const auto efx_left_direction = DirectionFromEmum(left_direction);
  201. assert(efx_left_direction.has_value());
  202. al_effect_props_.Fshifter.LeftDirection = *efx_left_direction;
  203. }
  204. void EaxFrequencyShifterEffect::set_efx_right_direction()
  205. {
  206. const auto right_direction = clamp(
  207. static_cast<ALint>(props_.ulRightDirection),
  208. AL_FREQUENCY_SHIFTER_MIN_RIGHT_DIRECTION,
  209. AL_FREQUENCY_SHIFTER_MAX_RIGHT_DIRECTION);
  210. const auto efx_right_direction = DirectionFromEmum(right_direction);
  211. assert(efx_right_direction.has_value());
  212. al_effect_props_.Fshifter.RightDirection = *efx_right_direction;
  213. }
  214. void EaxFrequencyShifterEffect::set_efx_defaults()
  215. {
  216. set_efx_frequency();
  217. set_efx_left_direction();
  218. set_efx_right_direction();
  219. }
  220. void EaxFrequencyShifterEffect::get(const EaxCall& call, const Props& props)
  221. {
  222. switch(call.get_property_id())
  223. {
  224. case EAXFREQUENCYSHIFTER_NONE: break;
  225. case EAXFREQUENCYSHIFTER_ALLPARAMETERS: call.set_value<Exception>(props); break;
  226. case EAXFREQUENCYSHIFTER_FREQUENCY: call.set_value<Exception>(props.flFrequency); break;
  227. case EAXFREQUENCYSHIFTER_LEFTDIRECTION: call.set_value<Exception>(props.ulLeftDirection); break;
  228. case EAXFREQUENCYSHIFTER_RIGHTDIRECTION: call.set_value<Exception>(props.ulRightDirection); break;
  229. default: fail_unknown_property_id();
  230. }
  231. }
  232. void EaxFrequencyShifterEffect::set(const EaxCall& call, Props& props)
  233. {
  234. switch(call.get_property_id())
  235. {
  236. case EAXFREQUENCYSHIFTER_NONE: break;
  237. case EAXFREQUENCYSHIFTER_ALLPARAMETERS: defer<AllValidator>(call, props); break;
  238. case EAXFREQUENCYSHIFTER_FREQUENCY: defer<FrequencyValidator>(call, props.flFrequency); break;
  239. case EAXFREQUENCYSHIFTER_LEFTDIRECTION: defer<LeftDirectionValidator>(call, props.ulLeftDirection); break;
  240. case EAXFREQUENCYSHIFTER_RIGHTDIRECTION: defer<RightDirectionValidator>(call, props.ulRightDirection); break;
  241. default: fail_unknown_property_id();
  242. }
  243. }
  244. bool EaxFrequencyShifterEffect::commit_props(const Props& props)
  245. {
  246. auto is_dirty = false;
  247. if (props_.flFrequency != props.flFrequency)
  248. {
  249. is_dirty = true;
  250. set_efx_frequency();
  251. }
  252. if (props_.ulLeftDirection != props.ulLeftDirection)
  253. {
  254. is_dirty = true;
  255. set_efx_left_direction();
  256. }
  257. if (props_.ulRightDirection != props.ulRightDirection)
  258. {
  259. is_dirty = true;
  260. set_efx_right_direction();
  261. }
  262. return is_dirty;
  263. }
  264. } // namespace
  265. EaxEffectUPtr eax_create_eax_frequency_shifter_effect(const EaxCall& call)
  266. {
  267. return eax_create_eax4_effect<EaxFrequencyShifterEffect>(call);
  268. }
  269. #endif // ALSOFT_EAX