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#if defined(_MSC_VER)
203 auto init = std::begin(c.init);
204 if constexpr(std::is_same_v<std::decay_t<decltype(init)>, const char*>)
205 return new Process::FolderChooser{QString::fromUtf8(init, std::size(c.init)), qname, id, parent};
206 else
207 return new Process::FolderChooser{QString::fromUtf8(c.init.data(), c.init.size()), qname, id, parent};
208#else
209 return new Process::FolderChooser{QString::fromUtf8(std::begin(c.init), std::size(c.init)), qname, id, parent};
210#endif
211 } else {
212 return new Process::FolderChooser{"", qname, id, parent};
213 }
214 }
215 else if constexpr(widg.widget == avnd::widget_type::range_slider)
216 {
217 static constexpr auto c = avnd::get_range<T>();
218 if constexpr(std::is_integral_v<value_type>)
219 {
220 auto [start, end] = c.init;
221 return new Process::IntRangeSlider{c.min, c.max, {(float)start, (float)end},
222 qname, id, parent};
223 }
224 else
225 {
226 auto [start, end] = c.init;
227 return new Process::FloatRangeSlider{c.min, c.max, {(float)start, (float)end},
228 qname, id, parent};
229 }
230 }
231 else if constexpr(widg.widget == avnd::widget_type::range_spinbox)
232 {
233 static constexpr auto c = avnd::get_range<T>();
234 if constexpr(std::is_integral_v<value_type>)
235 {
236 auto [start, end] = c.init;
237 return new Process::IntRangeSpinBox{c.min, c.max, {(float)start, (float)end},
238 qname, id, parent};
239 }
240 else
241 {
242 auto [start, end] = c.init;
243 return new Process::FloatRangeSpinBox{c.min, c.max, {(float)start, (float)end},
244 qname, id, parent};
245 }
246 }
247 else if constexpr(widg.widget == avnd::widget_type::multi_slider)
248 {
249 std::vector<ossia::value> init;
250 return new Process::MultiSlider{init, qname, id, parent};
251 }
252 else if constexpr(widg.widget == avnd::widget_type::multi_slider_xy)
253 {
254 std::vector<ossia::value> init;
255 return new Process::MultiSliderXY{init, qname, id, parent};
256 }
257 else if constexpr(widg.widget == avnd::widget_type::path_generator_xy)
258 {
259 std::vector<ossia::value> init;
260 return new Process::PathGeneratorXY{init, qname, id, parent};
261 }
262 else if constexpr(widg.widget == avnd::widget_type::spinbox)
263 {
264 static constexpr auto c = avnd::get_range<T>();
265 if constexpr(std::is_integral_v<value_type>)
266 {
267 return new Process::IntSpinBox{c.min, c.max, c.init, qname, id, parent};
268 }
269 else
270 {
271 return new Process::FloatSpinBox{c.min, c.max, c.init, qname, id, parent};
272 }
273 }
274 else if constexpr(widg.widget == avnd::widget_type::knob)
275 {
276 static constexpr auto c = avnd::get_range<T>();
277 if constexpr(std::is_integral_v<value_type>)
278 {
279 // FIXME do a IntKnob
280 return new Process::IntSlider{c.min, c.max, c.init, qname, id, parent};
281 }
282 else
283 {
284 if constexpr(avnd::has_mapper<T>)
285 {
286 return new CustomFloatControl<Node, avnd::field_index<N>>{c.min, c.max, c.init,
287 qname, id, parent};
288 }
289 else
290 {
291 return new Process::FloatKnob{c.min, c.max, c.init, qname, id, parent};
292 }
293 }
294 }
295 else if constexpr(widg.widget == avnd::widget_type::lineedit)
296 {
297 if constexpr(avnd::has_range<T>)
298 {
299 static constexpr auto c = avnd::get_range<T>();
300 if constexpr(avnd::program_parameter<T>)
301 {
302 auto p = new Process::ProgramEdit{c.init.data(), qname, id, parent};
303 p->language = T::language();
304 return p;
305 }
306 else
307 return new Process::LineEdit{c.init.data(), qname, id, parent};
308 }
309 else
310 {
311 if constexpr(avnd::program_parameter<T>)
312 {
313 auto p = new Process::ProgramEdit{"", qname, id, parent};
314 p->language = T::language();
315 return p;
316 }
317 else
318 return new Process::LineEdit{"", qname, id, parent};
319 }
320 }
321 else if constexpr(widg.widget == avnd::widget_type::combobox)
322 {
323 static constexpr auto c = avnd::get_range<T>();
324 using init_type = std::decay_t<decltype(c.init)>;
325 ossia::value init;
326 if constexpr(std::is_integral_v<init_type> || std::is_enum_v<init_type>)
327 init = static_cast<int>(c.init);
328 else
329 init = c.init;
330 return new Process::ComboBox{
331 to_combobox_range(c.values), std::move(init), qname, id, parent};
332 }
333 else if constexpr(widg.widget == avnd::widget_type::choices)
334 {
335 static constexpr auto c = avnd::get_range<T>();
336 auto enums = avnd::to_enum_range(c.values);
337 static_assert(
338 std::is_integral_v<decltype(c.init)> || std::is_enum_v<decltype(c.init)>);
339 auto init = enums[static_cast<int>(c.init)];
340 std::vector<QString> pixmaps;
341 if constexpr(avnd::has_pixmaps<T>)
342 {
343 for(std::string_view pix : avnd::get_pixmaps<T>())
344 {
345 pixmaps.push_back(QString::fromLatin1(pix.data(), pix.size()));
346 }
347 }
348 return new Process::Enum{
349 std::move(enums), pixmaps, std::move(init), qname, id, parent};
350 }
351 else if constexpr(widg.widget == avnd::widget_type::xy)
352 {
353 static constexpr auto c = avnd::get_range<T>();
354 if constexpr(requires {
355 c.min == 0.f;
356 c.max == 0.f;
357 c.init == 0.f;
358 })
359 {
360 return new Process::XYSlider{
361 {c.min, c.min}, {c.max, c.max}, {c.init, c.init}, qname, id, parent};
362 }
363 else
364 {
365 auto [mx, my] = c.min;
366 auto [Mx, My] = c.max;
367 auto [ix, iy] = c.init;
368 return new Process::XYSlider{{mx, my}, {Mx, My}, {ix, iy}, qname, id, parent};
369 }
370 }
371 else if constexpr(widg.widget == avnd::widget_type::xyz)
372 {
373 static constexpr auto c = avnd::get_range<T>();
374 if constexpr(requires {
375 c.min == 0.f;
376 c.max == 0.f;
377 c.init == 0.f;
378 })
379 {
380 return new Process::XYZSlider{
381 {c.min, c.min, c.min},
382 {c.max, c.max, c.max},
383 {c.init, c.init, c.init},
384 qname,
385 id,
386 parent};
387 }
388 else
389 {
390 auto [mx, my, mz] = c.min;
391 auto [Mx, My, Mz] = c.max;
392 auto [ix, iy, iz] = c.init;
393 return new Process::XYZSlider{{mx, my, mz}, {Mx, My, Mz}, {ix, iy, iz},
394 qname, id, parent};
395 }
396 }
397 else if constexpr(widg.widget == avnd::widget_type::xy_spinbox)
398 {
399 using data_type = std::decay_t<decltype(value_type{}.x)>;
400 static constexpr auto c = avnd::get_range<T>();
401 if constexpr(requires {
402 c.min == 0.f;
403 c.max == 0.f;
404 c.init == 0.f;
405 })
406 {
407 return new Process::XYSpinboxes{
408 {c.min, c.min},
409 {c.max, c.max},
410 {c.init, c.init},
411 std::is_integral_v<data_type>,
412 qname,
413 id,
414 parent};
415 }
416 else
417 {
418 auto [mx, my] = c.min;
419 auto [Mx, My] = c.max;
420 auto [ix, iy] = c.init;
421 return new Process::XYSpinboxes{
422 {mx, my}, {Mx, My}, {ix, iy}, std::is_integral_v<data_type>,
423 qname, id, parent};
424 }
425 }
426 else if constexpr(widg.widget == avnd::widget_type::xyz_spinbox)
427 {
428 static constexpr auto c = avnd::get_range<T>();
429 if constexpr(requires {
430 c.min == 0.f;
431 c.max == 0.f;
432 c.init == 0.f;
433 })
434 {
435 return new Process::XYZSpinboxes{
436 {c.min, c.min, c.min},
437 {c.max, c.max, c.max},
438 {c.init, c.init, c.init},
439 false,
440 qname,
441 id,
442 parent};
443 }
444 else
445 {
446 auto [mx, my, mz] = c.min;
447 auto [Mx, My, Mz] = c.max;
448 auto [ix, iy, iz] = c.init;
449 return new Process::XYZSpinboxes{{mx, my, mz}, {Mx, My, Mz}, {ix, iy, iz},
450 qname, id, parent};
451 }
452 }
453 else if constexpr(widg.widget == avnd::widget_type::color)
454 {
455 static constexpr auto c = avnd::get_range<T>();
456 static constexpr auto i = c.init;
457 return new Process::HSVSlider{{i.r, i.g, i.b, i.a}, qname, id, parent};
458 }
459 else if constexpr(widg.widget == avnd::widget_type::control)
460 {
461 return new Process::ControlInlet{qname, id, parent};
462 }
463 else if constexpr(widg.widget == avnd::widget_type::no_control)
464 {
465 return new Process::ValueInlet{qname, id, parent};
466 }
467 else
468 {
469 static_assert(T::is_not_a_valid_control);
470 }
471}
472
473template <typename T, std::size_t N>
474static inline auto
475make_control_out(avnd::field_index<N>, Id<Process::Port>&& id, QObject* parent)
476{
477 constexpr auto name = avnd::get_name<T>();
478 constexpr auto widg = avnd::get_widget<T>();
479 QString qname = QString::fromUtf8(name.data(), name.size());
480
481 // FIXME log normalization & friends
482
483 if constexpr(widg.widget == avnd::widget_type::bargraph)
484 {
485 static constexpr auto c = avnd::get_range<T>();
486 return new Process::Bargraph{c.min, c.max, c.init, qname, id, parent};
487 }
488 else if constexpr(widg.widget == avnd::widget_type::control)
489 {
490 return new Process::ControlOutlet{qname, id, parent};
491 }
492 else if constexpr(widg.widget == avnd::widget_type::no_control)
493 {
494 return new Process::ValueOutlet{qname, id, parent};
495 }
496 else if constexpr(avnd::fp_ish<decltype(T::value)>)
497 {
498 static constexpr auto c = avnd::get_range<T>();
499 return new Process::Bargraph{c.min, c.max, c.init, qname, id, parent};
500 }
501 else
502 {
503 return new Process::ControlOutlet{qname, id, parent};
504 }
505}
506
507template <typename T>
508static inline constexpr auto make_control_out(const T& t)
509{
510 return make_control_out<T>();
511}
512}
513
514namespace oscr
515{
517{
518 ossia::audio_vector* buffer{};
519 int64_t offset{};
520 int64_t duration{};
521
522 std::span<const double> operator[](std::size_t i) const noexcept
523 {
524 auto& chan = (*buffer)[i];
525 int64_t min_dur = std::min(int64_t(chan.size()) - offset, duration);
526 if(min_dur < 0)
527 min_dur = 0;
528
529 return std::span<const double>{chan.data() + offset, std::size_t(min_dur)};
530 }
531
532 std::size_t channels() const noexcept { return buffer->size(); }
533 void resize(std::size_t i) const noexcept { return buffer->resize(i); }
534 void reserve(std::size_t channels, std::size_t bufferSize)
535 {
536 resize(channels);
537 for(auto& vec : *buffer)
538 vec.reserve(bufferSize);
539 }
540};
541
543{
544 ossia::audio_vector* buffer{};
545 int64_t offset{};
546 int64_t duration{};
547
548 std::span<double> operator[](std::size_t i) const noexcept
549 {
550 auto& chan = (*buffer)[i];
551 int64_t min_dur = std::min(int64_t(chan.size()) - offset, duration);
552 if(min_dur < 0)
553 min_dur = 0;
554
555 return std::span<double>{chan.data() + offset, std::size_t(min_dur)};
556 }
557
558 std::size_t channels() const noexcept { return buffer->size(); }
559 void resize(std::size_t channels, std::size_t samples_to_write) const noexcept
560 {
561 buffer->resize(channels);
562 for(auto& c : *buffer)
563 c.resize(offset + samples_to_write);
564 }
565
566 void reserve(std::size_t channels, std::size_t bufferSize)
567 {
568 buffer->resize(channels);
569 for(auto& c : *buffer)
570 c.reserve(bufferSize);
571 }
572};
573
574}
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:517
Definition Concepts.hpp:543