7#include "./color_mapper.h"
11Key::Key() : on_(false), sustained_(false), sustain_pedal_on_(false),
12 velocity_(0), idx_(0), event_time_(0) {}
14void Key::SetOn(uint8_t vel,
const ColorHSV& color, uint32_t now_ms) {
15 if (curr_color_.v_ < color.v_) {
20 orig_color_ = curr_color_;
25void Key::SetOff(uint32_t now_ms) {
26 orig_color_ = curr_color_;
32void Key::SetSustained() {
36void Key::Update(uint32_t now_ms, uint32_t delta_ms,
bool sustain_pedal_on) {
37 if (sustained_ && !sustain_pedal_on) {
41 sustain_pedal_on_ = sustain_pedal_on;
42 UpdateIntensity(now_ms, delta_ms);
45float Key::VelocityFactor()
const {
return velocity_ / 127.f; }
47float Key::CalcAttackDecayFactor(uint32_t delta_ms)
const {
48 bool dampened_key = (idx_ < kFirstNoteNoDamp);
49 float active_lights_factor = ::CalcDecayFactor(
56 return active_lights_factor;
59float Key::AttackRemapFactor(uint32_t now_ms) {
61 return ::AttackRemapFactor(now_ms - event_time_);
67float Key::IntensityFactor()
const {
71void Key::UpdateIntensity(uint32_t now_ms, uint32_t delta_ms) {
75 CalcAttackDecayFactor(now_ms - event_time_) *
77 AttackRemapFactor(now_ms);
81 intensity_ = (.9f * intensity) + (.1f * intensity_);
82 }
else if(intensity_ > 0.0f) {
84 if (sustain_pedal_on_) {
85 float delta_s = delta_ms / 1000.f;
86 if (intensity_ > .5f) {
87 const float kRate = .12f;
91 intensity_ = intensity_ * exp(-delta_s * kRate);
94 const float kRate = .05f;
95 intensity_ -= delta_s * kRate;
98 float delta_s = delta_ms / 1000.f;
99 if (intensity_ > .5f) {
100 const float kRate = 12.0f;
104 intensity_ = intensity_ * exp(-delta_s * kRate);
107 const float kRate = 2.0f;
108 intensity_ -= delta_s * kRate;
112 intensity_ = constrain(intensity_, 0.0f, 1.0f);
116void KeyboardState::HandleNoteOn(uint8_t midi_note, uint8_t velocity,
int color_selector_value, uint32_t now_ms) {
119 HandleNoteOff(midi_note, velocity, now_ms);
124 dprint(
"HandleNoteOn:");
126 dprint(
"midi_note = ");
129 dprint(
", velocity = ");
133 float brightness = ToBrightness(velocity);
135 dprint(
"brightness: "); dprintln(brightness);
137 ColorHSV pixel_color_hsv = SelectColor(midi_note, brightness,
138 color_selector_value);
142 Key* key = GetKey(midi_note);
144 dprint(
"key indx: "); dprintln(key->idx_);
146 key->SetOn(velocity, pixel_color_hsv, now_ms);
149void KeyboardState::HandleNoteOff(uint8_t midi_note, uint8_t , uint32_t now_ms) {
151 dprint(
"HandleNoteOff:");
153 dprint(
"midi_note = ");
156 dprint(
", velocity = ");
160 Key* key = GetKey(midi_note);
162 if (sustain_pedal_) {
169void KeyboardState::HandleControlChange(uint8_t d1, uint8_t d2) {
172 const bool foot_pedal = (d1 == kMidiFootPedal);
176 sustain_pedal_ = (d2 >= 64);
180void KeyboardState::HandleAfterTouchPoly(uint8_t note, uint8_t pressure) {
181 dprintln(
"HandleAfterTouchPoly");
186 dprint(
", pressure = ");
190KeyboardState::KeyboardState() : sustain_pedal_(false), keys_() {
191 for (
int i = 0; i < kNumKeys; ++i) {
196void KeyboardState::Update(uint32_t now_ms, uint32_t delta_ms) {
197 for (
int i = 0; i < kNumKeys; ++i) {
198 keys_[i].Update(now_ms, delta_ms, sustain_pedal_);
202uint8_t KeyboardState::KeyIndex(
int midi_pitch) {
204 return ::KeyIndex(midi_pitch);
207Key* KeyboardState::GetKey(
int midi_pitch) {
208 uint8_t idx = KeyIndex(midi_pitch);