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.

124 lines
3.5KB

  1. // Copyright 2013 Olivier Gillet.
  2. //
  3. // Author: Olivier Gillet (ol.gillet@gmail.com)
  4. //
  5. // Permission is hereby granted, free of charge, to any person obtaining a copy
  6. // of this software and associated documentation files (the "Software"), to deal
  7. // in the Software without restriction, including without limitation the rights
  8. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. // copies of the Software, and to permit persons to whom the Software is
  10. // furnished to do so, subject to the following conditions:
  11. //
  12. // The above copyright notice and this permission notice shall be included in
  13. // all copies or substantial portions of the Software.
  14. //
  15. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  18. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  20. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  21. // THE SOFTWARE.
  22. //
  23. // See http://creativecommons.org/licenses/MIT/ for more information.
  24. //
  25. // -----------------------------------------------------------------------------
  26. //
  27. // Bouncing ball.
  28. #ifndef PEAKS_MODULATIONS_BOUNCING_BALL_H_
  29. #define PEAKS_MODULATIONS_BOUNCING_BALL_H_
  30. #include "stmlib/stmlib.h"
  31. #include <algorithm>
  32. #include "stmlib/utils/dsp.h"
  33. #include "peaks/resources.h"
  34. #include "peaks/gate_processor.h"
  35. namespace peaks {
  36. class BouncingBall {
  37. public:
  38. BouncingBall() { }
  39. ~BouncingBall() { }
  40. void Init() {
  41. initial_amplitude_ = 65535L << 14;
  42. gravity_ = 40;
  43. bounce_loss_ = 4095;
  44. initial_velocity_ = 0;
  45. }
  46. void Configure(uint16_t* parameter, ControlMode control_mode) {
  47. if (control_mode == CONTROL_MODE_HALF) {
  48. set_initial_amplitude(65535);
  49. set_initial_velocity(0);
  50. set_gravity(parameter[0]);
  51. set_bounce_loss(parameter[1]);
  52. } else {
  53. set_gravity(parameter[0]);
  54. set_bounce_loss(parameter[1]);
  55. set_initial_amplitude(parameter[2]);
  56. set_initial_velocity(parameter[3] - 32768);
  57. }
  58. }
  59. inline int16_t ProcessSingleSample(uint8_t control) {
  60. if (control & CONTROL_GATE_RISING) {
  61. velocity_ = initial_velocity_;
  62. position_ = initial_amplitude_;
  63. }
  64. velocity_ -= gravity_;
  65. position_ += velocity_;
  66. if (position_ < 0) {
  67. position_ = 0;
  68. velocity_ = -(velocity_ >> 12) * bounce_loss_;
  69. }
  70. if (position_ > (32767L << 15)) {
  71. position_ = 32767L << 15;
  72. velocity_ = -(velocity_ >> 12) * bounce_loss_;
  73. }
  74. return position_ >> 15;
  75. }
  76. inline void set_gravity(uint16_t gravity) {
  77. gravity_ = stmlib::Interpolate88(lut_gravity, gravity);
  78. }
  79. inline void set_bounce_loss(uint16_t bounce_loss) {
  80. uint32_t b = 65535 - bounce_loss;
  81. b = b * b >> 16;
  82. bounce_loss_ = 4095 - (b >> 4);
  83. }
  84. inline void set_initial_amplitude(uint16_t initial_amplitude) {
  85. initial_amplitude_ = static_cast<int32_t>(initial_amplitude) << 14;
  86. }
  87. inline void set_initial_velocity(int16_t initial_velocity) {
  88. initial_velocity_ = static_cast<int32_t>(initial_velocity) << 4;
  89. }
  90. inline bool FillBuffer() const {
  91. return true;
  92. }
  93. private:
  94. int32_t gravity_;
  95. int32_t bounce_loss_;
  96. int32_t initial_amplitude_;
  97. int32_t initial_velocity_;
  98. int32_t velocity_;
  99. int32_t position_;
  100. DISALLOW_COPY_AND_ASSIGN(BouncingBall);
  101. };
  102. } // namespace peaks
  103. #endif // PEAKS_MODULATIONS_BOUNCING_BALL_H_