Skip to content

Commit

Permalink
Trigger MIDI bindings.
Browse files Browse the repository at this point in the history
  • Loading branch information
rerdavies committed Dec 6, 2024
1 parent 9d64cf1 commit b07f590
Show file tree
Hide file tree
Showing 10 changed files with 359 additions and 118 deletions.
17 changes: 14 additions & 3 deletions react/src/MidiBinding.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,8 @@ export default class MidiBinding {
result.push(new MidiBinding().deserialize(input[i]));
}
return result;

}

clone(): MidiBinding { return new MidiBinding().deserialize(this);}
equals(other: MidiBinding) : boolean
{
Expand All @@ -62,6 +62,12 @@ export default class MidiBinding {
static BINDING_TYPE_NOTE: number = 1;
static BINDING_TYPE_CONTROL: number = 2;

setBindingType(bindingType:number)
{
this.bindingType = bindingType;
}


symbol: string = "";

bindingType: number = MidiBinding.BINDING_TYPE_NONE;
Expand All @@ -76,9 +82,14 @@ export default class MidiBinding {

linearControlType: number = MidiBinding.LINEAR_CONTROL_TYPE;

static LATCH_CONTROL_TYPE: number = 0;
static TRIGGER_ON_RISING_EDGE: number = 0;
static TOGGLE_ON_RISING_EDGE: number = 0;
static MOMENTARY_CONTROL_TYPE: number = 1;
static TOGGLE_ON_VALUE: number = 1;
static TOGGLE_ON_ANY: number = 2;
static TRIGGER_ON_ANY: number = 2;


switchControlType: number = MidiBinding.LATCH_CONTROL_TYPE;
switchControlType: number = MidiBinding.TRIGGER_ON_RISING_EDGE;

};
213 changes: 144 additions & 69 deletions react/src/MidiBindingView.tsx

Large diffs are not rendered by default.

4 changes: 4 additions & 0 deletions react/src/PiPedalModel.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -1488,6 +1488,10 @@ export class PiPedalModel //implements PiPedalModel
}


sendPedalboardControlTrigger(instanceId: number, key: string, value: number) : void {
// no state change, no saving the value, just send it to the realtime thread/
this._setServerControl("previewControl", instanceId, key, value);
}


setPedalboardControl(instanceId: number, key: string, value: number): void {
Expand Down
13 changes: 7 additions & 6 deletions react/src/PluginControl.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -502,15 +502,16 @@ const PluginControl =
let uiControl = this.props.uiControl;
if (uiControl)
{
this.model.setPedalboardControl(this.props.instanceId,uiControl.symbol,1);
let value = uiControl.max_value;
if (uiControl.max_value === uiControl.default_value)
{
value = uiControl.min_value;
}
this.model.sendPedalboardControlTrigger(this.props.instanceId,uiControl.symbol,value);
}
}
handleTriggerMouseUp() {
let uiControl = this.props.uiControl;
if (uiControl)
{
this.model.setPedalboardControl(this.props.instanceId,uiControl.symbol,0);
}
// triggers are reset on the audio thread.
}
previewInputValue(value: number, commitValue: boolean) {
let range = this.valueToRange(value);
Expand Down
12 changes: 9 additions & 3 deletions src/Lv2Effect.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -305,6 +305,7 @@ void Lv2Effect::PreparePortIndices()
size_t nPorts = info->ports().size();
isInputControlPort.resize(nPorts);
this->defaultInputControlValues.resize(nPorts);
this->isInputTriggerControlPort.resize(nPorts);

for (int i = 0; i < info->ports().size(); ++i)
{
Expand Down Expand Up @@ -343,11 +344,16 @@ void Lv2Effect::PreparePortIndices()
}
else if (port->is_control_port())
{
controlIndex[port->symbol()] = port->index();
controlIndex[port->symbol()] = portIndex;
if (port->is_input())
{
this->isInputControlPort[i] = true;
this->defaultInputControlValues[i] = port->default_value();
this->isInputControlPort[portIndex] = true;
this->defaultInputControlValues[portIndex] = port->default_value();
if (port->trigger_property())
{
this->isInputTriggerControlPort[portIndex] = true;
}

}
}
}
Expand Down
1 change: 1 addition & 0 deletions src/Lv2Effect.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ namespace pipedal
uint64_t maxInputControlPort = 0;
std::vector<bool> isInputControlPort;
std::vector<float> defaultInputControlValues;
std::vector<bool> isInputTriggerControlPort;;

virtual std::string GetUri() const { return info->uri(); }

Expand Down
145 changes: 121 additions & 24 deletions src/Lv2Pedalboard.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,33 @@ std::vector<float *> Lv2Pedalboard::PrepareItems(
{
pLv2Effect->Run(frames, this->ringBufferWriter);
});

// Reset any trigger controls to default state after processing
if (pLv2Effect->IsLv2Effect())
{
Lv2Effect*lv2Effect = (Lv2Effect*)pLv2Effect;
auto pluginInfo = pHost->GetPluginInfo(item.uri());
if (pluginInfo) {
for (auto control: pluginInfo->ports())
{
if (control->trigger_property() && control->is_input() && control->is_control_port())
{
int controlIndex = lv2Effect->GetControlIndex(control->symbol());
if (controlIndex >= 0)
{
float defaultValue = control->default_value();
this->processActions.push_back(
[pLv2Effect,controlIndex,defaultValue](int32_t frames) {
pLv2Effect->SetControl(controlIndex,defaultValue);
}
);
}

}
}
}

}
}
}
if (pEffect)
Expand Down Expand Up @@ -287,13 +314,21 @@ void Lv2Pedalboard::PrepareMidiMap(const PedalboardItem &pedalboardItem)
mapping.controlIndex = controlIndex;
mapping.midiBinding = binding;
mapping.instanceId = pedalboardItem.instanceId();
if (pPortInfo->IsSwitch())

if (pPortInfo->trigger_property())
{
mapping.mappingType = MidiControlType::Trigger;
} else if (pPortInfo->IsSwitch())
{
mapping.mappingType = binding.switchControlType() == LATCH_CONTROL_TYPE ? MappingType::Latched : MappingType::Momentary;
mapping.mappingType = MidiControlType::Toggle;
}
else
else if (pPortInfo->enumeration_property())
{
mapping.mappingType = MidiControlType::Select;
}
else
{
mapping.mappingType = binding.linearControlType() == LATCH_CONTROL_TYPE ? MappingType::Linear : MappingType::Circular;
mapping.mappingType = MidiControlType::Dial;
}
if (binding.bindingType() == BINDING_TYPE_NOTE)
{
Expand Down Expand Up @@ -651,46 +686,108 @@ void Lv2Pedalboard::OnMidiMessage(size_t size, uint8_t *message,
{
switch (mapping.mappingType)
{
case MappingType::Circular:
case MappingType::Linear:
case MidiControlType::Trigger:
{
float thisRange = (mapping.midiBinding.maxValue() - mapping.midiBinding.minValue()) * range + mapping.midiBinding.minValue();
float value = mapping.pPortInfo->rangeToValue(thisRange);
this->SetControlValue(mapping.effectIndex, mapping.controlIndex, value);
pfnCallback(callbackHandle, mapping.instanceId, mapping.pPortInfo->index(), value);
bool triggered = false;
if (mapping.midiBinding.switchControlType() == SwitchControlTypeT::TRIGGER_ON_RISING_EDGE
|| mapping.midiBinding.bindingType() == BINDING_TYPE_NOTE
)
{
if (mapping.lastValue < range)
{
if (!mapping.lastValueIncreasing) {
triggered = true;
}
mapping.lastValueIncreasing = true;
mapping.lastValue = range;
} else {
mapping.lastValueIncreasing = false;
mapping.lastValue = range;
}
} else {
triggered = true;
}
if (triggered)
{
IEffect *pEffect = this->realtimeEffects[mapping.effectIndex];
float value = mapping.pPortInfo->max_value();
if (value == mapping.pPortInfo->default_value())
{
value = mapping.pPortInfo->min_value();
}
this->SetControlValue(mapping.effectIndex, mapping.controlIndex, value);
// do NOT notify anyone!
}
break;
}
case MappingType::Latched:
case MidiControlType::Toggle:
{
bool triggered = false;

range = std::round(range);

if (!mapping.hasLastValue)
if (mapping.midiBinding.switchControlType() == SwitchControlTypeT::TOGGLE_ON_RISING_EDGE)
{
mapping.lastValue = 0;
mapping.hasLastValue = true;
}
if (range != mapping.lastValue && range == 1)
if (range > mapping.lastValue)
{
if (!mapping.lastValueIncreasing) {
triggered = true;
}
mapping.lastValueIncreasing = true;
mapping.lastValue = range;
} else {
mapping.lastValueIncreasing = false;
mapping.lastValue = range;
}
if (triggered)
{
IEffect *pEffect = this->realtimeEffects[mapping.effectIndex];
float currentValue = pEffect->GetControlValue(mapping.controlIndex);

currentValue = currentValue == 0 ? 1 : 0;
pEffect->SetControl(mapping.controlIndex, currentValue);
pfnCallback(callbackHandle, mapping.instanceId, mapping.pPortInfo->index(), currentValue);
}
} else if (mapping.midiBinding.switchControlType() == SwitchControlTypeT::TOGGLE_ON_VALUE)
{
IEffect *pEffect = this->realtimeEffects[mapping.effectIndex];
triggered = true;
mapping.lastValue = range;
IEffect *pEffect = this->realtimeEffects[mapping.effectIndex];
float currentValue = pEffect->GetControlValue(mapping.controlIndex);
if (currentValue != range)
{
pEffect->SetControl(mapping.controlIndex, range);
pfnCallback(callbackHandle, mapping.instanceId, mapping.pPortInfo->index(), range);
}

} else {
// any control value toggles.
triggered = true;
mapping.lastValue = range;
IEffect *pEffect = this->realtimeEffects[mapping.effectIndex];
float currentValue = pEffect->GetControlValue(mapping.controlIndex);
currentValue = currentValue == 0 ? 1 : 0;
currentValue = currentValue == 0 ? 1: 0;
pEffect->SetControl(mapping.controlIndex, currentValue);
pfnCallback(callbackHandle, mapping.instanceId, mapping.pPortInfo->index(), currentValue);
}
mapping.lastValue = range;
break;
}
case MappingType::Momentary:
case MidiControlType::Select:
case MidiControlType::Dial:
{
IEffect *pEffect = this->realtimeEffects[mapping.effectIndex];
float value = mapping.pPortInfo->rangeToValue(range);
if (pEffect->GetControlValue(mapping.controlIndex) != value)
range = mapping.midiBinding.adjustRange(range);
float currentValue = mapping.pPortInfo->rangeToValue(range);
if (pEffect->GetControlValue(mapping.controlIndex) != currentValue)
{
this->SetControlValue(mapping.effectIndex, mapping.controlIndex, value);
pfnCallback(callbackHandle, mapping.instanceId, mapping.pPortInfo->index(), value);
this->SetControlValue(mapping.effectIndex, mapping.controlIndex,currentValue);
pfnCallback(callbackHandle, mapping.instanceId, mapping.pPortInfo->index(), currentValue);
}
break;
}
case MidiControlType::None:
default:
break;
}
}
}
Expand Down
16 changes: 9 additions & 7 deletions src/Lv2Pedalboard.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -72,12 +72,13 @@ namespace pipedal

RealtimeRingBufferWriter *ringBufferWriter;

enum class MappingType
enum class MidiControlType
{
Linear,
Circular,
Momentary,
Latched,
None,
Select,
Dial,
Toggle,
Trigger
};
class MidiMapping
{
Expand All @@ -89,8 +90,9 @@ namespace pipedal
int controlIndex = -1;
int key; // key to the note or control. internal use only.
bool hasLastValue = false;
float lastValue = -1;
MappingType mappingType;
bool lastValueIncreasing = false;
float lastValue = 0;
MidiControlType mappingType;
MidiBinding midiBinding;
};

Expand Down
25 changes: 21 additions & 4 deletions src/MidiBinding.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -57,10 +57,20 @@ class MapFeature;
const int LINEAR_CONTROL_TYPE = 0;
const int CIRCULAR_CONTROL_TYPE = 1;

const int LATCH_CONTROL_TYPE = 0;
const int MOMENTARY_CONTROL_TYPE = 1;


enum class SwitchControlTypeT {
TRIGGER_ON_RISING_EDGE = 0,
TRIGGER_ON_ANY = 2,
TOGGLE_ON_RISING_EDGE = 0,
TOGGLE_ON_VALUE = 1,
TOGGLE_ON_ANY = 2,

LATCH_CONTROL_TYPE = 0,
MOMENTARY_CONTROL_TYPE = 1,

};


class MidiBinding {
public:
Expand All @@ -74,7 +84,7 @@ class MidiBinding {
float maxValue_ = 1;
float rotaryScale_ = 1;
int linearControlType_ = LINEAR_CONTROL_TYPE;
int switchControlType_ = LATCH_CONTROL_TYPE;
int switchControlType_ = (int)SwitchControlTypeT::LATCH_CONTROL_TYPE;
public:
static MidiBinding SystemBinding(const std::string&symbol)
{
Expand Down Expand Up @@ -105,8 +115,15 @@ class MidiBinding {
GETTER_SETTER(maxValue);
GETTER_SETTER(rotaryScale);
GETTER_SETTER(linearControlType);
GETTER_SETTER(switchControlType);

SwitchControlTypeT switchControlType() const { return (SwitchControlTypeT)switchControlType_; }
void switchControlType(SwitchControlTypeT value) { switchControlType_ = (int)value; }


float adjustRange(float range)
{
return maxValue_*range+minValue_*(1.0f-range);
}
DECLARE_JSON_MAP(MidiBinding);

};
Expand Down
Loading

0 comments on commit b07f590

Please sign in to comment.