summaryrefslogtreecommitdiff
path: root/rotord/rotor.cpp
blob: 23e219695c5469a4e455d8619789c39fb553999e (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
#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;
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("bang",new Is_new_integer());
	add_type("signal_output",new Signal_output());
	add_type("testcard",new Testcard());
	add_type("video_output",new Video_output());
	add_type("video_input",new Video_input());
}

bool Signal_input::connect(Signal_node* source) {
	if (source->output_type=="signal") {
		connection=(Node*)source;
		return true;
	}
	else return false;
}
bool Image_input::connect(Image_node* source) {
	if (source->output_type=="image") {
		connection=(Node*)source;
		return true;
	}
	else return false;
}
bool Signal_output::render(const float duration, const float framerate,string &xml_out){
	//testing signal routes
	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<duration;f+=step) {
		float u=get_output(Time_spec(f,framerate));
		if (!fequal(u,v)) {
			xml_out+=("<signal time='"+ofToString(f)+"'>"+ofToString(u)+"</signal>\n");
			v=u;
		}
	}
	return true;
}

bool Audio_thumbnailer::init(int _channels,int _bits,int _samples,int _rate) {
	//base_audio_processor::init(_channels,_bits,_samples);
	channels=_channels;
	bits=_bits;
	samples=_samples;
	samples_per_column=samples/width;
	column=0; //point thumbnail bitmap
	out_sample=0; //sample in whole track
	offset=0x1<<(bits-1); //signed audio
	scale=1.0/offset;
	sample=0;
	samples=0;
	accum=0.0;
	return true;
}
int Audio_thumbnailer::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) {
		//continue the column
		while (sample<samples_per_column&&in_sample<samples_in_frame) {
			//accumulate samples for this column until we run out of samples
			for (int i=0;i<channels;i++) {
				unsigned int this_val=0;
				for (int j=0;j<bytes;j++) {
					this_val+=_data[(in_sample*stride)+(i*bytes)+j]<<(j*8);
				}
				//convert from integer data format - i.e s16p - to audio signal in -1..1 range
				//presume 16 bits for now...
				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)<<endl;
			//}
			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++;
			sample=0;
			samples=0;
			accum=0.0;
		}
	}
	return out_sample;
}
string Audio_thumbnailer::print(){
	//base64 encode the image data output it

	stringstream output;
	Poco::Base64Encoder *enc=new Poco::Base64Encoder(output);

	enc->write(data,width*height);
	//tring 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";
	}
	*/
	enc->close();
	delete enc;
	return output.str();
}
bool Audio_analysis::init(int _channels,int _bits,int _samples, int _rate) {
	//need these to make sense of data
	channels=_channels;
	bits=_bits;
	samples=_samples;

	return analyser.init(soname,id,_channels,_bits,_samples,_rate);

	//attempt to load vamp plugin and prepare to receive frames of data
	//should the audio analysis contain a vamphost or should it inherit?
	//maybe neater to contain it in terms of headers etc

}
int Audio_analysis::process_frame(uint8_t *data,int samples_in_frame) {
	analyser.process_frame(data,samples_in_frame);
	return 1;
}
void Audio_analysis::cleanup() {
	analyser.cleanup();
	//print_features();
}
void Audio_analysis::print_features(){
	for (auto i: analyser.features) {
		cerr<<i.second<<" "<<i.first<<endl;
	}
}

bool Video_output::render(const float duration, const float framerate,const string &output_filename,const string &audio_filename){

	//
	//setup defaults
	int outW=640;
	int outH=480;
	int bitRate=4000000;
	int frameRate=25;
	AVCodecID codecId=AV_CODEC_ID_MPEG4;
	std::string container ="mov";
	std::string input ="01.mp3";

    bool usingaudio=audioloader.setup(input);

	if (exporter->setup(outW,outH,bitRate,frameRate,container)) { //codecId,
		if (exporter->record(output_filename)) {

			cerr << "Rotor: Video_output rendering " << duration << " seconds at " << framerate << " fps" << endl;
			float step=1.0f/framerate;
			float v=0.0f;
			for (float f=0.0f;f<duration;f+=step) {
				if (!exporter->encodeFrame(get_output(Frame_spec(f,framerate,outW,outH))->RGBdata,audioloader.get_samples(exporter->get_audio_framesize()))){
				//if (!exporter->encodeFrame(get_output(Frame_spec(f,framerate,outW,outH))->RGBdata,audioloader.get_packet())){
					cerr << "Rotor: video output failed"<<endl;
				 	break;
				}
			}
			exporter->finishRecord();
			cerr << "Rotor: Video_output finished "<< endl;
			return true;
		}
	}

	return false;
}

bool Video_input::load(const string &filename){
    if (player->loadMovie(filename)){
        player->play();
        player->setPaused(true);
        cerr<<"loaded "<<filename<<", "<<player->getDuration()<<" seconds "<<", "<<player->getWidth()<<"x"<<player->getWidth()<<endl;
        return true;
    }
    return false;
}
Image* Video_input::get_output(const Frame_spec &frame){
    //wonder about the actual mechanism used by gstreamer
    //have to implment callback when seek is ready?
    //presume gstreamer caches a loaded frame?
    //if (player->isLoaded()){
    //    image.setup(player)

    //}
    return nullptr;
};