114 return uuid_from_string<Node>();
121 return static_key() == other || Execution::ProcessComponent::base_key_match(other);
124 [[no_unique_address]] ossia::type_if<int, is_gpu<Node>> node_id = -1;
128 element, ctx,
"Executor::ProcessModel<Info>", p}
133 setup_gpu(element, ctx, p);
138 setup_cpu(element, ctx, p);
141 if constexpr(avnd::tag_process_exec<Node>)
143 this->m_ossia_process = std::make_shared<CustomNodeProcess<Node>>(this->node);
147 this->m_ossia_process = std::make_shared<ossia::node_process>(this->node);
155 = *ctx.doc.findPlugin<Explorer::DeviceDocumentPlugin>()->networkContext();
159 auto st = ossia::exec_state_facade{ctx.execState.get()};
160 std::shared_ptr<safe_node<Node>> ptr;
161 auto node =
new safe_node<Node>{st.bufferSize(), (double)st.sampleRate(),
id};
162 node->root_inputs().reserve(element.inlets().size());
163 node->root_outputs().reserve(element.outlets().size());
165 node->prepare(*ctx.execState.get());
167 if_possible(node->impl.effect.ossia_state = st);
168 if_possible(node->impl.effect.io_context = &net_ctx.context);
169 if_possible(node->impl.effect.ossia_document_context = &ctx.doc);
173 if constexpr(
requires { ptr->impl.effect; })
174 if constexpr(std::is_same_v<std::decay_t<
decltype(ptr->impl.effect)>, Node>)
175 connect_message_bus(element, ctx, ptr->impl.effect);
176 connect_worker(ctx, ptr->impl);
178 node->dynamic_ports = element.dynamic_ports;
181 connect_controls(element, ctx, ptr);
182 update_controls(ptr);
184 &element, &Process::ProcessModel::inletsChanged,
this,
185 &Executor::recompute_ports);
187 &element, &Process::ProcessModel::outletsChanged,
this,
188 &Executor::recompute_ports);
191 node->audio_configuration_changed(st);
193 m_oldInlets = element.inlets();
194 m_oldOutlets = element.outlets();
210 using Gfx::gfx_exec_node::gfx_exec_node;
211 std::string label()
const noexcept override
213 return std::string(avnd::get_name<Node>());
217 auto node = std::make_shared<named_exec_node>(gfx_exec);
218 node->prepare(*ctx.execState);
225 for(
auto& ctl : element.inlets())
227 if(
auto ctrl = qobject_cast<Process::ControlInlet*>(ctl))
229 auto& p = node->add_control();
230 p->value = ctrl->value();
234 ctrl, &Process::ControlInlet::valueChanged,
this,
238 else if(
auto ctrl = qobject_cast<Process::ValueInlet*>(ctl))
240 auto& p = node->add_control();
244 else if(
auto ctrl = qobject_cast<Process::AudioInlet*>(ctl))
248 else if(
auto ctrl = qobject_cast<Gfx::TextureInlet*>(ctl))
250 ossia::texture_inlet& inl = *node->add_texture();
251 ctrl->setupExecution(inl,
this);
256 for(
auto* outlet : element.outlets())
258 if(
auto ctrl = qobject_cast<Process::ControlOutlet*>(outlet))
260 node->add_control_out();
262 else if(
auto ctrl = qobject_cast<Process::ValueOutlet*>(outlet))
264 node->add_control_out();
266 else if(
auto out = qobject_cast<Gfx::TextureOutlet*>(outlet))
268 node->add_texture_out();
269 out->nodeId = node_id;
274 std::weak_ptr qex_ptr = std::shared_ptr<Execution::ExecutionCommandQueue>(
275 ctx.alias.lock(), &ctx.executionQueue);
276 std::unique_ptr<score::gfx::Node> ptr;
279 auto gpu_node =
new CustomGpuNode<Node>(qex_ptr, node->control_outs,
id, ctx.doc);
284 auto gpu_node =
new GpuComputeNode<Node>(qex_ptr, node->control_outs,
id, ctx.doc);
290 =
new GfxNode<Node>(element, qex_ptr, node->control_outs,
id, ctx.doc);
295 for(
auto& ctl : element.inlets())
297 if(
auto ctrl = qobject_cast<Gfx::TextureInlet*>(ctl))
299 ossia::texture_inlet& inl
300 =
static_cast<ossia::texture_inlet&
>(*node->root_inputs()[i]);
301 ptr->process(i, inl.data);
305 node->id = gfx_exec.ui->register_node(std::move(ptr));
310 void recompute_ports()
313 auto n = std::dynamic_pointer_cast<safe_node<Node>>(this->node);
318 this->in_exec([dp = this->process().dynamic_ports, node = n] {
319 node->dynamic_ports = dp;
320 node->root_inputs().clear();
321 node->root_outputs().clear();
322 node->initialize_all_ports();
326 void connect_controls(
328 std::shared_ptr<safe_node<Node>>& ptr)
330 using dynamic_ports_port_type = avnd::dynamic_ports_input_introspection<Node>;
331 using control_inputs_type = avnd::control_input_introspection<Node>;
332 using curve_inputs_type = avnd::curve_input_introspection<Node>;
333 using soundfile_inputs_type = avnd::soundfile_input_introspection<Node>;
334 using midifile_inputs_type = avnd::midifile_input_introspection<Node>;
335 using raw_file_inputs_type = avnd::raw_file_input_introspection<Node>;
336 using control_outputs_type = avnd::control_output_introspection<Node>;
339 safe_node<Node>& node = *ptr;
340 avnd::effect_container<Node>& eff = node.impl;
345 if constexpr(dynamic_ports_port_type::size > 0)
347 for(
auto state : eff.full_state())
349 dynamic_ports_port_type::for_all_n2(
353 if constexpr(control_inputs_type::size > 0)
355 for(
auto state : eff.full_state())
357 control_inputs_type::for_all_n2(
361 if constexpr(curve_inputs_type::size > 0)
363 for(
auto state : eff.full_state())
365 curve_inputs_type::for_all_n2(
369 if constexpr(soundfile_inputs_type::size > 0)
371 soundfile_inputs_type::for_all_n2(
372 avnd::get_inputs<Node>(eff),
375 setup_soundfile_task_pool(element, ctx, ptr);
377 if constexpr(midifile_inputs_type::size > 0)
379 midifile_inputs_type::for_all_n2(
380 avnd::get_inputs<Node>(eff),
383 if constexpr(raw_file_inputs_type::size > 0)
385 raw_file_inputs_type::for_all_n2(
386 avnd::get_inputs<Node>(eff),
391 if constexpr(control_inputs_type::size > 0 || control_outputs_type::size > 0)
394 std::weak_ptr<safe_node<Node>> weak_node = ptr;
398 con(ctx.doc.coarseUpdateTimer, &QTimer::timeout,
this,
399 [timer_action = std::move(timer_action)] { timer_action(); },
400 Qt::QueuedConnection);
404 void setup_soundfile_task_pool(
406 std::shared_ptr<safe_node<Node>>& ptr)
408 safe_node<Node>& node = *ptr;
409 avnd::effect_container<Node>& eff = node.impl;
411 using soundfile_inputs_type = avnd::soundfile_input_introspection<Node>;
413 auto& tq = score::TaskPool::instance();
414 node.soundfiles.load_request
415 = [&tq, p = std::weak_ptr{ptr}, &ctx](std::string& str,
int idx) {
416 auto eff_ptr = p.lock();
419 tq.post([eff_ptr = std::move(eff_ptr), filename = str, &ctx, idx]()
mutable {
420 if(
auto file = loadSoundfile(filename, ctx.doc, ctx.execState))
422 ctx.executionQueue.enqueue(
423 [sf = std::move(file), p = std::weak_ptr{eff_ptr}, idx]()
mutable {
424 auto eff_ptr = p.lock();
428 avnd::effect_container<Node>& eff = eff_ptr->impl;
429 soundfile_inputs_type::for_nth_mapped_n2(
430 avnd::get_inputs<Node>(eff), idx,
431 [&]<std::size_t NField, std::size_t N>(
432 auto& field, avnd::predicate_index<N> p,
433 avnd::field_index<NField> f) {
434 eff_ptr->soundfile_loaded(sf, p, f);
442 void connect_message_bus(
446 if constexpr(avnd::has_gui_to_processor_bus<Node>)
448 element.from_ui = [p = QPointer{
this}, &eff](QByteArray b) {
452 p->in_exec([mess = std::move(b), &eff]()
mutable {
453 using refl = avnd::function_reflection<&Node::process_message>;
454 static_assert(refl::count <= 1);
456 if constexpr(refl::count == 0)
459 eff.process_message();
461 else if constexpr(refl::count == 1)
463 using arg_type = avnd::first_argument<&Node::process_message>;
464 std::decay_t<arg_type> arg;
467 eff.process_message(std::move(arg));
473 if constexpr(avnd::has_processor_to_gui_bus<Node>)
475 eff.send_message = [self = QPointer{
this}]<
typename T>(T&& b)
mutable {
479 sizeof(QPointer<QObject>) +
sizeof(b)
480 < Execution::ExecutionCommand::max_storage)
483 [proc = QPointer{&self->process()}, bb = std::move(b)]()
mutable {
491 [proc = QPointer{&self->process()},
492 bb = std::make_unique<std::decay_t<T>>(std::move(b))]()
mutable {
501 void connect_worker(const ::Execution::Context& ctx, avnd::effect_container<Node>& eff)
503 if constexpr(avnd::has_worker<Node>)
506 auto& tq = score::TaskPool::instance();
507 using worker_type =
decltype(eff.effect.worker);
508 for(
auto& eff : eff.effects())
510 std::weak_ptr eff_ptr = std::shared_ptr<Node>(this->node, &eff);
511 std::weak_ptr qex_ptr = std::shared_ptr<Execution::ExecutionCommandQueue>(
512 ctx.alias.lock(), &ctx.executionQueue);
515 = [&tq, qex_ptr = std::move(qex_ptr),
516 eff_ptr = std::move(eff_ptr)]<
typename... Args>(Args&&... f)
mutable {
519 tq.post([eff_ptr, qex_ptr, ... ff = std::forward<Args>(f)]()
mutable {
527 =
decltype(worker_type::work(std::forward<
decltype(ff)>(ff)...));
528 if constexpr(std::is_void_v<type_of_result>)
530 worker_type::work(std::forward<
decltype(ff)>(ff)...);
536 auto res = worker_type::work(std::forward<
decltype(ff)>(ff)...);
542 ossia::qt::run_async(
543 qApp, [eff_ptr = std::move(eff_ptr), qex_ptr = std::move(qex_ptr),
544 res = std::move(res)]()
mutable {
546 std::shared_ptr qex = qex_ptr.lock();
551 [eff_ptr = std::move(eff_ptr), res = std::move(res)]()
mutable {
555 if(
auto p = eff_ptr.lock())
567 void update_controls(std::shared_ptr<safe_node<Node>>& ptr)
569 avnd::effect_container<Node>& eff = ptr->impl;
571 for(
auto state : eff.full_state())
573 avnd::input_introspection<Node>::for_all(
574 state.inputs, [&](
auto& field) { if_possible(field.update(state.effect)); });
579 void cleanup()
override
581 if constexpr(
requires { this->process().from_ui; })
583 this->process().from_ui = [](QByteArray arr) {};
591 auto& gfx_exec = this->system().doc.template plugin<Gfx::DocumentPlugin>().exec;
593 gfx_exec.ui->unregister_node(node_id);
597 for(
auto* outlet : this->process().outlets())
599 if(
auto out = qobject_cast<Gfx::TextureOutlet*>(outlet))
605 ::Execution::ProcessComponent::cleanup();