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
|
#include "powerlist.h"
#include <stdlib.h>
#include <string.h>
#include <hugin.hpp>
#ifdef __STRICT_ANSI__
#undef __STRICT_ANSI__
#endif
#include <math.h>
#define MIN_SAMPLE_SET_SIZE 26
#define SIZE 500
static float box_muller_transform(float mean, float stddev)
{
float U1 = (float)rand() / (float)RAND_MAX;
float U2 = (float)rand() / (float)RAND_MAX;
float x = sqrt(-2.0 * log(U1)) * cos(2.0 * M_PI * U2);
return mean + stddev * x;
}
PowerList::PowerList()
{
power_max = 0;
power_min = 100000000;
lastsample = NULL;
}
#define THRES 1.0
void PowerList::add(Sample *sample)
{
PowerListItem item;
item.power = -1;
item.sample = sample;
samples.push_back(item);
}
Channel *PowerList::getMasterChannel()
{
std::map<Channel *, int> count;
std::vector<PowerListItem>::iterator si = samples.begin();
while(si != samples.end()) {
PowerListItem &item = *si;
Sample *sample = item.sample;
Channel *max_channel = NULL;
sample_t max_val = 0;
size_t ci = 0;
AudioFiles::iterator ai = sample->audiofiles.begin();
while(ai != sample->audiofiles.end()) {
Channel *c = ai->first;
AudioFile *af = ai->second;
af->load(SIZE);
float silence = 0;
size_t silence_length = 4;
for(size_t s = af->size; s > 0 && s > af->size - silence_length; s--) {
silence += af->data[s];
}
silence /= silence_length;
size_t s = 0;
for(; s < af->size; s++) {
float val = af->data[s] * af->data[s] * (1.0 / (float)(s+1));
if(val > max_val) {
max_val = val;
max_channel = c;
break;
}
}
af->unload();
ai++;
ci++;
}
if(max_channel) {
if(count.find(max_channel) == count.end()) count[max_channel] = 0;
count[max_channel]++;
}
si++;
}
Channel *master = NULL;
int max_count = -1;
std::map<Channel *, int>::iterator ci = count.begin();
while(ci != count.end()) {
if(ci->second > max_count &&
strstr(ci->first->name.c_str(), "Alesis") == 0) {
master = ci->first;
max_count = ci->second;
}
ci++;
}
return master;
}
void PowerList::finalise()
{
#ifdef AUTO_CALCULATE_POWER
Channel *master_channel = getMasterChannel();
if(master_channel == NULL) {
ERR(rand, "No master channel found!\n");
return;
}
DEBUG(rand, "Master channel: %s\n", master_channel->name.c_str());
#endif
std::vector<PowerListItem>::iterator si = samples.begin();
while(si != samples.end()) {
PowerListItem &item = *si;
Sample *sample = item.sample;
#ifdef AUTO_CALCULATE_POWER
DEBUG(rand, "Sample: %s\n", sample->name.c_str());
AudioFile *master = NULL;
AudioFiles::iterator afi = sample->audiofiles.begin();
while(afi != sample->audiofiles.end()) {
if(afi->first->name == master_channel->name) {
master = afi->second;
break;
}
afi++;
}
if(master == NULL) {
si++;
continue;
}
master->load();
#endif
#ifdef AUTO_CALCULATE_POWER
if(sample->power == -1) {
DEBUG(powerlist, "Calculating power\n");
float power = 0;
size_t s = 0;
for(; s < SIZE && s < master->size; s++) {
power += master->data[s] * master->data[s];
}
power = sqrt(power);
sample->power = power;
}
#endif
item.power = sample->power;
if(item.power > power_max) power_max = item.power;
if(item.power < power_min) power_min = item.power;
DEBUG(rand, " - power: %f\n", item.power);
si++;
}
}
Sample *PowerList::get(level_t level)
{
int retry = 3;
Sample *sample = NULL;
if(!samples.size()) return NULL;
float power_span = power_max - power_min;
float width = fmax(samples.size(), MIN_SAMPLE_SET_SIZE);
float stddev = power_span / width;
float mean = level * (power_span - stddev) + (stddev / 2.0);
again:
float lvl = box_muller_transform(mean, stddev);
lvl += power_min;
DEBUG(rand, "level: %f, lvl: %f (mean: %.2f, stddev: %.2f)\n",
level, lvl, mean, stddev);
float power = 0;
std::vector<PowerListItem>::iterator i = samples.begin();
while(i != samples.end()) {
if(sample == NULL) {
sample = i->sample;
power = i->power;
}
if(fabs(i->power - lvl) < fabs(power - lvl)) {
sample = i->sample;
power = i->power;
}
i++;
}
if(lastsample == sample && retry--) {
DEBUG(rand, "Retry [%d retries left]", retry);
goto again;
}
DEBUG(rand, "Found sample with power %f\n", power);
lastsample = sample;
return sample;
}
|