113 return uuid_from_string<Node>();
120 return static_key() == other || Execution::ProcessComponent::base_key_match(other);
123 [[no_unique_address]] ossia::type_if<int, is_gpu<Node>> node_id = -1;
127 element, ctx,
"Executor::ProcessModel<Info>", p}
132 setup_gpu(element, ctx, p);
137 setup_cpu(element, ctx, p);
140 if constexpr(avnd::tag_process_exec<Node>)
142 this->m_ossia_process = std::make_shared<CustomNodeProcess<Node>>(this->node);
146 this->m_ossia_process = std::make_shared<ossia::node_process>(this->node);
154 = *ctx.doc.findPlugin<Explorer::DeviceDocumentPlugin>()->networkContext();
158 auto st = ossia::exec_state_facade{ctx.execState.get()};
159 std::shared_ptr<safe_node<Node>> ptr;
160 auto node =
new safe_node<Node>{st.bufferSize(), (double)st.sampleRate(),
id};
161 node->root_inputs().reserve(element.inlets().size());
162 node->root_outputs().reserve(element.outlets().size());
164 node->prepare(*ctx.execState.get());
166 if_possible(node->impl.effect.ossia_state = st);
167 if_possible(node->impl.effect.io_context = &net_ctx.context);
168 if_possible(node->impl.effect.ossia_document_context = &ctx.doc);
172 if constexpr(
requires { ptr->impl.effect; })
173 if constexpr(std::is_same_v<std::decay_t<
decltype(ptr->impl.effect)>, Node>)
174 connect_message_bus(element, ctx, ptr->impl.effect);
175 connect_worker(ctx, ptr->impl);
177 node->dynamic_ports = element.dynamic_ports;
180 connect_controls(element, ctx, ptr);
181 update_controls(ptr);
183 &element, &Process::ProcessModel::inletsChanged,
this,
184 &Executor::recompute_ports);
186 &element, &Process::ProcessModel::outletsChanged,
this,
187 &Executor::recompute_ports);
190 node->audio_configuration_changed(st);
192 m_oldInlets = element.inlets();
193 m_oldOutlets = element.outlets();
209 using Gfx::gfx_exec_node::gfx_exec_node;
210 std::string label()
const noexcept override
212 return std::string(avnd::get_name<Node>());
216 auto node = std::make_shared<named_exec_node>(gfx_exec);
217 node->prepare(*ctx.execState);
224 for(
auto& ctl : element.inlets())
226 if(
auto ctrl = qobject_cast<Process::ControlInlet*>(ctl))
228 auto& p = node->add_control();
229 p->value = ctrl->value();
233 ctrl, &Process::ControlInlet::valueChanged,
this,
237 else if(
auto ctrl = qobject_cast<Process::ValueInlet*>(ctl))
239 auto& p = node->add_control();
243 else if(
auto ctrl = qobject_cast<Process::AudioInlet*>(ctl))
247 else if(
auto ctrl = qobject_cast<Gfx::TextureInlet*>(ctl))
249 ossia::texture_inlet& inl = *node->add_texture();
250 ctrl->setupExecution(inl,
this);
252 else if(
auto ctrl = qobject_cast<Gfx::GeometryInlet*>(ctl))
254 ossia::geometry_inlet& inl = *node->add_geometry();
255 ctrl->setupExecution(inl,
this);
260 for(
auto* outlet : element.outlets())
262 if(
auto ctrl = qobject_cast<Process::ControlOutlet*>(outlet))
264 node->add_control_out();
266 else if(
auto ctrl = qobject_cast<Process::ValueOutlet*>(outlet))
268 node->add_control_out();
270 else if(
auto out = qobject_cast<Gfx::TextureOutlet*>(outlet))
272 node->add_texture_out();
273 out->nodeId = node_id;
275 else if(
auto out = qobject_cast<Gfx::GeometryOutlet*>(outlet))
277 node->add_geometry_out();
282 std::weak_ptr qex_ptr = std::shared_ptr<Execution::ExecutionCommandQueue>(
283 ctx.alias.lock(), &ctx.executionQueue);
284 std::unique_ptr<score::gfx::Node> ptr;
287 auto gpu_node =
new CustomGpuNode<Node>(qex_ptr, node->control_outs,
id, ctx.doc);
292 auto gpu_node =
new GpuComputeNode<Node>(qex_ptr, node->control_outs,
id, ctx.doc);
298 =
new GfxNode<Node>(element, qex_ptr, node->control_outs,
id, ctx.doc);
303 for(
auto& ctl : element.inlets())
305 if(
auto ctrl = qobject_cast<Gfx::TextureInlet*>(ctl))
307 ossia::texture_inlet& inl
308 =
static_cast<ossia::texture_inlet&
>(*node->root_inputs()[i]);
309 ptr->process(i, inl.data);
313 node->id = gfx_exec.ui->register_node(std::move(ptr));
318 void recompute_ports()
321 auto n = std::dynamic_pointer_cast<safe_node<Node>>(this->node);
326 in_exec([dp = this->process().dynamic_ports, node = n] {
327 node->dynamic_ports = dp;
328 node->root_inputs().clear();
329 node->root_outputs().clear();
330 node->initialize_all_ports();
334 void connect_controls(
336 std::shared_ptr<safe_node<Node>>& ptr)
338 using dynamic_ports_port_type = avnd::dynamic_ports_input_introspection<Node>;
339 using control_inputs_type = avnd::control_input_introspection<Node>;
340 using curve_inputs_type = avnd::curve_input_introspection<Node>;
341 using soundfile_inputs_type = avnd::soundfile_input_introspection<Node>;
342 using midifile_inputs_type = avnd::midifile_input_introspection<Node>;
343 using raw_file_inputs_type = avnd::raw_file_input_introspection<Node>;
344 using control_outputs_type = avnd::control_output_introspection<Node>;
347 safe_node<Node>& node = *ptr;
348 avnd::effect_container<Node>& eff = node.impl;
353 if constexpr(dynamic_ports_port_type::size > 0)
355 for(
auto state : eff.full_state())
357 dynamic_ports_port_type::for_all_n2(
361 if constexpr(control_inputs_type::size > 0)
363 for(
auto state : eff.full_state())
365 control_inputs_type::for_all_n2(
369 if constexpr(curve_inputs_type::size > 0)
371 for(
auto state : eff.full_state())
373 curve_inputs_type::for_all_n2(
377 if constexpr(soundfile_inputs_type::size > 0)
379 soundfile_inputs_type::for_all_n2(
380 avnd::get_inputs<Node>(eff),
383 setup_soundfile_task_pool(element, ctx, ptr);
385 if constexpr(midifile_inputs_type::size > 0)
387 midifile_inputs_type::for_all_n2(
388 avnd::get_inputs<Node>(eff),
391 if constexpr(raw_file_inputs_type::size > 0)
393 raw_file_inputs_type::for_all_n2(
394 avnd::get_inputs<Node>(eff),
399 if constexpr(control_inputs_type::size > 0 || control_outputs_type::size > 0)
402 std::weak_ptr<safe_node<Node>> weak_node = ptr;
406 con(ctx.doc.coarseUpdateTimer, &QTimer::timeout,
this,
407 [timer_action = std::move(timer_action)] { timer_action(); },
408 Qt::QueuedConnection);
412 void setup_soundfile_task_pool(
414 std::shared_ptr<safe_node<Node>>& ptr)
416 safe_node<Node>& node = *ptr;
418 using soundfile_inputs_type = avnd::soundfile_input_introspection<Node>;
420 auto& tq = score::TaskPool::instance();
421 node.soundfiles.load_request
422 = [&tq, p = std::weak_ptr{ptr}, &ctx](std::string& str,
int idx) {
423 auto eff_ptr = p.lock();
426 tq.post([eff_ptr = std::move(eff_ptr), filename = str, &ctx, idx]()
mutable {
427 if(
auto file = loadSoundfile(filename, ctx.doc, ctx.execState))
429 ctx.executionQueue.enqueue(
430 [sf = std::move(file), p = std::weak_ptr{eff_ptr}, idx]()
mutable {
431 auto eff_ptr = p.lock();
435 avnd::effect_container<Node>& eff = eff_ptr->impl;
436 soundfile_inputs_type::for_nth_mapped_n2(
437 avnd::get_inputs<Node>(eff), idx,
438 [&]<std::size_t NField, std::size_t N>(
439 auto& field, avnd::predicate_index<N> p,
440 avnd::field_index<NField> f) {
441 eff_ptr->soundfile_loaded(sf, p, f);
449 void connect_message_bus(
453 if constexpr(avnd::has_gui_to_processor_bus<Node>)
455 element.from_ui = [qex_ptr = weak_exec, &eff](QByteArray b) {
456 auto qex = qex_ptr.lock();
460 qex->enqueue([mess = std::move(b), &eff]()
mutable {
461 using refl = avnd::function_reflection<&Node::process_message>;
462 static_assert(refl::count <= 1);
464 if constexpr(refl::count == 0)
467 eff.process_message();
469 else if constexpr(refl::count == 1)
471 using arg_type = avnd::first_argument<&Node::process_message>;
472 std::decay_t<arg_type> arg;
475 eff.process_message(std::move(arg));
481 if constexpr(avnd::has_processor_to_gui_bus<Node>)
483 if constexpr(
requires { eff.send_message = [](
auto&&) { }; })
485 eff.send_message = [proc = QPointer{&this->process()},
486 qed_ptr = weak_edit]<
typename T>(T&& b)
mutable {
487 auto qed = qed_ptr.lock();
491 sizeof(QPointer<QObject>) +
sizeof(b)
492 < Execution::ExecutionCommand::max_storage)
494 qed->enqueue([proc, bb = std::move(b)]()
mutable {
495 if(proc && proc->to_ui)
502 [proc, bb = std::make_unique<std::decay_t<T>>(std::move(b))]()
mutable {
503 if(proc && proc->to_ui)
509 else if constexpr(
requires { eff.send_message = []() { }; })
512 = [proc = QPointer{&this->process()}, qed_ptr = weak_edit]()
mutable {
515 auto qed = qed_ptr.lock();
519 qed->enqueue([proc]()
mutable {
520 if(proc && proc->to_ui)
528 void connect_worker(const ::Execution::Context& ctx, avnd::effect_container<Node>& eff)
530 if constexpr(avnd::has_worker<Node>)
533 auto& tq = score::TaskPool::instance();
534 using worker_type =
decltype(eff.effect.worker);
535 for(
auto& eff : eff.effects())
537 std::weak_ptr eff_ptr = std::shared_ptr<Node>(this->node, &eff);
538 std::weak_ptr qex_ptr = std::shared_ptr<Execution::ExecutionCommandQueue>(
539 ctx.alias.lock(), &ctx.executionQueue);
542 = [&tq, qex_ptr = std::move(qex_ptr),
543 eff_ptr = std::move(eff_ptr)]<
typename... Args>(Args&&... f)
mutable {
546 tq.post([eff_ptr, qex_ptr, ... ff = std::forward<Args>(f)]()
mutable {
554 =
decltype(worker_type::work(std::forward<
decltype(ff)>(ff)...));
555 if constexpr(std::is_void_v<type_of_result>)
557 worker_type::work(std::forward<
decltype(ff)>(ff)...);
563 auto res = worker_type::work(std::forward<
decltype(ff)>(ff)...);
569 ossia::qt::run_async(
570 qApp, [eff_ptr = std::move(eff_ptr), qex_ptr = std::move(qex_ptr),
571 res = std::move(res)]()
mutable {
573 std::shared_ptr qex = qex_ptr.lock();
578 [eff_ptr = std::move(eff_ptr), res = std::move(res)]()
mutable {
582 if(
auto p = eff_ptr.lock())
594 void update_controls(std::shared_ptr<safe_node<Node>>& ptr)
596 avnd::effect_container<Node>& eff = ptr->impl;
598 for(
auto state : eff.full_state())
600 avnd::input_introspection<Node>::for_all(
601 state.inputs, [&](
auto& field) { if_possible(field.update(state.effect)); });
606 void cleanup()
override
608 if constexpr(
requires { this->process().from_ui; })
610 this->process().from_ui = [](QByteArray arr) {};
618 auto& gfx_exec = this->system().doc.template plugin<Gfx::DocumentPlugin>().exec;
620 gfx_exec.ui->unregister_node(node_id);
624 for(
auto* outlet : this->process().outlets())
626 if(
auto out = qobject_cast<Gfx::TextureOutlet*>(outlet))
632 ::Execution::ProcessComponent::cleanup();