summaryrefslogtreecommitdiff
path: root/NT/src/rotor.h
blob: 26f7fb062d2b26aec42431811adc67d2dbf2ff7e (plain)
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
#ifndef ROTOR_H
#define ROTOR_H

#include <string>
#include <sstream>
#include <iostream>
#include <map>
#include <vector>
#include <unordered_map>
#include <json/json.h>

#include "xmlIO.h"

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 void init(std::string s)=0;
			virtual ~Variable(){};
			virtual bool connect(Node* target)=0;
			Node* connection;
			bool connectable;
	};
	template <class T> class Variable_type : public Variable {
		public:
			Variable_type(bool _c){connectable=_c;};
			void init(std::string s);
	    	bool connect(Node* target);
			const T& get(const Frame_parameters &frame);
			T value;
	};
	class Node { //base type for node pointers
		public:
			virtual ~Node(){
				for (auto v:vars){
					delete v.second;
				}
			}
			virtual Node* clone(map<string,string> &_settings)=0;
			std::string type;
		protected:
			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(std::map<std::string,std::string> settings);
			bool connect(std::string v,Node *t);
			template <class IT> Variable_type<IT>* create_inlet(std::string name);
			template <class IT> Variable_type<IT>* create_attribute(std::string name);
	};
	class Node_factory{
		public:
			Node_factory();
			~Node_factory(){
				for (auto t:type_map) delete t.second;
			}
			void add_type(std::string type,Node* proto){
				type_map[type]=proto;
				type_map[type]->type=type;
			};
			void add_type(std::string type,Node* proto,std::vector<Rotor::Node*> &category){
				add_type(type,proto);
				category.push_back(proto);
			};
			Node *create(std::map<std::string,std::string> &settings){
				if (settings.find("type")!=settings.end()) {
					if (type_map.find(settings["type"])!=type_map.end()) {
						return type_map[settings["type"]]->clone(settings);
					}
				}
				return NULL;
			};
			bool list_node(const std::string &t,xmlIO XML);
			bool list_node(const std::string &t,Json::Value &JSON);
			void list_node(Rotor::Node* type,xmlIO XML,int i=0);
			Json::Value list_node(Rotor::Node* type);
			void list_nodes(xmlIO XML);
			void list_nodes(Json::Value &JSON);
			void list_categories(xmlIO XML);
			void list_categories(Json::Value &JSON);
		private:
			std::map<std::string,Node*> type_map;
			std::map<std::string,std::vector<Rotor::Node*> > category;
	};
}

#endif //ROTOR_H