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