FastLED 3.9.7
Loading...
Searching...
No Matches
util.cpp
1
2#include <Arduino.h>
3
4#include "./util.h"
5
6#include "ApproximatingFunction.h"
7#include "settings.h"
8
9/*
10// C - 0, C# - 1, D - 2, D# - 3... B - 11.
11// http://cote.cc/w/wp-content/uploads/drupal/blog/logic-midi-note-numbers.png
12*/
13uint8_t FundamentalNote(int midi_note) {
14 return midi_note % 12;
15}
16
17float mapf(float x, float in_min, float in_max, float out_min, float out_max) {
18 return (x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min;
19}
20
21// Given an input time.
22float AttackRemapFactor(uint32_t delta_t_ms) {
23 typedef InterpData<uint32_t, float> Datum;
24 static const Datum kData[] = {
25 Datum(0, .5),
26 Datum(80, 1.0),
27 };
28
29 static const int n = sizeof(kData) / sizeof(kData[0]);
30 return Interp(delta_t_ms, kData, n);
31}
32
33float MapDecayTime(uint8_t key_idx) {
34 typedef InterpData<uint8_t, float> Datum;
35 static const float bias = 1.3f;
36 // key then time for decay in milliseconds.
37 // First value is the KEY on the keyboard, second value is the
38 // time. The KEY must be IN ORDER or else the algorithm will fail.
39 static const Datum kInterpData[] = {
40 Datum(0, 21.0f * 1000.0f * bias),
41 Datum(11, 19.4 * 1000.0f * bias),
42 Datum(22, 15.1f * 1000.0f * bias),
43 Datum(35, 12.5f * 1000.0f * bias),
44 Datum(44, 10.f * 1000.0f * bias),
45 Datum(50, 8.1f * 1000.0f * bias),
46 Datum(53, 5.3f * 1000.0f * bias),
47 Datum(61, 4.0f * 1000.0f * bias),
48 Datum(66, 5.0f * 1000.0f * bias),
49 Datum(69, 4.6f * 1000.0f * bias),
50 Datum(70, 4.4f * 1000.0f * bias),
51 Datum(71, 4.3f * 1000.0f * bias),
52 Datum(74, 3.9f * 1000.0f * bias),
53 Datum(80, 1.9f * 1000.0f * bias),
54 Datum(81, 1.8f * 1000.0f * bias),
55 Datum(82, 1.7f * 1000.0f * bias),
56 Datum(83, 1.5f * 1000.0f * bias),
57 Datum(84, 1.3f * 1000.0f * bias),
58 Datum(86, 1.0f * 1000.0f * bias),
59 Datum(87, 0.9f * 1000.0f * bias),
60 };
61
62 static const int n = sizeof(kInterpData) / sizeof(kInterpData[0]);
63 float approx_val = Interp(key_idx, kInterpData, n);
64 return approx_val;
65}
66
67// Returns a value in the range 1->0 indicating how intense the note is. This
68// value will go to 0 as time progresses, and will be 1 when the note is first
69// pressed.
70float CalcDecayFactor(bool sustain_pedal_on,
71 bool key_on,
72 int key_idx,
73 float velocity,
74 bool dampened_key,
75 float time_elapsed_ms) {
76
77 static const float kDefaultDecayTime = .2f * 1000.f;
78 static const float kBias = 1.10;
79 float decay_time = kDefaultDecayTime; // default - no sustain.
80 if (key_on || sustain_pedal_on || !dampened_key) {
81 decay_time = MapDecayTime(key_idx) * max(0.25f, velocity);
82 }
83 // decay_interp is a value which starts off as 1.0 to signify the start of the
84 // key press and gradually decreases to 0.0. For example, if the decay time is 1 second
85 // then at the time = 0s, decay_interp is 1.0, and after one second decay_interp is 0.0
86 float intensity_factor = mapf(time_elapsed_ms,
87 0.0, decay_time * kBias,
88 1.0, 0.0); // Startup at full brightness -> no brighness.
89
90
91 // When decay_interp reaches 0, the lighting sequence is effectively finished. However
92 // because this is time based and time keeps on going this value will move into negative
93 // territory, we take care of this by simply clamping all negative values to 0.0.
94 intensity_factor = constrain(intensity_factor, 0.0f, 1.0f);
95 return intensity_factor;
96}
97
98float ToBrightness(int velocity) {
99 typedef InterpData<int, float> Datum;
100 static const Datum kData[] = {
101 Datum(0, 0.02),
102 Datum(32, 0.02),
103 Datum(64, 0.10),
104 Datum(80, 0.30),
105 Datum(90, 0.90),
106 Datum(100, 1.00),
107 Datum(120, 1.00),
108 Datum(127, 1.00)
109 };
110
111 static const int n = sizeof(kData) / sizeof(kData[0]);
112 const float val = Interp(velocity, kData, n);
113
114 return val;
115}