summaryrefslogtreecommitdiff
path: root/rotord/src/nodes_audio_analysis.h
blob: 7a0ed9e02f26585721056d92af724333a07a9bd6 (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
#ifndef ROTOR_NODES_AUDIO_ANALYSIS
#define ROTOR_NODES_AUDIO_ANALYSIS

#include "rotor.h"
#include "vampHost.h"

namespace Rotor {
#define VAMPHOST_Timeline 1
#define VAMPHOST_Timesteps 2
#define VAMPHOST_Valueline 3
#define VAMPHOST_Values 4
	class Vamp_node: public Audio_processor  {
			//base class for vamp plugin hosts
			public:
				Vamp_node(){
					create_attribute("mode","Data output mode","Mode","timeline",{"timeline","timesteps","valueline","values"});
				};
				bool init(int _channels,int _bits,int _samples,int _rate);
				void cleanup();
				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;
							float v1,v2;
							v1=v2=0.0f;
							if (i->second.values.size()) v2=i->second.values[0];
							i--;
							float lk=i->first;
							int ln=i->second.number;
							if (i->second.values.size()) v1=i->second.values[0];
							int m=attributes["mode"]->intVal;
							//
							switch (attributes["mode"]->intVal){
								case VAMPHOST_Timeline:
									return (((time.time-lk)/(uk-lk))+ln);
								case VAMPHOST_Timesteps:
									return (float)ln;
								case VAMPHOST_Valueline:
									return ((((time.time-lk)/(uk-lk))*(v2-v1))+v1);
								case VAMPHOST_Values:
									return v1;
							}
						}
					}
					return 0.0f;
				}
				string get_features();
				void print_summary(){
					cerr<<"vamp plugin "<<id<<" of library "<<soname<<" found "<<analyser.features.size()<<" features "<<endl;
				};
			protected:
				string soname,id;
				int outputNo;
				vampHost::Analyser analyser;
			private:
				//??
				map <string,float> params;
	};
	class Audio_analysis: public Vamp_node  {
		//vamp node that allows the user to choose a plugin
			public:
				Audio_analysis(){
					//create_attribute("soname","Plugin library to use","Plugin library","vamp-example-plugins",{"horiz","vert","horizR","vertR"});
					//create_attribute("id","ID of Plugin to use","Plugin ID","percussiononsets",{"horiz","vert","horizR","vertR"});
					create_attribute("analyser","Analyser Plugin to use","Analyser plugin","barbeattracker",{"barbeattracker","segmenter"});
					create_parameter("outputNo","number","Plugin output to use","Output number",0.0f);
					title="Audio analysis";
					description="Analyse audio and output";
				};
				Audio_analysis(map<string,string> &settings):Audio_analysis() {
					base_settings(settings);
					soname=find_setting(settings,"soname");
					id=find_setting(settings,"id");
					outputNo=find_setting(settings,"outputNo",0);
				};
				~Audio_analysis(){};
				Audio_analysis* clone(map<string,string> &_settings) { return new Audio_analysis(_settings);};
			private:
	};
	class Act_segmenter: public Vamp_node  {
	//vamp node that applies a ruleset to manage a set of acts via a cycler
		public:
			Act_segmenter(){
				create_parameter("outputNo","number","Plugin output to use","Output number",0.0f);
				create_parameter("acts","number","Number of acts defined","Acts",1.0f);
				title="Act manager";
				description="Applies a ruleset to manage acts based on segments";
			};
			Act_segmenter(map<string,string> &settings):Act_segmenter() {
				base_settings(settings);
				soname="qm-vamp-plugins";
				id="qm-segmenter";
				outputNo=find_setting(settings,"outputNo",0);
			};
			~Act_segmenter(){};
			Act_segmenter* clone(map<string,string> &_settings) { return new Act_segmenter(_settings);};
			void cleanup(){
				Vamp_node::cleanup();
				map<float,vampHost::feature> acts; //temporary storage for new set of features
				vector<int> act_count;
				for (int i=0;i<(int)parameters["acts"]->value;i++) act_count.push_back(0);

				map<float,int> durations; //list of segment durations
				int i=0;
				for (map<float,vampHost::feature>::iterator f=analyser.features.begin();f!=analyser.features.end();++f){
					auto g=f;
					if (++g!=analyser.features.end()){
						durations[g->first-f->first]=i;
					}
					i++;
				}
				//distribute acts amongst segments;
				if (analyser.features.size()==(int)parameters["acts"]->value+1){
					for (auto f: analyser.features){
						acts[f.first]=f.second;
					}
				}
				else if (analyser.features.size()<(int)parameters["acts"]->value+1){
					//iteratively split segments and refresh durations
					//this could work well on the original data
					//pick the longest and split it in two
					//refresh durations - this can be a function
					//keep going
					//finally copy out

				}
				else { //there are extra segments to assign to acts
					//assign act numbers to segments

					//work through the segments
					//have a list of segments that are to be filled
					//start at the ends and work inwards
					//have a list of how many times each segment has been used

					//start with a set of the segments numbers
					//(the aim: to access the segment numbers by the time that they start)
					map<float,vampHost::feature> segments=analyser.features;
					segments.erase(--segments.end());

					//assign the acts from the beginning and end in towards the middle
					bool take_start=true;
					while (segments.size()){
						int act=-1;
						vampHost::feature f;
						float t;
						if (take_start){
							f=(*segments.begin()).second;
							t=(*segments.begin()).first;
							segments.erase(segments.begin());
							for (int i=0;i<(int)parameters["acts"]->value-1;i++) {
								if (act_count[i]==0||(act_count[i+1]>act_count[i]&&act_count[i]<i)) {
									act=i;
									break;
								}
							}
						}
						else {
							f=(*--segments.end()).second;
							t=(*--segments.end()).first;
							segments.erase(--segments.end());
							for (int i=(int)parameters["acts"]->value-1;i>0;i--) {
								if (act_count[i]==0||(act_count[i-1]>act_count[i]&&act_count[i]<(int)parameters["acts"]->value-i)) {
									act=i;
									break;
								}
							}

						}
						if (act==-1){ //all slots are filled equally
							act=(int)parameters["acts"]->value/2;
						}
						act_count[act]++;
						f.number=act;
						acts[t]=f;
						take_start=!take_start;
					}
				}
				//add last item back on
				acts[(*--analyser.features.end()).first]=analyser.features[(*--analyser.features.end()).first];
				analyser.features=acts;
			}
		private:

	};
}

#endif