Skip to content

Commit

Permalink
[wip] Improve data node
Browse files Browse the repository at this point in the history
  • Loading branch information
jcelerier committed Nov 5, 2024
1 parent 1900cb9 commit 238e903
Show file tree
Hide file tree
Showing 2 changed files with 144 additions and 22 deletions.
26 changes: 19 additions & 7 deletions examples/Advanced/Utilities/RangeFilter.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,25 +22,37 @@ struct RangeFilter

struct inputs_t
{
halp::val_port<"In", std::optional<float>> in;
halp::spinbox_f32<"Min", halp::range{-1e6, 1e6, 0.}> min;
halp::spinbox_f32<"Max", halp::range{-1e6, 1e6, 1.}> max;
struct : halp::toggle<"Invert">
{
halp_meta(
description,
"Invert the filter operation: only let values outside the range go through.")
} invert;
} inputs;

struct
{
halp::val_port<"Out", std::optional<float>> out;
} outputs;

void operator()() noexcept
std::optional<float> operator()(float v) noexcept
{
if(inputs.in.value)
if(inputs.invert)
{
float v = *inputs.in.value;
if(v >= inputs.min && v <= inputs.max)
outputs.out = v;
{
return v;
}
}
else
{
if(v <= inputs.min || v >= inputs.max)
{
return v;
}
}
return std::nullopt;
}
};

}
140 changes: 125 additions & 15 deletions include/avnd/binding/ossia/data_node.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -60,9 +60,9 @@ class safe_node<T> : public safe_node_base<T, safe_node<T>>
constexpr bool scan_audio_input_channels() { return false; }
// This function goes from a host-provided tick to what the plugin expects
template <typename Tick>
auto invoke_effect(auto&& val, const Tick& t)
static auto invoke_effect(T& obj, auto&& val, const Tick& t)
{
return this->impl.effect(val);
return obj(val);
/*
// clang-format off
if constexpr(std::is_integral_v<Tick>)
Expand Down Expand Up @@ -94,28 +94,51 @@ class safe_node<T> : public safe_node_base<T, safe_node<T>>
*/
}

template <typename Tick>
using tick_t = decltype(avnd::get_tick_or_frames(
safe_node<T>::impl, std::declval<tick_info>()));
using input_value_type = std::remove_cvref_t<
boost::mp11::mp_first<typename avnd::function_reflection_o<T>::arguments>>;
using operator_ret = decltype(invoke_effect(
std::declval<T&>(), std::declval<input_value_type>(), tick_t{}));

template <typename ValType, typename Tick>
struct process_value
{
static constexpr inline struct
{
ValType value;
} fake_port;
safe_node& self;
const Tick& tick;
ossia::value_port& out;
int ts{};
void operator()() { }
void operator()(ossia::impulse) { out.write_value(self.invoke_effect(0, tick), 0); }
void operator()(bool v) { out.write_value(self.invoke_effect(v ? 1 : 0, tick), 0); }
void operator()(int v) { out.write_value(self.invoke_effect(v, tick), 0); }
void operator()(float v) { out.write_value(self.invoke_effect(v, tick), 0); }
void operator()(ossia::impulse)
{
out.write_value(self.invoke_effect(self.impl.effect, 0, tick), 0);
}
void operator()(bool v)
{
out.write_value(self.invoke_effect(self.impl.effect, v ? 1 : 0, tick), 0);
}
void operator()(int v)
{
out.write_value(self.invoke_effect(self.impl.effect, v, tick), 0);
}
void operator()(float v)
{
out.write_value(self.invoke_effect(self.impl.effect, v, tick), 0);
}
void operator()(const std::string& v)
{
out.write_value(self.invoke_effect(std::stof(v), tick), 0);
out.write_value(self.invoke_effect(self.impl.effect, std::stof(v), tick), 0);
}
template <std::size_t N>
void operator()(std::array<float, N> v)
{
std::array<float, N> res;
for(int i = 0; i < N; i++)
res[i] = self.invoke_effect(v[i], tick);
res[i] = self.invoke_effect(self.impl.effect, v[i], tick);
out.write_value(res, 0);
}

Expand All @@ -126,18 +149,93 @@ class safe_node<T> : public safe_node_base<T, safe_node<T>>
res.reserve(v.size());

for(std::size_t i = 0; i < v.size(); i++)
res.push_back(self.invoke_effect(ossia::convert<float>(v[i]), tick));
res.push_back(
self.invoke_effect(self.impl.effect, ossia::convert<float>(v[i]), tick));

out.write_value(std::move(res), 0);
}
void operator()(const ossia::value_map_type& v)
{
ossia::value_map_type res;
for(auto& [k, val] : v)
{
res.emplace_back(
k, self.invoke_effect(self.impl.effect, ossia::convert<float>(val), tick));
}
out.write_value(std::move(res), 0);
}
};

template <typename ValType, typename Tick>
struct process_value_opt
{
static constexpr inline struct
{
ValType value;
} fake_port;
safe_node& self;
const Tick& tick;
ossia::value_port& out;
int ts{};
void operator()() { }
void operator()(ossia::impulse)
{
if(auto res = self.invoke_effect(self.impl.effect, 0, tick))
out.write_value(*res, 0);
}
void operator()(bool v)
{
if(auto res = self.invoke_effect(self.impl.effect, v ? 1 : 0, tick))
out.write_value(*res, 0);
}
void operator()(int v)
{
if(auto res = self.invoke_effect(self.impl.effect, v, tick))
out.write_value(*res, 0);
}
void operator()(float v)
{
if(auto res = self.invoke_effect(self.impl.effect, v, tick))
out.write_value(*res, 0);
}
void operator()(const std::string& v)
{
if(auto res = self.invoke_effect(self.impl.effect, std::stof(v), tick))
out.write_value(*res, 0);
}
template <std::size_t N>
void operator()(std::array<float, N> v)
{
// FIXME
// std::array<float, N> res;
// for(int i = 0; i < N; i++)
// res[i] = self.invoke_effect(self.impl.effect, v[i], tick);
// out.write_value(res, 0);
}

// FIXME handle recursion
void operator()(const std::vector<ossia::value>& v)
{
/*
std::vector<ossia::value> res;
res.reserve(v.size());
for(std::size_t i = 0; i < v.size(); i++)
res.push_back(self.invoke_effect(self.impl.effect, ossia::convert<float>(v[i]), tick));
out.write_value(std::move(res), 0);
*/
}
void operator()(const ossia::value_map_type& v)
{
/*
ossia::value_map_type res;
for(auto& [k, val] : v)
{
res.emplace_back(k, self.invoke_effect(ossia::convert<float>(val), tick));
res.emplace_back(k, self.invoke_effect(self.impl.effect, ossia::convert<float>(val), tick));
}
out.write_value(std::move(res), 0);
*/
}
};

Expand Down Expand Up @@ -166,13 +264,25 @@ class safe_node<T> : public safe_node_base<T, safe_node<T>>

const auto tick
= avnd::get_tick_or_frames(this->impl, tick_info{*this, tk, st, frames});
process_value<decltype(tick)> proc{*this, tick, *this->arg_value_ports.out};

for(const ossia::timed_value& val : this->arg_value_ports.in->get_data())
if constexpr(avnd::optional_ish<operator_ret>)
{
val.value.apply(proc);
process_value_opt<input_value_type, decltype(tick)> proc{
*this, tick, *this->arg_value_ports.out};
for(const ossia::timed_value& val : this->arg_value_ports.in->get_data())
{
val.value.apply(proc);
}
}
else
{
process_value<input_value_type, decltype(tick)> proc{
*this, tick, *this->arg_value_ports.out};
for(const ossia::timed_value& val : this->arg_value_ports.in->get_data())
{
val.value.apply(proc);
}
}

this->finish_run();
}
};
Expand Down

0 comments on commit 238e903

Please sign in to comment.