CPUNode.hpp
1 #pragma once
2 
3 #include <JS/Executor/JSAPIWrapper.hpp>
4 #include <JS/Qml/Metatypes.hpp>
5 #include <JS/Qml/QmlObjects.hpp>
6 #include <JS/Qml/ValueTypes.Qt6.hpp>
7 
8 #include <ossia/dataflow/graph/graph_interface.hpp>
9 #include <ossia/dataflow/graph_edge.hpp>
10 #include <ossia/dataflow/graph_edge_helpers.hpp>
11 #include <ossia/dataflow/graph_node.hpp>
12 #include <ossia/dataflow/node_process.hpp>
13 #include <ossia/dataflow/port.hpp>
14 #include <ossia/detail/logger.hpp>
15 #include <ossia/detail/ssize.hpp>
16 #include <ossia/editor/scenario/time_interval.hpp>
17 #include <ossia/editor/state/message.hpp>
18 #include <ossia/editor/state/state.hpp>
19 
20 #include <ossia-qt/invoke.hpp>
21 #include <ossia-qt/js_utilities.hpp>
22 #include <ossia-qt/time.hpp>
23 #include <ossia-qt/token_request.hpp>
24 namespace ossia::qt
25 {
26 class qml_engine_functions;
27 }
28 
29 namespace JS
30 {
31 class js_node final : public ossia::graph_node
32 {
33 public:
34  explicit js_node(ossia::execution_state& st);
35  ~js_node();
36 
37  [[nodiscard]] std::string label() const noexcept override { return "javascript"; }
38 
39  void run(const ossia::token_request& t, ossia::exec_state_facade) noexcept override;
40  void setupComponent();
41  void setScript(const QString& val);
42 
43  ossia::execution_state& m_st;
44 
45  QQmlEngine* m_engine{};
46  std::vector<Inlet*> m_jsInlets;
47  std::vector<std::pair<ControlInlet*, ossia::inlet_ptr>> m_ctrlInlets;
48  std::vector<std::pair<Impulse*, ossia::inlet_ptr>> m_impulseInlets;
49  std::vector<std::pair<ValueInlet*, ossia::inlet_ptr>> m_valInlets;
50  std::vector<std::pair<ValueOutlet*, ossia::outlet_ptr>> m_valOutlets;
51  std::vector<std::pair<AudioInlet*, ossia::inlet_ptr>> m_audInlets;
52  std::vector<std::pair<AudioOutlet*, ossia::outlet_ptr>> m_audOutlets;
53  std::vector<std::pair<MidiInlet*, ossia::inlet_ptr>> m_midInlets;
54  std::vector<std::pair<MidiOutlet*, ossia::outlet_ptr>> m_midOutlets;
55  JS::Script* m_object{};
56  ossia::qt::qml_engine_functions* m_execFuncs{};
57  QJSValueList m_tickCall;
58  std::size_t m_gcIndex{};
59 
60  bool triggerStart{};
61  bool triggerStop{};
62  bool triggerPause{};
63  bool triggerResume{};
64 
65  void setControl(std::size_t index, const QVariant& val)
66  {
67  if(index > m_jsInlets.size())
68  return;
69  if(auto v = qobject_cast<ValueInlet*>(m_jsInlets[index]))
70  v->setValue(val);
71  else if(auto v = qobject_cast<ControlInlet*>(m_jsInlets[index]))
72  v->setValue(val);
73  }
74  void impulse(std::size_t index)
75  {
76  if(index > m_jsInlets.size())
77  return;
78  if(auto v = qobject_cast<Impulse*>(m_jsInlets[index]))
79  v->impulse();
80  }
81 };
82 
83 struct js_process final : public ossia::node_process
84 {
85  using node_process::node_process;
86  js_node& js() const { return static_cast<js_node&>(*node); }
87  void start() override { js().triggerStart = true; }
88  void stop() override { js().triggerStop = true; }
89  void pause() override { js().triggerPause = true; }
90  void resume() override { js().triggerResume = true; }
91  void transport_impl(ossia::time_value date) override
92  {
93  QMetaObject::invokeMethod(
94  js().m_object, "transport", Qt::DirectConnection,
95  Q_ARG(QVariant, double(date.impl)));
96  }
97  void offset_impl(ossia::time_value date) override
98  {
99  QMetaObject::invokeMethod(
100  js().m_object, "offset", Qt::DirectConnection,
101  Q_ARG(QVariant, double(date.impl)));
102  }
103 };
104 
105 }
Definition: CPUNode.hpp:32
Definition: CPUNode.hpp:84