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
|
/*
requirement driven design
do we store graphs as files or in a db with UUID as key?
we traverse the graph as recursive function calls until we satisfy all dependencies
NO NODE HAS MORE THAN ONE OUTPUT
WE DON'T LINK TO AN OUTPUT OBJECT WE LINK TO THE NODE - GET_OUTPUT IS THE RENDER FUNCTION
MORE THAN ONE NODE CAN LINK THE SAME OUTPUT
NODES CACHE AT LEAST ONE FRAME
??the only node with more than 1 output is audio?
??lets rethink this
??audio analysis nodes can be seperate - they can all load from the same audio file - were gonna have to process each pass
??output splitter? channel splitter? these can be done as 1 object per channel?
??I think so
settings - how do we deal with settings being controllable
signal inputs can have a gui representation as well
other gui items don't have an input
scaling to come
time is always in floating points seconds - time has to be requested when rendering - either a preview
what about testing a float for equality?
maybe we should look at time in int (frames) - - what does this imply
is it easier to have a function like:
bool Same_frame(float time1, float time2);
nb where a signal enters a channel comp input - it is duplicated - so nodes should cache a value (more for time effects)
sql stuff
NB best way to use is: interface uploads audio and makes thumbnail: graph determines what kind of audio analysis is used by referring to plugins
jesus where is it hanging in the frigging debugger
GOOD GOOD GOOD
next - build signal_output and make a working chain with dummy data
main definitions of libavcodec.h are in utils.c
*/
#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 <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/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 "avCodec.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;
//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 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): description(_desc){};
Node* connection;
string description;
};
class Image_input: public Input{
public:
};
class Signal_input: public Input{
public:
bool connect(Signal_node* source);
Signal_input(const string &_desc): Input(_desc){};
};
class Node{
public:
virtual Node* clone(map<string,string> &_settings)=0;
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
void create_signal_input(const string &description) {inputs.push_back(new Signal_input(description));};
string description;
string type;
string output_type;
string ID;
string check(map<string,string> &settings,string key){ if (settings.find(key)!=settings.end()) return settings[key]; else return "";};
void base_settings(map<string,string> &settings) {
description=check(settings,"description");
type=check(settings,"type");
output_type=check(settings,"output");
ID=check(settings,"ID");
}
};
class Image{
char* data;
};
class Signal_node: public Node{
public:
virtual float get_output(const float &time) { return 0.0f; };
/*{ //default is to pass through first input, if disconnected returns 0
cerr << "getting output for " << type << "," << ID << endl;
if (inputs.size()) {
if (inputs[0]->connection) return ((Signal_node*)(inputs[0]->connection))->get_output(time);
}
return 0.0f;
}
*/
};
class Image_node: public Node{
public:
vector<Image_input> image_inputs; //image node also has image inputs and outputs
Image* get_output(float time){ //sample implementation
//do something with the inputs
//and then
return ((Image_node*)image_inputs[0].connection)->get_output(time);
}
void get_preview(float time);
Image* image; //this can be privately allocated or just passed on as the node see fit
private:
float image_time;
};
//actual nodes-------------------------------------------------
class Audio_analysis: public Signal_node {
public:
Audio_analysis(){};
Audio_analysis(map<string,string> &settings) {
base_settings(settings);
};
Audio_analysis* clone(map<string,string> &_settings) { return new Audio_analysis(_settings);};
float get_output(const float &time) {
float t=time;
return t;
}
};
class Signal_divide: public Signal_node {
//divides incoming signal by a fixed amount
public:
Signal_divide(){};
Signal_divide(map<string,string> &settings) {
base_settings(settings);
divide_amount=ofToFloat(check(settings,"amount"));
};
Signal_divide* clone(map<string,string> &_settings) { return new Signal_divide(_settings);};
float get_output(const float &time) {
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 {
//does this require knowing what the framerate is?
//for now, assume 25
//what to cache? for now, don't cache
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);};
float get_output(const float &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-.04));
if (((int)s1)>((int)s2)) {
return 1.0f;
}
}
return 0.0f;
}
};
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);
float get_output(const float &time) {
if (inputs[0]->connection) {
return ((Signal_node*)(inputs[0]->connection))->get_output(time);
}
else return 0.0f;
}
};
//-------------------------------------------------------------------
class Node_factory{
public:
Node_factory();
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(){framerate=25.0f;duration=10.0f;};
Graph(const string& _uid,const string& _desc){ uid=_uid;description=_desc;framerate=25.0f;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;
Node* find(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 NULL;
};
bool signal_render(const float _fr,string &signal_xml) {
if (_fr>.001) framerate=_fr;
if (find("signal_output")) {
Signal_output *signal_output=dynamic_cast<Signal_output*>(find("signal_output"));
return signal_output->render(duration,framerate,signal_xml);
}
else return false;
}
private:
Node_factory factory;
float framerate;
float duration;
};
class base_audio_processor {
public:
virtual int process_frame(uint8_t *data,int samples)=0;
void init(int _channels,int _bits,int _samples) {
channels=_channels;
bits=_bits;
samples=_samples;
};
int channels,bits,samples;
};
class audio_thumbnailer: public base_audio_processor {
//how to deal with the fact that frames don't correspond with pixels?
//buffer the data somehow
//draw pixels based on rms value
public:
audio_thumbnailer(){
height=64;
width=128; //fit
data=new uint8_t[height*width];
memset(data,0,height*width);
}
~audio_thumbnailer(){
delete[] data;
}
void init(int _channels,int _bits,int _samples) {
base_audio_processor::init(_channels,_bits,_samples);
samples_per_column=samples/width;
column=0; //point thumbnail bitmap
out_sample=0; //sample in whole track
offset=(int)(pow(2.0,bits)/2.0);
scale=1.0/offset;
}
int process_frame(uint8_t *data,int samples_in_frame){
//begin by processing remaining samples
//samples per column could be larger than a frame! (probably is)
//but all we are doing is averaging
int bytes=(bits>>3);
int stride=channels*bytes;
int in_sample=0;
while (in_sample<samples_in_frame&&column<width) {
int sample=0;
int samples=0;
double accum=0;
while (sample<samples_per_column&&in_sample<samples_in_frame) {
//accumulate squares for this column
for (int i=0;i<channels;i++) {
int this_val=0;
for (int j=0;j<bytes;j++) {
this_val+=data[(sample*stride)+(i*bytes)+j]<<j;
}
//convert from integer data format - i.e s16p - to audio signal in -1..1 range
//don't know how many bytes we are dealing with necessarily?
double val=(this_val-offset)*scale;
accum+=val*val;
samples++;
}
in_sample++;
sample++;
out_sample++;
}
//get root-mean
double mean=pow(accum/samples,0.5);
int colheight=height*mean*0.5;
int hh=height>>1;
for (int i=0;i<height;i++) {
data[i*width+column]=abs(i-hh)<colheight?0xff:0x00;
}
column++;
}
return out_sample;
}
string print(){
string output;
for (int j=0;j<height;j++) {
for (int i=0;i<width;i++) {
output+=data[j*width+i]<0x7f?"0":"1";
}
output +="\n";
}
return output;
}
uint8_t *data;
int height,width,samples_per_column;
int column,out_sample;
//for drawing graph
int offset;
double scale;
};
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;
};
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?
int load_graph(Poco::UUID uid);
bool load_graph(string &graph_filename); //should eventually be as above
UUID save_graph(); //returns UUID of saved graph
bool load_audio(const string &filename,vector<base_audio_processor*> processors);
Render_requirements get_requirements();
int load_video(int num,string &filename); //can be performance or clip
private:
int state;
double 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;
audio_thumbnailer *audio_thumb;
vampHost::QMAnalyser audio_analyser;
xmlIO xml;
Graph graph;
Node_factory factory;
};
}
/*
coding style
Types begin with capitals 'CamelCase'
variables/ instances use lower case with underscore as a seperator
*/
|