summaryrefslogtreecommitdiff
path: root/rotord/src/graph.cpp
blob: 95444466a6f1861ef0ed374f6a961a9d3dfd82c3 (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
#include "graph.h"

using namespace Rotor;
using Poco::Logger;

const string Graph::graphToString(){
	string xmlgraph;
	if (loaded) {
		xml.copyXmlToString(xmlgraph);
		return xmlgraph;
	}
	else return "";
}
vector<Node*> Graph::find_nodes(const string &type){
	vector<Node*> found;
	for (std::unordered_map<string,Node*>::iterator it=nodes.begin();it!=nodes.end();++it) {
		if (it->second->type==type) found.push_back(it->second);
	}
	return found;
};
Node* Graph::find_node(const string &type){
	for (std::unordered_map<string,Node*>::iterator it=nodes.begin();it!=nodes.end();++it) {
		if (it->second->type==type) return it->second;
	}
	return nullptr; //can be tested against
};
bool Graph::signal_render(xmlIO &XML,const float framerate) {
	if (find_node("signal_output")) {
		Signal_output *signal_output=dynamic_cast<Signal_output*>(find_node("signal_output"));
		//return signal_output->render(duration,framerate,signal_xml);
		float sig=0.0f;
		string val="";
		for (float i=0;i<duration;i+=1.0f/framerate){
			float s=(signal_output->get_output(Time_spec(i,framerate,duration))+1.0f)/10.0f;
			if (!fequal(sig,s)){
				val+=toString(i)+":"+toString(s)+" ";
				sig=s;
			}
		}
		XML.addValue("signal",val);
		return true;
	}
	cerr<<"Rotor: signal output node not found"<<endl;

	return false;
}
bool Graph::print_features(xmlIO &XML,string &node){
	if (nodes.find(node)!=nodes.end()){
		if (dynamic_cast<Audio_processor*>(nodes[node])){
			XML.addValue("features",dynamic_cast<Audio_processor*>(nodes[node])->get_features());
			return true;
		}
		XML.addValue("error","node /"+node+"/ is not an Audio processor");
		return false;
	}
	XML.addValue("error","could not find node /"+node+"/");
	return false;
}
bool Graph::preview(xmlIO &XML,string &node,string &_format,int frame,int w,int h){
	if (nodes.find(node)!=nodes.end()){
		float t=frame/framerate;
		XML.addTag("preview");
		XML.addAttribute("preview","frame",toString(frame),0);
		XML.addAttribute("preview","nodeID",node,0);
		XML.pushTag("preview");
		if (dynamic_cast<Signal_node*>(nodes[node])){
			Time_spec ts=Time_spec(t,framerate,0.0f);
			XML.addValue("signal",dynamic_cast<Signal_node*>(nodes[node])->get_output(ts));
		}
		if (dynamic_cast<Image_node*>(nodes[node])){
			Frame_spec fs=Frame_spec(t,framerate,0.0f,w,h);
			Image *img=dynamic_cast<Image_node*>(nodes[node])->get_image_output(fs);
			vector<unsigned char> buf;
			string format=(_format==""?".png":_format);
			if (cv::imencode(format,img->rgb,buf)){ //, const vector<int>& params=vector<int>())
				stringstream output;
				Poco::Base64Encoder *enc=new Poco::Base64Encoder(output);
				enc->write((char*)buf.data(),buf.size());
				enc->close();
				delete enc;
				XML.addValue("image",output.str());
				XML.addAttribute("image","format",format,0);
			}
		}
		XML.popTag();
		return true;
	}
	return false;
}

bool Graph::video_render(const string &output_filename,const float framerate,int start, int stop) {
	if (output_filename.size()==0) return false;
	//https://www.adobe.com/devnet/video/articles/mp4_movie_atom.html
	//https://www.google.ie/search?q=ffmbc&aq=f&oq=ffmbc&aqs=chrome.0.57j0l2j60j0j60.4360j0&sourceid=chrome&ie=UTF-8#q=ffmbc+git

	//vector<Node*> loaders=find_nodes("video_loader");
	//for (auto i:loaders){
	//	if (!dynamic_cast<Video_loader*>(i)->isLoaded) {
	//		cerr<<"Rotor: all loaders must be populated before rendering"<<endl;
	//		return false;
	//	}
	//}
	if (find_node("video_output")) {
		Video_output *video_output=dynamic_cast<Video_output*>(find_node("video_output"));

		if (audio_filename!=""){ //BETTER WAY TO KNOW IF WE ARE USING AUDIO?
			video_output->create_envelope(audio_thumb->audiodata);
		}

		for (auto f: find_nodes("video_feedback")){
			(dynamic_cast<Video_feedback*>(f))->set_feedback(&(video_output->image));
		}
		//
		//setup defaults

		std::string container;
		Poco::StringTokenizer t(output_filename,".");
        if (t.count()>1){
            container="."+t[t.count()-1];
        }
        else container=".mp4";

	    //at the moment it crashes if you render before audio is loaded and also on 2nd render
	    libav::exporter exporter;

	    Image* i;

		if (exporter.setup(outW,outH,bitRate,framerate,container,use_fragmentation)) { //codecId,
			if (exporter.record(output_filename)) {

				libav::audio_decoder audioloader;

				bool usingaudio=audioloader.open(audio_filename);
				
				Logger& logger = Logger::get("Rotor");
				logger.information("Video_output rendering "+output_filename+": "+toString(duration)+" seconds at "+toString(framerate)+" fps, audio frame size: "+toString(exporter.get_audio_framesize()));
				//25fps video and 43.06640625fps audio? hmm
				//how to get the timecodes correct for the interleaved files

				struct timeval _start, _end;

			    gettimeofday(&_start, NULL);

			    uint16_t *audioframe=nullptr;
			    uint16_t *audio=nullptr;
			    int samples_in_frame;

			    if (usingaudio){
				    samples_in_frame=(audioloader.get_sample_rate())/framerate;
				    string whether=usingaudio?"Loading":"Cannot load";
				    logger.information(whether+" audio file: "+audio_filename+", each frame contains "+toString(samples_in_frame)+" samples at "+toString(audioloader.get_sample_rate())+" hz");
				    audioframe=new uint16_t[(samples_in_frame+exporter.get_audio_framesize())*audioloader.get_number_channels()];
				    audio=new uint16_t[samples_in_frame*audioloader.get_number_channels()];
				}

				float vstep=1.0f/framerate;
				float vf=start*vstep;
				float af=start*vstep;
				int aoffs=0;
				int audioend=0;
				Audio_frame *a;
				int64_t sample_start=(start*audioloader.get_sample_rate())/framerate;
				while (vf<min(duration,stop*vstep)&&!cancelled){ //-vstep) {

					if (usingaudio) {
						if (audioloader.get_samples(audio,sample_start,samples_in_frame)) {
							if (aoffs>0){
								//shift down samples
								int s=0;
								while ((s+aoffs)<audioend) {
									for (int j=0;j<audioloader.get_number_channels();j++){
										audioframe[s*audioloader.get_number_channels()+j]=audioframe[(s+aoffs)*audioloader.get_number_channels()+j];
									}
									s++;
								}
								aoffs=s;
							}
							for (int i=0;i<samples_in_frame;i++){
								for (int j=0;j<audioloader.get_number_channels();j++){
									audioframe[(aoffs+i)*audioloader.get_number_channels()+j]=audio[i*audioloader.get_number_channels()+j];
								}
							}
							audioend=aoffs+samples_in_frame;
							aoffs=0;
						    while (aoffs+exporter.get_audio_framesize()<audioend) {
			                    //insert audio frames until we are only 1 audio frame behind the next video frame
			                    //send audio_framesize() of them through until buffer is used
			                    //pass full buffer within frame_spec for av nodes
			                    exporter.encodeFrame(audioframe+(aoffs*audioloader.get_number_channels()));
			                    af+=exporter.get_audio_step();
			                    aoffs+=exporter.get_audio_framesize();
			                }
			                a=new Audio_frame(audio,audioloader.get_number_channels(),samples_in_frame);
			                sample_start+=samples_in_frame;
						}
						else usingaudio=false;

		            }


	                //[mp3 @ 0x7fffe40330e0] max_analyze_duration 5000000 reached at 5015510 microseconds
	                //[mp3 @ 0x7fffe4033ec0] Insufficient thread locking around avcodec_open/close()
	                //[mp3 @ 0x7fffe40330e0] Estimating duration from bitrate, this may be inaccurate
	                //[libx264 @ 0x7fffe8003940] using cpu capabilities: MMX2 SSE2Fast SSSE3 FastShuffle SSE4.2
	                //[libx264 @ 0x7fffe8003940] profile High, level 3.0
	                //[libx264 @ 0x7fffe8003940] 264 - core 123 r2189 35cf912 - H.264/MPEG-4 AVC codec - Copyleft 2003-2012 - http://www.videolan.org/x264.html - options: cabac=1 ref=3 deblock=1:0:0 analyse=0x3:0x113 me=hex subme=7 psy=1 psy_rd=1.00:0.00 mixed_ref=1 me_range=16 chroma_me=1 trellis=1 8x8dct=1 cqm=0 deadzone=21,11 fast_pskip=1 chroma_qp_offset=-2 threads=12 sliced_threads=0 nr=0 decimate=1 interlaced=0 bluray_compat=0 constrained_intra=0 bframes=3 b_pyramid=2 b_adapt=1 b_bias=0 direct=1 weightb=1 open_gop=0 weightp=2 keyint=10 keyint_min=1 scenecut=40 intra_refresh=0 rc_lookahead=10 rc=abr mbtree=1 bitrate=400 ratetol=1.0 qcomp=0.60 qpmin=0 qpmax=69 qpstep=4 ip_ratio=1.40 aq=1:1.00
	                //Assertion ff_avcodec_locked failed at libavcodec/utils.c:2967

		            //cerr<<"videoloader: "<<vf<<" seconds, vstep "<<vstep<<" ,asking for frame "<<((int)((vf*framerate)+0.5))<<endl


		            if (usingaudio) {
				    	i=video_output->get_image_output(Frame_spec(vf,framerate,duration,outW,outH,a));
				    }
				    else i=video_output->get_image_output(Frame_spec(vf,framerate,duration,outW,outH));
				    if (i) {
				    	exporter.encodeFrame(i->RGBdata);
				    }
				    vf+=vstep;
				    //mutex.lock();
				    progress=vf/duration;
				    //mutex.unlock();
				    if (usingaudio) {delete a;};
				}


				//exporter.encodeFrame(i->RGBdata,true); //final keyframe;

				exporter.finishRecord();

				gettimeofday(&_end, NULL);

			    float mtime = ((_end.tv_sec-_start.tv_sec) + (_end.tv_usec-_start.tv_usec)/1000000.0) + 0.5;

				logger.information("Video_output: rendered "+output_filename+": in "+toString(mtime)+" seconds");

				if (usingaudio) {
					audioloader.cleanup();
					delete[] audioframe;
					delete[] audio;
				}



				return true;
			}
		}

		return false;
		}

	cerr<<"Rotor: video output node not found"<<endl;
	return false;
}

bool Graph::set_resolution(int w,int h){
	if (w>64&&h>48){
		outW=w;
		outH=h;
		return true;
	}
	else return false;
}
bool Graph::load(string data,string media_path){
	if (xml.loadFromBuffer(data)){
		return parseXml(media_path);
	}
	return parseJson(data,media_path);
	return false;
}
bool Graph::loadFile(string &filename,string media_path){
	//if (loaded)
	printf("loading graph: %s\n",(filename).c_str());
	if (xml.loadFile(filename)){
		return parseXml(media_path);
	}
	Poco::FileInputStream fis(filename);
	Poco::CountingInputStream countingIstr(fis);
	std::string str;
	Poco::StreamCopier::copyToString(countingIstr, str);
	return parseJson(str,media_path);
}
bool Graph::check_audio(string audio,string path){
	if (audio!="")	{
		Poco::File f=Poco::File(path+audio);
		if (f.exists()) {
			audio_filename=path+audio;
			audio_loaded=true;
			cerr<<"Rotor: loading "<<path+audio<<" from graph"<<endl;
			return true;
		}
		cerr<<"Rotor: audio file "<<path+audio<<" not found"<<endl;
	}
	return false;
}
bool Graph::parseJson(string &data,string &media_path){
	//cerr<<data<<endl;
	//cerr<<"Trying to load JSON"<<endl;
	Json::Value root;   // will contains the root value after parsing.
	Json::Reader reader;
	bool parsingSuccessful = reader.parse( data, root );
	if ( !parsingSuccessful )
	{
	    // report to the user the failure and their locations in the document.
	    std::cout  << "Failed to parse configuration\n"
	               << reader.getFormattedErrorMessages();
	    return false;
	}
	//we know the json validates so clear the existing graph
	clear();
	Node_factory factory;
	analysis_seed=root["seed"].asInt();
	check_audio(root["audio"].asString(),media_path);
	init(root["ID"].asString(),root["description"].asString());
	Json::Value jnodes = root["nodeDefinitions"];
	for ( uint32_t i = 0; i < jnodes.size(); ++i ) {
	   string nodeID=jnodes[i]["id"].asString();
	   //cerr<<"json found node: "<<jnodes[i]["type"].asString()<<endl;
	   map<string,string> settings;
	   vector<string> attrs;
	   settings["type"]=jnodes[i]["type"].asString();
		//attributes
		settings["media_path"]=media_path;
	  	Node* node=factory.create(settings);
	  	for (uint32_t m=0;m<jnodes[i]["attributes"].size();m++){
			string attribute=jnodes[i]["attributes"][m]["name"].asString();
			string val="";
			if (node->attributes.find(attribute)!=node->attributes.end()){
				Attribute *attr=node->attributes.find(attribute)->second;
				if (attr->type=="enum"){
				    val=jnodes[i]["attributes"][m]["value"].asString();
				    attr->init(val);		
				}
				if (attr->type=="string") {
				    val=jnodes[i]["attributes"][m]["value"].asString();
				    attr->value=val;
				}
				if (attr->type=="array"){
					std::vector<std::string> vals;

					for (uint32_t i5 = 0; i5 < jnodes[i]["attributes"][m]["value"].size(); i5++ )
					{
						vals.push_back(jnodes[i]["attributes"][m]["value"][i5].asString());
					}
					attr->init(vals);
				}
				//cerr << "Rotor: setting attribute '"<<attribute<<"' of "<<nodeID<<" type "<<attr->type<<" to "<<val<<endl;
				cerr << "Rotor: setting attribute '"<<attribute<<"' of "<<nodeID<<" type "<<attr->type<<" to "<<val<<endl;
					
			}
			//settings[attribute]=val;
		}
	   	if (node) {
			if (nodes.find(nodeID)==nodes.end()){
				cerr << "Rotor: creating node '"<<nodeID<<"': '"<< settings["type"] << "'" << endl;
				nodes[nodeID]=node;
				//signal inputs
			   	for (uint32_t j=0;j< jnodes[i]["signal_inputs"].size();j++){
			   		if ((nodes[nodeID])->inputs.size()>j) {
						string fromID=jnodes[i]["signal_inputs"][j]["from"].asString();
						if (fromID!=""){
							if (nodes.find(fromID)!=nodes.end()) {
								if (!nodes[nodeID]->inputs[j]->connect((Signal_node*)nodes[fromID])){
									cerr << "Rotor: graph loader cannot connect input " << j << " of node '" << nodeID << "' to node '" << fromID << "'" << endl;
									return false;
								}
								else cerr << "Rotor: linked input " << j << " of node '" << nodeID << "' to node '" << fromID << "'" << endl;
							}
							else cerr << "Rotor: linking input " << j << " of node: '" << nodeID << "', cannot find target '" << fromID << "'" << endl;
						}
					}
					else cerr << "Rotor: input " << j << " of node: '" << nodeID << "' does not exist" << endl;
				}


				//image inputs
				if (dynamic_cast<Image_node*>(nodes[nodeID])!=nullptr) {


					//handle expandable inputs
					if ((((Image_node*)nodes[nodeID])->image_inputs.size()<=jnodes[i]["image_inputs"].size())&&((Image_node*)nodes[nodeID])->duplicate_inputs){
						string desc=((Image_node*)nodes[nodeID])->image_inputs[0]->description;
						string title=((Image_node*)nodes[nodeID])->image_inputs[0]->title;
						while(((Image_node*)nodes[nodeID])->image_inputs.size()<=jnodes[i]["image_inputs"].size()){
							((Image_node*)nodes[nodeID])->create_image_input(desc,title);
							cerr<<"creating an image input"<<endl;
						}
					}


					for (uint32_t k=0;k<jnodes[i]["image_inputs"].size();k++){
						if (((Image_node*)nodes[nodeID])->image_inputs.size()<=k) {
							if (nodes[nodeID]->duplicate_inputs) {
								while(((Image_node*)nodes[nodeID])->image_inputs.size()<=k){
									((Image_node*)nodes[nodeID])->create_image_input(settings["description"],settings["title"]);
								}
							}
						}
						if (((Image_node*)nodes[nodeID])->image_inputs.size()>k) {
							string fromID=jnodes[i]["image_inputs"][k]["from"].asString();
							if (fromID!=""){
								if (nodes.find(fromID)!=nodes.end()) {
									if (dynamic_cast<Image_node*>(nodes[fromID])!=nullptr) {
										if (!dynamic_cast<Image_node*>(nodes[nodeID])->image_inputs[k]->connect((Image_node*)nodes[fromID])){
											cerr << "Rotor: graph loader cannot connect image input " << k << " of node '" << nodeID << "' to node '" << fromID << "'" << endl;
											return false;
										}
										else cerr << "Rotor: linked image input " << k << " of node '" << nodeID << "' to node '" << fromID << "'" << endl;
									}
									else cerr << "Rotor: cannot link image input "<< k << " of node '" << nodeID << "' to node '" << fromID << "' : not an image node" << endl;
								}
								else cerr << "Rotor: linking image input " << k << " of node: '" << nodeID << "', cannot find target '" << fromID << "'" << endl;
							}
						}
						else cerr << "Rotor: image input number " << k << " of node: '" << nodeID << "' does not exist" << endl;
					}
				}
				//parameters
				for (uint32_t l=0;l<jnodes[i]["parameters"].size();l++){

					string parameter=jnodes[i]["parameters"][l]["name"].asString();

					if (nodes[nodeID]->parameters.find(parameter)!=nodes[nodeID]->parameters.end()) {
						float val=jnodes[i]["parameters"][l]["value"].asFloat();
						if (val!=nodes[nodeID]->parameters.find(parameter)->second->value){
							nodes[nodeID]->parameters.find(parameter)->second->value=val;
							cerr << "Rotor: set parameter '"<<parameter<<"' of "<<nodeID<<" to "<<val<<endl;
						}
						string fromID=jnodes[i]["parameters"][l]["from"].asString();
						if (nodes.find(fromID)!=nodes.end()) {
							if (!nodes[nodeID]->parameters[parameter]->connect(nodes[fromID])){
								cerr << "Rotor: graph loader cannot connect parameter " << parameter << " of node '" << nodeID << "' to node '" << fromID << "'" << endl;
								return false;
							}
							else cerr << "Rotor: linked parameter " << parameter << " of node '" << nodeID << "' to node '" << fromID << "'" << endl;
						}
						else if (fromID!="") cerr << "Rotor: linking parameter " << parameter << " of node: '" << nodeID << "', cannot find target '" << fromID << "'" << endl;
					}
					else cerr << "Rotor: cannot find parameter '" << parameter << "' of "<<settings["type"]<<" "<< nodeID << endl;

				}
			}
			else cerr << "Rotor: duplicate node '"<<nodeID<<"' "<< endl;
   		}
	   	else {
			cerr << "Rotor: graph loader cannot find node '" << settings["type"] << "'" << endl;
			return false;
		}

	}
	loaded=true;
	return true;
}
bool Graph::parseXml(string media_path){
	clear();
	Node_factory factory;
	check_audio(xml.getAttribute("patchbay","audio","",0),media_path);
	analysis_seed=xml.getAttribute("patchbay","seed",0,0);
	init(xml.getAttribute("patchbay","ID","",0),xml.getValue("patchbay","",0));
		if(xml.pushTag("patchbay")) {
			int n1=xml.getNumTags("node");
			for (int i1=0;i1<n1;i1++){
				map<string,string> settings;
				vector<string> attrs;
				xml.getAttributeNames("node",attrs,i1);
				for (auto& attr: attrs) {
					settings[attr]=xml.getAttribute("node",attr,"",i1);
					//cerr << "Got attribute: " << attr << ":" << xml.getAttribute("node",attr,"",i1) << endl;
				}
				settings["media_path"]=media_path;
				Node* node=factory.create(settings);
				if (node) {
					string nodeID=xml.getAttribute("node","ID","",i1);
					if (nodes.find(nodeID)==nodes.end()){
						string nodetype=xml.getAttribute("node","type","",i1);
						cerr << "Rotor: creating node '"<<nodeID<<"': '"<< xml.getAttribute("node","type","",i1) << "'" << endl;
						nodes[nodeID]=node;
						if(xml.pushTag("node",i1)) {
							uint32_t n2=xml.getNumTags("signal_input");
							for (uint32_t i2=0;i2<n2;i2++){
								//TODO expand
								//nodes[nodeID]->create_signal_input(xml.getAttribute("signal_input","description","",i2),xml.getAttribute("signal_input","title","",i2));
								if ((nodes[nodeID])->inputs.size()>i2) {
									string fromID=xml.getAttribute("signal_input","from","",i2);
									if (nodes.find(fromID)!=nodes.end()) {
										if (!nodes[nodeID]->inputs[i2]->connect((Signal_node*)nodes[fromID])){
											cerr << "Rotor: graph loader cannot connect input " << i2 << " of node '" << nodeID << "' to node '" << fromID << "'" << endl;
											return false;
										}
										else cerr << "Rotor: linked input " << i2 << " of node '" << nodeID << "' to node '" << fromID << "'" << endl;
									}
									else cerr << "Rotor: linking input " << i2 << " of node: '" << nodeID << "', cannot find target '" << fromID << "'" << endl;
								}
								else cerr << "Rotor: input " << i2 << " of node: '" << nodeID << "' does not exist" << endl;

							}
							uint32_t n3=xml.getNumTags("image_input");
							for (uint32_t i3=0;i3<n3;i3++){
								//handle expandable inputs
								if ((((Image_node*)nodes[nodeID])->image_inputs.size()<=i3)&&((Image_node*)nodes[nodeID])->duplicate_inputs){
									string desc=((Image_node*)nodes[nodeID])->image_inputs[0]->description;
									string title=((Image_node*)nodes[nodeID])->image_inputs[0]->title;
									while(((Image_node*)nodes[nodeID])->image_inputs.size()<=i3){
										((Image_node*)nodes[nodeID])->create_image_input(desc,title);
									}
								}
								//((Image_node*)nodes[nodeID])->create_image_input(xml.getValue("image_input","",i3));
								if (((Image_node*)nodes[nodeID])->image_inputs.size()>i3) {
									string fromID=xml.getAttribute("image_input","from","",i3);
									if (nodes.find(fromID)!=nodes.end()) {
										if (!(((Image_node*)nodes[nodeID])->image_inputs[i3]->connect((Image_node*)nodes[fromID]))){
											cerr << "Rotor: graph loader cannot connect image input " << i3 << " of node '" << nodeID << "' to node '" << fromID << "'" << endl;
											return false;
										}
										else cerr << "Rotor: linked image input " << i3 << " of node '" << nodeID << "' to node '" << fromID << "'" << endl;
									}
									else cerr << "Rotor: linking image input " << i3 << " of node: '" << nodeID << "', cannot find target '" << fromID << "'" << endl;
								}
								else cerr << "Rotor: image input number " << i3 << " of node: '" << nodeID << "' does not exist" << endl;
							}
							int n4=xml.getNumTags("parameter");
							for (int i4=0;i4<n4;i4++){
								string parameter=xml.getAttribute("parameter","name","",i4);
								if (nodes[nodeID]->parameters.find(parameter)!=nodes[nodeID]->parameters.end()) {
									string val=xml.getAttribute("parameter","value","",i4);
									if (val!="") nodes[nodeID]->parameters.find(parameter)->second->value=toFloat(val);
									string fromID=xml.getAttribute("parameter","from","",i4);
									if (nodes.find(fromID)!=nodes.end()) {
										if (!nodes[nodeID]->parameters[parameter]->connect(nodes[fromID])){
											cerr << "Rotor: graph loader cannot connect parameter input " << i4 << " of node '" << nodeID << "' to node '" << fromID << "'" << endl;
											return false;
										}
										else cerr << "Rotor: linked parameter input " << i4 << " of node '" << nodeID << "' to node '" << fromID << "'" << endl;
									}
									else if (fromID!="") cerr << "Rotor: linking parameter input " << i4 << " of node: '" << nodeID << "', cannot find target '" << fromID << "'" << endl;
								}
								else cerr << "Rotor: cannot find parameter input '" << parameter << "' of "<<nodetype<<" "<< nodeID << endl;
							}
							//still support old parameter_input
							n4=xml.getNumTags("parameter_input");
							for (int i4=0;i4<n4;i4++){
								string parameter=xml.getAttribute("parameter_input","name","",i4);
								if (nodes[nodeID]->parameters.find(parameter)!=nodes[nodeID]->parameters.end()) {
									string val=xml.getAttribute("parameter_input","value","",i4);
									if (val!="") nodes[nodeID]->parameters.find(parameter)->second->value=toFloat(val);
									string fromID=xml.getAttribute("parameter_input","from","",i4);
									if (nodes.find(fromID)!=nodes.end()) {
										if (!nodes[nodeID]->parameters[parameter]->connect(nodes[fromID])){
											cerr << "Rotor: graph loader cannot connect parameter input " << i4 << " of node '" << nodeID << "' to node '" << fromID << "'" << endl;
											return false;
										}
										else cerr << "Rotor: linked parameter input " << i4 << " of node '" << nodeID << "' to node '" << fromID << "'" << endl;
									}
									else cerr << "Rotor: linking parameter input " << i4 << " of node: '" << nodeID << "', cannot find target '" << fromID << "'" << endl;
								}
								else cerr << "Rotor: cannot find parameter input '" << parameter << "' of "<<nodetype<<" "<< nodeID << endl;
							}
							//extra key/value pairs that can be specific to sub-settings - WHAT WAS THIS
							//int n5=xml.getNumTags("parameter");
							//for (int i5=0;i5<n5;i5++){
							//	nodes[nodeID]->set_parameter(xml.getAttribute("parameter","name","",i5),xml.getAttribute("parameter","value","",i5));
							//}
							//if (n5>0) cerr << "Rotor: found " << n5 << " extra parameters for node '" << nodeID << "'" << endl;
							//support attributes in tags
							n4=xml.getNumTags("attribute");
							for (int i4=0;i4<n4;i4++){
								string attribute=xml.getAttribute("attribute","name","",i4);
								if (nodes[nodeID]->attributes.find(attribute)!=nodes[nodeID]->attributes.end()) {
									string val=xml.getAttribute("attribute","value","",i4);
									if (val!="") nodes[nodeID]->attributes.find(attribute)->second->value=val;
									string type=xml.getAttribute("attribute","type","",i4);
									if (nodes[nodeID]->attributes.find(attribute)->second->type=="array"){
										if(xml.pushTag("attribute",i4)) {
											int n5=xml.getNumTags("value");
											std::vector<std::string> vals;
											for (int i5=0;i5<n5;i5++){
												vals.push_back(xml.getValue("value","",i5));
											}
											nodes[nodeID]->attributes.find(attribute)->second->init(vals);
											xml.popTag();
										}
									}
								}
								else cerr << "Rotor: cannot find attribute '" << attribute << "' of "<<nodetype<<" "<< nodeID << endl;
							}

							xml.popTag();
						}
					}
					else cerr << "Rotor: duplicate node '"<<nodeID<<"' "<< endl;
				}
				else {
					cerr << "Rotor: graph loader cannot find node '" << xml.getAttribute("node","type","",i1) << "'" << endl;
					return false;
				}
			}
			xml.popTag();
		}
		loaded=true;
		return true;
}
bool Graph::load_audio(const string &filename,vector<Audio_processor*> processors){
	if (filename.size()==0) return false;
	Logger& logger = Logger::get("Rotor");
	logger.information("Analysing "+filename+" seed:"+toString(analysis_seed));


	//audio_loaded=false;
	libav::audio_decoder loader;
	if (!loader.open(filename)) {
		logger.error("ERROR: Could not open audio: "+filename);
		return false;
	}

	duration=loader.get_duration();

    int rate = loader.get_sample_rate();
	int samples = loader.get_number_samples();
	int channels= loader.get_number_channels();
	int bits = loader.get_bit_depth();

	for (auto p: processors) {
		if(!p->init(channels,bits,samples,rate) ){
			logger.error("ERROR: Audio plugin failed to initialse");
			return false;
		}
	}

	bool finished=false;
	uint16_t *audio=new uint16_t[1024*loader.get_number_channels()];
	uint64_t sample=0;

	srand(analysis_seed);

	while (!finished&&!cancelled)
	{
		if (loader.get_samples(audio,sample,1024)) {
			//now we can pass the data to the processor(s)
			for (auto p: processors) {
				p->process_frame((uint8_t*)audio,1024);
			}
			sample+=1024;
			//mutex.lock();
			progress=((float)sample)/samples; //atomic on 64 bit?
			//mutex.unlock();

		}
		else finished=true;
	}

	loader.cleanup();

	for (auto p: processors) {
		p->cleanup();
		p->print_summary();
	}

	logger.information("Finished audio analysis");
	//audio_loaded=true;
	return true;
}
bool Graph::load_video(const string &nodeID,const string &filename){
	//this is a good standard example of how to find
	//a node of a specific type by ID and do something
	if (nodes.find(nodeID)!=nodes.end()){
		if (nodes[nodeID]->type=="video_loader") {
			if (((Video_loader*)nodes[nodeID])->load(filename)) {
				return true;
			}
		}
	}
	return false;
}