Loading...
Searching...
No Matches
Concepts.hpp
1#pragma once
2#include <Process/Dataflow/WidgetInlets.hpp>
3#include <Process/ProcessFlags.hpp>
4#include <Process/ProcessMetadata.hpp>
5
6#include <Dataflow/CurveInlet.hpp>
7#include <Engine/Node/CommonWidgets.hpp>
8
9#include <score/plugins/UuidKey.hpp>
10
11#include <ossia/dataflow/audio_port.hpp>
12#include <ossia/dataflow/port.hpp>
13#include <ossia/dataflow/safe_nodes/tick_policies.hpp>
14
15#include <boost/container/vector.hpp>
16
17#include <avnd/binding/ossia/qt.hpp>
18#include <avnd/binding/ossia/uuid.hpp>
19#include <avnd/common/concepts_polyfill.hpp>
20#include <avnd/common/struct_reflection.hpp>
21#include <avnd/concepts/audio_port.hpp>
22#include <avnd/concepts/channels.hpp>
23#include <avnd/concepts/curve.hpp>
24#include <avnd/concepts/gfx.hpp>
25#include <avnd/concepts/midi_port.hpp>
26#include <avnd/concepts/parameter.hpp>
27#include <avnd/introspection/input.hpp>
28#include <avnd/wrappers/metadatas.hpp>
29#include <avnd/wrappers/widgets.hpp>
30
31#include <cmath>
32
33#include <span>
34#include <type_traits>
35
36#define make_uuid(text) score::uuids::string_generator::compute((text))
37
38namespace oscr
39{
40template <typename Node, typename FieldIndex>
41struct CustomFloatControl;
42}
43
44template <typename Node, typename FieldIndex>
45struct is_custom_serialized<oscr::CustomFloatControl<Node, FieldIndex>> : std::true_type
46{
47};
48
49namespace oscr
50{
51
53{
54 using Process::ControlInlet::ControlInlet;
55
57 float min, float max, float init, const QString& name, Id<Process::Port> id,
58 QObject* parent)
59 : ControlInlet{name, id, parent}
60 {
61 displayHandledExplicitly = true;
62 setValue(init);
63 setInit(init);
64 setDomain(ossia::make_domain(min, max));
65 }
66
67 auto getMin() const noexcept { return domain().get().template convert_min<float>(); }
68 auto getMax() const noexcept { return domain().get().template convert_max<float>(); }
69
70 void setupExecution(ossia::inlet& inl, QObject* exec_context) const noexcept override
71 {
72 auto& port = **safe_cast<ossia::value_inlet*>(&inl);
73 port.type = ossia::val_type::FLOAT;
74 port.domain = domain().get();
75 }
76};
77
78template <typename Node, typename FieldIndex>
80{
81 using CustomFloatControlBase::CustomFloatControlBase;
82
83 static key_type static_concreteKey() noexcept
84 {
85 return make_field_uuid<Node>(true, FieldIndex{});
86 }
87 key_type concreteKey() const noexcept override { return static_concreteKey(); }
88
89 void serialize_impl(const VisitorVariant& vis) const noexcept override
90 {
91 score::serialize_dyn(vis, *this);
92 }
93
94 ~CustomFloatControl() = default;
95};
96
97}
98template <typename Node, typename FieldIndex>
99struct TSerializer<DataStream, oscr::CustomFloatControl<Node, FieldIndex>>
100{
102 static void readFrom(DataStream::Serializer& s, const model_type& p)
103 {
104 s.read((const Process::ControlInlet&)p);
105 }
106
107 static void writeTo(DataStream::Deserializer& s, model_type& eff) { }
108};
109
110template <typename Node, typename FieldIndex>
111struct TSerializer<JSONObject, oscr::CustomFloatControl<Node, FieldIndex>>
112{
114 static void readFrom(JSONObject::Serializer& s, const model_type& p)
115 {
116 s.read((const Process::ControlInlet&)p);
117 }
118
119 static void writeTo(JSONObject::Deserializer& s, model_type& eff) { }
120};
121
122namespace oscr
123{
124
125template <typename Node, typename T, std::size_t N>
126static inline auto
127make_control_in(avnd::field_index<N>, Id<Process::Port>&& id, QObject* parent)
128{
129 using value_type = as_type(T::value);
130 constexpr auto name = avnd::get_name<T>();
131 QString qname = QString::fromUtf8(name.data(), name.size());
132
133 constexpr auto widg = avnd::get_widget<T>();
134
135 // FIXME log normalization & friends
136
137 if constexpr(avnd::curve_port<T>)
138 {
139 return new Dataflow::CurveInlet(id, parent);
140 }
141 else if constexpr(widg.widget == avnd::widget_type::bang)
142 {
143 return new Process::ImpulseButton{qname, id, parent};
144 }
145 else if constexpr(widg.widget == avnd::widget_type::button)
146 {
147 return new Process::Button{qname, id, parent};
148 }
149 else if constexpr(widg.widget == avnd::widget_type::toggle)
150 {
151 static constexpr auto c = avnd::get_range<T>();
152 if constexpr(requires { c.values(); })
153 {
154 return new Process::ChooserToggle{
155 {c.values[0], c.values[1]}, c.init, qname, id, parent};
156 }
157 else
158 {
159 return new Process::Toggle{bool(c.init), qname, id, parent};
160 }
161 }
162 else if constexpr(widg.widget == avnd::widget_type::slider)
163 {
164 static constexpr auto c = avnd::get_range<T>();
165 if constexpr(std::is_integral_v<value_type>)
166 {
167 return new Process::IntSlider{c.min, c.max, c.init, qname, id, parent};
168 }
169 else
170 {
171 if constexpr(avnd::has_mapper<T>)
172 {
173 return new CustomFloatControl<Node, avnd::field_index<N>>{c.min, c.max, c.init,
174 qname, id, parent};
175 }
176 else
177 {
178 return new Process::FloatSlider{c.min, c.max, c.init, qname, id, parent};
179 }
180 }
181 }
182 else if constexpr(widg.widget == avnd::widget_type::log_slider)
183 {
184 static constexpr auto c = avnd::get_range<T>();
185 return new Process::LogFloatSlider{c.min, c.max, c.init, qname, id, parent};
186 }
187 else if constexpr(widg.widget == avnd::widget_type::log_knob)
188 {
189 // FIXME
190 static constexpr auto c = avnd::get_range<T>();
191 return new Process::LogFloatSlider{c.min, c.max, c.init, qname, id, parent};
192 }
193 else if constexpr(widg.widget == avnd::widget_type::time_chooser)
194 {
195 static constexpr auto c = avnd::get_range<T>();
196 return new Process::TimeChooser{c.min, c.max, c.init, qname, id, parent};
197 }
198 else if constexpr(widg.widget == avnd::widget_type::folder)
199 {
200 if constexpr(avnd::has_range<T>) {
201 static constexpr auto c = avnd::get_range<T>();
202 return new Process::FolderChooser{QString::fromUtf8(std::begin(c.init), std::size(c.init)), qname, id, parent};
203 } else {
204 return new Process::FolderChooser{"", qname, id, parent};
205 }
206 }
207 else if constexpr(widg.widget == avnd::widget_type::range_slider)
208 {
209 static constexpr auto c = avnd::get_range<T>();
210 if constexpr(std::is_integral_v<value_type>)
211 {
212 auto [start, end] = c.init;
213 return new Process::IntRangeSlider{c.min, c.max, {(float)start, (float)end},
214 qname, id, parent};
215 }
216 else
217 {
218 auto [start, end] = c.init;
219 return new Process::FloatRangeSlider{c.min, c.max, {(float)start, (float)end},
220 qname, id, parent};
221 }
222 }
223 else if constexpr(widg.widget == avnd::widget_type::range_spinbox)
224 {
225 static constexpr auto c = avnd::get_range<T>();
226 if constexpr(std::is_integral_v<value_type>)
227 {
228 auto [start, end] = c.init;
229 return new Process::IntRangeSpinBox{c.min, c.max, {(float)start, (float)end},
230 qname, id, parent};
231 }
232 else
233 {
234 auto [start, end] = c.init;
235 return new Process::FloatRangeSpinBox{c.min, c.max, {(float)start, (float)end},
236 qname, id, parent};
237 }
238 }
239 else if constexpr(widg.widget == avnd::widget_type::multi_slider)
240 {
241 std::vector<ossia::value> init;
242 return new Process::MultiSlider{init, qname, id, parent};
243 }
244 else if constexpr(widg.widget == avnd::widget_type::multi_slider_xy)
245 {
246 std::vector<ossia::value> init;
247 return new Process::MultiSliderXY{init, qname, id, parent};
248 }
249 else if constexpr(widg.widget == avnd::widget_type::path_generator_xy)
250 {
251 std::vector<ossia::value> init;
252 return new Process::PathGeneratorXY{init, qname, id, parent};
253 }
254 else if constexpr(widg.widget == avnd::widget_type::spinbox)
255 {
256 static constexpr auto c = avnd::get_range<T>();
257 if constexpr(std::is_integral_v<value_type>)
258 {
259 return new Process::IntSpinBox{c.min, c.max, c.init, qname, id, parent};
260 }
261 else
262 {
263 return new Process::FloatSpinBox{c.min, c.max, c.init, qname, id, parent};
264 }
265 }
266 else if constexpr(widg.widget == avnd::widget_type::knob)
267 {
268 static constexpr auto c = avnd::get_range<T>();
269 if constexpr(std::is_integral_v<value_type>)
270 {
271 // FIXME do a IntKnob
272 return new Process::IntSlider{c.min, c.max, c.init, qname, id, parent};
273 }
274 else
275 {
276 if constexpr(avnd::has_mapper<T>)
277 {
278 return new CustomFloatControl<Node, avnd::field_index<N>>{c.min, c.max, c.init,
279 qname, id, parent};
280 }
281 else
282 {
283 return new Process::FloatKnob{c.min, c.max, c.init, qname, id, parent};
284 }
285 }
286 }
287 else if constexpr(widg.widget == avnd::widget_type::lineedit)
288 {
289 if constexpr(avnd::has_range<T>)
290 {
291 static constexpr auto c = avnd::get_range<T>();
292 if constexpr(avnd::program_parameter<T>)
293 {
294 auto p = new Process::ProgramEdit{c.init.data(), qname, id, parent};
295 p->language = T::language();
296 return p;
297 }
298 else
299 return new Process::LineEdit{c.init.data(), qname, id, parent};
300 }
301 else
302 {
303 if constexpr(avnd::program_parameter<T>)
304 {
305 auto p = new Process::ProgramEdit{"", qname, id, parent};
306 p->language = T::language();
307 return p;
308 }
309 else
310 return new Process::LineEdit{"", qname, id, parent};
311 }
312 }
313 else if constexpr(widg.widget == avnd::widget_type::combobox)
314 {
315 static constexpr auto c = avnd::get_range<T>();
316 using init_type = std::decay_t<decltype(c.init)>;
317 ossia::value init;
318 if constexpr(std::is_integral_v<init_type> || std::is_enum_v<init_type>)
319 init = static_cast<int>(c.init);
320 else
321 init = c.init;
322 return new Process::ComboBox{
323 to_combobox_range(c.values), std::move(init), qname, id, parent};
324 }
325 else if constexpr(widg.widget == avnd::widget_type::choices)
326 {
327 static constexpr auto c = avnd::get_range<T>();
328 auto enums = avnd::to_enum_range(c.values);
329 static_assert(
330 std::is_integral_v<decltype(c.init)> || std::is_enum_v<decltype(c.init)>);
331 auto init = enums[static_cast<int>(c.init)];
332 std::vector<QString> pixmaps;
333 if constexpr(avnd::has_pixmaps<T>)
334 {
335 for(std::string_view pix : avnd::get_pixmaps<T>())
336 {
337 pixmaps.push_back(QString::fromLatin1(pix.data(), pix.size()));
338 }
339 }
340 return new Process::Enum{
341 std::move(enums), pixmaps, std::move(init), qname, id, parent};
342 }
343 else if constexpr(widg.widget == avnd::widget_type::xy)
344 {
345 static constexpr auto c = avnd::get_range<T>();
346 if constexpr(requires {
347 c.min == 0.f;
348 c.max == 0.f;
349 c.init == 0.f;
350 })
351 {
352 return new Process::XYSlider{
353 {c.min, c.min}, {c.max, c.max}, {c.init, c.init}, qname, id, parent};
354 }
355 else
356 {
357 auto [mx, my] = c.min;
358 auto [Mx, My] = c.max;
359 auto [ix, iy] = c.init;
360 return new Process::XYSlider{{mx, my}, {Mx, My}, {ix, iy}, qname, id, parent};
361 }
362 }
363 else if constexpr(widg.widget == avnd::widget_type::xyz)
364 {
365 static constexpr auto c = avnd::get_range<T>();
366 if constexpr(requires {
367 c.min == 0.f;
368 c.max == 0.f;
369 c.init == 0.f;
370 })
371 {
372 return new Process::XYZSlider{
373 {c.min, c.min, c.min},
374 {c.max, c.max, c.max},
375 {c.init, c.init, c.init},
376 qname,
377 id,
378 parent};
379 }
380 else
381 {
382 auto [mx, my, mz] = c.min;
383 auto [Mx, My, Mz] = c.max;
384 auto [ix, iy, iz] = c.init;
385 return new Process::XYZSlider{{mx, my, mz}, {Mx, My, Mz}, {ix, iy, iz},
386 qname, id, parent};
387 }
388 }
389 else if constexpr(widg.widget == avnd::widget_type::xy_spinbox)
390 {
391 using data_type = std::decay_t<decltype(value_type{}.x)>;
392 static constexpr auto c = avnd::get_range<T>();
393 if constexpr(requires {
394 c.min == 0.f;
395 c.max == 0.f;
396 c.init == 0.f;
397 })
398 {
399 return new Process::XYSpinboxes{
400 {c.min, c.min},
401 {c.max, c.max},
402 {c.init, c.init},
403 std::is_integral_v<data_type>,
404 qname,
405 id,
406 parent};
407 }
408 else
409 {
410 auto [mx, my] = c.min;
411 auto [Mx, My] = c.max;
412 auto [ix, iy] = c.init;
413 return new Process::XYSpinboxes{
414 {mx, my}, {Mx, My}, {ix, iy}, std::is_integral_v<data_type>,
415 qname, id, parent};
416 }
417 }
418 else if constexpr(widg.widget == avnd::widget_type::xyz_spinbox)
419 {
420 static constexpr auto c = avnd::get_range<T>();
421 if constexpr(requires {
422 c.min == 0.f;
423 c.max == 0.f;
424 c.init == 0.f;
425 })
426 {
427 return new Process::XYZSpinboxes{
428 {c.min, c.min, c.min},
429 {c.max, c.max, c.max},
430 {c.init, c.init, c.init},
431 false,
432 qname,
433 id,
434 parent};
435 }
436 else
437 {
438 auto [mx, my, mz] = c.min;
439 auto [Mx, My, Mz] = c.max;
440 auto [ix, iy, iz] = c.init;
441 return new Process::XYZSpinboxes{{mx, my, mz}, {Mx, My, Mz}, {ix, iy, iz},
442 qname, id, parent};
443 }
444 }
445 else if constexpr(widg.widget == avnd::widget_type::color)
446 {
447 static constexpr auto c = avnd::get_range<T>();
448 static constexpr auto i = c.init;
449 return new Process::HSVSlider{{i.r, i.g, i.b, i.a}, qname, id, parent};
450 }
451 else if constexpr(widg.widget == avnd::widget_type::control)
452 {
453 return new Process::ControlInlet{qname, id, parent};
454 }
455 else if constexpr(widg.widget == avnd::widget_type::no_control)
456 {
457 return new Process::ValueInlet{qname, id, parent};
458 }
459 else
460 {
461 static_assert(T::is_not_a_valid_control);
462 }
463}
464
465template <typename T, std::size_t N>
466static inline auto
467make_control_out(avnd::field_index<N>, Id<Process::Port>&& id, QObject* parent)
468{
469 constexpr auto name = avnd::get_name<T>();
470 constexpr auto widg = avnd::get_widget<T>();
471 QString qname = QString::fromUtf8(name.data(), name.size());
472
473 // FIXME log normalization & friends
474
475 if constexpr(widg.widget == avnd::widget_type::bargraph)
476 {
477 static constexpr auto c = avnd::get_range<T>();
478 return new Process::Bargraph{c.min, c.max, c.init, qname, id, parent};
479 }
480 else if constexpr(widg.widget == avnd::widget_type::control)
481 {
482 return new Process::ControlOutlet{qname, id, parent};
483 }
484 else if constexpr(widg.widget == avnd::widget_type::no_control)
485 {
486 return new Process::ValueOutlet{qname, id, parent};
487 }
488 else if constexpr(avnd::fp_ish<decltype(T::value)>)
489 {
490 static constexpr auto c = avnd::get_range<T>();
491 return new Process::Bargraph{c.min, c.max, c.init, qname, id, parent};
492 }
493 else
494 {
495 return new Process::ControlOutlet{qname, id, parent};
496 }
497}
498
499template <typename T>
500static inline constexpr auto make_control_out(const T& t)
501{
502 return make_control_out<T>();
503}
504}
505
506namespace oscr
507{
509{
510 ossia::audio_vector* buffer{};
511 int64_t offset{};
512 int64_t duration{};
513
514 std::span<const double> operator[](std::size_t i) const noexcept
515 {
516 auto& chan = (*buffer)[i];
517 int64_t min_dur = std::min(int64_t(chan.size()) - offset, duration);
518 if(min_dur < 0)
519 min_dur = 0;
520
521 return std::span<const double>{chan.data() + offset, std::size_t(min_dur)};
522 }
523
524 std::size_t channels() const noexcept { return buffer->size(); }
525 void resize(std::size_t i) const noexcept { return buffer->resize(i); }
526 void reserve(std::size_t channels, std::size_t bufferSize)
527 {
528 resize(channels);
529 for(auto& vec : *buffer)
530 vec.reserve(bufferSize);
531 }
532};
533
535{
536 ossia::audio_vector* buffer{};
537 int64_t offset{};
538 int64_t duration{};
539
540 std::span<double> operator[](std::size_t i) const noexcept
541 {
542 auto& chan = (*buffer)[i];
543 int64_t min_dur = std::min(int64_t(chan.size()) - offset, duration);
544 if(min_dur < 0)
545 min_dur = 0;
546
547 return std::span<double>{chan.data() + offset, std::size_t(min_dur)};
548 }
549
550 std::size_t channels() const noexcept { return buffer->size(); }
551 void resize(std::size_t channels, std::size_t samples_to_write) const noexcept
552 {
553 buffer->resize(channels);
554 for(auto& c : *buffer)
555 c.resize(offset + samples_to_write);
556 }
557
558 void reserve(std::size_t channels, std::size_t bufferSize)
559 {
560 buffer->resize(channels);
561 for(auto& c : *buffer)
562 c.reserve(bufferSize);
563 }
564};
565
566}
Definition VisitorInterface.hpp:53
Definition DataStreamVisitor.hpp:27
void read(const T &)
Definition DataStreamVisitor.hpp:202
Definition VisitorInterface.hpp:61
Definition JSONVisitor.hpp:52
Definition JSONVisitor.hpp:423
Definition Port.hpp:205
Definition Port.hpp:426
Definition Port.hpp:492
Definition Port.hpp:515
Definition UuidKey.hpp:345
The id_base_t class.
Definition Identifier.hpp:59
Definition Factories.hpp:19
Definition CurveInlet.hpp:47
Definition WidgetInlets.hpp:646
Definition WidgetInlets.hpp:488
Definition WidgetInlets.hpp:335
Definition WidgetInlets.hpp:444
Definition WidgetInlets.hpp:464
Definition WidgetInlets.hpp:172
Definition WidgetInlets.hpp:233
Definition WidgetInlets.hpp:265
Definition WidgetInlets.hpp:158
Definition WidgetInlets.hpp:296
Definition WidgetInlets.hpp:393
Definition WidgetInlets.hpp:512
Definition WidgetInlets.hpp:499
Definition WidgetInlets.hpp:217
Definition WidgetInlets.hpp:249
Definition WidgetInlets.hpp:202
Definition WidgetInlets.hpp:281
Definition WidgetInlets.hpp:349
Definition WidgetInlets.hpp:187
Definition WidgetInlets.hpp:601
Definition WidgetInlets.hpp:616
Definition WidgetInlets.hpp:630
Definition WidgetInlets.hpp:431
Definition WidgetInlets.hpp:310
Definition WidgetInlets.hpp:324
Definition WidgetInlets.hpp:525
Definition WidgetInlets.hpp:559
Definition WidgetInlets.hpp:542
Definition WidgetInlets.hpp:583
Definition VisitorInterface.hpp:13
The VisitorVariant struct.
Definition VisitorInterface.hpp:26
Definition Concepts.hpp:53
Definition Concepts.hpp:80
Definition Concepts.hpp:509
Definition Concepts.hpp:535