OSSIA
Open Scenario System for Interactive Application
OssiaUI.h
1 /************************************************************************
2  FAUST Architecture File
3  Copyright (C) 2003-2011 GRAME, Centre National de Creation Musicale
4  ---------------------------------------------------------------------
5  This Architecture section is free software; you can redistribute it
6  and/or modify it under the terms of the GNU General Public License
7  as published by the Free Software Foundation; either version 3 of
8  the License, or (at your option) any later version.
9 
10  This program is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  GNU General Public License for more details.
14 
15  You should have received a copy of the GNU General Public License
16  along with this program; If not, see <http://www.gnu.org/licenses/>.
17 
18  EXCEPTION : As a special exception, you may create a larger work
19  that contains this FAUST architecture section and distribute
20  that work under terms of your choice, so long as this FAUST
21  architecture section is not modified.
22 
23 
24  ************************************************************************
25  ************************************************************************/
26 
27 #ifndef FAUST_OSSIAUI_H
28 #define FAUST_OSSIAUI_H
29 
30 #ifndef FAUSTFLOAT
31 #define FAUSTFLOAT float
32 #endif
33 
34 #include "faust/gui/PathBuilder.h"
35 #include "faust/gui/UI.h"
36 
37 #include <ossia/network/domain/domain.hpp>
38 #include <ossia/network/generic/generic_device.hpp>
39 #include <ossia/network/generic/generic_node.hpp>
40 #include <ossia/network/generic/generic_parameter.hpp>
41 #include <ossia/network/oscquery/oscquery_server.hpp>
42 
43 class OssiaUI final : public PathBuilder, public UI
44 {
45  ossia::net::generic_device m_dev;
46  ossia::net::node_base* m_curNode{};
47 
48  std::vector<std::pair<ossia::net::parameter_base*, FAUSTFLOAT*>> m_values;
49  std::atomic_bool m_running;
50 
51 public:
52  OssiaUI(uint16_t osc_port, uint16_t ws_port)
53  : m_dev{std::make_unique<ossia::oscquery::oscquery_server_protocol>(osc_port, ws_port), "Faust"}
54  , m_curNode{&m_dev.get_root_node()}
55  {
56  }
57 
58  ~OssiaUI()
59  {
60  }
61 
62  void run(int ms)
63  {
64  while(true)
65  {
66  std::this_thread::sleep_for(std::chrono::milliseconds(ms));
67  for(auto p : m_values)
68  p.first->push_value(*p.second);
69  }
70  }
71 
72 private:
73  void openTabBox(const char* label) override
74  {
75  m_curNode = m_curNode->create_child(label);
76  }
77  void openHorizontalBox(const char* label) override
78  {
79  m_curNode = m_curNode->create_child(label);
80  }
81  void openVerticalBox(const char* label) override
82  {
83  m_curNode = m_curNode->create_child(label);
84  }
85  void closeBox() override
86  {
87  m_curNode = m_curNode->get_parent();
88  }
89 
90  // -- active widgets
91  void addButton(const char* label, FAUSTFLOAT* zone) override
92  {
93  auto n = m_curNode->create_child(label);
94  auto a = n->create_parameter(ossia::val_type::BOOL);
95  a->add_callback(
96  [zone](const ossia::value& val) { *zone = val.get<bool>() ? 1.0 : 0.0; });
97  }
98 
99  void addCheckButton(const char* label, FAUSTFLOAT* zone) override
100  {
101  addButton(label, zone);
102  }
103 
104  void addVerticalSlider(
105  const char* label, FAUSTFLOAT* zone, FAUSTFLOAT init, FAUSTFLOAT min,
106  FAUSTFLOAT max, FAUSTFLOAT step) override
107  {
108  auto n = m_curNode->create_child(label);
109  auto a = n->create_parameter(ossia::val_type::FLOAT);
110  ossia::net::set_default_value(*n, init);
111  ossia::net::set_domain(*n, ossia::make_domain(min, max));
112  ossia::net::set_value_step_size(*n, step);
113  a->add_callback([zone](const ossia::value& val) { *zone = val.get<float>(); });
114  }
115 
116  void addHorizontalSlider(
117  const char* label, FAUSTFLOAT* zone, FAUSTFLOAT init, FAUSTFLOAT min,
118  FAUSTFLOAT max, FAUSTFLOAT step) override
119  {
120  addVerticalSlider(label, zone, init, min, max, step);
121  }
122 
123  void addNumEntry(
124  const char* label, FAUSTFLOAT* zone, FAUSTFLOAT init, FAUSTFLOAT min,
125  FAUSTFLOAT max, FAUSTFLOAT step) override
126  {
127  addVerticalSlider(label, zone, init, min, max, step);
128  }
129 
130  // -- passive widgets
131  void addHorizontalBargraph(
132  const char* label, FAUSTFLOAT* zone, FAUSTFLOAT min, FAUSTFLOAT max) override
133  {
134  auto n = m_curNode->create_child(label);
135  auto a = n->create_parameter(ossia::val_type::FLOAT);
136  ossia::net::set_domain(*n, ossia::make_domain(min, max));
137  ossia::net::set_access_mode(*n, ossia::access_mode::GET);
138 
139  m_values.push_back({a, zone});
140  }
141  void addVerticalBargraph(
142  const char* label, FAUSTFLOAT* zone, FAUSTFLOAT min, FAUSTFLOAT max) override
143  {
144  addHorizontalBargraph(label, zone, min, max);
145  }
146 
147  // -- metadata declarations
148  void declare(FAUSTFLOAT* zone, const char* key, const char* val) override
149  {
150  std::cout << "declare key : " << key << " val : " << val << std::endl;
151  }
152 };
153 
154 #endif
The node_base class.
Definition: network/base/node.hpp:48
The value class.
Definition: value.hpp:173
Definition: git_info.h:7
@ BOOL
ossia::impulse
constexpr OSSIA_INLINE auto min(const T a, const U b) noexcept -> typename std::conditional<(sizeof(T) > sizeof(U)), T, U >::type
min function tailored for values
Definition: math.hpp:125
@ GET
The value can be retrieved and changed.
constexpr OSSIA_INLINE auto max(const T a, const U b) noexcept -> typename std::conditional<(sizeof(T) > sizeof(U)), T, U >::type
max function tailored for values
Definition: math.hpp:96