Skip to content

Commit

Permalink
[vst3] Work on OSC binding
Browse files Browse the repository at this point in the history
  • Loading branch information
jcelerier committed Mar 15, 2024
1 parent 06b5c32 commit e770c7a
Show file tree
Hide file tree
Showing 2 changed files with 135 additions and 2 deletions.
121 changes: 121 additions & 0 deletions include/avnd/binding/standalone/oscquery_minimal_mapper.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
#pragma once

/* SPDX-License-Identifier: GPL-3.0-or-later */

#include <avnd/binding/ossia/from_value.hpp>
#include <avnd/binding/ossia/to_value.hpp>
#include <avnd/concepts/all.hpp>
#include <avnd/introspection/input.hpp>
#include <avnd/wrappers/effect_container.hpp>
#include <avnd/wrappers/widgets.hpp>
#include <ossia/detail/config.hpp>
#include <ossia/network/base/node_functions.hpp>
#include <ossia/network/common/complex_type.hpp>
#include <ossia/network/context.hpp>
#include <ossia/network/context_functions.hpp>
#include <ossia/network/generic/generic_device.hpp>
#include <ossia/network/generic/generic_parameter.hpp>
#include <ossia/protocols/oscquery/oscquery_server_asio.hpp>

#include <iostream>
#include <thread>
#include <chrono>
#if __has_include(<jthread>)
#include <jthread>
#endif
namespace standalone
{
template <typename T>
struct oscquery_minimal_mapper
{
using inputs_type = typename avnd::inputs_type<T>::type;
std::shared_ptr<ossia::net::network_context> m_context;
ossia::net::generic_device m_dev;
std::jthread m_thread;

explicit oscquery_minimal_mapper(std::string name, int osc_port, int ws_port)
: m_context{std::make_shared<ossia::net::network_context>()}
, m_dev{
std::make_unique<ossia::oscquery_asio::oscquery_server_protocol>(
m_context),
name}
{
m_thread = std::jthread([this] (std::stop_token tk){
std::this_thread::sleep_for(std::chrono::seconds(1));
while(!tk.stop_requested())
{
std::cerr << ("Starting...") << std::endl;
m_context->run();
}
std::cerr << ("Exited") << std::endl;
});
}

~oscquery_minimal_mapper()
{
std::cerr <<("oh shit");
}

template <avnd::parameter Field>
requires(!avnd::enum_parameter<Field>)
void setup_control(Field& field, ossia::net::parameter_base& param)
{
std::cerr << ("Adding a control") << avnd::get_name<Field>() << std::endl;
param.set_value_type(oscr::type_for_arg<decltype(Field::value)>());

// Set-up the metadata
if constexpr(avnd::parameter_with_minmax_range<Field>)
{
constexpr auto ctl = avnd::get_range<Field>();
param.set_domain(ossia::make_domain(ctl.min, ctl.max));
param.set_value(ctl.init);
}

param.set_access(ossia::access_mode::BI);

// Set-up the external callback
param.add_callback([&field](const ossia::value& val) {
oscr::from_ossia_value(field, val, field.value);
// FIXME callback into the origin
});
}

template <avnd::enum_parameter Field>
void setup_control(Field& field, ossia::net::parameter_base& param)
{
param.set_value_type(ossia::val_type::STRING);

static constexpr const auto choices = avnd::get_enum_choices<Field>();
static constexpr const auto choices_count = choices.size();
ossia::domain_base<std::string> dom{{choices.begin(), choices.end()}};
// Set-up the metadata
param.set_value(dom.values[(int)avnd::get_range<Field>().init]);
param.set_domain(std::move(dom));
param.set_access(ossia::access_mode::BI);

// Set-up the external callback
param.add_callback([&field](const ossia::value& val) {
oscr::from_ossia_value(field, val, field.value);
});
}

template <avnd::parameter Field>
void create_control(Field& field)
{
ossia::net::node_base& node = m_dev.get_root_node();
std::string name{avnd::get_path(field)};

if(auto param
= ossia::net::create_parameter<ossia::net::generic_parameter>(node, name))
{
setup_control(field, *param);
}
}

void create_ports(typename avnd::inputs_type<T>::type& inputs)
{
avnd::parameter_input_introspection<T>::for_all(inputs,
[this]<typename Field>(Field& f) { create_control(f); });
}
};
}
16 changes: 14 additions & 2 deletions include/avnd/binding/vst3/controller.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@

#include <cstdio>

#if defined(AVND_ADD_OSCQUERY_BINDINGS)
#include <avnd/binding/standalone/oscquery_minimal_mapper.hpp>
#endif
namespace stv3
{
template <typename T>
Expand Down Expand Up @@ -60,12 +63,22 @@ class Controller final

using inputs_t = typename avnd::inputs_type<T>::type;
inputs_t inputs_mirror{};
#undef AVND_ADD_OSCQUERY_BINDINGS
#if AVND_ADD_OSCQUERY_BINDINGS
standalone::oscquery_minimal_mapper<T> oscquery_mapper{
std::string(avnd::get_name<T>()), 1234, 5678};
#endif

using inputs_info_t = avnd::parameter_input_introspection<T>;
static const constexpr int32_t parameter_count = inputs_info_t::size;

public:
Controller() { }
Controller()
{
#if AVND_ADD_OSCQUERY_BINDINGS
oscquery_mapper.create_ports(inputs_mirror);
#endif
}

virtual ~Controller();

Expand Down Expand Up @@ -170,7 +183,6 @@ class Controller final
{
bool ok = inputs_info_t::for_all_unless(
this->inputs_mirror, [&]<typename C>(C& field) -> bool {

double param = 0.f;
if(streamer.readDouble(param) == false)
return false;
Expand Down

0 comments on commit e770c7a

Please sign in to comment.