Skip to content

Commit

Permalink
#1097 Ignore Mackie fader touch for learning and introduce "Learnable…
Browse files Browse the repository at this point in the history
…" option for virtual targets
  • Loading branch information
helgoboss committed Aug 14, 2024
1 parent df04983 commit 60627a6
Show file tree
Hide file tree
Showing 16 changed files with 117 additions and 47 deletions.
2 changes: 2 additions & 0 deletions api/src/persistence/target.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1301,6 +1301,8 @@ pub struct VirtualTarget {
pub id: VirtualControlElementId,
#[serde(skip_serializing_if = "Option::is_none")]
pub character: Option<VirtualControlElementCharacter>,
#[serde(skip_serializing_if = "Option::is_none")]
pub learnable: Option<bool>,
}

#[derive(Eq, PartialEq, Serialize, Deserialize)]
Expand Down
1 change: 1 addition & 0 deletions csi/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -453,6 +453,7 @@ fn virtual_target(id: String, character: VirtualControlElementCharacter) -> Opti
let t = VirtualTarget {
id: VirtualControlElementId::Named(id),
character: Some(character),
learnable: None,
};
Some(Target::Virtual(t))
}
Expand Down
17 changes: 16 additions & 1 deletion main/src/application/target_model.rs
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ pub enum TargetCommand {
SetUnit(TargetUnit),
SetControlElementCharacter(VirtualControlElementCharacter),
SetControlElementId(VirtualControlElementId),
SetLearnable(bool),
SetTargetType(ReaperTargetType),
SetAction(Option<Action>),
SetActionInvocationType(ActionInvocationType),
Expand Down Expand Up @@ -179,6 +180,7 @@ pub enum TargetProp {
Unit,
ControlElementType,
ControlElementId,
Learnable,
TargetType,
Action,
ActionInvocationType,
Expand Down Expand Up @@ -303,6 +305,10 @@ impl<'a> Change<'a> for TargetModel {
self.control_element_id = v;
One(P::ControlElementId)
}
C::SetLearnable(v) => {
self.learnable = v;
One(P::Learnable)
}
C::SetTargetType(v) => {
self.r#type = v;
One(P::TargetType)
Expand Down Expand Up @@ -661,6 +667,7 @@ pub struct TargetModel {
// # For virtual targets
control_element_character: VirtualControlElementCharacter,
control_element_id: VirtualControlElementId,
learnable: bool,
// # For REAPER targets
// TODO-low Rename this to reaper_target_type
r#type: ReaperTargetType,
Expand Down Expand Up @@ -838,6 +845,7 @@ impl Default for TargetModel {
unit: Default::default(),
control_element_character: VirtualControlElementCharacter::default(),
control_element_id: Default::default(),
learnable: true,
r#type: ReaperTargetType::Dummy,
action: None,
action_invocation_type: ActionInvocationType::default(),
Expand Down Expand Up @@ -950,6 +958,10 @@ impl TargetModel {
self.control_element_id
}

pub fn learnable(&self) -> bool {
self.learnable
}

pub fn target_type(&self) -> ReaperTargetType {
self.r#type
}
Expand Down Expand Up @@ -2622,7 +2634,10 @@ impl TargetModel {
Ok(UnresolvedCompoundMappingTarget::Reaper(Box::new(target)))
}
Virtual => {
let virtual_target = VirtualTarget::new(self.create_control_element());
let virtual_target = VirtualTarget {
control_element: self.create_control_element(),
learnable: self.learnable,
};
Ok(UnresolvedCompoundMappingTarget::Virtual(virtual_target))
}
}
Expand Down
47 changes: 32 additions & 15 deletions main/src/application/unit_model.rs
Original file line number Diff line number Diff line change
Expand Up @@ -468,12 +468,15 @@ impl UnitModel {
}
}

pub fn find_mapping_with_source(
pub fn find_mapping_with_learnable_source(
&self,
compartment: CompartmentKind,
source_value: IncomingCompoundSourceValue,
) -> Option<&SharedMapping> {
let virtual_source_value = self.virtualize_source_value(source_value);
let virtualization = self.virtualize_source_value(source_value);
if virtualization.is_none() {

Check failure on line 477 in main/src/application/unit_model.rs

View workflow job for this annotation

GitHub Actions / Clippy

this block may be rewritten with the `?` operator
return None;
}
let instance_state = self.unit.borrow();
use CompoundMappingSource::*;
self.mappings(compartment).find(|m| {
Expand All @@ -482,8 +485,8 @@ impl UnitModel {
return false;
}
let mapping_source = m.source_model.create_source();
if let (Virtual(virtual_source), Some(v)) = (&mapping_source, &virtual_source_value) {
virtual_source.control(v).is_some()
if let (Virtual(virtual_source), Some(v)) = (&mapping_source, &virtualization) {
virtual_source.control(&v.virtual_source_value).is_some()
} else {
mapping_source
.reacts_to_source_value_with(source_value)
Expand Down Expand Up @@ -694,18 +697,22 @@ impl UnitModel {
self.incoming_msg_captured_subject.next(event);
}

pub fn create_compound_source(
pub fn create_compound_source_for_learning(
&self,
event: MessageCaptureEvent,
) -> Option<CompoundMappingSource> {
// At first, try virtualized
if event.allow_virtual_sources {
if let Some(virt_source) = self
.virtualize_source_value(event.result.message())
.map(VirtualSource::from_source_value)
{
return Some(CompoundMappingSource::Virtual(virt_source));
if let Some(virtualization) = self.virtualize_source_value(event.result.message()) {
if !virtualization.learnable {
return None;
}
let virtual_source =
VirtualSource::from_source_value(virtualization.virtual_source_value);
return Some(CompoundMappingSource::Virtual(virtual_source));
}
}
// Then direct
CompoundMappingSource::from_message_capture_event(event)
}

Expand All @@ -715,7 +722,7 @@ impl UnitModel {
pub fn virtualize_source_value(
&self,
source_value: IncomingCompoundSourceValue,
) -> Option<VirtualSourceValue> {
) -> Option<SourceValueVirtualization> {
let instance_state = self.unit.borrow();
let res = self
.active_virtual_controller_mappings(&instance_state)
Expand All @@ -729,9 +736,14 @@ impl UnitModel {
ControlResult::Consumed => ControlValue::AbsoluteContinuous(UnitValue::MIN),
ControlResult::Processed(v) => v,
};
let virtual_source_value =
VirtualSourceValue::new(m.target_model.create_control_element(), control_value);
Some(virtual_source_value)
let virtualization = SourceValueVirtualization {
virtual_source_value: VirtualSourceValue::new(
m.target_model.create_control_element(),
control_value,
),
learnable: m.target_model.learnable(),
};
Some(virtualization)
});
res
}
Expand Down Expand Up @@ -1812,7 +1824,7 @@ impl UnitModel {
let mut session = shared_session.borrow_mut();
let qualified_id = session.unit.borrow().mapping_which_learns_source().get();
if let Some(qualified_id) = qualified_id {
if let Some(source) = session.create_compound_source(event) {
if let Some(source) = session.create_compound_source_for_learning(event) {
// The learn process should stop when removing a mapping but just in case,
// let's react gracefully if the mapping doesn't exist anymore (do nothing).
let _ = session.change_mapping_by_id_with_closure(
Expand Down Expand Up @@ -3095,3 +3107,8 @@ impl PresetLoadInstruction {
}
}
}

pub struct SourceValueVirtualization {
pub virtual_source_value: VirtualSourceValue,
pub learnable: bool,
}
7 changes: 2 additions & 5 deletions main/src/domain/virtual.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,11 @@ use std::str::FromStr;

#[derive(Copy, Clone, Eq, PartialEq, Debug, Hash)]
pub struct VirtualTarget {
control_element: VirtualControlElement,
pub control_element: VirtualControlElement,
pub learnable: bool,
}

impl VirtualTarget {
pub fn new(control_element: VirtualControlElement) -> VirtualTarget {
VirtualTarget { control_element }
}

pub fn control_element(&self) -> VirtualControlElement {
self.control_element
}
Expand Down
1 change: 1 addition & 0 deletions main/src/infrastructure/api/convert/defaults.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,3 +48,4 @@ pub const TARGET_SAVE_MAPPING_SNAPSHOT_ACTIVE_MAPPINGS_ONLY: bool = false;
pub const TARGET_STOP_COLUMN_IF_SLOT_EMPTY: bool = false;
pub const TARGET_USE_SELECTION_GANGING: bool = false;
pub const TARGET_USE_TRACK_GROUPING: bool = false;
pub const TARGET_LEARNABLE: bool = true;
1 change: 1 addition & 0 deletions main/src/infrastructure/api/convert/from_data/target.rs
Original file line number Diff line number Diff line change
Expand Up @@ -861,6 +861,7 @@ fn convert_virtual_target(data: TargetModelData, style: ConversionStyle) -> pers
persistence::Target::Virtual(persistence::VirtualTarget {
id: convert_control_element_id(data.control_element_index),
character: style.required_value(data.control_element_type),
learnable: style.required_value_with_default(data.learnable, defaults::TARGET_LEARNABLE),
})
}

Expand Down
1 change: 1 addition & 0 deletions main/src/infrastructure/api/convert/to_data/target.rs
Original file line number Diff line number Diff line change
Expand Up @@ -976,6 +976,7 @@ pub fn convert_target(t: Target) -> ConversionResult<TargetModelData> {
category: TargetCategory::Virtual,
control_element_type: d.character.unwrap_or_default(),
control_element_index: convert_control_element_id(d.id),
learnable: d.learnable.unwrap_or(defaults::TARGET_LEARNABLE),
..Default::default()
},
};
Expand Down
4 changes: 4 additions & 0 deletions main/src/infrastructure/data/target_model_data.rs
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,8 @@ pub struct TargetModelData {
skip_serializing_if = "is_default"
)]
pub control_element_index: VirtualControlElementIdData,
#[serde(default = "bool_true", skip_serializing_if = "is_bool_true")]
pub learnable: bool,
#[serde(
default,
deserialize_with = "deserialize_null_default",
Expand Down Expand Up @@ -567,6 +569,7 @@ impl TargetModelData {
control_element_index: VirtualControlElementIdData::from_model(
model.control_element_id(),
),
learnable: model.learnable(),
fx_snapshot: model.fx_snapshot().cloned(),
touched_parameter_type: model.touched_track_parameter_type(),
touched_route_parameter_type: model.touched_route_parameter_type(),
Expand Down Expand Up @@ -814,6 +817,7 @@ impl TargetModelData {
model.change(C::SetControlElementId(
self.control_element_index.to_model(),
));
model.change(C::SetLearnable(self.learnable));
model.change(C::SetFxSnapshot(self.fx_snapshot.clone()));
model.change(C::SetTouchedTrackParameterType(self.touched_parameter_type));
model.change(C::SetTouchedRouteParameterType(
Expand Down
6 changes: 3 additions & 3 deletions main/src/infrastructure/plugin/actions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,23 +48,23 @@ pub const ACTION_DEFS: &[ActionDef] = &[
section: ActionSection::ReaLearn,
command_name: "REALEARN_LEARN_MAPPING_REASSIGNING_SOURCE",
action_name: "Learn single mapping (reassigning source)",
op: BackboneShell::learn_mapping_reassigning_source,
op: BackboneShell::learn_mapping_reassigning_learnable_source,
requires_instance: true,
..DEFAULT_DEF
},
ActionDef {
section: ActionSection::ReaLearn,
command_name: "REALEARN_LEARN_MAPPING_REASSIGNING_SOURCE_OPEN",
action_name: "Learn single mapping (reassigning source) and open it",
op: BackboneShell::learn_mapping_reassigning_source_open,
op: BackboneShell::learn_mapping_reassigning_learnable_source_open,
requires_instance: true,
..DEFAULT_DEF
},
ActionDef {
section: ActionSection::ReaLearn,
command_name: "REALEARN_FIND_FIRST_MAPPING_BY_SOURCE",
action_name: "Find first mapping by source",
op: BackboneShell::find_first_mapping_by_source,
op: BackboneShell::find_first_mapping_by_learnable_source,
requires_instance: true,
..DEFAULT_DEF
},
Expand Down
Loading

0 comments on commit 60627a6

Please sign in to comment.