114 return uuid_from_string<Node>();
121 return static_key() == other || Execution::ProcessComponent::base_key_match(other);
124#if defined(SCORE_PLUGIN_GFX)
125 [[no_unique_address]] ossia::type_if<int, is_gpu<Node>> node_id = score::gfx::invalid_node_index;
130 element, ctx,
"Executor::ProcessModel<Info>", p}
135 setup_gpu(element, ctx, p);
140 setup_cpu(element, ctx, p);
143 if constexpr(avnd::tag_process_exec<Node>)
145 this->m_ossia_process = std::make_shared<CustomNodeProcess<Node>>(this->node);
149 this->m_ossia_process = std::make_shared<ossia::node_process>(this->node);
157 = *ctx.doc.findPlugin<Explorer::DeviceDocumentPlugin>()->networkContext();
161 auto st = ossia::exec_state_facade{ctx.execState.get()};
162 std::shared_ptr<safe_node<Node>> ptr;
163 auto node =
new safe_node<Node>{st.bufferSize(), (double)st.sampleRate(),
id};
164 node->root_inputs().reserve(element.inlets().size());
165 node->root_outputs().reserve(element.outlets().size());
167 node->prepare(*ctx.execState.get());
169 if_possible(node->impl.effect.ossia_state = st);
170 if_possible(node->impl.effect.io_context = &net_ctx.context);
171 if_possible(node->impl.effect.ossia_document_context = &ctx.doc);
175 if constexpr(
requires { ptr->impl.effect; })
176 if constexpr(std::is_same_v<std::decay_t<
decltype(ptr->impl.effect)>, Node>)
177 connect_message_bus(element, ctx, ptr->impl.effect);
178 connect_worker(ctx, ptr->impl);
180 node->dynamic_ports = element.dynamic_ports;
183 connect_controls(element, ctx, ptr);
184 update_controls(ptr);
186 &element, &Process::ProcessModel::inletsChanged,
this,
187 &Executor::recompute_ports);
189 &element, &Process::ProcessModel::outletsChanged,
this,
190 &Executor::recompute_ports);
193 node->audio_configuration_changed(st);
195 m_oldInlets = element.inlets();
196 m_oldOutlets = element.outlets();
212 using Gfx::gfx_exec_node::gfx_exec_node;
213 std::string label()
const noexcept override
215 return std::string(avnd::get_name<Node>());
219 auto node = std::make_shared<named_exec_node>(gfx_exec);
220 node->prepare(*ctx.execState);
227 for(
auto& ctl : element.inlets())
229 if(
auto ctrl = qobject_cast<Process::ControlInlet*>(ctl))
231 auto& p = node->add_control();
232 p->value = ctrl->value();
236 ctrl, &Process::ControlInlet::valueChanged,
this,
240 else if(
auto ctrl = qobject_cast<Process::ValueInlet*>(ctl))
242 auto& p = node->add_control();
246 else if(
auto ctrl = qobject_cast<Process::AudioInlet*>(ctl))
250 else if(
auto ctrl = qobject_cast<Gfx::TextureInlet*>(ctl))
252 ossia::texture_inlet& inl = *node->add_texture();
253 ctrl->setupExecution(inl,
this);
255 else if(
auto ctrl = qobject_cast<Gfx::GeometryInlet*>(ctl))
257 ossia::geometry_inlet& inl = *node->add_geometry();
258 ctrl->setupExecution(inl,
this);
263 for(
auto* outlet : element.outlets())
265 if(
auto ctrl = qobject_cast<Process::ControlOutlet*>(outlet))
267 node->add_control_out();
269 else if(
auto ctrl = qobject_cast<Process::ValueOutlet*>(outlet))
271 node->add_control_out();
273 else if(
auto out = qobject_cast<Gfx::TextureOutlet*>(outlet))
275 node->add_texture_out();
276 out->nodeId = node_id;
278 else if(
auto out = qobject_cast<Gfx::GeometryOutlet*>(outlet))
280 node->add_geometry_out();
285 std::weak_ptr qex_ptr = std::shared_ptr<Execution::ExecutionCommandQueue>(
286 ctx.alias.lock(), &ctx.executionQueue);
287 std::unique_ptr<score::gfx::Node> ptr;
290 auto gpu_node =
new CustomGpuNode<Node>(qex_ptr, node->control_outs,
id, ctx.doc);
295 auto gpu_node =
new GpuComputeNode<Node>(qex_ptr, node->control_outs,
id, ctx.doc);
301 =
new GfxNode<Node>(element, qex_ptr, node->control_outs,
id, ctx.doc);
306 for(
auto& ctl : element.inlets())
308 if(
auto ctrl = qobject_cast<Gfx::TextureInlet*>(ctl))
310 ossia::texture_inlet& inl
311 =
static_cast<ossia::texture_inlet&
>(*node->root_inputs()[i]);
312 ptr->process(i, inl.data);
316 node->id = gfx_exec.ui->register_node(std::move(ptr));
321 void recompute_ports()
324 auto n = std::dynamic_pointer_cast<safe_node<Node>>(this->node);
329 in_exec([dp = this->process().dynamic_ports, node = n] {
330 node->dynamic_ports = dp;
331 node->root_inputs().clear();
332 node->root_outputs().clear();
333 node->initialize_all_ports();
337 void connect_controls(
339 std::shared_ptr<safe_node<Node>>& ptr)
341 using dynamic_ports_port_type = avnd::dynamic_ports_input_introspection<Node>;
342 using control_inputs_type = avnd::control_input_introspection<Node>;
343 using curve_inputs_type = avnd::curve_input_introspection<Node>;
344 using soundfile_inputs_type = avnd::soundfile_input_introspection<Node>;
345 using midifile_inputs_type = avnd::midifile_input_introspection<Node>;
346 using raw_file_inputs_type = avnd::raw_file_input_introspection<Node>;
347 using control_outputs_type = avnd::control_output_introspection<Node>;
350 safe_node<Node>& node = *ptr;
351 avnd::effect_container<Node>& eff = node.impl;
356 if constexpr(dynamic_ports_port_type::size > 0)
358 for(
auto state : eff.full_state())
360 dynamic_ports_port_type::for_all_n2(
364 if constexpr(control_inputs_type::size > 0)
366 for(
auto state : eff.full_state())
368 control_inputs_type::for_all_n2(
372 if constexpr(curve_inputs_type::size > 0)
374 for(
auto state : eff.full_state())
376 curve_inputs_type::for_all_n2(
380 if constexpr(soundfile_inputs_type::size > 0)
382 soundfile_inputs_type::for_all_n2(
383 avnd::get_inputs<Node>(eff),
386 setup_soundfile_task_pool(element, ctx, ptr);
388 if constexpr(midifile_inputs_type::size > 0)
390 midifile_inputs_type::for_all_n2(
391 avnd::get_inputs<Node>(eff),
394 if constexpr(raw_file_inputs_type::size > 0)
396 raw_file_inputs_type::for_all_n2(
397 avnd::get_inputs<Node>(eff),
402 if constexpr(control_inputs_type::size > 0 || control_outputs_type::size > 0)
405 std::weak_ptr<safe_node<Node>> weak_node = ptr;
409 con(ctx.doc.coarseUpdateTimer, &QTimer::timeout,
this,
410 [timer_action = std::move(timer_action)] { timer_action(); },
411 Qt::QueuedConnection);
415 void setup_soundfile_task_pool(
417 std::shared_ptr<safe_node<Node>>& ptr)
419 safe_node<Node>& node = *ptr;
421 using soundfile_inputs_type = avnd::soundfile_input_introspection<Node>;
423 auto& tq = score::TaskPool::instance();
424 node.soundfiles.load_request
425 = [&tq, p = std::weak_ptr{ptr}, &ctx](std::string& str,
int idx) {
426 auto eff_ptr = p.lock();
429 tq.post([eff_ptr = std::move(eff_ptr), filename = str, &ctx, idx]()
mutable {
430 if(
auto file = loadSoundfile(filename, ctx.doc, ctx.execState))
432 ctx.executionQueue.enqueue(
433 [sf = std::move(file), p = std::weak_ptr{eff_ptr}, idx]()
mutable {
434 auto eff_ptr = p.lock();
438 avnd::effect_container<Node>& eff = eff_ptr->impl;
439 soundfile_inputs_type::for_nth_mapped_n2(
440 avnd::get_inputs<Node>(eff), idx,
441 [&]<std::size_t NField, std::size_t N>(
442 auto& field, avnd::predicate_index<N> p,
443 avnd::field_index<NField> f) {
444 eff_ptr->soundfile_loaded(sf, p, f);
452 void connect_message_bus(
456 if constexpr(avnd::has_gui_to_processor_bus<Node>)
458 element.from_ui = [qex_ptr = weak_exec, &eff](QByteArray b) {
459 auto qex = qex_ptr.lock();
463 qex->enqueue([mess = std::move(b), &eff]()
mutable {
464 using refl = avnd::function_reflection<&Node::process_message>;
465 static_assert(refl::count <= 1);
467 if constexpr(refl::count == 0)
470 eff.process_message();
472 else if constexpr(refl::count == 1)
474 using arg_type = avnd::first_argument<&Node::process_message>;
475 std::decay_t<arg_type> arg;
478 eff.process_message(std::move(arg));
484 if constexpr(avnd::has_processor_to_gui_bus<Node>)
486 if constexpr(
requires { eff.send_message = [](
auto&&) { }; })
488 eff.send_message = [proc = QPointer{&this->process()},
489 qed_ptr = weak_edit]<
typename T>(T&& b)
mutable {
490 auto qed = qed_ptr.lock();
494 sizeof(QPointer<QObject>) +
sizeof(b)
495 < Execution::ExecutionCommand::max_storage)
497 qed->enqueue([proc, bb = std::move(b)]()
mutable {
498 if(proc && proc->to_ui)
505 [proc, bb = std::make_unique<std::decay_t<T>>(std::move(b))]()
mutable {
506 if(proc && proc->to_ui)
512 else if constexpr(
requires { eff.send_message = []() { }; })
515 = [proc = QPointer{&this->process()}, qed_ptr = weak_edit]()
mutable {
518 auto qed = qed_ptr.lock();
522 qed->enqueue([proc]()
mutable {
523 if(proc && proc->to_ui)
531 void connect_worker(const ::Execution::Context& ctx, avnd::effect_container<Node>& eff)
533 if constexpr(avnd::has_worker<Node>)
536 auto& tq = score::TaskPool::instance();
537 using worker_type =
decltype(eff.effect.worker);
538 for(
auto& eff : eff.effects())
540 std::weak_ptr eff_ptr = std::shared_ptr<Node>(this->node, &eff);
541 std::weak_ptr qex_ptr = std::shared_ptr<Execution::ExecutionCommandQueue>(
542 ctx.alias.lock(), &ctx.executionQueue);
545 = [&tq, qex_ptr = std::move(qex_ptr),
546 eff_ptr = std::move(eff_ptr)]<
typename... Args>(Args&&... f)
mutable {
549 tq.post([eff_ptr, qex_ptr, ... ff = std::forward<Args>(f)]()
mutable {
557 =
decltype(worker_type::work(std::forward<
decltype(ff)>(ff)...));
558 if constexpr(std::is_void_v<type_of_result>)
560 worker_type::work(std::forward<
decltype(ff)>(ff)...);
566 auto res = worker_type::work(std::forward<
decltype(ff)>(ff)...);
572 ossia::qt::run_async(
573 qApp, [eff_ptr = std::move(eff_ptr), qex_ptr = std::move(qex_ptr),
574 res = std::move(res)]()
mutable {
576 std::shared_ptr qex = qex_ptr.lock();
581 [eff_ptr = std::move(eff_ptr), res = std::move(res)]()
mutable {
585 if(
auto p = eff_ptr.lock())
597 void update_controls(std::shared_ptr<safe_node<Node>>& ptr)
599 avnd::effect_container<Node>& eff = ptr->impl;
601 for(
auto state : eff.full_state())
603 avnd::input_introspection<Node>::for_all(
604 state.inputs, [&](
auto& field) { if_possible(field.update(state.effect)); });
609 void cleanup()
override
611 if constexpr(
requires { this->process().from_ui; })
613 this->process().from_ui = [](QByteArray arr) {};
621 auto& gfx_exec = this->system().doc.template plugin<Gfx::DocumentPlugin>().exec;
623 gfx_exec.ui->unregister_node(node_id);
627 for(
auto* outlet : this->process().outlets())
629 if(
auto out = qobject_cast<Gfx::TextureOutlet*>(outlet))
635 ::Execution::ProcessComponent::cleanup();