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.

89 lines
2.3KB

  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. // Exponential decay excitation.
  28. #ifndef BRAIDS_EXCITATION_H_
  29. #define BRAIDS_EXCITATION_H_
  30. #include "stmlib/stmlib.h"
  31. namespace braids {
  32. class Excitation {
  33. public:
  34. Excitation() { }
  35. ~Excitation() { }
  36. void Init() {
  37. delay_ = 0;
  38. decay_ = 4093;
  39. counter_ = 0;
  40. state_ = 0;
  41. }
  42. void set_delay(uint16_t delay) {
  43. delay_ = delay;
  44. }
  45. void set_decay(uint16_t decay) {
  46. decay_ = decay;
  47. }
  48. void Trigger(int32_t level) {
  49. level_ = level;
  50. counter_ = delay_ + 1;
  51. }
  52. bool done() {
  53. return counter_ == 0;
  54. }
  55. inline int32_t Process() {
  56. state_ = (state_ * decay_ >> 12);
  57. if (counter_ > 0) {
  58. --counter_;
  59. if (counter_ == 0) {
  60. state_ += level_ < 0 ? -level_ : level_;
  61. }
  62. }
  63. return level_ < 0 ? -state_ : state_;
  64. }
  65. private:
  66. uint32_t delay_;
  67. uint32_t decay_;
  68. int32_t counter_;
  69. int32_t state_;
  70. int32_t level_;
  71. DISALLOW_COPY_AND_ASSIGN(Excitation);
  72. };
  73. } // namespace braids
  74. #endif // BRAIDS_EXCITATION_H_