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.6KB

  1. // Copyright 2014 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. // Lorenz system.
  28. #include "streams/lorenz_generator.h"
  29. #include "streams/resources.h"
  30. namespace streams {
  31. using namespace stmlib;
  32. const int64_t sigma = 10.0 * (1 << 24);
  33. const int64_t rho = 28.0 * (1 << 24);
  34. const int64_t beta = 8.0 / 3.0 * (1 << 24);
  35. void LorenzGenerator::Init() {
  36. x_ = 0.1 * (1 << 24);
  37. y_ = 0;
  38. z_ = 0;
  39. vcf_amount_ = 0;
  40. vca_amount_ = 0;
  41. }
  42. void LorenzGenerator::Process(
  43. int16_t audio,
  44. int16_t excite,
  45. uint16_t* gain,
  46. uint16_t* frequency) {
  47. vcf_amount_ += (target_vcf_amount_ - vcf_amount_) >> 8;
  48. vca_amount_ += (target_vca_amount_ - vca_amount_) >> 8;
  49. int32_t rate = rate_ + (excite >> 8);
  50. CONSTRAIN(rate, 0, 256);
  51. int64_t dt = static_cast<int64_t>(lut_lorenz_rate[rate]);
  52. int32_t x = x_ + (dt * ((sigma * (y_ - x_)) >> 24) >> 24);
  53. int32_t y = y_ + (dt * ((x_ * (rho - z_) >> 24) - y_) >> 24);
  54. int32_t z = z_ + (dt * ((x_ * int64_t(y_) >> 24) - (beta * z_ >> 24)) >> 24);
  55. x_ = x;
  56. y_ = y;
  57. z_ = z;
  58. int32_t z_scaled = z >> 14;
  59. int32_t x_scaled = (x >> 14) + 32768;
  60. if (index_) {
  61. // On channel 2, z and y are inverted to get more variety!
  62. z = z_scaled;
  63. z_scaled = x_scaled;
  64. x_scaled = z;
  65. }
  66. *gain = z_scaled * vca_amount_ >> 15;
  67. *frequency = 65535 + ((x_scaled - 65535) * vcf_amount_ >> 15);
  68. }
  69. } // namespace streams