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.

155 lines
4.0KB

  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. // LFO.
  28. #ifndef PEAKS_MODULATIONS_LFO_H_
  29. #define PEAKS_MODULATIONS_LFO_H_
  30. #include "stmlib/stmlib.h"
  31. #include "stmlib/algorithms/pattern_predictor.h"
  32. #include "peaks/gate_processor.h"
  33. namespace peaks {
  34. enum LfoShape {
  35. LFO_SHAPE_SINE,
  36. LFO_SHAPE_TRIANGLE,
  37. LFO_SHAPE_SQUARE,
  38. LFO_SHAPE_STEPS,
  39. LFO_SHAPE_NOISE,
  40. LFO_SHAPE_LAST
  41. };
  42. class Lfo {
  43. public:
  44. typedef int16_t (Lfo::*ComputeSampleFn)();
  45. Lfo() { }
  46. ~Lfo() { }
  47. void Init();
  48. void Process(const GateFlags* gate_flags, int16_t* out, size_t size);
  49. void Configure(uint16_t* parameter, ControlMode control_mode) {
  50. if (control_mode == CONTROL_MODE_HALF) {
  51. if (sync_) {
  52. set_shape_integer(parameter[0]);
  53. set_parameter(parameter[1] - 32768);
  54. } else {
  55. set_rate(parameter[0]);
  56. set_shape_parameter_preset(parameter[1]);
  57. }
  58. set_reset_phase(0);
  59. set_level(40960);
  60. } else {
  61. if (sync_) {
  62. set_level(parameter[0]);
  63. set_shape_integer(parameter[1]);
  64. set_parameter(parameter[2] - 32768);
  65. set_reset_phase(parameter[3] - 32768);
  66. } else {
  67. set_level(40960);
  68. set_rate(parameter[0]);
  69. set_shape_integer(parameter[1]);
  70. set_parameter(parameter[2] - 32768);
  71. set_reset_phase(parameter[3] - 32768);
  72. }
  73. }
  74. }
  75. inline void set_rate(uint16_t rate) {
  76. rate_ = rate;
  77. }
  78. inline void set_shape(LfoShape shape) {
  79. shape_ = shape;
  80. }
  81. inline void set_shape_integer(uint16_t value) {
  82. shape_ = static_cast<LfoShape>(value * LFO_SHAPE_LAST >> 16);
  83. }
  84. void set_shape_parameter_preset(uint16_t value);
  85. inline void set_parameter(int16_t parameter) {
  86. parameter_ = parameter;
  87. }
  88. inline void set_reset_phase(int16_t reset_phase) {
  89. reset_phase_ = static_cast<int32_t>(reset_phase) << 16;
  90. }
  91. inline void set_sync(bool sync) {
  92. if (!sync_ && sync) {
  93. pattern_predictor_.Init();
  94. }
  95. sync_ = sync;
  96. }
  97. inline void set_level(uint16_t level) {
  98. level_ = level >> 1;
  99. }
  100. private:
  101. int16_t ComputeSampleSine();
  102. int16_t ComputeSampleTriangle();
  103. int16_t ComputeSampleSquare();
  104. int16_t ComputeSampleSteps();
  105. int16_t ComputeSampleNoise();
  106. uint16_t rate_;
  107. LfoShape shape_;
  108. int16_t parameter_;
  109. int32_t reset_phase_;
  110. int32_t level_;
  111. bool sync_;
  112. uint32_t sync_counter_;
  113. stmlib::PatternPredictor<32, 8> pattern_predictor_;
  114. uint32_t phase_;
  115. uint32_t phase_increment_;
  116. uint32_t period_;
  117. uint32_t end_of_attack_;
  118. uint32_t attack_factor_;
  119. uint32_t decay_factor_;
  120. int16_t previous_parameter_;
  121. int32_t value_;
  122. int32_t next_value_;
  123. static ComputeSampleFn compute_sample_fn_table_[];
  124. DISALLOW_COPY_AND_ASSIGN(Lfo);
  125. };
  126. } // namespace peaks
  127. #endif // PEAKS_MODULATIONS_LFO_H_