1 // Copyright 2013 Emilie Gillet. 2 // 3 // Author: Emilie Gillet (emilie.o.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 29 #ifndef PEAKS_MODULATIONS_BOUNCING_BALL_H_ 30 #define PEAKS_MODULATIONS_BOUNCING_BALL_H_ 31 32 #include "stmlib/stmlib.h" 33 34 #include <algorithm> 35 36 #include "stmlib/utils/dsp.h" 37 #include "peaks/resources.h" 38 39 #include "peaks/gate_processor.h" 40 41 namespace peaks { 42 43 class BouncingBall { 44 public: BouncingBall()45 BouncingBall() { } ~BouncingBall()46 ~BouncingBall() { } 47 Init()48 void Init() { 49 initial_amplitude_ = 65535L << 14; 50 gravity_ = 40; 51 bounce_loss_ = 4095; 52 initial_velocity_ = 0; 53 } 54 Configure(uint16_t * parameter,ControlMode control_mode)55 void Configure(uint16_t* parameter, ControlMode control_mode) { 56 if (control_mode == CONTROL_MODE_HALF) { 57 set_initial_amplitude(65535); 58 set_initial_velocity(0); 59 set_gravity(parameter[0]); 60 set_bounce_loss(parameter[1]); 61 } else { 62 set_gravity(parameter[0]); 63 set_bounce_loss(parameter[1]); 64 set_initial_amplitude(parameter[2]); 65 set_initial_velocity(parameter[3] - 32768); 66 } 67 } 68 Process(const GateFlags * gate_flags,int16_t * out,size_t size)69 void Process(const GateFlags* gate_flags, int16_t* out, size_t size) { 70 while (size--) { 71 GateFlags gate_flag = *gate_flags++; 72 if (gate_flag & GATE_FLAG_RISING) { 73 velocity_ = initial_velocity_; 74 position_ = initial_amplitude_; 75 } 76 velocity_ -= gravity_; 77 position_ += velocity_; 78 if (position_ < 0) { 79 position_ = 0; 80 velocity_ = -(velocity_ >> 12) * bounce_loss_; 81 } 82 if (position_ > (32767L << 15)) { 83 position_ = 32767L << 15; 84 velocity_ = -(velocity_ >> 12) * bounce_loss_; 85 } 86 *out++ = position_ >> 15; 87 } 88 } 89 set_gravity(uint16_t gravity)90 inline void set_gravity(uint16_t gravity) { 91 gravity_ = stmlib::Interpolate88(lut_gravity, gravity); 92 } 93 set_bounce_loss(uint16_t bounce_loss)94 inline void set_bounce_loss(uint16_t bounce_loss) { 95 uint32_t b = 65535 - bounce_loss; 96 b = b * b >> 16; 97 bounce_loss_ = 4095 - (b >> 4); 98 } 99 set_initial_amplitude(uint16_t initial_amplitude)100 inline void set_initial_amplitude(uint16_t initial_amplitude) { 101 initial_amplitude_ = static_cast<int32_t>(initial_amplitude) << 14; 102 } 103 set_initial_velocity(int16_t initial_velocity)104 inline void set_initial_velocity(int16_t initial_velocity) { 105 initial_velocity_ = static_cast<int32_t>(initial_velocity) << 4; 106 } 107 FillBuffer()108 inline bool FillBuffer() const { 109 return true; 110 } 111 112 private: 113 int32_t gravity_; 114 int32_t bounce_loss_; 115 int32_t initial_amplitude_; 116 int32_t initial_velocity_; 117 118 int32_t velocity_; 119 int32_t position_; 120 121 DISALLOW_COPY_AND_ASSIGN(BouncingBall); 122 }; 123 124 } // namespace peaks 125 126 #endif // PEAKS_MODULATIONS_BOUNCING_BALL_H_ 127