-
Notifications
You must be signed in to change notification settings - Fork 6
/
pianolizer.hpp
620 lines (564 loc) · 19 KB
/
pianolizer.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
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
/**
* @file pianolizer.hpp
* @brief Musical tone pitch detection library based on the Sliding Discrete Fourier Transform algorithm.
* @see http://github.com/creaktive/pianolizer
* @author Stanislaw Pusep
* @copyright MIT
*/
#pragma once
#define _USE_MATH_DEFINES
#include <cmath>
#include <complex>
#include <cstring>
#include <memory>
#include <vector>
// For C++11 compatibility: https://herbsutter.com/gotw/_102/
#if __cplusplus < 201402L
namespace std {
template<typename T, typename... Args>
std::unique_ptr<T> make_unique(Args&&... args) {
return std::unique_ptr<T>(new T(std::forward<Args>(args)...));
}
}
#endif
/**
* Reasonably fast Ring Buffer implementation.
* Caveat: the size of the allocated memory is always a power of two!
*
* @class RingBuffer
* @par EXAMPLE
* auto rb = RingBuffer(100);
* for (unsigned i = 0; i < 200; i++)
* rb.write(i);
* // prints 174:
* std::cout << rb.read(25) << std::endl;
*/
class RingBuffer {
private:
unsigned mask;
unsigned index = 0;
std::vector<float> buffer;
public:
unsigned size;
/**
* Creates an instance of RingBuffer.
* @param requestedSize How long the RingBuffer is expected to be.
* @memberof RingBuffer
*/
RingBuffer(const unsigned requestedSize) {
const unsigned bits = std::ceil(std::log2(requestedSize));
size = static_cast<unsigned>(1) << bits;
mask = size - 1;
buffer.reserve(size);
memset(buffer.data(), 0, sizeof(float) * size);
}
/**
* Shifts the RingBuffer and stores the value in the latest position.
*
* @param value Value to be stored.
* @memberof RingBuffer
*/
void write(const float value) {
index &= mask;
buffer[index++] = value;
}
/**
* Retrieves the value stored at the position.
*
* @param position Position within the RingBuffer.
* @return The value at the position.
* @memberof RingBuffer
*/
float read(const unsigned position) {
return buffer[(index + (~position)) & mask];
}
};
/**
* Discrete Fourier Transform computation for one single bin.
*
* @class DFTBin
* @par EXAMPLE
* // Detect a 441Hz tone when the sample rate is 44100Hz
* const unsigned N = 1700;
* auto bin = DFTBin(17, N);
* auto rb = RingBuffer(N);
* for (unsigned i = 0; i < 2000; i++) {
* const double currentSample = sin(M_PI / 50 * i); // sine wave oscillator
* rb.write(currentSample);
* // previousSample should be taken N samples before currentSample is taken
* const double previousSample = rb.read(N);
* bin.update(previousSample, currentSample);
* }
* std::cout << bin.rms() << std::endl;
* std::cout << bin.amplitudeSpectrum() << std::endl;
* std::cout << bin.normalizedAmplitudeSpectrum() << std::endl;
* std::cout << bin.logarithmicUnitDecibels() << std::endl;
*/
class DFTBin {
private:
double totalPower = 0.;
double r;
std::complex<double> coeff;
std::complex<double> dft = std::complex<double>(0., 0.);
public:
double k, N;
double referenceAmplitude = 1.; // 0 dB level
/**
* Creates an instance of DFTBin.
* @param k_ Frequency divided by the bandwidth.
* @param N_ Sample rate divided by the bandwidth.
* @memberof DFTBin
* @par EXAMPLE
* // (provided the sample rate of 44100Hz)
* // center: 439.96Hz
* // bandwidth: 25.88Hz
* auto bin = DFTBin(17, 1704);
* // samples are *NOT* complex!
* bin.update(previousSample, currentSample);
*/
DFTBin(const unsigned k_, const unsigned N_)
: k(k_), N(N_) {
if (k == 0)
throw std::invalid_argument("k=0 (DC) not implemented");
else if (N == 0)
throw std::invalid_argument("N=0 is so not supported (Y THO?)");
const double q = 2. * M_PI * k / N;
r = 2. / N;
coeff = std::complex<double>(cos(q), -sin(q));
}
/**
* Do the Sliding DFT computation.
*
* @param previousSample Sample from N frames ago.
* @param currentSample The latest sample.
* @memberof DFTBin
*/
void update(const double previousSample, const double currentSample) {
totalPower += currentSample * currentSample;
totalPower -= previousSample * previousSample;
dft = coeff * (dft - std::complex<double>(previousSample, 0.) + std::complex<double>(currentSample, 0.));
}
/**
* Root Mean Square.
*
* @memberof DFTBin
*/
double rms() {
return std::sqrt(totalPower / N);
}
/**
* Amplitude spectrum in volts RMS.
*
* @see https://www.sjsu.edu/people/burford.furman/docs/me120/FFT_tutorial_NI.pdf
* @memberof DFTBin
*/
double amplitudeSpectrum() {
return M_SQRT2 * std::sqrt(norm(dft)) / N;
}
/**
* Normalized amplitude (always returns a value between 0.0 and 1.0).
* This is well suited to detect pure tones, and can be used to decode DTMF or FSK modulation.
* Depending on the application, you might need sqrt(d.normalizedAmplitudeSpectrum()).
*
* @memberof DFTBin
*/
double normalizedAmplitudeSpectrum() {
return totalPower > 0.
// ? amplitudeSpectrum() / rms()
? r * norm(dft) / totalPower // same as the square of the above, but uses less FLOPs
: 0.;
}
/**
* Using this unit of measure, it is easy to view wide dynamic ranges; that is,
* it is easy to see small signal components in the presence of large ones.
*
* @memberof DFTBin
*/
double logarithmicUnitDecibels() {
return 20. * std::log10(amplitudeSpectrum() / referenceAmplitude);
}
};
/**
* Base class for FastMovingAverage & HeavyMovingAverage. Must implement the update(levels) method.
*
* @class MovingAverage
*/
class MovingAverage {
public:
unsigned channels, sampleRate;
int averageWindow = -1;
int targetAverageWindow;
std::vector<float> sum;
/**
* Creates an instance of MovingAverage.
* @param channels_ Number of channels to process.
* @param sampleRate_ Sample rate, used to convert between time and amount of samples.
* @memberof MovingAverage
*/
MovingAverage(const unsigned channels_, const unsigned sampleRate_)
: channels(channels_), sampleRate(sampleRate_) {
sum.reserve(channels);
memset(sum.data(), 0, sizeof(float) * channels);
}
virtual ~MovingAverage() = default;
/**
* Get the current window size (in seconds).
*
* @memberof MovingAverage
*/
float averageWindowInSeconds() {
return averageWindow / static_cast<float>(sampleRate);
}
/**
* Set the current window size (in seconds).
*
* @memberof MovingAverage
*/
void averageWindowInSeconds(const float value) {
targetAverageWindow = std::round(value * sampleRate);
if (averageWindow == -1)
averageWindow = targetAverageWindow;
}
/**
* Adjust averageWindow in steps.
*
* @memberof MovingAverage
*/
void updateAverageWindow() {
if (targetAverageWindow > averageWindow)
averageWindow++;
else if (targetAverageWindow < averageWindow)
averageWindow--;
}
/**
* Retrieve the current moving average value for a given channel.
*
* @param n Number of channel to retrieve the moving average for.
* @return Current moving average value for the specified channel.
* @memberof MovingAverage
*/
float read(const unsigned n) {
return sum[n] / averageWindow;
}
virtual void update(const std::vector<float>& levels) = 0;
};
/**
* Moving average of the output (effectively a low-pass to get the general envelope).
* Fast approximation of the MovingAverage; requires significantly less memory.
*
* @see https://www.daycounter.com/LabBook/Moving-Average.phtml
* @class FastMovingAverage
* @extends MovingAverage
* @par EXAMPLE
* // initialize the moving average object
* unsigned channels = 2;
* auto movingAverage = FastMovingAverage(
* channels,
* sampleRate
* );
* // averageWindowInSeconds can be updated on-fly!
* movingAverage.averageWindowInSeconds(0.05);
* // for every processed frame
* movingAverage.update(levels);
* // overwrite the levels with the averaged ones
* for (unsigned band = 0; band < channels; band++)
* levels[band] = movingAverage.read(band);
*/
class FastMovingAverage : public MovingAverage {
public:
FastMovingAverage(const unsigned channels_, const unsigned sampleRate_)
: MovingAverage{ channels_, sampleRate_ }
{}
/**
* Update the internal state with from the input.
*
* @param levels Array of level values, one per channel.
* @memberof FastMovingAverage
*/
void update(const std::vector<float>& levels) {
updateAverageWindow();
for (unsigned n = 0; n < channels; n++) {
const float currentSum = sum[n];
sum[n] = averageWindow
? currentSum + levels[n] - currentSum / averageWindow
: levels[n];
}
}
};
/**
* Moving average of the output (effectively a low-pass to get the general envelope).
* This is the "proper" implementation; it does require lots of memory allocated for the RingBuffers!
*
* @class HeavyMovingAverage
* @extends MovingAverage
* @par EXAMPLE
* // initialize the moving average object
* unsigned channels = 2;
* movingAverage = HeavyMovingAverage(
* channels,
* sampleRate,
* round(sampleRate * maxAverageWindowInSeconds)
* )
* // averageWindowInSeconds can be updated on-fly!
* movingAverage.averageWindowInSeconds(0.05);
* // for every processed frame
* movingAverage.update(levels);
* // overwrite the levels with the averaged ones
* for (unsigned band = 0; band < channels; band++)
* levels[band] = movingAverage.read(band);
*/
class HeavyMovingAverage : public MovingAverage {
private:
std::vector<std::unique_ptr<RingBuffer>> history;
public:
/**
* Creates an instance of HeavyMovingAverage.
* @param channels Number of channels to process.
* @param sampleRate Sample rate, used to convert between time and amount of samples.
* @param [maxWindow=sampleRate] Preallocate buffers of this size, per channel.
* @memberof HeavyMovingAverage
*/
HeavyMovingAverage(const unsigned channels_, const unsigned sampleRate_, const unsigned maxWindow = 0)
: MovingAverage{ channels_, sampleRate_ }
{
history.reserve(channels);
for (unsigned n = 0; n < channels; n++)
history.push_back(std::make_unique<RingBuffer>(maxWindow ? maxWindow : sampleRate));
}
/**
* Update the internal state with from the input.
*
* @param levels Array of level values, one per channel.
* @memberof HeavyMovingAverage
*/
void update(const std::vector<float>& levels) {
for (unsigned n = 0; n < channels; n++) {
const float value = levels[n];
history[n]->write(value);
sum[n] += value;
if (targetAverageWindow == averageWindow) {
sum[n] -= history[n]->read(static_cast<unsigned>(averageWindow));
} else if (targetAverageWindow < averageWindow) {
sum[n] -= history[n]->read(static_cast<unsigned>(averageWindow));
sum[n] -= history[n]->read(static_cast<unsigned>(averageWindow) - 1);
}
}
updateAverageWindow();
}
};
/**
* Base class for PianoTuning. Must implement mapping().
*
* @class Tuning
*/
class Tuning {
public:
unsigned sampleRate, bands;
struct tuningValues {
unsigned k, N;
};
/**
* Creates an instance of Tuning.
* @param sampleRate_ Self-explanatory.
* @param bands_ How many filters.
*/
Tuning(const unsigned sampleRate_, const unsigned bands_)
: sampleRate(sampleRate_), bands(bands_)
{}
virtual ~Tuning() = default;
/**
* Approximate k & N values for DFTBin.
*
* @param frequency In Hz.
* @param bandwidth In Hz.
* @return tuningValues struct containing k & N that best approximate for the given frequency & bandwidth.
* @memberof Tuning
*/
const tuningValues frequencyAndBandwidthToKAndN(const double frequency, const double bandwidth) {
double N = std::floor(sampleRate / bandwidth);
const double k = std::floor(frequency / bandwidth);
// find such N that (sampleRate * (k / N)) is the closest to freq
// (sacrifices the bandwidth precision; bands will be *wider*, and, therefore, will overlap a bit!)
double delta = std::fabs(sampleRate * (k / N) - frequency);
for (unsigned i = N - 1; ; i--) {
const double tmpDelta = std::fabs(sampleRate * (k / i) - frequency);
if (tmpDelta < delta) {
delta = tmpDelta;
N = i;
} else {
return { static_cast<unsigned>(k), static_cast<unsigned>(N) };
}
}
}
const virtual std::vector<tuningValues> mapping() = 0;
};
/**
* Essentially, creates an instance that provides the 'mapping',
* which is an array of objects providing the values for i, k & N.
*
* @class PianoTuning
* @extends Tuning
* @example
* // a common sample rate
* auto tuning = PianoTuning(44100);
*
* // prints 17 for the note C2:
* std::cout << tuning.mapping[0].k << std::endl;
* // prints 11462 for the note C2:
* std::cout << tuning.mapping[0].N << std::endl;
*
* // prints 17 for the note C7:
* std::cout << tuning.mapping[60].k << std::endl;
* // prints 358 for the note C7:
* std::cout << tuning.mapping[60].N << std::endl;
*/
class PianoTuning : public Tuning {
private:
unsigned referenceKey;
double pitchFork;
double tolerance;
public:
/**
* Creates an instance of PianoTuning.
* @param sampleRate This directly influences the memory usage: 44100Hz or 48000Hz will both allocate a buffer of 64KB (provided 32-bit floats are used).
* @param [keysNum=61] Most pianos will have 61 keys.
* @param [referenceKey=33] Key index for the pitchFork reference (A4 is the default).
* @param [pitchFork=440.0] A4 is 440 Hz by default.
* @param [tolerance=1.0] frequency tolerance, range (0.0, 1.0].
* @memberof PianoTuning
*/
PianoTuning(
const unsigned sampleRate_,
const unsigned keysNum = 61,
const unsigned referenceKey_ = 33,
const double pitchFork_ = 440.0,
const double tolerance_ = 1.
) : Tuning{ sampleRate_, keysNum }, referenceKey(referenceKey_), pitchFork(pitchFork_), tolerance(tolerance_)
{}
/**
* Converts the piano key number to it's fundamental frequency.
*
* @see https://en.wikipedia.org/wiki/Piano_key_frequencies
* @param key
* @return frequency
* @memberof PianoTuning
*/
double keyToFreq(const double key) {
return pitchFork * std::pow(2., (key - referenceKey) / 12.);
}
/**
* Computes the array of tuningValues structs that specify the frequencies to analyze.
*
* @memberof PianoTuning
*/
const std::vector<tuningValues> mapping() {
std::vector<tuningValues> output;
output.reserve(bands);
for (unsigned key = 0; key < bands; key++) {
const double frequency = keyToFreq(key);
const double bandwidth = 2. * (keyToFreq(key + .5 * tolerance) - frequency);
output.push_back(frequencyAndBandwidthToKAndN(frequency, bandwidth));
}
return output;
}
};
/**
* Sliding Discrete Fourier Transform implementation for (westerns) musical frequencies.
*
* @see https://www.comm.utoronto.ca/~dimitris/ece431/slidingdft.pdf
* @class SlidingDFT
* @par EXAMPLE
* // a common sample rate
* auto tuning = PianoTuning(44100);
* // no moving average
* auto slidingDFT = SlidingDFT(tuning);
* auto input = make_unique<float[]>(128);
* // fill the input buffer with the samples
* float *output = nullptr;
* // just process; no moving average
* output = slidingDFT.process(input);
*/
class SlidingDFT {
private:
std::vector<std::shared_ptr<DFTBin>> bins;
std::vector<float> levels;
std::unique_ptr<RingBuffer> ringBuffer;
#ifndef DISABLE_MOVING_AVERAGE
std::shared_ptr<MovingAverage> movingAverage;
#endif
public:
unsigned sampleRate, bands;
/**
* Creates an instance of SlidingDFT.
* @param tuning Tuning instance (a class derived from Tuning; for instance, PianoTuning).
* @param [maxAverageWindowInSeconds=0] Positive values are passed to MovingAverage implementation; negative values trigger FastMovingAverage implementation. Zero disables averaging.
* @memberof SlidingDFT
*/
SlidingDFT(const std::shared_ptr<Tuning> tuning, const double maxAverageWindowInSeconds = 0.) {
sampleRate = tuning->sampleRate;
bands = tuning->bands;
bins.reserve(bands);
levels.reserve(bands);
unsigned maxN = 0;
for (auto band : tuning->mapping()) {
bins.push_back(std::make_shared<DFTBin>(band.k, band.N));
maxN = std::max(maxN, band.N);
}
ringBuffer = std::make_unique<RingBuffer>(maxN);
#ifndef DISABLE_MOVING_AVERAGE
if (maxAverageWindowInSeconds > 0.) {
movingAverage = std::make_shared<HeavyMovingAverage>(
bands,
sampleRate,
std::round(sampleRate * maxAverageWindowInSeconds)
);
} else if (maxAverageWindowInSeconds < 0.) {
movingAverage = std::make_shared<FastMovingAverage>(
bands,
sampleRate
);
} else {
movingAverage = nullptr;
}
#endif
}
/**
* Process a batch of samples.
*
* @param samples Array with the batch of samples to process. Value range is irrelevant (can be from -1.0 to 1.0 or 0 to 255 or whatever, as long as it is consistent).
* @param [averageWindowInSeconds=0] Adjust the moving average window size.
* @return Snapshot of the *squared* levels after processing all the samples. Value range is between 0.0 and 1.0. Depending on the application, you might need sqrt() of the level values (for visualization purposes it is actually better as is).
* @memberof SlidingDFT
*/
const float* process(const float samples[], const size_t samplesLength, const double averageWindowInSeconds = 0.) {
#ifndef DISABLE_MOVING_AVERAGE
if (movingAverage != nullptr)
movingAverage->averageWindowInSeconds(averageWindowInSeconds);
#endif
const unsigned binsNum = bins.size();
// store in the ring buffer & process
for (unsigned i = 0; i < samplesLength; i++) {
const float currentSample = samples[i];
ringBuffer->write(currentSample);
unsigned band = 0;
for (auto bin : bins) {
const float previousSample = ringBuffer->read(bin->N);
bin->update(previousSample, currentSample);
levels[band] = bin->normalizedAmplitudeSpectrum();
// levels[band] = bin->logarithmicUnitDecibels();
band++;
}
#ifdef DISABLE_MOVING_AVERAGE
}
#else
if (movingAverage != nullptr)
movingAverage->update(levels);
}
// snapshot of the levels, after smoothing
if (movingAverage != nullptr && movingAverage->averageWindow > 0)
for (unsigned band = 0; band < binsNum; band++)
levels[band] = movingAverage->read(band);
#endif
return levels.data();
}
};