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.

83 lines
2.5KB

  1. // Copyright 2015 Matthias Puech.
  2. //
  3. // Author: Matthias Puech (matthias.puech@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. // Smoothed random oscillator
  28. #include "../resources.h"
  29. #include "stmlib/utils/random.h"
  30. #include "stmlib/dsp/dsp.h"
  31. #ifndef CLOUDS_RANDOM_OSCILLATOR_H_
  32. #define CLOUDS_RANDOM_OSCILLATOR_H_
  33. using namespace stmlib;
  34. namespace clouds
  35. {
  36. const float kOscillationMinimumGap = 0.3f;
  37. class RandomOscillator
  38. {
  39. public:
  40. void Init() {
  41. value_ = 0.0f;
  42. next_value_ = Random::GetFloat() * 2.0f - 1.0f;
  43. }
  44. inline void set_slope(float slope) {
  45. phase_increment_ = 1.0f / fabs(next_value_ - value_) * slope;
  46. if (phase_increment_ > 1.0f)
  47. phase_increment_ = 1.0f;
  48. }
  49. float Next() {
  50. phase_ += phase_increment_;
  51. if (phase_ > 1.0f) {
  52. phase_--;
  53. value_ = next_value_;
  54. direction_ = !direction_;
  55. float rnd = (1.0f - kOscillationMinimumGap) * Random::GetFloat() + kOscillationMinimumGap;
  56. next_value_ = direction_ ?
  57. value_ + (1.0f - value_) * rnd :
  58. value_ - (1.0f + value_) * rnd;
  59. }
  60. float sin = Interpolate(lut_raised_cos, phase_, LUT_RAISED_COS_SIZE-1);
  61. return value_ + (next_value_ - value_) * sin;
  62. }
  63. private:
  64. float phase_;
  65. float phase_increment_;
  66. float value_;
  67. float next_value_;
  68. bool direction_;
  69. };
  70. }
  71. #endif