Skip to content

Commit

Permalink
Keyswitch display in editor
Browse files Browse the repository at this point in the history
  • Loading branch information
jpcima committed Feb 23, 2021
1 parent c1e47b0 commit 1e21157
Show file tree
Hide file tree
Showing 3 changed files with 83 additions and 4 deletions.
18 changes: 18 additions & 0 deletions plugins/editor/src/editor/Editor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,7 @@ struct Editor::Impl : EditorController::Receiver, IControlListener {
void updateStretchedTuningLabel(float stretchedTuning);

void updateKeyUsed(unsigned key, bool used);
void updateKeyswitchUsed(unsigned key, bool used);
void updateCCUsed(unsigned cc, bool used);
void updateCCValue(unsigned cc, float value);
void updateCCDefaultValue(unsigned cc, float value);
Expand Down Expand Up @@ -223,6 +224,7 @@ void Editor::open(CFrame& frame)

// request the whole Key and CC information
impl.sendQueuedOSC("/key/slots", "", nullptr);
impl.sendQueuedOSC("/sw/slots", "", nullptr);
impl.sendQueuedOSC("/cc/slots", "", nullptr);
}

Expand All @@ -249,6 +251,7 @@ void Editor::Impl::uiReceiveValue(EditId id, const EditValue& v)

// request the whole Key and CC information
sendQueuedOSC("/key/slots", "", nullptr);
sendQueuedOSC("/sw/slots", "", nullptr);
sendQueuedOSC("/cc/slots", "", nullptr);
}
break;
Expand Down Expand Up @@ -410,6 +413,14 @@ void Editor::Impl::uiReceiveMessage(const char* path, const char* sig, const sfi
updateKeyUsed(key, used);
}
}
else if (Messages::matchOSC("/sw/slots", path, indices) && !strcmp(sig, "b")) {
size_t numBits = 8 * args[0].b->size;
ConstBitSpan bits { args[0].b->data, numBits };
for (unsigned key = 0; key < 128; ++key) {
bool used = key < numBits && bits.test(key);
updateKeyswitchUsed(key, used);
}
}
else if (Messages::matchOSC("/cc/slots", path, indices) && !strcmp(sig, "b")) {
size_t numBits = 8 * args[0].b->size;
ConstBitSpan bits { args[0].b->data, numBits };
Expand Down Expand Up @@ -983,6 +994,7 @@ void Editor::Impl::changeSfzFile(const std::string& filePath)

// request the whole Key and CC information
sendQueuedOSC("/key/slots", "", nullptr);
sendQueuedOSC("/sw/slots", "", nullptr);
sendQueuedOSC("/cc/slots", "", nullptr);
}

Expand Down Expand Up @@ -1294,6 +1306,12 @@ void Editor::Impl::updateKeyUsed(unsigned key, bool used)
piano->setKeyUsed(key, used);
}

void Editor::Impl::updateKeyswitchUsed(unsigned key, bool used)
{
if (SPiano* piano = piano_)
piano->setKeyswitchUsed(key, used);
}

void Editor::Impl::updateCCUsed(unsigned cc, bool used)
{
if (SControlsPanel* panel = controlsPanel_)
Expand Down
58 changes: 54 additions & 4 deletions plugins/editor/src/editor/GUIPiano.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,18 @@ void SPiano::setKeyUsed(unsigned key, bool used)
invalid();
}

void SPiano::setKeyswitchUsed(unsigned key, bool used)
{
if (key >= 128)
return;

if (keyswitchUsed_.test(key) == used)
return;

keyswitchUsed_.set(key, used);
invalid();
}

void SPiano::setKeyValue(unsigned key, float value)
{
if (key >= 128)
Expand All @@ -64,6 +76,19 @@ void SPiano::setKeyValue(unsigned key, float value)
invalid();
}

SPiano::KeyRole SPiano::getKeyRole(unsigned key)
{
if (key >= 128)
return KeyRole::Unused;

if (keyUsed_.test(key))
return KeyRole::Note;
if (keyswitchUsed_.test(key))
return KeyRole::Switch;

return KeyRole::Unused;
}

void SPiano::draw(CDrawContext* dc)
{
const Dimensions dim = getDimensions(false);
Expand All @@ -85,12 +110,24 @@ void SPiano::draw(CDrawContext* dc)
if (!black[key % 12]) {
CRect rect = keyRect(key);

SColorHCY hcy(keyUsedHue_, 1.0, whiteKeyLuma_);
if (!keyUsed_[key] || allKeysUsed) {
SColorHCY hcy(0.0, 1.0, whiteKeyLuma_);

switch (getKeyRole(key)) {
case KeyRole::Note:
if (allKeysUsed)
goto whiteKeyDefault;
hcy.h = keyUsedHue_;
break;
case KeyRole::Switch:
hcy.h = keySwitchHue_;
break;
default: whiteKeyDefault:
hcy.y = 1.0;
if (keyval_[key])
hcy.c = 0.0;
break;
}

if (keyval_[key])
hcy.y = std::max(0.0f, hcy.y - keyLumaPressDelta_);

Expand All @@ -113,9 +150,22 @@ void SPiano::draw(CDrawContext* dc)
if (black[key % 12]) {
CRect rect = keyRect(key);

SColorHCY hcy(keyUsedHue_, 1.0, blackKeyLuma_);
if (!keyUsed_[key] || allKeysUsed)
SColorHCY hcy(0.0, 1.0, blackKeyLuma_);

switch (getKeyRole(key)) {
case KeyRole::Note:
if (allKeysUsed)
goto blackKeyDefault;
hcy.h = keyUsedHue_;
break;
case KeyRole::Switch:
hcy.h = keySwitchHue_;
break;
default: blackKeyDefault:
hcy.c = 0.0;
break;
}

if (keyval_[key])
hcy.y = std::max(0.0f, hcy.y - keyLumaPressDelta_);

Expand Down
11 changes: 11 additions & 0 deletions plugins/editor/src/editor/GUIPiano.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,17 @@ class SPiano : public CView {
void setNumOctaves(unsigned octs);

void setKeyUsed(unsigned key, bool used);
void setKeyswitchUsed(unsigned key, bool used);
void setKeyValue(unsigned key, float value);

enum class KeyRole {
Unused,
Note,
Switch,
};

KeyRole getKeyRole(unsigned key);

std::function<void(unsigned, float)> onKeyPressed;
std::function<void(unsigned, float)> onKeyReleased;

Expand Down Expand Up @@ -57,6 +66,7 @@ class SPiano : public CView {
unsigned octs_ {};
std::vector<float> keyval_;
std::bitset<128> keyUsed_;
std::bitset<128> keyswitchUsed_;
unsigned mousePressedKey_ = ~0u;

CCoord innerPaddingX_ = 4.0;
Expand All @@ -67,6 +77,7 @@ class SPiano : public CView {
float backgroundRadius_ = 5.0;

float keyUsedHue_ = 0.55;
float keySwitchHue_ = 0.0;
float whiteKeyLuma_ = 0.9;
float blackKeyLuma_ = 0.5;
float keyLumaPressDelta_ = 0.2;
Expand Down

0 comments on commit 1e21157

Please sign in to comment.