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.

103 lines
2.9KB

  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. // Poly LFO.
  28. #ifndef FRAMES_POLY_LFO_H_
  29. #define FRAMES_POLY_LFO_H_
  30. #include "stmlib/stmlib.h"
  31. #include "frames/keyframer.h"
  32. namespace frames {
  33. class PolyLfo {
  34. public:
  35. PolyLfo() { }
  36. ~PolyLfo() { }
  37. void Init();
  38. void Render(int32_t frequency);
  39. inline void set_shape(uint16_t shape) {
  40. shape_ = shape;
  41. }
  42. inline void set_shape_spread(uint16_t shape_spread) {
  43. shape_spread_ = static_cast<int16_t>(shape_spread - 32768) >> 1;
  44. }
  45. inline void set_spread(uint16_t spread) {
  46. if (spread < 32768) {
  47. int32_t x = spread - 32768;
  48. int32_t scaled = -(x * x >> 15);
  49. spread_ = (x + 3 * scaled) >> 2;
  50. } else {
  51. spread_ = spread - 32768;
  52. }
  53. }
  54. inline void set_coupling(uint16_t coupling) {
  55. int32_t x = coupling - 32768;
  56. int32_t scaled = x * x >> 15;
  57. scaled = x > 0 ? scaled : - scaled;
  58. scaled = (x + 3 * scaled) >> 2;
  59. coupling_ = (scaled >> 4) * 10;
  60. }
  61. inline uint8_t level(uint8_t index) const {
  62. return level_[index];
  63. }
  64. inline const uint8_t* color() const {
  65. return &color_[0];
  66. }
  67. inline const uint16_t dac_code(uint8_t index) const {
  68. return dac_code_[index];
  69. }
  70. static uint32_t FrequencyToPhaseIncrement(int32_t frequency);
  71. void Reset();
  72. void Randomize();
  73. private:
  74. static const uint8_t rainbow_[17][3];
  75. uint16_t shape_;
  76. int16_t shape_spread_;
  77. int32_t spread_;
  78. int16_t coupling_;
  79. int16_t value_[kNumChannels];
  80. uint32_t phase_[kNumChannels];
  81. uint8_t level_[kNumChannels];
  82. uint16_t dac_code_[kNumChannels];
  83. uint8_t color_[3];
  84. DISALLOW_COPY_AND_ASSIGN(PolyLfo);
  85. };
  86. } // namespace frames
  87. #endif // FRAMES_POLY_LFO_H_