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.

140 lines
4.1KB

  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. #include "frames/poly_lfo.h"
  29. #include <cstdio>
  30. #include <algorithm>
  31. #include "stmlib/utils/dsp.h"
  32. #include "frames/resources.h"
  33. #include "stmlib/utils/random.h"
  34. namespace frames {
  35. using namespace std;
  36. using namespace stmlib;
  37. /* static */
  38. const uint8_t PolyLfo::rainbow_[17][3] = {
  39. { 255, 0, 0 },
  40. { 255, 32, 0 },
  41. { 255, 192, 0 },
  42. { 255, 240, 0 },
  43. { 240, 255, 0 },
  44. { 192, 255, 0 },
  45. { 32, 255, 0 },
  46. { 0, 255, 0 },
  47. { 0, 255, 32 },
  48. { 0, 255, 192 },
  49. { 0, 255, 255 },
  50. { 0, 192, 255 },
  51. { 0, 32, 255 },
  52. { 0, 0, 255 },
  53. { 32, 0, 255 },
  54. { 192, 0, 192 },
  55. { 255, 0, 128 },
  56. };
  57. void PolyLfo::Init() {
  58. spread_ = 0;
  59. shape_ = 0;
  60. shape_spread_ = 0;
  61. coupling_ = 0;
  62. std::fill(&value_[0], &value_[kNumChannels], 0);
  63. }
  64. /* static */
  65. uint32_t PolyLfo::FrequencyToPhaseIncrement(int32_t frequency) {
  66. int32_t shifts = frequency / 5040;
  67. int32_t index = frequency - shifts * 5040;
  68. uint32_t a = lut_increments[index >> 5];
  69. uint32_t b = lut_increments[(index >> 5) + 1];
  70. return (a + ((b - a) * (index & 0x1f) >> 5)) << shifts;
  71. }
  72. void PolyLfo::Render(int32_t frequency) {
  73. if (frequency < 0) frequency = 0;
  74. uint16_t rainbow_index = frequency > 65535 ? 65535 : frequency;
  75. for (uint8_t i = 0; i < 3; ++i) {
  76. int16_t a = rainbow_[rainbow_index >> 12][i];
  77. int16_t b = rainbow_[(rainbow_index >> 12) + 1][i];
  78. color_[i] = a + ((b - a) * (rainbow_index & 0x0fff) >> 12);
  79. }
  80. // Advance phasors.
  81. if (spread_ >= 0) {
  82. phase_[0] += FrequencyToPhaseIncrement(frequency);
  83. uint32_t phase_difference = static_cast<uint32_t>(spread_) << 15;
  84. phase_[1] = phase_[0] + phase_difference;
  85. phase_[2] = phase_[1] + phase_difference;
  86. phase_[3] = phase_[2] + phase_difference;
  87. } else {
  88. for (uint8_t i = 0; i < kNumChannels; ++i) {
  89. phase_[i] += FrequencyToPhaseIncrement(frequency);
  90. frequency -= 5040 * spread_ >> 15;
  91. }
  92. }
  93. const uint8_t* sine = &wt_lfo_waveforms[17 * 257];
  94. uint16_t wavetable_index = shape_;
  95. // Wavetable lookup
  96. for (uint8_t i = 0; i < kNumChannels; ++i) {
  97. uint32_t phase = phase_[i];
  98. if (coupling_ > 0) {
  99. phase += value_[(i + 1) % kNumChannels] * coupling_;
  100. } else {
  101. phase += value_[(i + kNumChannels - 1) % kNumChannels] * -coupling_;
  102. }
  103. const uint8_t* a = &wt_lfo_waveforms[(wavetable_index >> 12) * 257];
  104. const uint8_t* b = a + 257;
  105. int16_t value = Crossfade(a, b, phase, wavetable_index << 4);
  106. value_[i] = Interpolate824(sine, phase);
  107. level_[i] = (value + 32768) >> 8;
  108. dac_code_[i] = Keyframer::ConvertToDacCode(value + 32768, 0);
  109. wavetable_index += shape_spread_;
  110. }
  111. }
  112. void PolyLfo::Reset() {
  113. for (uint8_t i = 0; i < kNumChannels; ++i) {
  114. phase_[i] = 0;
  115. }
  116. }
  117. void PolyLfo::Randomize() {
  118. for (int i=0; i<4; i++)
  119. phase_[i] = Random::GetWord();
  120. }
  121. } // namespace frames