-
Notifications
You must be signed in to change notification settings - Fork 7
/
PartialTracker.hpp
444 lines (340 loc) · 11.4 KB
/
PartialTracker.hpp
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
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
#ifndef PARTIAL_TRACKER_H
#define PARTIAL_TRACKER_H
#include <algorithm>
#include <cmath>
#include <limits>
#include <tuple>
#include "Allocator.hpp"
template <typename T>
class peak
{
public:
peak(T freq, T amp)
: m_freq(freq)
, m_amp(amp)
, m_pitch(std::numeric_limits<T>::infinity())
, m_db(std::numeric_limits<T>::infinity())
{}
peak() : peak(0, 0) {}
const T &freq() const { return m_freq; }
const T &() const { return m_amp; }
const T &pitch() const
{
if (m_pitch == std::numeric_limits<T>::infinity())
m_pitch = std::log2(m_freq / 440.0) * 12.0 + 69.0;
return m_pitch;
}
const T &db() const
{
if (m_db == std::numeric_limits<T>::infinity())
m_db = std::log10(m_amp) * 20.0;
return m_db;
}
private:
T m_freq;
T m_amp;
mutable T m_pitch;
mutable T m_db;
};
template <typename T>
using cpeak = const peak<T>;
template <typename T>
struct track
{
enum class State { Off, Start, Continue, Switch };
track() : m_peak(), m_state(State::Off) {}
void set_peak(cpeak<T>& peak, bool start)
{
m_peak = peak;
m_state = start ? (active() ? State::Switch : State::Start): State::Continue;
}
bool active() const { return m_state != State::Off; }
peak<T> m_peak;
State m_state;
};
template <typename T, bool Impl>
class change_tracker
{
public:
change_tracker() : m_active(true)
{
reset();
}
void add_change(cpeak<T>& now, cpeak<T>& prev, bool use_pitch, bool use_db)
{
T f_change;
T a_change;
if (!m_active)
return;
if (use_pitch)
f_change = now.pitch() - prev.pitch();
else
f_change = now.freq() - prev.freq();
if (use_db)
a_change = now.db() - prev.db();
else
a_change = now.amp() - prev.amp();
m_freq_sum += f_change;
m_freq_abs += std::abs(f_change);
m_amp_sum += a_change;
m_amp_abs += std::abs(a_change);
m_count++;
}
void complete()
{
if (m_count)
{
T recip = T(1) / static_cast<T>(m_count);
m_freq_sum *= recip;
m_freq_abs *= recip;
m_amp_sum *= recip;
m_amp_abs *= recip;
}
}
void reset()
{
m_freq_sum = 0;
m_freq_abs = 0;
m_amp_sum = 0;
m_amp_abs = 0;
m_count = 0;
}
T freq_sum() const { return m_freq_sum; }
T freq_abs() const { return m_freq_abs; }
T amp_sum() const { return m_amp_sum; }
T amp_abs() const { return m_amp_abs; }
void active(bool on) { m_active = on; }
private:
T m_freq_sum;
T m_freq_abs;
T m_amp_sum;
T m_amp_abs;
bool m_active;
size_t m_count;
};
template <typename T>
class change_tracker<T, false>
{
public:
void add_change(cpeak<T>& now, cpeak<T>& prev, bool use_pitch, bool use_db) {}
void complete() {}
void reset() {}
};
template <typename T, bool Changes = false, typename Allocator = malloc_allocator>
class partial_tracker
{
typedef T (*CostType)(T, T, T);
typedef const T&(peak<T>::*GetMethod)() const;
using cost = std::tuple<T, size_t, size_t>;
template <bool B>
using enable_if_t = typename std::enable_if<B, int>::type;
public:
template <typename U = Allocator, enable_if_t<std::is_default_constructible<U>::value> = 0>
partial_tracker(size_t n_tracks, size_t n_peaks)
{
init(n_tracks, n_peaks);
}
template <typename U = Allocator, enable_if_t<std::is_copy_constructible<U>::value> = 0>
partial_tracker(const Allocator& allocator, size_t n_tracks, size_t n_peaks)
: m_allocator(allocator)
{
init(n_tracks, n_peaks);
}
template <typename U = Allocator, enable_if_t<std::is_default_constructible<U>::value> = 0>
partial_tracker(Allocator&& allocator, size_t n_tracks, size_t n_peaks)
: m_allocator(allocator)
{
init(n_tracks, n_peaks);
}
~partial_tracker()
{
m_allocator.deallocate(m_tracks);
m_allocator.deallocate(m_costs);
m_allocator.deallocate(m_peak_assigned);
m_allocator.deallocate(m_track_assigned);
}
partial_tracker(const partial_tracker&) = delete;
partial_tracker & operator=(const partial_tracker&) = delete;
void set_cost_calculation(bool square_cost, bool use_pitch, bool use_db)
{
m_square_cost = square_cost;
m_use_pitch = use_pitch;
m_use_db = use_db;
}
void set_cost_scaling(T freq_unit, T amp_unit, T max_cost)
{
m_freq_scale = 1 / freq_unit;
m_amp_scale = 1 / amp_unit;
m_max_cost = max_cost;
}
void reset()
{
for (size_t i = 0; i < max_tracks(); i++)
m_tracks[i] = track<T>{};
m_changes.reset();
}
void process(peak<T> *peaks, size_t n_peaks, T start_threshold)
{
// Setup
n_peaks = std::min(n_peaks, max_peaks());
reset_assignments();
m_changes.reset();
// Calculate cost functions
size_t n_costs = find_costs(peaks, n_peaks);
// Sort costs
std::sort(m_costs, m_costs + n_costs,
[](const cost& a, const cost& b)
{ return std::get<0>(a) < std::get<0>(b); });
// Assign in order of lowest cost
for (size_t i = 0; i < n_costs; i++)
{
size_t peak_idx = std::get<1>(m_costs[i]);
size_t track_idx = std::get<2>(m_costs[i]);
if (!m_peak_assigned[peak_idx] && !m_track_assigned[track_idx])
{
cpeak<T>& new_peak = peaks[peak_idx];
cpeak<T>& old_peak = m_tracks[track_idx].m_peak;
m_changes.add_change(new_peak, old_peak, m_use_pitch, m_use_db);
m_tracks[track_idx].set_peak(new_peak, false);
assign(peak_idx, track_idx);
}
}
m_changes.complete();
// Start new tracks (prioritised by input order)
for (size_t i = 0, j = 0; i < n_peaks && j < max_tracks(); i++)
{
if (!m_peak_assigned[i] && peaks[i].amp() >= start_threshold)
{
for (; j < max_tracks(); j++)
{
if (!m_track_assigned[j])
{
m_tracks[j].set_peak(peaks[i], true);
assign(i, j);
break;
}
}
}
}
// Make unassigned tracks inactive
for (size_t i = 0; i < max_tracks(); i++)
{
if (!m_track_assigned[i])
m_tracks[i] = track<T>{};
}
}
const track<T> &get_track(size_t idx) { return m_tracks[idx]; }
size_t max_peaks() const { return m_max_peaks; }
size_t max_tracks() const { return m_max_tracks; }
template<bool B = Changes, enable_if_t<B> = 0>
void calc_changes(bool calc) { return m_changes.active(calc); }
template<bool B = Changes, enable_if_t<B> = 0>
T freq_change_sum() const { return m_changes.freq_sum(); }
template<bool B = Changes, enable_if_t<B> = 0>
T freq_change_abs() const { return m_changes.freq_abs(); }
template<bool B = Changes, enable_if_t<B> = 0>
T amp_change_sum() const { return m_changes.amp_sum(); }
template<bool B = Changes, enable_if_t<B> = 0>
T amp_change_abs() const { return m_changes.amp_abs(); }
private:
void init(size_t n_peaks, size_t n_tracks)
{
m_max_peaks = n_peaks;
m_max_tracks = n_tracks;
m_tracks = m_allocator.template allocate<track<T>>(n_tracks);
m_costs = m_allocator.template allocate<cost>(n_tracks * n_peaks);
m_peak_assigned = m_allocator.template allocate<bool>(n_peaks);
m_track_assigned = m_allocator.template allocate<bool>(n_tracks);
reset();
set_cost_calculation(true, true, true);
set_cost_scaling(T(0.5), T(6), T(1));
}
void reset_assignments()
{
for (size_t i = 0; i < max_peaks(); i++)
m_peak_assigned[i] = false;
for (size_t i = 0; i < max_tracks(); i++)
m_track_assigned[i] = false;
}
void assign(size_t peak_idx, size_t track_idx)
{
m_peak_assigned[peak_idx] = true;
m_track_assigned[track_idx] = true;
}
static T cost_abs(T a, T b, T scale)
{
return std::abs(a - b) * scale;
}
static T cost_sq(T a, T b, T scale)
{
const T c = (a - b);
return c * c * scale;
}
static T cost_useful(T cost)
{
return 1 - std::exp(-cost);
}
template<CostType CostFunc, GetMethod Freq, GetMethod Amp>
size_t find_costs(cpeak<T> *peaks, size_t n_peaks)
{
size_t n_costs = 0;
T freq_scale = m_square_cost ? m_freq_scale * m_freq_scale : m_freq_scale;
T amp_scale = m_square_cost ? m_amp_scale * m_amp_scale : m_amp_scale;
for (size_t i = 0; i < n_peaks; i++)
{
for (size_t j = 0; j < max_tracks(); j++)
{
if (m_tracks[j].active())
{
cpeak<T>& a = peaks[i];
cpeak<T>& b = m_tracks[j].m_peak;
T cost = CostFunc((a.*Freq)(), (b.*Freq)(), freq_scale)
+ CostFunc((a.*Amp)(), (b.*Amp)(), amp_scale);
if (cost < m_max_cost)
m_costs[n_costs++] = std::make_tuple(cost, i, j);
}
}
}
return n_costs;
}
size_t find_costs(peak<T> *peaks, size_t n_peaks)
{
// Conveniences for code length
using p = peak<T>;
const auto np = n_peaks;
size_t select = (m_square_cost ? 4 : 0) +
(m_use_pitch ? 2 : 0) +
(m_use_db ? 1 : 0);
// Create inner loops
switch (select)
{
case 0: return find_costs<cost_abs, &p::freq, &p::amp>(peaks, np);
case 1: return find_costs<cost_abs, &p::freq, &p::db>(peaks, np);
case 2: return find_costs<cost_abs, &p::pitch, &p::amp>(peaks, np);
case 3: return find_costs<cost_abs, &p::pitch, &p::db>(peaks, np);
case 4: return find_costs<cost_sq, &p::freq, &p::amp>(peaks, np);
case 5: return find_costs<cost_sq, &p::freq, &p::db>(peaks, np);
case 6: return find_costs<cost_sq, &p::pitch, &p::amp>(peaks, np);
default: return find_costs<cost_sq, &p::pitch, &p::db>(peaks, np);
}
}
// Allocator
Allocator m_allocator;
// Sizes
size_t m_max_peaks;
size_t m_max_tracks;
// Cost parameters
bool m_use_pitch;
bool m_use_db;
bool m_square_cost;
T m_freq_scale;
T m_amp_scale;
T m_max_cost;
// Tracking data
track<T> *m_tracks;
cost *m_costs;
bool *m_peak_assigned;
bool *m_track_assigned;
change_tracker<T, Changes> m_changes;
};
#endif