6#include "ApproximatingFunction.h"
13uint8_t FundamentalNote(
int midi_note) {
14 return midi_note % 12;
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;
22float AttackRemapFactor(uint32_t delta_t_ms) {
24 static const Datum kData[] = {
29 static const int n =
sizeof(kData) /
sizeof(kData[0]);
30 return Interp(delta_t_ms, kData, n);
33float MapDecayTime(uint8_t key_idx) {
35 static const float bias = 1.3f;
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),
62 static const int n =
sizeof(kInterpData) /
sizeof(kInterpData[0]);
63 float approx_val = Interp(key_idx, kInterpData, n);
70float CalcDecayFactor(
bool sustain_pedal_on,
75 float time_elapsed_ms) {
77 static const float kDefaultDecayTime = .2f * 1000.f;
78 static const float kBias = 1.10;
79 float decay_time = kDefaultDecayTime;
80 if (key_on || sustain_pedal_on || !dampened_key) {
81 decay_time = MapDecayTime(key_idx) * max(0.25f, velocity);
86 float intensity_factor = mapf(time_elapsed_ms,
87 0.0, decay_time * kBias,
94 intensity_factor = constrain(intensity_factor, 0.0f, 1.0f);
95 return intensity_factor;
98float ToBrightness(
int velocity) {
100 static const Datum kData[] = {
111 static const int n =
sizeof(kData) /
sizeof(kData[0]);
112 const float val = Interp(velocity, kData, n);