FastLED 3.9.15
Loading...
Searching...
No Matches
util.cpp
Go to the documentation of this file.
1
2#include <Arduino.h>
3
4#include "./util.h"
5
7#include "fl/math/math.h"
8#include "settings.h"
9
10/*
11// C - 0, C# - 1, D - 2, D# - 3... B - 11.
12// http://cote.cc/w/wp-content/uploads/drupal/blog/logic-midi-note-numbers.png
13*/
14uint8_t FundamentalNote(int midi_note) {
15 return midi_note % 12;
16}
17
18float mapf(float x, float in_min, float in_max, float out_min, float out_max) {
19 return (x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min;
20}
21
22// Given an input time.
23float AttackRemapFactor(uint32_t delta_t_ms) {
24 typedef InterpData<uint32_t, float> Datum;
25 static const Datum kData[] = {
26 Datum(0, .5),
27 Datum(80, 1.0),
28 };
29
30 static const int n = sizeof(kData) / sizeof(kData[0]);
31 return Interp(delta_t_ms, kData, n);
32}
33
34float MapDecayTime(uint8_t key_idx) {
35 typedef InterpData<uint8_t, float> Datum;
36 static const float bias = 1.3f;
37 // key then time for decay in milliseconds.
38 // First value is the KEY on the keyboard, second value is the
39 // time. The KEY must be IN ORDER or else the algorithm will fail.
40 static const Datum kInterpData[] = {
41 Datum(0, 21.0f * 1000.0f * bias),
42 Datum(11, 19.4 * 1000.0f * bias),
43 Datum(22, 15.1f * 1000.0f * bias),
44 Datum(35, 12.5f * 1000.0f * bias),
45 Datum(44, 10.f * 1000.0f * bias),
46 Datum(50, 8.1f * 1000.0f * bias),
47 Datum(53, 5.3f * 1000.0f * bias),
48 Datum(61, 4.0f * 1000.0f * bias),
49 Datum(66, 5.0f * 1000.0f * bias),
50 Datum(69, 4.6f * 1000.0f * bias),
51 Datum(70, 4.4f * 1000.0f * bias),
52 Datum(71, 4.3f * 1000.0f * bias),
53 Datum(74, 3.9f * 1000.0f * bias),
54 Datum(80, 1.9f * 1000.0f * bias),
55 Datum(81, 1.8f * 1000.0f * bias),
56 Datum(82, 1.7f * 1000.0f * bias),
57 Datum(83, 1.5f * 1000.0f * bias),
58 Datum(84, 1.3f * 1000.0f * bias),
59 Datum(86, 1.0f * 1000.0f * bias),
60 Datum(87, 0.9f * 1000.0f * bias),
61 };
62
63 static const int n = sizeof(kInterpData) / sizeof(kInterpData[0]);
64 float approx_val = Interp(key_idx, kInterpData, n);
65 return approx_val;
66}
67
68// Returns a value in the range 1->0 indicating how intense the note is. This
69// value will go to 0 as time progresses, and will be 1 when the note is first
70// pressed.
71float 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
99float ToBrightness(int velocity) {
100 typedef InterpData<int, float> Datum;
101 static const Datum kData[] = {
102 Datum(0, 0.02),
103 Datum(32, 0.02),
104 Datum(64, 0.10),
105 Datum(80, 0.30),
106 Datum(90, 0.90),
107 Datum(100, 1.00),
108 Datum(120, 1.00),
109 Datum(127, 1.00)
110 };
111
112 static const int n = sizeof(kData) / sizeof(kData[0]);
113 const float val = Interp(velocity, kData, n);
114
115 return val;
116}
ValT Interp(const KeyT &k, const InterpData< KeyT, ValT > *array, const int n)
int x
Definition simple.h:92
constexpr common_type_t< T, U > max(T a, U b) FL_NOEXCEPT
Definition math.h:75
float mapf(float x, float in_min, float in_max, float out_min, float out_max)
Definition util.cpp:18
float CalcDecayFactor(bool sustain_pedal_on, bool key_on, int key_idx, float velocity, bool dampened_key, float time_elapsed_ms)
Definition util.cpp:71
uint8_t FundamentalNote(int midi_note)
Definition util.cpp:14
float AttackRemapFactor(uint32_t delta_t_ms)
Definition util.cpp:23
float ToBrightness(int velocity)
Definition util.cpp:99
float MapDecayTime(uint8_t key_idx)
Definition util.cpp:34