summaryrefslogtreecommitdiff
path: root/rotord/rotor.h
blob: ed566c73cd044569db49656e3d15a79933c4e151 (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
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
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
/*
nodes can have many inputs but only 1 output

image nodes that use an image as input can pass on the incoming image only if its unchanged.

TODO - parameter class that automatically links variable to correctly named inputs
*/

#include <unordered_map>
#include <deque>
#include <math.h>
#include <memory>

#include "Poco/Net/HTTPServer.h"
#include "Poco/Net/HTTPResponse.h"
#include "Poco/UUID.h"
#include "Poco/UUIDGenerator.h"
#include "Poco/Notification.h"
#include "Poco/NotificationCenter.h"
#include "Poco/Observer.h"
#include "Poco/ThreadPool.h"
#include "Poco/Thread.h"
#include "Poco/Task.h"
#include "Poco/Runnable.h"
#include "Poco/Mutex.h"
#include "Poco/Random.h"
#include "Poco/AutoPtr.h"
#include "Poco/File.h"
#include "Poco/Base64Encoder.h"
#include "Poco/Path.h"
#include <iostream>


using Poco::UUID;
using Poco::UUIDGenerator;
using Poco::Net::HTTPResponse;

/*
extern "C" {
	#include <libavcodec/avcodec.h>
	#include <libavformat/avformat.h>
	#include <libavutil/opt.h>
	#include <libavutil/channel_layout.h>
	#include <libavutil/common.h>
	#include <libavutil/imgutils.h>
	#include <libavutil/mathematics.h>
	#include <libavutil/samplefmt.h>

	#include <libavutil/dict.h>
	//#include <libavutil/dict.c> stops the compiler error but causes a linker error. does libavcodec need to be statically linked?
	#include <libavutil/imgutils.h>
	#include <libavutil/samplefmt.h>
	//#include <libavutil/timestamp.h>
}
*/


#define AUDIO_INBUF_SIZE 20480
#define AUDIO_REFILL_THRESH 4096

#include "vampHost.h"
#include "xmlIO.h"
#include "utils.h" //fequal
#include "libavwrapper.h"

namespace Rotor {
#define IDLE 0
#define ANALYSING_AUDIO 1
#define AUDIO_READY 2
#define CREATING_PREVIEW 3
#define PREVIEW_READY 4
#define RENDERING 5
#define RENDER_READY 6

#define ANALYSE_AUDIO 1
#define PREVIEW 2
#define RENDER 3



	//forward declaration
	class Node;
	class Signal_node;
	class Image_node;
	class Parameter_input;

	//http://blog.tomaka17.com/2012/03/libavcodeclibavformat-tutorial/
	struct Packet {
		explicit Packet(AVFormatContext* ctxt = nullptr) {
			av_init_packet(&packet);
			packet.data = nullptr;
			packet.size=0;
			if (ctxt) reset(ctxt);
		}

		Packet(Packet&& other) : packet(std::move(other.packet)) {
			other.packet.data = nullptr;
		}

		~Packet() {
			if (packet.data)
				av_free_packet(&packet);
		}

		void reset(AVFormatContext* ctxt) {
			if (packet.data)
				av_free_packet(&packet);
			if (av_read_frame(ctxt, &packet) < 0)
				packet.data = nullptr;
		}

		AVPacket packet;
	};
	class Time_spec{
		public:
			Time_spec(){};
			Time_spec(float _time,float _framerate,float _duration){ time=_time; framerate=_framerate; duration=_duration;};
			float time;
			float framerate;
			float duration;
			Time_spec lastframe() const{
				return Time_spec(time-(1.0f/framerate),framerate,duration);
			}
	};
	class Frame_spec: public Time_spec{
		public:
			Frame_spec(float _time,float _framerate,float _duration,int _w,int _h){ time=_time; framerate=_framerate; duration=_duration; w=_w; h=_h;};
			Frame_spec(int _frame,float _framerate,float _duration,int _w,int _h){ time=((float)_frame)/_framerate; framerate=_framerate; duration=_duration; w=_w; h=_h;};
			//Frame_spec(time,_framerate,_duration,_w,_h);};

			//float time; //this hould probably be implemented with a num/denom scheme eventually for accuracy
			//float framerate;
			int h,w;
			//Frame_spec lastframe(){
			//	return Frame_spec(time-(1.0f/framerate),framerate,w,h);
			//}
			int frame(){
				return (int)((time*framerate)+0.5); //rounded to the nearest frame
			}
	};
	class Colour{
		public:
			Colour(){
				r=g=b=0;
			}
			Colour(int c){
				r=c&0xFF;
				g=(c&0xFF00)>>8;
				b=(c&0xFF0000)>>16;
			}
			Colour(std::string s){
				r=(uint8_t)ofHexToChar(s.substr(0,2));
				g=(uint8_t)ofHexToChar(s.substr(2,2));
				b=(uint8_t)ofHexToChar(s.substr(4,2));
			}
			uint8_t r,g,b;
	};
	class Image{
		public:
			Image(){
				zero();
			};
			Image(int _w,int _h){
				zero();
				setup(_w,_h);
			};
			~Image() {
				free();
			};
			void free(){
				if (RGBdata&&ownsRGBdata) delete[] RGBdata;
				if (Adata&&ownsAdata) delete[] Adata;
				if (Zdata&&ownsZdata) delete[] Zdata;
				zero();
			}
			void zero(){
				RGBdata=nullptr;
				Adata=nullptr;
				Zdata=nullptr;
				w=0;
				h=0;
				ownsRGBdata=ownsAdata=ownsZdata=false;
			}
			bool setup(int _w,int _h){ //set up with internal data
				if (w!=_w||h!=_h||!ownsRGBdata||!ownsAdata||!ownsZdata){
					free();
					w=_w;
					h=_h;
					RGBdata=new uint8_t[w*h*3];
					Adata=new uint8_t[w*h];
					Zdata=new uint16_t[w*h];
					ownsRGBdata=ownsAdata=ownsZdata=true;
					return true;
				}
				else return false;
			}
			bool setup_fromRGB(int _w,int _h,uint8_t *pRGBdata){ //possibility of just resetting pointer?
				if (w!=_w||h!=_h||ownsRGBdata||!ownsAdata||!ownsZdata){
					free();
					w=_w;
					h=_h;
					RGBdata=pRGBdata;
					Adata=new uint8_t[w*h];
					Zdata=new uint16_t[w*h];
					ownsRGBdata=false;
					ownsAdata=ownsZdata=true;
					return true;
				}
				return false;
			}
			Image* clone(){
				Image *t=new Image(w,h);
				for (int i=0;i<w*h*3;i++) {
					t->RGBdata[i]=RGBdata[i];
				}
				return t;
			}
			Image & operator+=(const Image &other) {
				if (other.w!=w||other.h!=h) {
					cerr<<"Rotor: cannot add images with different sizes! (wanted "<<w<<"x"<<h<<", got "<<other.w<<"x"<<other.h<<")"<<endl;
				}
				else {
					for (int i=0;i<w*h*3;i++){
						//creates rainbow overload RGBdata[i]=(unsigned char)(((int)other.RGBdata[i]+(int)RGBdata[i])&0x00FF);
						RGBdata[i]=(unsigned char)min((int)other.RGBdata[i]+(int)RGBdata[i],0x00FF);
					}
				}
				return *this;
			}
			Image & add_wrap(const Image &other) {
				if (other.w!=w||other.h!=h) {
					cerr<<"Rotor: cannot add images with different sizes! (wanted "<<w<<"x"<<h<<", got "<<other.w<<"x"<<other.h<<")"<<endl;
				}
				else {
					for (int i=0;i<w*h*3;i++){
						//creates rainbow overload
						RGBdata[i]=(unsigned char)(((int)other.RGBdata[i]+(int)RGBdata[i]));
					}
				}
				return *this;
			}
			Image * operator*(const float &amount) {
				Image *other=new Image(w,h);
				uint8_t *LUT=new uint8_t[0xFF];
				for (int i=0;i<0xFF;i++) {
					LUT[i]=(uint8_t)min(0xFF,max(0,(int)(i*amount)));
				}
				for (int i=0;i<w*h*3;i++){
					other->RGBdata[i]=LUT[RGBdata[i]];
				}
				delete[] LUT;
				return other;
			}
			Image * operator+(const float &amount) {
				Image *other=new Image(w,h);
				uint8_t *LUT=new uint8_t[0xFF];
				for (int i=0;i<0xFF;i++) {
					LUT[i]=(uint8_t)min(0xFF,max(0,(int)(i+(amount*255.0f))));
				}
				for (int i=0;i<w*h*3;i++){
					other->RGBdata[i]=LUT[RGBdata[i]];
				}
				delete[] LUT;
				return other;
			}
			Image * operator-(const float &amount) {
				Image *other=new Image(w,h);
				uint8_t *LUT=new uint8_t[0xFF];
				for (int i=0;i<0xFF;i++) {
					LUT[i]=(uint8_t)min(0xFF,max(0,(int)(i-(amount*255.0f))));
				}
				for (int i=0;i<w*h*3;i++){
					other->RGBdata[i]=LUT[RGBdata[i]];
				}
				delete[] LUT;
				return other;
			}
			Image * operator/(const float &amount) {
				Image *other=new Image(w,h);
				uint8_t *LUT=new uint8_t[0xFF];
				for (int i=0;i<0xFF;i++) {
					LUT[i]=(uint8_t)min(0xFF,max(0,(int)(i/amount)));
				}
				for (int i=0;i<w*h*3;i++){
					other->RGBdata[i]=LUT[RGBdata[i]];
				}
				delete[] LUT;
				return other;
			}
			uint8_t *RGBdata;
			uint8_t *Adata;
			uint16_t *Zdata;
			int h,w;
			bool ownsRGBdata,ownsAdata,ownsZdata; //better done through auto_ptr?
	};
	class Render_status{
		public:
			int id;
			float progress;
	};
	class Render_requirements{
		public:
			int num_performances;
			int num_clips;
	};
	class Command_response{
		public:
		    Command_response() { status=Poco::Net::HTTPResponse::HTTP_OK; }
		    std::string description;
		    Poco::Net::HTTPResponse::HTTPStatus status;
	};
	class Input{
		public:
			Input(const string &_desc): connection(nullptr),description(_desc){};
			Node* connection;
			string description;
	};
	class Image_input: public Input{
		public:
			bool connect(Image_node *source);
			Image_input(const string &_desc): Input(_desc){};
	};
	class Signal_input: public Input{
		public:
			bool connect(Signal_node *source);
			Signal_input(const string &_desc): Input(_desc){};
	};
	class Parameter_input: public Signal_input{
		public:
			Parameter_input(const string &_param,const string &_desc): Signal_input(_desc),receiver(nullptr),parameter(_param){};
			float *receiver;
			void update(const Time_spec& time);
			string parameter;
	};
	class Node{
		public:
			virtual Node* clone(map<string,string> &_settings)=0;
			virtual ~Node(){};
			UUID uid;									//every usable node has a UUID
			int id;
			vector<Signal_input*> inputs;				//simple node can have signal inputs, output depends on node type
			vector<Parameter_input*> parameter_inputs;		//linked parameters can convert from settings to inputs
			void create_signal_input(const string &description) {inputs.push_back(new Signal_input(description));};
			void create_parameter_input(const string &parameter,const string &description) {parameter_inputs.push_back(new Parameter_input(parameter,description));};
			string description;
			string type;
			string output_type;
			string ID;
			string find_setting(map<string,string> &settings,string key,string def=""){ if (settings.find(key)!=settings.end()) return settings[key]; else return def;};
			float find_setting(map<string,string> &settings,string key,float def){ if (settings.find(key)!=settings.end()) return ofToFloat(settings[key]); else return def;};
			int find_setting(map<string,string> &settings,string key,int def){ if (settings.find(key)!=settings.end()) return ofToInt(settings[key]); else return def;};
			void base_settings(map<string,string> &settings) {
				description=find_setting(settings,"description");
				type=find_setting(settings,"type");
				output_type=find_setting(settings,"output");
				ID=find_setting(settings,"ID");
			}
			virtual void set_parameter(const std::string &key,const std::string &value){};
			virtual void link_params(){}; //TODO make param classes that link automatically
			void update_params(const Time_spec& time);
		};
	class Signal_node: public Node{
		public:
			virtual ~Signal_node(){};
			const float get_output(const Time_spec &time) { update_params(time); return output(time); };
			virtual const float output(const Time_spec &time) { return 0.0f; };
	};
	class Image_node: public Node{
		public:
			virtual ~Image_node(){};
			vector<Image_input*> image_inputs;			//image node also has image inputs and outputs
			void create_image_input(const string &description) {image_inputs.push_back(new Image_input(description));};
			Image *get_output(const Frame_spec &frame) { update_params((Time_spec)frame); return output(frame); };
			virtual const Image *output(const Frame_spec &frame)=0;
			Image *get_preview(const Frame_spec &frame);
			Image *image; 								//this can be privately allocated or just passed on as the node see fit
		private:
			float image_time;
	};
	class Base_audio_processor: public Signal_node {
		public:
			virtual ~Base_audio_processor(){};
			virtual int process_frame(uint8_t *data,int samples)=0;
			virtual bool init(int _channels,int _bits,int _samples,int _rate)=0;
			virtual void cleanup()=0;
			virtual void print_summary(){};
			int channels,bits,samples,rate;
	};
	//actual nodes-------------------------------------------------
	class Audio_analysis: public Base_audio_processor  {
		public:
			Audio_analysis(){};
			Audio_analysis(map<string,string> &settings) {
				base_settings(settings);
				soname=find_setting(settings,"soname");
				id=find_setting(settings,"id");
				outputNo=find_setting(settings,"outputNo",0);
			};
			Audio_analysis* clone(map<string,string> &_settings) { return new Audio_analysis(_settings);};
			bool init(int _channels,int _bits,int _samples,int _rate);
			void cleanup();
			void set_parameter(const std::string &key,const std::string &value){params[key]=ofToFloat(value);};
			int process_frame(uint8_t *data,int samples_in_frame);
			const float output(const Time_spec &time) {
				if (analyser.features.size()) {
					auto i=analyser.features.upper_bound(time.time); //the first element in the container whose key is considered to go after k
					if (i!=analyser.features.end()){
						float uk=i->first;
						i--;
						float lk=i->first;
						int ln=i->second;
						return (((time.time-lk)/(uk-lk))+ln);
					}
				}
				return 0.0f;
			}
			void print_features();
			void print_summary(){
				cerr<<"vamp plugin "<<id<<" of library "<<soname<<" found "<<analyser.features.size()<<" features "<<endl;
			};
		private:
			string soname,id;
			int outputNo;
			vampHost::Analyser analyser;
			map <string,float> params;
	};
	class Track_time: public Signal_node {
		public:
			Track_time(){};
			Track_time(map<string,string> &settings) {
				base_settings(settings);
			};
			Track_time* clone(map<string,string> &_settings) { return new Track_time(_settings);};
			const float output(const Time_spec &time) {
				return time.time/time.duration;
			}
	};
#define COMPARISON_Equal 1
#define COMPARISON_Not_equal 2
#define COMPARISON_Greater 3
#define COMPARISON_Less 4
#define COMPARISON_Greater_or_equal 5
#define COMPARISON_Less_or_equal 6
	class Comparison: public Signal_node {
		public:
			Comparison(){};
			Comparison(map<string,string> &settings) {
				base_settings(settings);
				value=find_setting(settings,"value",0.0f);
				string _op=find_setting(settings,"operator","==");
				if (_op=="==") op=COMPARISON_Equal;
				if (_op=="!=") op=COMPARISON_Not_equal;
				if (_op==">") op=COMPARISON_Greater;
				if (_op=="<") op=COMPARISON_Less;
				if (_op==">=") op=COMPARISON_Greater_or_equal;
				if (_op=="<=") op=COMPARISON_Less_or_equal;
			}
			void link_params() {
				for (auto p:parameter_inputs){
					if (p->parameter=="value") p->receiver=&value;
				}
			};
			Comparison* clone(map<string,string> &_settings) { return new Comparison(_settings);};
			const float output(const Time_spec &time) {
				if (inputs.size()) { //there should there be a way to specify number of inputs in the code rather than in xml
                    if (inputs[0]->connection) {
                        float in= (((Signal_node*)inputs[0]->connection)->get_output(time));
                        switch (op) {
                        	case COMPARISON_Equal:
                        		return fequal(value,in)?1.0f:0.0f;
                        		break;
							case COMPARISON_Not_equal:
								return fequal(value,in)?0.0f:1.0f;
                        		break;
							case COMPARISON_Greater:
								return fgreater(value,in)?1.0f:0.0f;
                        		break;
							case COMPARISON_Less:
								return fless(value,in)?1.0f:0.0f;
                        		break;
							case COMPARISON_Greater_or_equal:
								return fgreater_or_equal(value,in)?1.0f:0.0f;
                        		break;
							case COMPARISON_Less_or_equal:
								return fless_or_equal(value,in)?1.0f:0.0f;
                        		break;
                        }
                    }
			    }
				return 0.0f;
			}
			int op;
			float value;
	};
#define ARITHMETIC_plus 1
#define ARITHMETIC_minus 2
#define ARITHMETIC_multiply 3
#define ARITHMETIC_divide 4
#define ARITHMETIC_modulo 5
	class Arithmetic: public Signal_node {
		public:
			Arithmetic(){};
			Arithmetic(map<string,string> &settings) {
				base_settings(settings);
				value=find_setting(settings,"value",0.0f);
				string _op=find_setting(settings,"operator","+");
				if (_op=="+") op=ARITHMETIC_plus;
				if (_op=="-") op=ARITHMETIC_minus;
				if (_op=="*") op=ARITHMETIC_multiply;
				if (_op=="/") op=ARITHMETIC_divide;
				if (_op=="%") op=ARITHMETIC_modulo;
			}
			void link_params() {
				for (auto p:parameter_inputs){
					p->receiver=nullptr;
					if (p->parameter=="value") p->receiver=&value;
				}
			};
			Arithmetic* clone(map<string,string> &_settings) { return new Arithmetic(_settings);};
			const float output(const Time_spec &time) {
				if (inputs.size()) { //there should there be a way to specify number of inputs in the code rather than in xml
                    if (inputs[0]->connection) {
                        float in= (((Signal_node*)inputs[0]->connection)->get_output(time));
                        switch (op) {
                        	case ARITHMETIC_plus:
                        		return in+value;
                        		break;
							case ARITHMETIC_minus:
								return in-value;
                        		break;
							case ARITHMETIC_multiply:
								return in*value;
                        		break;
							case ARITHMETIC_divide:
								return in/value;
                        		break;
							case ARITHMETIC_modulo:
								return fmod(in,value);
                        		break;
                        }
                    }
			    }
				return 0.0f;
			}
			int op;
			float value;
	};
	class Signal_divide: public Signal_node {
		public:
			Signal_divide(){};
			Signal_divide(map<string,string> &settings) {
				base_settings(settings);
				divide_amount=ofToFloat(find_setting(settings,"amount"));
				for (auto p:parameter_inputs){
					if (p->parameter=="amount") p->receiver=&divide_amount;
				}
			};
			Signal_divide* clone(map<string,string> &_settings) { return new Signal_divide(_settings);};
			const float output(const Time_spec &time) {
			    if (inputs.size()) { //there should there be a way to specify number of inputs in the code rather than in xml
                    if (inputs[0]->connection) {
                        return (((Signal_node*)inputs[0]->connection)->get_output(time))/divide_amount;
                    }
			    }
				return 0.0f;
			}
			float divide_amount;
	};
	class Is_new_integer: public Signal_node {
		public:
			Is_new_integer(){};
			Is_new_integer(map<string,string> &settings) {
				base_settings(settings);
			};
			Is_new_integer* clone(map<string,string> &_settings) { return new Is_new_integer(_settings);};
			const float output(const Time_spec &time) {
				if (inputs[0]->connection) {
					float s1=(((Signal_node*)(inputs[0]->connection))->get_output(time));
					float s2=(((Signal_node*)(inputs[0]->connection))->get_output(time.lastframe()));
					if (((int)s1)>((int)s2)) {
						return 1.0f;
					}
				}
				return 0.0f;
			}
	};
	class On_off: public Signal_node {
		public:
			On_off(){};
			On_off(map<string,string> &settings) {
				base_settings(settings);
			};
			On_off* clone(map<string,string> &_settings) { return new On_off(_settings);};
			const float output(const Time_spec &time) {
				if (inputs[0]->connection) {
					float s1=(((Signal_node*)(inputs[0]->connection))->get_output(time));
					if ((int)s1%2) return 1.0f;
				}
				return 0.0f;
			}
	};
	//pseudo random repeatable hash function
	//http://create.stephan-brumme.com/fnv-hash/
	const uint32_t Prime = 0x01000193; //   16777619
	const uint32_t Seed  = 0x811C9DC5; // 2166136261
	class Random: public Signal_node {
		public:
			Random(){};
			Random(map<string,string> &settings) {
				base_settings(settings);
				seed=ofToFloat(find_setting(settings,"amount"));
				for (auto p:parameter_inputs){
					if (p->parameter=="seed") p->receiver=&seed;
				}
				key=0xffffffff;
			};
			Random* clone(map<string,string> &_settings) { return new Random(_settings);};
			const float output(const Time_spec &time) {
			    if (inputs.size()) { //there should there be a way to specify number of inputs in the code rather than in xml
                    if (inputs[0]->connection) {
                    	return 0.0f;
                        //return (((Signal_node*)inputs[0]->connection)->get_output(time))/divide_amount;
                    }
			    }
				return 0.0f;
			}
			float seed;
			uint32_t key;
	};
	class Signal_output: public Signal_node {
		public:
			Signal_output(){};
			Signal_output(map<string,string> &settings) {
				base_settings(settings);
			};
			Signal_output* clone(map<string,string> &_settings) { return new Signal_output(_settings);};
			bool render(const float duration, const float framerate,string &xml_out);
			const float output(const Time_spec &time) {
				if (inputs[0]->connection) {
					return ((Signal_node*)(inputs[0]->connection))->get_output(time);
				}
				else return 0.0f;
			}
	};
	class Testcard: public Image_node {
		public:
			Testcard(){};
			Testcard(map<string,string> &settings) {
				base_settings(settings);
				image=new Image();
			};
			~Testcard(){ delete image;};
			Testcard* clone(map<string,string> &_settings) { return new Testcard(_settings);};
			Image *output(const Frame_spec &frame){
				if (image->setup(frame.w,frame.h)) {

				}
				//always create testcard
				//float ws=(255.0f/frame.w);
				float hs=(255.0f/frame.h);
				for (int i=0;i<frame.h;i++){
					for (int j=0;j<frame.w;j++){
						image->RGBdata[(i*frame.w+j)*3]=(uint8_t)((int)((i+(frame.time*25.0f)*hs))%255);
						image->RGBdata[((i*frame.w+j)*3)+1]=(uint8_t)((int)((j+(frame.time*100.0f)*hs))%255);
						image->RGBdata[((i*frame.w+j)*3)+2]=(uint8_t)(0);
						image->Adata[i*frame.w+j]=(uint8_t)255;
						image->Zdata[i*frame.w+j]=(uint16_t)512; //1.0 in fixed point 8.8 bits
					}
				}
				return image;
			}
		private:
			Image *image; //is an image generator
	};
	class Invert: public Image_node {
		public:
			Invert(){};
			Invert(map<string,string> &settings) {
				base_settings(settings);
				image=new Image();
			};
			~Invert(){ delete image;};
			Invert* clone(map<string,string> &_settings) { return new Invert(_settings);};
			Image *output(const Frame_spec &frame){
				if (inputs.size()) {
					if (image_inputs[0]->connection){
	                    if (inputs[0]->connection) {
	                    	if (fgreater_or_equal(1.0f,(((Signal_node*)inputs[0]->connection)->get_output((Time_spec)frame)))) {
	                            Image *in=(((Image_node*)image_inputs[0]->connection)->get_output(frame));
	                            if (in){
                                    image->setup(frame.w,frame.h);
                                    for (int i=0;i<in->w*in->h*3;i++) {
                                        image->RGBdata[i]=255-in->RGBdata[i];
                                    }
                                    return image;
	                            }
	                        }
	                    }
	                    return (((Image_node*)image_inputs[0]->connection)->get_output(frame));
	                }

			    }
                if (image_inputs[0]->connection) {
                    return image;
                }
				return nullptr;
			}
		private:
			Image *image; //is an image generator
			//bool invert;
	};
	class Video_output: public Image_node {
		public:
			Video_output(){};
			Video_output(map<string,string> &settings) {
				base_settings(settings);
			};
			~Video_output(){ }; 
			Image *output(const Frame_spec &frame){
				if (image_inputs[0]->connection) {
					return ((Image_node*)(image_inputs[0]->connection))->get_output(frame);
				}
				else return nullptr;
			};
			Video_output* clone(map<string,string> &_settings) { return new Video_output(_settings);};
			bool render(const float duration, const float framerate,const string &output_filename,const string &audio_filename,float& progress);

		private:

	};
	class Video_loader: public Image_node {
		public:
			Video_loader(){};
			Video_loader(map<string,string> &settings) {
				base_settings(settings);
				isLoaded=false;
			};
			~Video_loader(){};
			bool load(const string &filename);
			Image *output(const Frame_spec &frame);
			Video_loader* clone(map<string,string> &_settings) { return new Video_loader(_settings);};
		private:
			libav::decoder player;
			Image image;
			bool isLoaded;
	};
	class Video_cycler: public Image_node {
		//cycles through video inputs in order
		public:
			Video_cycler(){};
			Video_cycler(map<string,string> &settings) {
				base_settings(settings);
			};
			~Video_cycler(){};
			bool load(const string &filename);
			Image *output(const Frame_spec &frame){
				int which_input=0;
				if (inputs[0]->connection) {
					which_input=((int)((Signal_node*)inputs[0]->connection)->get_output((Time_spec)frame))%image_inputs.size();
				}
				if (image_inputs.size()) {
					if (image_inputs[which_input]->connection){
						return (((Image_node*)image_inputs[which_input]->connection)->get_output(frame));
					}
				}
				return nullptr;
			}
			Video_cycler* clone(map<string,string> &_settings) { return new Video_cycler(_settings);};
		private:
	};
	class Signal_colour: public Image_node {
		//cycles through video inputs in order
		public:
			Signal_colour(){};
			Signal_colour(map<string,string> &settings) {
				base_settings(settings);
				string colours=find_setting(settings,"palette","");
				for (int i=0;i<colours.size()/6;i++){
					palette.push_back(Colour(colours.substr(i*6,6)));
				}
				for (auto i: palette) {
					cerr << "Signal_colour found palette colour: "<<(int)i.r<<" "<<(int)i.g<<" "<<(int)i.b<<endl;					
				}
				prevcol=-1;
			};
			~Signal_colour(){};
			Image *output(const Frame_spec &frame){
				if (palette.size()) {
					if (inputs.size()) {
						if (inputs[0]->connection){
							int col= ((int)(((Signal_node*)inputs[0]->connection)->get_output(frame)))%palette.size();
							if (col!=prevcol||image.w!=frame.w||image.h!=frame.h){
								image.setup(frame.w,frame.h);
								for (int i=0;i<image.w*image.h;i++){
									image.RGBdata[i*3]=palette[col].r;
									image.RGBdata[i*3+1]=palette[col].g;
									image.RGBdata[i*3+2]=palette[col].b;
								}
								prevcol=col;
							}
							return &image;
						}
					}
				}
				return nullptr;
			}
			Signal_colour* clone(map<string,string> &_settings) { return new Signal_colour(_settings);};
		private:
			vector<Rotor::Colour> palette;
			Image image;
			int prevcol;
	};
	class Signal_greyscale: public Image_node {
		//Draws signal bars in greyscale
		public:
			Signal_greyscale(){};
			Signal_greyscale(map<string,string> &settings) {
				base_settings(settings);
				prevcol=-1;
			};
			~Signal_greyscale(){};
			Image *output(const Frame_spec &frame){
				if (inputs.size()) {
					if (inputs[0]->connection){
						float sig= ((((Signal_node*)inputs[0]->connection)->get_output(frame)));
						uint8_t col=255-((uint8_t)(sig*255.0f));
						if (col!=prevcol||image.w!=frame.w||image.h!=frame.h){
							image.setup(frame.w,frame.h);
							for (int i=0;i<image.w*image.h*3;i++){
								image.RGBdata[i]=col;
							}
							prevcol=col;
						}
						return &image;
					}
				}
			return nullptr;
			}
			Signal_greyscale* clone(map<string,string> &_settings) { return new Signal_greyscale(_settings);};
		private:
			Image image;
			uint8_t prevcol;
	};
	class Image_arithmetic: public Image_node {
		//Draws signal bars in greyscale
		public:
			Image_arithmetic(){};
			Image_arithmetic(map<string,string> &settings) {
				base_settings(settings);
				value=find_setting(settings,"value",0.0f);
				string _op=find_setting(settings,"operator","+");
				if (_op=="+") op=ARITHMETIC_plus;
				if (_op=="-") op=ARITHMETIC_minus;
				if (_op=="*") op=ARITHMETIC_multiply;
				if (_op=="/") op=ARITHMETIC_divide;
				//if (_op=="%") op=ARITHMETIC_modulo;  ??what would this even mean?
				image=nullptr;
			}
			void link_params() {
				for (auto p:parameter_inputs){
					if (p->parameter=="value") {
						p->receiver=&value;
					}
				}
				
			};
			~Image_arithmetic(){if (image) delete image;};
			Image *output(const Frame_spec &frame){
				if (image_inputs.size()) {
					if (image_inputs[0]->connection){
						if (image) delete image;
						Image *in=(((Image_node*)image_inputs[0]->connection)->get_output(frame));
                        switch (op) {
                        	case ARITHMETIC_plus:
                        		image=(*in)+value;
                        		break;
							case ARITHMETIC_minus:
								image=(*in)-value;
                        		break;
							case ARITHMETIC_multiply:
								image=(*in)*value;
                        		break;
							case ARITHMETIC_divide:
								image=(*in)/value;
                        		break;
                        }
                        return image;
					}
				}
			return nullptr;
			}
			Image_arithmetic* clone(map<string,string> &_settings) { return new Image_arithmetic(_settings);};
		private:
			Image *image;
			float value;
			int op;
	};

	class Luma_levels: public Image_node {
		//applies LUT To RGB channels equally
		public:
			Luma_levels(){LUT=nullptr;image=nullptr;};
			Luma_levels(map<string,string> &settings) {
				base_settings(settings);
				levels_settings(settings);
				image=new Image();
			}
			void link_params() {
				for (auto p:parameter_inputs){
					if (p->parameter=="black_in") p->receiver=&black_in;
					if (p->parameter=="white_in") p->receiver=&white_in;
					if (p->parameter=="gamma") p->receiver=&gamma;
					if (p->parameter=="black_out") p->receiver=&black_out;
					if (p->parameter=="white_out") p->receiver=&white_out;
				}
			};
			~Luma_levels(){if (LUT) {delete[] LUT;} if (image) {delete image;} };
			void levels_settings(map<string,string> &settings){
				black_in=find_setting(settings,"black_in",0.0f);
				white_in=find_setting(settings,"white_in",1.0f);
				gamma=find_setting(settings,"gamma",1.0f);
				black_out=find_setting(settings,"black_out",0.0f);
				white_out=find_setting(settings,"white_out",1.0f);
				LUT=nullptr;
				generate_LUT();
			}
			void generate_LUT(){ //check this
				if (LUT) delete[] LUT;
				LUT=new unsigned char[256];
				float fltmax=(255.0f/256.0f);
				for (int i=0;i<256;i++){
					LUT[i]=(unsigned char)(((pow(min(fltmax,max(0.0f,(((((float)i)/256.0f)-black_in)/(white_in-black_in)))),(1.0/gamma))*(white_out-black_out))+black_out)*256.0f);
				}
			}
			void apply_LUT(const Image& in){
				apply_LUT(in,*image);
			}
			void apply_LUT(const Image& in,Image &out){ //facility to apply to other images for ingherited classes
				out.setup(in.w,in.h);
				for (int i=0;i<out.w*out.h*3;i++){
					out.RGBdata[i]=LUT[in.RGBdata[i]];
				}
			}
			Image *output(const Frame_spec &frame){
				if (image_inputs.size()) {
					if (image_inputs[0]->connection){
						if (LUT) {
							apply_LUT(*(((Image_node*)image_inputs[0]->connection)->get_output(frame)));
							return image;
						}
					}
				}
				return nullptr;
			}
			Luma_levels* clone(map<string,string> &_settings) { return new Luma_levels(_settings);};
		protected:
			unsigned char *LUT;
			Image *image;
			float black_in,white_in,gamma,black_out,white_out;
	};
	class Echo_trails: public Luma_levels {
		//draw trail frames additively that fade off over time
		//the hard thing here is how to cache frames, if its done cleverly it could have no impact when
		//used linearly
		//Image needs to overload operator+
		//need a clever data structure to cache frames - maybe a map of Image pointers

		//we know the frames we want to overlay as offsets ie -25,-20,-15,-10,-5
		//do we keep 25 frames loaded in order to benefit? 25 PAL frames is 60MB so probably so
		//OK so:
		//make a new set of pointers
		//identify if any of the new pointers can inherit old frames
		//delete unneeded old frames
		//load new frames
		//do the calculations

		//new set of pointers? or track frames by absolute frame number?
		//with relative pointers and switching frames, could use auto_ptr?

		//this cache mechanism should maybe be inheritable too?

		//it could be hugely beneficial to only do the LUT once?
		//although maybe the way to do the fading is to have a LUT for each frame?

		//2 inputs - "This frame" and "keyed"
		//hmm, that is clumsy, could it inherit from luma_levels

		//or is it actually best to use alpha keying after all!
		public:
			Echo_trails(){};
			Echo_trails(map<string,string> &settings) {
				base_settings(settings);
				//duration=find_setting(settings,"duration",1.0f);
				number=find_setting(settings,"number",1);
				fadeto=find_setting(settings,"fadeto",1.0f);
				levels_settings(settings);
				image=new Image();
				lastframe=-1;
				mode=find_setting(settings,"mode",0.0f);
			}
			void link_params() {
				for (auto p:parameter_inputs){
					if (p->parameter=="black_in") p->receiver=&black_in;
					if (p->parameter=="white_in") p->receiver=&white_in;
					if (p->parameter=="gamma") p->receiver=&gamma;
					if (p->parameter=="black_out") p->receiver=&black_out;
					if (p->parameter=="white_out") p->receiver=&white_out;

					//TODO: control an integer
					if (p->parameter=="mode") p->receiver=&mode;
				}
			};
			//~Echo_trails(){if (image) {delete image;} };
			~Echo_trails(){
				for (auto i:images) {delete i.second;}
			};
			Image *output(const Frame_spec &frame){
				//check if cache is valid
				if (frame.w!=image->w||frame.h!=image->h){ //or framerate changed?
					//clear cache and start over
					images.clear();
					lastframe=-1;
					//calculate frame interval
					//interval=(int)(((duration/number)*frame.framerate)+0.5);
					//total=interval*number;
				}
				int thisframe=frame.frame();
				//iterate cache and throw out any obsolete frames
				auto i = std::begin(images);
				while (i != std::end(images)) {
				    // check if the image is in the range we need
				    if (thisframe-(*i).first>number||thisframe-(*i).first<0) {
                        delete (*i).second;
				        i = images.erase(i);
				    }
				    else
				        ++i;
				}
				//if frame has already been calculated just return it
				if (thisframe==lastframe) {
					return image;
				}
				else {
					if (image_inputs.size()) {
						if (image_inputs[0]->connection){
							if (LUT) {
								//copy incoming image **writable
								image->free();
								image=(((Image_node*)image_inputs[0]->connection)->get_output(frame))->clone();
								for (int i=1;i<number;i++){
									//check echo frame isn't at negative time
									int absframe=thisframe-i;
									if (absframe>-1){
										//check if image is in the cache
										if (images.find(absframe)==images.end()){
											images[absframe]=new Image(frame.w,frame.h);
											Frame_spec wanted=Frame_spec(absframe,frame.framerate,frame.duration,frame.w,frame.h);
											apply_LUT(*(((Image_node*)image_inputs[0]->connection)->get_output(wanted)),*(images[absframe]));
										}
										//cerr<<"Rotor: about to apply image ("<<images[absframe].w<<"x"<<images[absframe].h<<")"<<endl;
										if (fless(1.0f,fadeto)){
											float amount=((((float)number-i)/number)*(1.0f-fadeto))+(1.0f-fadeto);
											Image *temp=*images[absframe]*amount;
											if (mode<0.5) {
												(*image)+=*temp;
											}
											else {
												image->add_wrap(*temp);
											}
											delete temp;
										}
										else {
                                             if (mode<0.5) (*image)+=*(images[absframe]);
                                             else (*image)=image->add_wrap(*(images[absframe]));
										}
									}
								}
								//for (int i=0;i<frame.w*frame.h*3;i++){
								//	image->RGBdata[i]=LUT[in->RGBdata[i]];
								//}
								lastframe=thisframe;
								return image;
							}
						}
					}
				}
				return nullptr;
			}
			Echo_trails* clone(map<string,string> &_settings) { return new Echo_trails(_settings);};
		protected:
			float duration,fadeto;
			int number;
			int interval,total,lastframe; //number of frames between displayed echoes
			unordered_map<int,Image*> images;
			float mode; //TODO make int, enum string parameter types
	};
	//-------------------------------------------------------------------
	class Node_factory{
		public:
			Node_factory();
			~Node_factory(){
				for (auto t:type_map) delete t.second;
			}
			void add_type(string type,Node* proto){
				type_map[type]=proto;
			};
			Node *create(map<string,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;
			};
		private:
			unordered_map<string,Node*> type_map;
	};
	class Graph{
		public:
			Graph(){duration=10.0f;loaded = false;};
			Graph(const string& _uid,const string& _desc){init(_uid,_desc);};
			void init(const string& _uid,const string& _desc){ uid=_uid;description=_desc;duration=10.0f;};
			string uid;								//every version of a graph has a UUID, no particular need to actually read its data(?)
													//?? is it faster than using strings??
			string description;
			std::unordered_map<string,Node*> nodes;
			vector<Node*> find_nodes(const string &type); //could be a way of finding a set based on capabilities?
			Node* find_node(const string &type);
			bool signal_render(string &signal_xml,const float framerate);
			bool video_render(const string &output_filename,const string &audio_filename,const float framerate,float& progress);
			int load(Poco::UUID uid);
			bool load(string &graph_filename);
			UUID save();									//save to DB, returns UUID of saved graph
			bool loaded;
			float duration;
			const string toString();
		private:
			Node_factory factory;
			xmlIO xml;
	};
	class Audio_thumbnailer: public Base_audio_processor {
		public:
			Audio_thumbnailer(){
				height=128;
				width=512;  //fit
				data=new uint8_t[height*width];
				memset(data,0,height*width);
			};
			~Audio_thumbnailer(){
				delete[] data;
			};
			Audio_thumbnailer* clone(map<string,string> &_settings) { return new Audio_thumbnailer();};
			bool init(int _channels,int _bits,int _samples,int _rate);
			void cleanup(){};
			int process_frame(uint8_t *data,int samples_in_frame);
			string print();
			uint8_t *data;
			int height,width,samples_per_column;
			int column,out_sample,sample,samples;
			int offset;
			double scale,accum;
	};
	class Render_context: public Poco::Task {				//Poco task object
															//manages a 'patchbay'
															//high level interfaces for the wizard
															//and low level interface onto the graph
		public:
			Render_context(const std::string& name): Task(name)  {
				audio_thumb=new Audio_thumbnailer();
				state=IDLE;
				output_framerate=25.0f;
				audio_loaded=false;

				xmlIO xml;
				if(xml.loadFile("settings.xml") ){
					graph_dir=xml.getAttribute("Rotor","graph_dir","",0);
					media_dir=xml.getAttribute("Rotor","media_dir","",0);
					output_dir=xml.getAttribute("Rotor","output_dir","",0);
				}
				else cerr<<"Rotor: settings.xml not found, using defaults"<<endl;
			};
			~Render_context(){delete audio_thumb;};
			void runTask();
			void add_queue(int item);
			Command_response session_command(const std::vector<std::string>& command);
			Render_status get_status();
			void cancel();									//interrupt locking process
			int make_preview(int nodeID, float time);		//starts a frame preview - returns status code -  how to retrieve?
			bool load_audio(const string &filename,vector<Base_audio_processor*> processors);
			bool _load_audio(const string &filename,vector<Base_audio_processor*> processors);
			Render_requirements get_requirements();
			bool load_video(const string &nodeID,const string &filename);//can be performance or clip
		private:
			int state;
			float progress;								//for a locking process: audio analysis or rendering
															//thread only does one thing at once
			std::deque<int> work_queue;
			Poco::Mutex mutex;            					//lock for access from parent thread
			std::string audio_filename;
			std::string output_filename;
			std::string graph_dir;
			std::string media_dir;
			std::string output_dir;

			Audio_thumbnailer *audio_thumb;
			Graph graph;
			Node_factory factory;
			float output_framerate;
			bool audio_loaded;

	};
}

/*
coding style
Types begin with capitals 'New_type'
variables/ instances use lower case with underscore as a seperator
*/