Returns a value in the range 1->0 indicating how intense the note is. This value will go to 0 as time progresses, and will be 1 when the note is first pressed.
| 69 | // value will go to 0 as time progresses, and will be 1 when the note is first |
| 70 | // pressed. |
| 71 | float CalcDecayFactor(bool sustain_pedal_on, |
| 72 | bool key_on, |
| 73 | int key_idx, |
| 74 | float velocity, |
| 75 | bool dampened_key, |
| 76 | float time_elapsed_ms) { |
| 77 | |
| 78 | static const float kDefaultDecayTime = .2f * 1000.f; |
| 79 | static const float kBias = 1.10; |
| 80 | float decay_time = kDefaultDecayTime; // default - no sustain. |
| 81 | if (key_on || sustain_pedal_on || !dampened_key) { |
| 82 | decay_time = MapDecayTime(key_idx) * fl::max(0.25f, velocity); |
| 83 | } |
| 84 | // decay_interp is a value which starts off as 1.0 to signify the start of the |
| 85 | // key press and gradually decreases to 0.0. For example, if the decay time is 1 second |
| 86 | // then at the time = 0s, decay_interp is 1.0, and after one second decay_interp is 0.0 |
| 87 | float intensity_factor = mapf(time_elapsed_ms, |
| 88 | 0.0, decay_time * kBias, |
| 89 | 1.0, 0.0); // Startup at full brightness -> no brighness. |
| 90 | |
| 91 | |
| 92 | // When decay_interp reaches 0, the lighting sequence is effectively finished. However |
| 93 | // because this is time based and time keeps on going this value will move into negative |
| 94 | // territory, we take care of this by simply clamping all negative values to 0.0. |
| 95 | intensity_factor = constrain(intensity_factor, 0.0f, 1.0f); |
| 96 | return intensity_factor; |
| 97 | } |
| 98 | |
| 99 | float ToBrightness(int velocity) { |
| 100 | typedef InterpData<int, float> Datum; |
no test coverage detected