Skip to content

Commit

Permalink
#800 More steps towards control transformations that output relative …
Browse files Browse the repository at this point in the history
…values
  • Loading branch information
helgoboss committed Oct 26, 2024
1 parent e8720c3 commit 5c87ca9
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 22 deletions.
2 changes: 1 addition & 1 deletion main/lib/helgoboss-learn
33 changes: 24 additions & 9 deletions main/src/domain/eel_transformation.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
use crate::base::eel;
use helgoboss_learn::{Transformation, TransformationInput, TransformationOutput, UnitValue};
use helgoboss_learn::{
ControlValue, ControlValueKind, Transformation, TransformationInput, TransformationInstruction,
TransformationOutput, UnitValue,
};
use std::os::raw::c_void;

use reaper_medium::reaper_str;
Expand Down Expand Up @@ -38,7 +41,7 @@ pub trait Script {
input: TransformationInput<UnitValue>,
output_value: UnitValue,
additional_input: AdditionalTransformationInput,
) -> Result<TransformationOutput<UnitValue>, &'static str>;
) -> Result<TransformationOutput<ControlValue>, &'static str>;
}

impl Script for () {
Expand All @@ -51,7 +54,7 @@ impl Script for () {
input: TransformationInput<UnitValue>,
output_value: UnitValue,
additional_input: AdditionalTransformationInput,
) -> Result<TransformationOutput<UnitValue>, &'static str> {
) -> Result<TransformationOutput<ControlValue>, &'static str> {
let _ = (input, output_value, additional_input);
Err("not supported")
}
Expand All @@ -76,7 +79,7 @@ impl Script for EelTransformation {
input: TransformationInput<UnitValue>,
output_value: UnitValue,
additional_input: AdditionalTransformationInput,
) -> Result<TransformationOutput<UnitValue>, &'static str> {
) -> Result<TransformationOutput<ControlValue>, &'static str> {
self.transform_continuous(input, output_value, additional_input)
}
}
Expand Down Expand Up @@ -156,14 +159,26 @@ impl Transformation for EelTransformation {
eel_unit.program.execute();
output_var.get()
};
let output = if v == STOP {
TransformationOutput::Stop
let (out_val, instruction) = if v == STOP {
// Stop only
(None, Some(TransformationInstruction::Stop))
} else if v == NONE {
TransformationOutput::None
// Neither control nor stop
(None, None)
} else if (CONTROL_AND_STOP_MAGIC..=CONTROL_AND_STOP_MAGIC + 1.0).contains(&v) {
TransformationOutput::ControlAndStop(v - CONTROL_AND_STOP_MAGIC)
// Both control and stop
(
Some(v - CONTROL_AND_STOP_MAGIC),
Some(TransformationInstruction::Stop),
)
} else {
TransformationOutput::Control(v)
// Control only
(Some(v), None)
};
let output = TransformationOutput {
produced_kind: ControlValueKind::AbsoluteContinuous,
value: out_val,
instruction,
};
Ok(output)
}
Expand Down
26 changes: 14 additions & 12 deletions main/src/infrastructure/ui/egui_views/advanced_script_editor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ use egui::plot::{Legend, MarkerShape, Plot, Points, VLine};
use egui::{CentralPanel, Color32, RichText, ScrollArea, Ui};
use egui::{Context, SidePanel, TextEdit};
use helgoboss_learn::{
TransformationInput, TransformationInputMetaData, TransformationOutput, UnitValue,
ControlValue, TransformationInput, TransformationInputMetaData, TransformationInstruction,
TransformationOutput, UnitValue,
};
use std::ptr;
use std::sync::{Arc, Mutex};
Expand Down Expand Up @@ -150,20 +151,21 @@ fn plot_build_outcome(ui: &mut Ui, build_outcome: &BuildOutcome) {
// plot_ui.set_plot_bounds(PlotBounds::from_min_max([0.0, 0.0], [1.0, 1.0]));
for e in &build_outcome.plot_entries {
x = e.input;
prev_y = match e.output {
TransformationOutput::None => {
let unit_output_value = e.output.value.and_then(|v| v.to_unit_value().ok());
prev_y = match (unit_output_value, e.output.instruction) {
(None, None) => {
none_points.push([x, prev_y]);
prev_y
}
TransformationOutput::Control(v) => {
(Some(v), None) => {
normal_points.push([x, v.get()]);
v.get()
}
TransformationOutput::ControlAndStop(v) => {
(Some(v), Some(TransformationInstruction::Stop)) => {
stop_points.push([x, v.get()]);
v.get()
}
TransformationOutput::Stop => {
(None, Some(TransformationInstruction::Stop)) => {
stop_points.push([x, prev_y]);
prev_y
}
Expand Down Expand Up @@ -225,7 +227,7 @@ struct BuildOutcome {
#[derive(Debug)]
struct PlotEntry {
input: f64,
output: TransformationOutput<UnitValue>,
output: TransformationOutput<ControlValue>,
}

#[derive(Derivative)]
Expand Down Expand Up @@ -270,9 +272,9 @@ impl Toolbox {
},
);
let additional_input = AdditionalTransformationInput { y_last: 0.0 };
let output = match script.evaluate(input, prev_y, additional_input).ok() {
None => continue,
Some(e) => e,
let Some(output) = script.evaluate(input, prev_y, additional_input).ok() else {
// No sample for that point
continue;
};
let entry = PlotEntry {
input: if uses_time {
Expand All @@ -282,11 +284,11 @@ impl Toolbox {
},
output,
};
if let Some(v) = output.value() {
if let Some(v) = output.value.and_then(|v| v.to_unit_value().ok()) {
prev_y = v;
}
plot_entries.push(entry);
if output.is_stop() {
if output.instruction == Some(TransformationInstruction::Stop) {
break;
}
}
Expand Down

0 comments on commit 5c87ca9

Please sign in to comment.