1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
|
#ifndef ROTOR_H
#define ROTOR_H
#define ENABLE_TYPENAME(A) template<> struct TypeName<A> { static const char *Get() { return #A; }};
#include <string>
#include <sstream>
#include <iostream>
#include <map>
#include <vector>
#include <unordered_map>
#include <json/json.h>
#include <typeinfo>
template <typename T>
struct TypeName
{
static const char* Get()
{
return typeid(T).name();
}
};
template <>
struct TypeName<int> {
static const char* Get() {
return "int";
}
};
template <>
struct TypeName<double> {
static const char* Get() {
return "number";
}
};
template <>
struct TypeName<std::string> {
static const char* Get() {
return "string";
}
};
namespace Rotor {
class Node;
template <class NT> class Node_type;
class Audio_frame{
public:
Audio_frame(uint16_t *_samples,int _channels,int _numsamples){
samples=_samples;
channels=_channels;
numsamples=_numsamples;
}
uint16_t *samples;
int channels,numsamples;
};
class Frame_parameters{
public:
Frame_parameters(double _time,double _framerate,double _duration,int _w,int _h,Audio_frame *_audio=nullptr)
{ time=_time; framerate=_framerate; duration=_duration; w=_w; h=_h;audio=_audio;};
Frame_parameters(int _frame,double _framerate,double _duration,int _w,int _h,Audio_frame *_audio=nullptr)
{ time=((double)_frame)/_framerate; framerate=_framerate; duration=_duration; w=_w; h=_h;audio=_audio;};
int h,w;
Frame_parameters lastframe() const{
return Frame_parameters(time-(1.0/framerate),framerate,duration,w,h);
}
Frame_parameters nextframe() const{
return Frame_parameters(time+(1.0/framerate),framerate,duration,w,h);
}
double time; //num/denom ?
double framerate;
double duration;
Audio_frame *audio;
};
class Variable { //pure virtual base type for variable pointers
public:
Variable(){connection=nullptr;};
virtual ~Variable(){};
virtual Json::Value to_json()=0;
virtual void init(Json::Value s)=0;
virtual bool connect(Node* target)=0;
virtual std::string get_type()=0;
Node* connection;
bool connectable;
std::string name;
};
template <class T> class Variable_type : public Variable {
public:
Variable_type(std::string _name,bool _connectable){name=_name;connectable=_connectable;};
void init(Json::Value s){
std::istringstream cur(s["value"].asString());
cur >> value;
name=s["name"].asString();
}
Json::Value to_json();
std::string get_type(){ //need this to output node templates
return TypeName<T>::Get();
}
//have to cast connect and get_output to use templated return types
bool connect(Node* target){
if (connectable){
if (dynamic_cast<Node_type<T>*>(target)){
connection=target;
return true;
}
}
return false;
}
const T& get(const Frame_parameters &frame){
if (connection){
return (dynamic_cast<Node_type<T>*>(connection))->get_output(frame);
}
return value;
}
T value;
};
class Variable_array: public Variable {
public:
Variable_array(){};
std::vector<Variable> values;
};
template <class T> class Variable_array_type: public Variable_array {
public:
Variable_array_type(std::string n){name=n;connectable=true;};
void init(Json::Value s){
std::istringstream cur(s["value"].asString());
cur >> value;
name=s["name"].asString();
}
Json::Value to_json();
std::string get_type(){ //need this to output node templates
return TypeName<T>::Get();
}
bool connect(Node* target){
if (connectable){
if (dynamic_cast<Node_type<T>*>(target)){
connection=target;
return true;
}
}
return false;
}
void add(std::string _name="",bool _connectable=true,int num=1){
for (int i=0;i<num;i++) values.push_back(Variable_type<T>(_name,_connectable));
}
bool connect(int which,Node* target){
if (values.size()>which){
return values[which].connect(target);
}
return false;
}
const T& get(int which,const Frame_parameters &frame){
if (values.size()>which){
return values[which].get(frame);
}
return value;
}
std::vector<Variable_type<T>> values;
T value;
};
//don't forget the dupliicate inlets
//it needs to be a property of a var
//vars need to be:
//std::unordered_map<std::string,std::pair<bool,std::vector<Variable*>>>
//??
//or could it be that a var has properties
//var needs to be a class?
//or multi var could be a subclass of var?
//with an extra accessor const T& get(int which,const Frame_parameters &frame)
//in Node we could have create_multi_inlet() and add_multi_inlet()
class Node { //base type for node pointers
public:
virtual ~Node(){
for (auto v:vars){
delete v.second;
}
}
bool connect(std::string v,Node *t){
auto var=vars.find(v);
if (var!=vars.end()){
if ((*var).second->connect(t)){
return true;
}
}
return false;
}
Json::Value to_json();
virtual Node* clone(Json::Value &_settings)=0;
virtual std::string output_type()=0;
std::string type;
std::string id;
std::string type_id;
std::string description;
std::string title;
std::string ui_type;
std::unordered_map<std::string,Variable*> vars;
};
template <class NT> class Node_type : public Node {
public:
virtual const NT& get_output(const Frame_parameters &frame)=0;
void init(Json::Value settings){
if (settings["vars"]!=nullptr){
for ( uint32_t i = 0; i < settings["vars"].size(); ++i ) {
vars[settings["vars"][i]["name"].asString()]->init(settings["vars"][i]);
}
}
if (settings["id"]!=nullptr) id=settings["id"].asString();
}
std::string output_type(){return TypeName<NT>::Get();};
template <class IT> Variable_type<IT>* create_inlet(std::string name){
vars[name]=new Variable_type<IT>(name,true);
return (dynamic_cast<Variable_type<IT>*>(vars[name]));
}
template <class IT> Variable_type<IT>* create_attribute(std::string name){
vars[name]=new Variable_type<IT>(name,false);
return (dynamic_cast<Variable_type<IT>*>(vars[name]));
}
template <class IT> Variable_array_type<IT>* create_array(std::string name){
vars[name]=new Variable_array_type<IT>(name);
return (dynamic_cast<Variable_array_type<IT>*>(vars[name]));
}
};
}
#endif //ROTOR_H
|