#include "rotor.h" //float equality bool fequal(const float u,const float v){ if (abs(u-v)<.001) return true; else return false; }; using namespace Rotor; void Render_context::runTask() { while (!isCancelled()) { int cmd=0; mutex.lock(); if (work_queue.size()){ cmd=work_queue[0]; work_queue.pop_front(); } mutex.unlock(); if (cmd==ANALYSE_AUDIO) { state=ANALYSING_AUDIO; vector proc; proc.push_back(audio_thumb); if (load_audio(audio_filename,proc)) { state=AUDIO_READY; } else state=IDLE; } sleep(100); } printf("Rotor: stopping thread\n"); } void Render_context::add_queue(int item) { mutex.lock(); work_queue.push_back(item); mutex.unlock(); } bool Signal_input::connect(Signal_node* source) { if (source->output_type=="signal") { connection=(Node*)source; return true; } else return false; }; bool Signal_output::render(const float duration, const float framerate,string &xml_out){ cerr << "Rotor: Signal_output rendering " << duration << " seconds at " << framerate << " frames per second" << endl; float step=1.0f/framerate; float v=0.0f; for (float f=0.0f;f"+ofToString(u)+"\n"); v=u; } } return true; } Command_response Render_context::session_command(const std::vector& command){ //method,id,command1,{command2,}{body} //here we allow the controlling server to communicate with running tasks Command_response response; response.status=HTTPResponse::HTTP_BAD_REQUEST; if (command[2]=="audio") { if (command[0]=="PUT") { //get audio file location and initiate analysis if (command.size()>2) { if (state==IDLE) { //check file exists Poco::File f=Poco::File(command[3]); if (f.exists()) { //pass to worker thread ??if engine is ready?? ??what if engine has finished but results aren't read?? audio_filename=command[3]; //for now, store session variables in memory add_queue(ANALYSE_AUDIO); response.description="Starting audio analysis: "+command[3]+"\n"; } else { response.status=HTTPResponse::HTTP_NOT_FOUND; response.description="File "+command[3]+" not found\n"; } } else { response.status=HTTPResponse::HTTP_BAD_REQUEST; response.description="Rotor: session busy\n"; } } } if (command[0]=="GET") { if (state==ANALYSING_AUDIO) { response.status=HTTPResponse::HTTP_OK; response.description="Rotor: analysing audio\n"; char c[20]; sprintf(c,"%02f",progress); response.description+=""+string(c)+"\n"; } if (state==AUDIO_READY) { //not sure about this-- should this state be retained? //can the data only be read once? //for now response.status=HTTPResponse::HTTP_OK; response.description="Rotor: audio ready\n"; response.description+=""; state=IDLE; } } if (command[0]=="DELETE") { //for now audio_filename=""; response.description="1\n"; response.status=HTTPResponse::HTTP_OK; } } if (command[2]=="graph") { if (command[0]=="GET") { if (xml.bDocLoaded) { response.status=HTTPResponse::HTTP_OK; xml.copyXmlToString(response.description); } else { response.description="Rotor: graph not loaded\n"; } } if (command[0]=="PUT") { //get new graph from file if (command.size()>2) { //should interrupt whatever is happening? //before begining to load from xml if (state==IDLE) { //eventually not like this Poco::File f=Poco::File(command[3]); if (f.exists()) { string graph_filename=command[3]; if (load_graph(graph_filename)) { response.status=HTTPResponse::HTTP_OK; //response.description="Rotor: loaded graph "+command[3]+"\n"; string xmlstring; xml.copyXmlToString(xmlstring); response.description=xmlstring; //the graph could actually contain an xml object and we could just print it here? //or could our nodes even be subclassed from xml nodes? } else { response.status=HTTPResponse::HTTP_INTERNAL_SERVER_ERROR; //~/sources/poco-1.4.6-all/Net/include/Poco/Net/HTTPResponse.h response.description="Rotor: could not load graph "+command[3]+"\n"; } } else { response.status=HTTPResponse::HTTP_NOT_FOUND; response.description="File "+command[3]+" not found\n"; } } } } if (command[0]=="DELETE") { //for now graph=Graph(); response.description="1\n"; response.status=HTTPResponse::HTTP_OK; } } if (command[2]=="signal") { if (command[0]=="GET") { //generate xml from 1st signal output if (state==IDLE) { //direct call for testing float framerate=0.0f; if (command.size()>2) { framerate=ofToFloat(command[3]); } string signal_xml; if (graph.signal_render(framerate,signal_xml)){ response.status=HTTPResponse::HTTP_OK; response.description=signal_xml; } else { response.status=HTTPResponse::HTTP_INTERNAL_SERVER_ERROR; response.description="Rotor: could not render output signal\n"; } } else { response.status=HTTPResponse::HTTP_NOT_FOUND; response.description="Signal output not found\n"; } } else { response.status=HTTPResponse::HTTP_SERVICE_UNAVAILABLE; response.description="Rotor: context busy\n"; } } if (command[2]=="video") { if (command[0]=="GET") { //DUMMY RESPONSE response.status=HTTPResponse::HTTP_OK; response.description="DUMMY RESPONSE Rotor: analysing video\n"; response.description+="45.2\n"; } if (command[0]=="PUT") { //get vide file location and initiate analysis if (command.size()>2) { if (state==IDLE) { //check file exists Poco::File f=Poco::File(command[3]); if (f.exists()) { //pass to worker thread ??if engine is ready?? ??what if engine has finished but results aren't read?? //DUMMY RESPONSE response.description="DUMMY RESPONSE Starting video analysis: "+command[3]+"\n"; } else { response.status=HTTPResponse::HTTP_NOT_FOUND; response.description="File "+command[3]+" not found\n"; } } else { response.status=HTTPResponse::HTTP_BAD_REQUEST; response.description="Rotor: session busy\n"; } } } if (command[0]=="DELETE") { //DUMMY RESPONSE response.description="DUMMY RESPONSE 1\n"; response.status=HTTPResponse::HTTP_OK; } } if (command[2]=="render") { if (command[0]=="GET") { //DUMMY RESPONSE response.status=HTTPResponse::HTTP_OK; response.description="DUMMY RESPONSE Rotor: rendering video\n"; response.description+="25.2\n"; } if (command[0]=="PUT") { //DUMMY RESPONSE //SHOULD CHECK REQUIREMENTS response.status=HTTPResponse::HTTP_OK; response.description="DUMMY RESPONSE Rotor: starting render\n"; } if (command[0]=="DELETE") { //DUMMY RESPONSE //SHOULD CHECK REQUIREMENTS response.status=HTTPResponse::HTTP_OK; response.description="DUMMY RESPONSE Rotor: cancelling render\n"; } } return response; } //http://blog.tomaka17.com/2012/03/libavcodeclibavformat-tutorial/ //great to use c++11 features bool Render_context::load_audio(const string &filename,vector processors){ av_register_all(); AVFrame* frame = avcodec_alloc_frame(); if (!frame) { std::cout << "Error allocating the frame" << std::endl; return false; } AVFormatContext* formatContext = NULL; if (avformat_open_input(&formatContext, filename.c_str(), NULL, NULL) != 0) { av_free(frame); std::cout << "Error opening the file" << std::endl; return false; } if (avformat_find_stream_info(formatContext, NULL) < 0) { av_free(frame); av_close_input_file(formatContext); std::cout << "Error finding the stream info" << std::endl; return false; } AVStream* audioStream = NULL; for (unsigned int i = 0; i < formatContext->nb_streams; ++i) { if (formatContext->streams[i]->codec->codec_type == AVMEDIA_TYPE_AUDIO) { audioStream = formatContext->streams[i]; break; } } if (audioStream == NULL) { av_free(frame); av_close_input_file(formatContext); std::cout << "Could not find any audio stream in the file" << std::endl; return false; } AVCodecContext* codecContext = audioStream->codec; codecContext->codec = avcodec_find_decoder(codecContext->codec_id); if (codecContext->codec == NULL) { av_free(frame); av_close_input_file(formatContext); std::cout << "Couldn't find a proper decoder" << std::endl; return false; } else if (avcodec_open2(codecContext, codecContext->codec, NULL) != 0) { av_free(frame); av_close_input_file(formatContext); std::cout << "Couldn't open the context with the decoder" << std::endl; return false; } av_dump_format(formatContext, 0, 0, false); //avformat.h line 1256 int samples = ((formatContext->duration + 5000)*codecContext->sample_rate)/AV_TIME_BASE; std::cout << "This stream has " << codecContext->channels << " channels, a sample rate of " << codecContext->sample_rate << "Hz and "<sample_fmt<< " (aka "<< av_get_sample_fmt_name(codecContext->sample_fmt) << ") "<init(codecContext->channels,16,samples); } AVPacket packet; av_init_packet(&packet); int sample_processed=0; bool diag=true; while (true) { int ret=av_read_frame(formatContext, &packet); if (ret<0) { cerr << "finished with code "<index) { // Try to decode the packet into a frame int frameFinished = 0; int bytes = avcodec_decode_audio4(codecContext, frame, &frameFinished, &packet); // Some frames rely on multiple packets, so we have to make sure the frame is finished before // we can use it if (frameFinished) { // frame now has usable audio data in it. How it's stored in the frame depends on the format of // the audio. If it's packed audio, all the data will be in frame->data[0]. If it's in planar format, // the data will be in frame->data and possibly frame->extended_data. Look at frame->data, frame->nb_samples, // frame->linesize, and other related fields on the FFmpeg docs. I don't know how you're actually using // the audio data, so I won't add any junk here that might confuse you. Typically, if I want to find // documentation on an FFmpeg structure or function, I just type " doxygen" into google (like // "AVFrame doxygen" for AVFrame's docs) //av_get_channel_layout_string (char *buf, int buf_size, int nb_channels, uint64_t channel_layout) if (diag) { cerr << "first frame: "<nb_samples<<" samples in "<format)<<" format with channel layout "<channel_layout<< std::endl; diag=false; } //now we can pass the data to the processor(s) for (auto p: processors) { sample_processed=p->process_frame(frame->data[0],frame->nb_samples); } mutex.lock(); progress=((double)sample_processed)/samples; mutex.unlock(); } } // You *must* call av_free_packet() after each call to av_read_frame() or else you'll leak memory av_free_packet(&packet); } // Some codecs will cause frames to be buffered up in the decoding process. If the CODEC_CAP_DELAY flag // is set, there can be buffered up frames that need to be flushed, so we'll do that if (codecContext->codec->capabilities & CODEC_CAP_DELAY) { av_init_packet(&packet); // Decode all the remaining frames in the buffer, until the end is reached int frameFinished = 0; int bytes = avcodec_decode_audio4(codecContext, frame, &frameFinished, &packet); while (bytes >= 0 && frameFinished) { for (auto p: processors) { p->process_frame(frame->data[0],frame->nb_samples); } mutex.lock(); progress=((double)sample_processed)/samples; mutex.unlock(); } } cerr << "finished processing: "< settings; vector 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["description"]=xml.getValue("node","",i1); Node* node=factory.create(settings); if (node) { cerr << "Rotor: created '" << xml.getAttribute("node","type","",i1) << "'" << endl; string nodeID=xml.getAttribute("node","ID","",i1); graph.nodes[nodeID]=node; if(xml.pushTag("node",i1)) { int n2=xml.getNumTags("signal_input"); for (int i2=0;i2create_signal_input(xml.getValue("signal_input","",i2)); string fromID=xml.getAttribute("signal_input","from","",i2); if (graph.nodes.find(fromID)!=graph.nodes.end()) { if (!graph.nodes[nodeID]->inputs[i2]->connect((Signal_node*)graph.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; } xml.popTag(); } } else { cerr << "Rotor: graph loader cannot find node '" << xml.getAttribute("node","type","",i1) << "'" << endl; return false; } } xml.popTag(); } return true; } else return false; } Node_factory::Node_factory(){ //for now, statically load prototype map in constructor add_type("audio_analysis",new Audio_analysis()); add_type("divide",new Signal_divide()); add_type("==",new Is_new_integer()); add_type("signal_output",new Signal_output()); } void audio_thumbnailer::init(int _channels,int _bits,int _samples) { //base_audio_processor::init(_channels,_bits,_samples); channels=_channels; bits=_bits; samples=_samples; cerr << "init audio thumbnailer with "<>3); int stride=channels*bytes; int in_sample=0; while (in_sampleoffset) this_val=-(this_val-(offset*2)); //this_val -=offset; double val=((double)((int16_t)this_val))*scale; accum+=val*val; samples++; } in_sample++; sample++; out_sample++; } if (sample==samples_per_column) { //finished a column //get root-mean double mean=pow(accum/samples,0.5); if (column==0) { cerr << "first column total: "<< accum << " in " << samples << " samples, average " << (accum/samples)<>1; for (int i=0;i