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
|
#include "nodes_audio_analysis.h"
namespace Rotor{
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;
offset=0x1<<(bits-1); //signed audio
scale=1.0/offset;
out_sample=0; //sample in whole track
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) {
//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
//why does valgrind complain here about uninitialised vars
double mean=pow(accum/samples,0.5f);
audiodata.push_back(mean);
sample=0;
samples=0;
accum=0.0;
}
}
return out_sample;
}
void Audio_thumbnailer::print_vector(xmlIO XML){
string vdata;
int i=0;
for (auto sample: audiodata){
if (i>0) vdata+=",";
vdata+=toString(sample);
}
XML.addValue("data",vdata);
}
bool Vamp_node::init(int _channels,int _bits,int _samples, int _rate) {
//need these to make sense of data
channels=_channels;
bits=_bits;
samples=_samples;
features.clear();
return analyser.init(soname,id,_channels,_bits,_samples,_rate,outputNo,params);
//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 Vamp_node::process_frame(uint8_t *data,int samples_in_frame) {
analyser.process_frame(data,samples_in_frame);
return 1;
}
void Vamp_node::cleanup() {
analyser.cleanup();
features=analyser.features;
}
string Vamp_node::get_features(){
string data;
for (auto i: features) {
data=data+" ["+toString(i.second.number)+":"+toString(i.first);
if (i.second.values.size()) {
data+=" (";
bool first=true;
for (auto j: i.second.values) {
if (first){
first=false;
}
else data+=",";
data=data+toString(j);
}
data+=") ";
}
data+="]";
}
return data;
}
bool sortsegments(std::pair<int,double> i,std::pair<int,double> j){
return (i.second<j.second);
}
bool sortseggrps(std::pair<double,vector<pair<double,int> > > i,std::pair<double,vector<pair<double,int> > > j){
return (i.first<j.first);
}
void Intensity_segmenter::cleanup(){
//algorithm idea:
//get average tempo and intensity for each segment and store them
//scale by the range to get a value from 0.0 to 1.0
//add tempo and intensity according to a weighting
//score the results (ie 1st place, 2nd place) to end up with a set of integer numbers
//how to group with similarity?
//segments come with similarity groups
// 1 - are the wanted git checksegments less than discovered?
// N - do nothing
// 2 - get intensity and tempo averages
// 2 - count the groups
// 3 - are the groups less than the discovered segments?
// N - group by intensity as normal
// 4 - are the groups less than the wanted levels?
//for (auto a:analysers) a.second.cleanup(); //WHY NOT WORK - its as if the call is const
analysers["segmenter"].cleanup();
analysers["tempo"].cleanup();
analysers["intensity"].cleanup();
//combine with similarity numbers
// 1. count similarity numbers
map<int,vector<int> > similarities;
//what do we want to know about these similarities?
// how many are there? map.size()
// how many members are in each one? map[item].size()
// which members are they? auto m: map[item]
uint32_t i=0;
for (auto f:analysers["segmenter"].features) {
if (f.second.values.size()) {
int group=f.second.values[0];
if (similarities.find(group)==similarities.end()){
similarities[group]={};
}
similarities[group].push_back(i);
}
i++;
}
for (auto s:similarities) cerr<<"group "<<s.first<<" count: "<<s.second.size()<<endl;
cerr<<analysers["segmenter"].features.size()<<" segments"<<endl;
cerr<<analysers["tempo"].features.size()<<" tempo features"<<endl;
cerr<<analysers["intensity"].features.size()<<" intensity features"<<endl;
i=0;
double min_tempo=9999999.0;
double min_intensity=9999999.0;
double max_tempo=0.0;
double max_intensity=0.0;
vector<double> tempos;
vector<double> intensities;
vector<double> times;
auto g=++analysers["segmenter"].features.begin();
for (auto f=analysers["segmenter"].features.begin();g!=analysers["segmenter"].features.end();f++,g++,i++){
cerr<<"segment "<<i<<": "<<f->first<<" to "<<g->first<<endl;
times.push_back(f->first);
//integrate tempo and intensity algorithmically
double tempo=0;
if (analysers["tempo"].features.size()) {
double pt=f->first;
double pv=analysers["tempo"].get_value(f->first);
for (auto u=analysers["tempo"].features.upper_bound(f->first);u!=analysers["tempo"].features.upper_bound(g->first);u++){
tempo +=(u->first-pt)*(u->second.values[0]+pv)*0.5f; //area of the slice
pt=u->first;
pv=u->second.values[0];
}
tempo +=(g->first-pt)*(analysers["tempo"].get_value(g->first)+pv)*0.5f; //area of the last slice
tempo /=g->first-f->first; //average value;
}
if (tempo>max_tempo) max_tempo=tempo;
if (tempo<min_tempo) min_tempo=tempo;
tempos.push_back(tempo);
cerr<<"segment "<<i<<" average tempo: "<<tempo<<endl;
double intensity=0;
if (analysers["intensity"].features.size()) {
double pt=f->first;
double pv=analysers["intensity"].get_value(f->first);
for (auto u=analysers["intensity"].features.upper_bound(f->first);u!=analysers["intensity"].features.upper_bound(g->first);u++){
intensity +=(u->first-pt)*(u->second.values[0]+pv)*0.5f; //area of the slice
pt=u->first;
pv=u->second.values[0];
}
intensity +=(g->first-pt)*(analysers["intensity"].get_value(g->first)+pv)*0.5f; //area of the last slice
intensity /=g->first-f->first; //average value;
}
if (intensity>max_intensity) max_intensity=intensity;
if (intensity<min_intensity) min_intensity=intensity;
intensities.push_back(intensity);
}
//make relative scale 0.0-1.0 and save weighted totals
vector< pair<int,double>> totals;
vector<double> totalsmap;
for (i=0;i<tempos.size();i++){
tempos[i]=(tempos[i]-min_tempo)/(max_tempo-min_tempo);
intensities[i]=(intensities[i]-min_intensity)/(max_intensity-min_intensity);
totals.push_back(make_pair(i,(tempos[i]*parameters["tempo_weight"]->value)+(intensities[i]*parameters["intensity_weight"]->value)));
totalsmap.push_back((tempos[i]*parameters["tempo_weight"]->value)+(intensities[i]*parameters["intensity_weight"]->value));
}
//sort and convert to features
std::sort(totals.begin(),totals.end(),sortsegments);
for (i=0;i<totals.size();i++) {
cerr<<"segment "<<totals[i].first<<" average intensity: "<<totals[i].second<<endl;
}
vector<double> bucketoffsets;
for (auto t:totals) bucketoffsets.push_back(0.0);
if (parameters["levels"]->value>0.0&¶meters["levels"]->value<totals.size()){
//use bucketoffsets to redistribute into smaller number of buckets
int numbertoredistribute=totals.size()-((int)parameters["levels"]->value);
double numberperbin=((double)numbertoredistribute/totals.size());
double toadd=0.5f;
int added=0;
for (int j=0;j<totals.size();j++){
int numbertoadd=min(numbertoredistribute-added,(int)toadd);
toadd=(toadd+numberperbin)-numbertoadd;
added+=numbertoadd;
bucketoffsets[j]=added;
}
if (numbertoredistribute>0) {
cerr<<"reducing number of levels by "<<numbertoredistribute<<", offsets:"<<endl;
for (auto o:bucketoffsets) {
cerr<<o<<":";
}
cerr<<endl;
}
}
for (i=0;i<totals.size();i++){
vampHost::feature f;
f.values.push_back((double)i-bucketoffsets[i]);
features[times[totals[i].first]]=f;
}
}
}
/*
A data structure to represent segments and their mapping to output levels
how do we merge the intensity groups with the similarities?
we create a list
... we iterate through the list of segments and place the right output number
*/
|