MicroMapping.hpp
1 #pragma once
2 #include <Fx/MathMapping_generic.hpp>
3 #include <Fx/Types.hpp>
4 
5 #include <ossia/dataflow/value_port.hpp>
6 
7 #include <halp/layout.hpp>
8 
9 namespace Nodes::MicroMapping
10 {
11 struct Node
12 {
13  halp_meta(name, "Micromap")
14  halp_meta(c_name, "MicroMapping")
15  halp_meta(category, "Control/Mappings")
16  halp_meta(
17  manual_url, "https://ossia.io/score-docs/processes/exprtk.html#exprtk-support")
18  halp_meta(author, "ossia score, ExprTK (Arash Partow)")
19  halp_meta(description, "Applies a math expression to an input.")
20  halp_meta(uuid, "25c64b87-a44a-4fed-9f60-0a48906fd3ec")
21 
22  struct ins
23  {
24  struct : halp::val_port<"in", ossia::value>
25  {
26  // Messages to this port trigger a new computation cycle with updated timestamps
27  halp_flag(active_port);
28  } port;
29  halp::lineedit<"Expression", "x / 127"> expr;
30  } inputs;
31 
32  struct
33  {
34  value_out port{};
35  } outputs;
36 
37  struct State
38  {
39  State()
40  {
41  xv.resize(1024);
42  pxv.resize(1024);
43  pov.resize(1024);
44  expr.add_vector("xv", xv);
45  expr.add_vector("pxv", pxv);
46  expr.add_vector("pov", pov);
47 
48  expr.add_variable("x", x);
49  expr.add_variable("px", px);
50  expr.add_variable("po", po);
51 
52  expr.add_variable("t", cur_time);
53  expr.add_variable("dt", cur_deltatime);
54  expr.add_variable("pos", cur_pos);
55  expr.add_constants();
56 
57  expr.register_symbol_table();
58  }
59 
60  std::vector<double> xv;
61  std::vector<double> pxv;
62  std::vector<double> pov;
63 
64  double x{};
65  double px{};
66  double po{};
67 
68  double cur_time{};
69  double cur_deltatime{};
70  double cur_pos{};
71 
72  ossia::math_expression expr;
73  int64_t last_value_time{};
74 
75  //bool ok = false;
76  } state;
77 
78  halp::setup setup;
79  void prepare(halp::setup s) { setup = s; }
80 
81  using tick = halp::tick_flicks;
82 
83  void operator()(const tick& tk)
84  {
85  auto& self = this->state;
86  if(!self.expr.set_expression(this->inputs.expr))
87  return;
88 
89  if(self.expr.has_variable("xv"))
91  inputs.port.value, outputs.port.call, tk, state);
92  else
94  inputs.port.value, outputs.port.call, tk, state);
95  }
96 
97  struct ui
98  {
99  halp_meta(layout, halp::layouts::vbox)
100 
101  struct : halp::control<&ins::expr>
102  {
103  halp_flag(dynamic_size);
104  } expr;
105  };
106 };
107 }
Utilities for OSSIA data structures.
Definition: DeviceInterface.hpp:33
Definition: MathMapping_generic.hpp:12
Definition: MicroMapping.hpp:98
Definition: MicroMapping.hpp:12