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.

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