OSSIA
Open Scenario System for Interactive Application
data_copy.hpp
1 #pragma once
2 #include <ossia/dataflow/audio_port.hpp>
3 #include <ossia/dataflow/data.hpp>
4 #include <ossia/dataflow/geometry_port.hpp>
5 #include <ossia/dataflow/midi_port.hpp>
6 #include <ossia/dataflow/typed_value.hpp>
7 #include <ossia/dataflow/value_port.hpp>
9 #include <ossia/detail/nullable_variant.hpp>
10 #include <ossia/network/base/parameter.hpp>
11 namespace ossia
12 {
13 
14 struct clear_data
15 {
16  void operator()(value_port& p) const { p.clear(); }
17 
18  void operator()(midi_port& p) const { p.messages.clear(); }
19 
20  void operator()(audio_port& p) const { p.set_channels(0); }
21 
22  void operator()(geometry_port& p) const { p.clear(); }
23 
24  void operator()() const { }
25 };
26 
27 struct data_size
28 {
29  std::size_t operator()(const value_delay_line& p) const { return p.data.size(); }
30 
31  std::size_t operator()(const midi_delay_line& p) const { return p.messages.size(); }
32 
33  std::size_t operator()(const audio_delay_line& p) const { return p.samples.size(); }
34 
35  std::size_t operator()(const geometry_delay_line& p) const { return p.meshes.size(); }
36 
37  std::size_t operator()(const ossia::monostate&) const { return 0; }
38  std::size_t operator()() const { return 0; }
39 };
40 
41 struct move_data
42 {
43  void operator()(value_port& out, value_port& in)
44  {
45  in.add_port_values(std::move(out));
46  }
47 
48  void operator()(audio_port& out, audio_port& in)
49  {
50  auto tmp = std::move(in.get());
51  in.get() = std::move(out.get());
52  out.get() = std::move(tmp);
53  }
54 
55  void operator()(midi_port& out, midi_port& in)
56  {
57  auto tmp = std::move(in.messages);
58  in.messages = std::move(out.messages);
59  out.messages = std::move(tmp);
60  }
61 
62  void operator()(geometry_port& out, geometry_port& in)
63  {
64  // OPTIMIZEME
65  // if(out.flags & geometry_port::dirty_meshes)
66  in.meshes = out.meshes; //std::move(out.meshes);
67  // if(out.flags & geometry_port::dirty_transform)
68  in.transform = out.transform;
69  in.flags = out.flags;
70  out.flags = {};
71  }
72 };
73 
74 struct copy_data
75 {
77  void
78  operator()(const value_vector<std::pair<ossia::typed_value, int>>& out, value_port& in)
79  {
80  // Called in local_pull_visitor
81  for(auto& val : out)
82  in.add_local_value(val.first);
83  }
84  void operator()(const value_vector<ossia::typed_value>& out, value_port& in)
85  {
86  // Called in copy_data_pos, when copying from a delay line to a port
87  for(auto& val : out)
88  in.add_local_value(val);
89  }
90 
91  void operator()(const value_port& out, value_port& in)
92  {
93  // Called in init_node_visitor::copy, when copying from a node to another
94  in.add_port_values(out);
95  }
96 
97  void operator()(const net::parameter_base& param, value_port& in)
98  {
99  // Called from global_pull_visitor
100  in.add_global_value(param, param.value());
101  }
102 
103  void operator()(
104  const net::parameter_base& param, const value_vector<ossia::value>& vec,
105  value_port& in)
106  {
107  // Called from global_pull_visitor
108  in.add_global_values(param, vec);
109  }
110 
111  void operator()(const value_port& out, value_delay_line& in)
112  {
113  // Called in env_writer, when copying from a node to a delay line
114  value_vector<ossia::typed_value> vec;
115  vec.reserve(out.get_data().size());
116  for(const ossia::timed_value& val : out.get_data())
117  {
118  vec.emplace_back(val, out.index, out.type);
119  }
120  in.data.push_back(std::move(vec));
121  }
122 
124  void operator()(const audio_port& out, audio_delay_line& in)
125  {
126  // Called in env_writer, when copying from a node to a delay line
127  in.samples.push_back(out.get());
128  }
129 
130  void operator()(const audio_port& out, audio_port& in)
131  {
132  // Called in init_node_visitor::copy, when copying from a node to another
133  mix(out.get(), in.get());
134  }
135 
137  void operator()(const midi_port& out, midi_port& in)
138  {
139  // Called in init_node_visitor::copy, when copying from a node to another
140  operator()(out.messages, in);
141  }
142 
143  void operator()(const value_vector<libremidi::message>& out, midi_port& in)
144  {
145  // Called in copy_data_pos, when copying from a delay line to a port
146  for(const auto& data : out)
147  in.messages.push_back(data);
148  }
149 
150  void operator()(const midi_port& out, midi_delay_line& in)
151  {
152  // Called in env_writer, when copying from a node to a delay line
153  in.messages.push_back(out.messages);
154  }
155 
157  void operator()(const geometry_port& out, geometry_port& in)
158  {
159  // Called in init_node_visitor::copy, when copying from a node to another
160  //if(out.flags & geometry_port::dirty_meshes)
161  in.meshes = out.meshes;
162  //if(out.flags & geometry_port::dirty_transform)
163  in.transform = out.transform;
164  in.flags = out.flags;
165  }
166 
167  void operator()(const mesh_list_ptr& out, geometry_port& in)
168  {
169  // Called in copy_data_pos below
170  in.meshes = out;
171  }
172 
173  void operator()(const geometry_port& out, geometry_delay_line& in)
174  {
175  // Called in env_writer, when copying from a node to a delay line
176  // if(out.flags & geometry_port::dirty_meshes)
177  in.meshes.push_back(out.meshes);
178  }
179 };
180 
181 struct copy_data_pos
182 {
183  const std::size_t pos;
184 
185  template <typename T, typename U>
186  void operator()(const T&, const U&) const
187  {
188  }
189 
190  void operator()(const value_delay_line& out, value_port& in)
191  {
192  if(pos < out.data.size())
193  {
194  copy_data{}(out.data[pos], in);
195  }
196  }
197 
198  void operator()(const audio_delay_line& out, audio_port& in)
199  {
200  if(pos < out.samples.size())
201  {
202  mix(out.samples[pos], in.get());
203  }
204  }
205 
206  void operator()(const midi_delay_line& out, midi_port& in)
207  {
208  if(pos < out.messages.size())
209  {
210  copy_data{}(out.messages[pos], in);
211  }
212  }
213  void operator()(const geometry_delay_line& out, geometry_port& in)
214  {
215  if(pos < out.meshes.size())
216  {
217  copy_data{}(out.meshes[pos], in);
218  }
219  }
220 };
221 }
Definition: git_info.h:7
Definition: size.hpp:7