-
Notifications
You must be signed in to change notification settings - Fork 588
/
common.cpp
1222 lines (973 loc) · 34.6 KB
/
common.cpp
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
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*! \file common.cpp
* \brief Enter description here.
*/
#include "common.h"
#include "constants.h"
#include <cstring>
#include <cmath>
#include <thread>
#include <mutex>
#include <fstream>
#include <deque>
#include <algorithm>
#ifndef pi
#define pi 3.1415926535897932384626433832795
#endif
#ifndef sqrt2
#define sqrt2 (2.0 * 0.707106781186547524401)
#endif
#ifndef sqrt2over2
#define sqrt2over2 0.707106781186547524401
#endif
namespace {
template <typename TSampleInput, typename TSample>
bool readWaveform(std::ifstream & fin, TWaveformT<TSample> & res, int32_t offset, std::streamsize size) {
if (std::is_same<TSample, TSampleI16>::value) {
std::vector<TSampleInput> buf(size/sizeof(TSampleInput));
res.resize(offset + size/sizeof(TSampleInput));
fin.read((char *)(buf.data()), size);
double amax = calcAbsMax(buf);
double iamax = amax != 0.0 ? 1.0/amax : 1.0;
for (auto i = 0; i < (int) buf.size(); ++i) res[offset + i] = std::round(std::numeric_limits<TSampleI16>::max()*(buf[i]*iamax));
} else if (std::is_same<TSample, TSampleF>::value) {
res.resize(offset + size/sizeof(TSample));
fin.read((char *)(res.data() + offset), size);
} else {
return false;
}
return true;
}
}
constexpr float iRAND_MAX = 1.0f/float(RAND_MAX);
float frand() { return ((float)rand())*iRAND_MAX; }
float frandGaussian(float mu, float sigma) {
static const float two_pi = 2.0*3.14159265358979323846;
thread_local float z1;
thread_local bool generate;
generate = !generate;
if (!generate)
return z1 * sigma + mu;
float u1 = frand();
float u2 = frand();
float t = sqrt(-2.0f * log(1.0f - u1));
float z0 = t*cos(two_pi*u2);
z1 = t*sin(two_pi*u2);
return z0 * sigma + mu;
}
uint64_t t_ms() {
return std::chrono::duration_cast<std::chrono::milliseconds>(std::chrono::system_clock::now().time_since_epoch()).count(); // duh ..
}
std::map<std::string, std::string> parseCmdArguments(int argc, char ** argv) {
int last = argc;
std::map<std::string, std::string> res;
for (int i = 1; i < last; ++i) {
if (argv[i][0] == '-') {
if (strlen(argv[i]) > 1) {
res[std::string(1, argv[i][1])] = strlen(argv[i]) > 2 ? argv[i] + 2 : "";
}
}
}
return res;
}
template <typename TSampleSrc, typename TSampleDst>
bool convert(const TWaveformT<TSampleSrc> & src, TWaveformT<TSampleDst> & dst) {
static_assert(std::is_same<TSampleSrc, TSampleDst>::value == false, "Required different sample types");
static_assert(std::is_same<TSampleSrc, TSampleF>::value, "Source sample type not supported");
static_assert(std::is_same<TSampleDst, TSampleI16>::value, "Destination sample type not supported");
dst.resize(src.size());
double amax = calcAbsMax(src);
double iamax = amax != 0.0 ? 1.0/amax : 1.0;
for (auto i = 0; i < (int) src.size(); ++i) dst[i] = std::round(std::numeric_limits<TSampleDst>::max()*(src[i]*iamax));
return true;
}
template bool convert<TSampleF, TSampleI16>(const TWaveformT<TSampleF> & src, TWaveformT<TSampleI16> & dst);
template <typename TSample>
void filter(TWaveformT<TSample> & waveform, EAudioFilter filterId, float freqCutoff_Hz, int64_t sampleRate) {
switch (filterId) {
case EAudioFilter::None:
{
return;
}
break;
case EAudioFilter::FirstOrderHighPass:
{
auto filterCoefficients = ::calculateCoefficientsFirstOrderHighPass(freqCutoff_Hz, sampleRate);
for (auto & s : waveform) {
s = ::filterFirstOrderHighPass(filterCoefficients, s);
}
return;
}
break;
case EAudioFilter::SecondOrderButterworthHighPass:
{
auto filterCoefficients = ::calculateCoefficientsSecondOrderButterworthHighPass(freqCutoff_Hz, sampleRate);
for (auto & s : waveform) {
s = ::filterSecondOrderButterworthHighPass(filterCoefficients, s);
}
return;
}
break;
}
fprintf(stderr, "Unknown filter type: %d\n", filterId);
}
template void filter<TSampleF>(TWaveformT<TSampleF> & waveform, EAudioFilter filterId, float freqCutoff_Hz, int64_t sampleRate);
template <typename TSample>
double calcAbsMax(const TWaveformT<TSample> & waveform) {
double amax = 0.0f;
for (auto i = 0; i < (int) waveform.size(); ++i) if (std::abs(waveform[i]) > amax) amax = std::abs(waveform[i]);
return amax;
}
template double calcAbsMax<TSampleF>(const TWaveformT<TSampleF> & waveform);
template <typename TSample>
bool saveToFile(const std::string & fname, TWaveformT<TSample> & waveform) {
static_assert(std::is_same<TSample, TSampleF>::value, "Sample type not supported");
std::ofstream fout(fname, std::ios::binary);
if (fout.good() == false) {
return false;
}
auto totalSize_bytes = sizeof(TSample)*waveform.size();
fout.write((char *)(waveform.data()), totalSize_bytes);
fout.close();
printf("Total data saved: %g MB\n", ((float)(totalSize_bytes)/1024.0f/1024.0f));
return true;
}
template bool saveToFile<TSampleF>(const std::string & fname, TWaveformT<TSampleF> & waveform);
template <typename TSampleInput, typename TSample>
bool readFromFile(const std::string & fname, TWaveformT<TSample> & res) {
std::ifstream fin(fname, std::ios::binary | std::ios::ate);
if (fin.good() == false) {
return false;
}
{
std::streamsize size = fin.tellg();
fin.seekg(0, std::ios::beg);
static_assert(std::is_same<TSampleInput, TSampleF>::value, "TSampleInput not supported");
static_assert(std::is_same<TSample, TSampleF>::value ||
std::is_same<TSample, TSampleI16>::value, "TSample not supported");
if (readWaveform<TSampleInput>(fin, res, 0, size) == false) {
return false;
}
}
fin.close();
return true;
}
template bool readFromFile<TSampleF, TSampleI16>(const std::string & fname, TWaveformT<TSampleI16> & res);
template bool readFromFile<TSampleF, TSampleF>(const std::string & fname, TWaveformT<TSampleF> & res);
template <typename TSampleInput, typename TSample>
bool readFromFile(const std::string & fname, TWaveformT<TSample> & res, TTrainKeys & trainKeys, int32_t & bufferSize_frames) {
trainKeys.clear();
std::ifstream fin(fname, std::ios::binary);
if (fin.good() == false) {
return false;
}
bufferSize_frames = 1;
fin.read((char *)(&bufferSize_frames), sizeof(bufferSize_frames));
{
static_assert(std::is_same<TSampleInput, TSampleF>::value, "TSampleInput not supported");
static_assert(std::is_same<TSample, TSampleF>::value ||
std::is_same<TSample, TSampleI16>::value, "TSample not supported");
int32_t offset = 0;
std::streamsize size = bufferSize_frames*kSamplesPerFrame*sizeof(TSampleInput);
while (true) {
TKey keyPressed = 0;
fin.read((char *)(&keyPressed), sizeof(keyPressed));
if (fin.eof()) break;
trainKeys.push_back(keyPressed);
if (readWaveform<TSampleInput>(fin, res, offset, size) == false) {
return false;
}
offset += size/sizeof(TSampleInput);
if (fin.eof()) break;
}
}
fin.close();
return true;
}
template bool readFromFile<TSampleF, TSampleI16>(const std::string & fname, TWaveformT<TSampleI16> & res, TTrainKeys & trainKeys, int32_t & bufferSize_frames);
//
// filters
//
TFilterCoefficients calculateCoefficientsFirstOrderHighPass(int fc, int fs) {
TFilterCoefficients res;
float th = 2.0 * pi * fc / fs;
float g = cos(th) / (1.0 + sin(th));
res.a0 = (1.0 + g) / 2.0;
res.a1 = -((1.0 + g) / 2.0);
res.a2 = 0.0;
res.b1 = -g;
res.b2 = 0.0;
return res;
}
TFilterCoefficients calculateCoefficientsSecondOrderButterworthHighPass(int fc, int fs) {
TFilterCoefficients res;
float c = tan(pi*fc / fs);
res.a0 = 1.0 / (1.0 + sqrt2*c + pow(c, 2.0));
res.a1 = -2.0 * res.a0;
res.a2 = res.a0;
res.b1 = 2.0 * res.a0*(pow(c, 2.0) - 1.0);
res.b2 = res.a0 * (1.0 - sqrt2*c + pow(c, 2.0));
return res;
}
TSampleF filterFirstOrderHighPass(TFilterCoefficients & coefficients, TSampleF sample) {
TSampleF xn = sample;
TSampleF yn =
coefficients.a0*xn + coefficients.a1*coefficients.xnz1 + coefficients.a2*coefficients.xnz2 -
coefficients.b1*coefficients.ynz1 - coefficients.b2*coefficients.ynz2;
coefficients.xnz2 = coefficients.xnz1;
coefficients.xnz1 = xn;
coefficients.ynz2 = coefficients.ynz1;
coefficients.ynz1 = yn;
return yn;
}
TSampleF filterSecondOrderButterworthHighPass(TFilterCoefficients & coefficients, TSampleF sample) {
TSampleF xn = sample;
TSampleF yn =
coefficients.a0*xn + coefficients.a1*coefficients.xnz1 + coefficients.a2*coefficients.xnz2 -
coefficients.b1*coefficients.ynz1 - coefficients.b2*coefficients.ynz2;
coefficients.xnz2 = coefficients.xnz1;
coefficients.xnz1 = xn;
coefficients.ynz2 = coefficients.ynz1;
coefficients.ynz1 = yn;
return yn;
}
//
// calcCC
//
std::tuple<double, double> calcSum(const TKeyWaveformF & waveform, int is0, int is1) {
double sum = 0.0f;
double sum2 = 0.0f;
for (int is = is0; is < is1; ++is) {
auto a0 = waveform[is];
sum += a0;
sum2 += a0*a0;
}
return std::tuple<double, double>(sum, sum2);
}
template<>
std::tuple<int64_t, int64_t> calcSum(const TWaveformViewT<TSampleMI16> & waveform) {
int64_t sum = 0;
int64_t sum2 = 0;
auto samples = waveform.samples;
auto n = waveform.n;
for (int is = 0; is < n; ++is) {
for (int j = 0; j < TSampleMI16::N; j++) {
int32_t a0 = samples[is][j];
sum += a0;
sum2 += a0*a0;
}
}
return std::tuple<int64_t, int64_t>(sum, sum2);
}
template<typename T>
std::tuple<int64_t, int64_t> calcSum(const TWaveformViewT<T> & waveform) {
int64_t sum = 0;
int64_t sum2 = 0;
auto samples = waveform.samples;
auto n = waveform.n;
for (int is = 0; is < n; ++is) {
int32_t a0 = samples[is];
sum += a0;
sum2 += a0*a0;
}
return std::tuple<int64_t, int64_t>(sum, sum2);
}
// calcSum : specializations
template std::tuple<int64_t, int64_t> calcSum<TSampleI16>(const TWaveformViewT<TSampleI16> & waveform);
//
// calcCC
//
TValueCC calcCC(
const TKeyWaveformF & waveform0,
const TKeyWaveformF & waveform1,
double sum0, double sum02,
int is00, int is0, int is1) {
TValueCC cc = -1.0f;
double sum1 = 0.0f;
double sum12 = 0.0f;
double sum01 = 0.0f;
for (int is = 0; is < is1 - is0; ++is) {
auto a0 = waveform0[is00 + is];
auto a1 = waveform1[is0 + is];
#ifdef MY_DEBUG
if (is00 + is < 0 || is00 + is >= waveform0.size()) printf("BUG 0\n");
if (is0 + is < 0 || is0 + is >= waveform1.size()) {
printf("BUG 1\n");
printf("%d %d %d\n", is0, is, (int) waveform1.size());
}
#endif
sum1 += a1;
sum12 += a1*a1;
sum01 += a0*a1;
}
int ncc = (is1 - is0);
{
double nom = sum01*ncc - sum0*sum1;
double den2a = sum02*ncc - sum0*sum0;
double den2b = sum12*ncc - sum1*sum1;
cc = (nom)/(sqrt(den2a*den2b));
}
return cc;
}
template<>
TValueCC calcCC(
const TWaveformViewT<TSampleMI16> & waveform0,
const TWaveformViewT<TSampleMI16> & waveform1,
int64_t sum0, int64_t sum02) {
TValueCC cc = -1.0f;
int64_t sum1 = 0;
int64_t sum12 = 0;
int64_t sum01 = 0;
auto samples0 = waveform0.samples;
auto n0 = waveform0.n;
auto samples1 = waveform1.samples;
auto n1 = waveform1.n;
#ifdef MY_DEBUG
if (n0 != n1) {
printf("BUG 234f8273\n");
}
#endif
auto n = std::min(n0, n1);
for (int64_t is = 0; is < n; ++is) {
for (int j = 0; j < TSampleMI16::N; j++) {
int32_t a0 = samples0[is][j];
int32_t a1 = samples1[is][j];
sum1 += a1;
sum12 += a1*a1;
sum01 += a0*a1;
}
}
n *= TSampleMI16::N;
{
double nom = sum01*n - sum0*sum1;
double den2a = sum02*n - sum0*sum0;
double den2b = sum12*n - sum1*sum1;
cc = (nom)/(sqrt(den2a*den2b));
}
return cc;
}
template<typename T>
TValueCC calcCC(
const TWaveformViewT<T> & waveform0,
const TWaveformViewT<T> & waveform1,
int64_t sum0, int64_t sum02) {
TValueCC cc = -1.0f;
int64_t sum1 = 0;
int64_t sum12 = 0;
int64_t sum01 = 0;
auto samples0 = waveform0.samples;
auto n0 = waveform0.n;
auto samples1 = waveform1.samples;
auto n1 = waveform1.n;
#ifdef MY_DEBUG
if (n0 != n1) {
printf("BUG 234f8273\n");
}
#endif
auto n = std::min(n0, n1);
for (int64_t is = 0; is < n; ++is) {
int32_t a0 = samples0[is];
int32_t a1 = samples1[is];
sum1 += a1;
sum12 += a1*a1;
sum01 += a0*a1;
}
{
double nom = sum01*n - sum0*sum1;
double den2a = sum02*n - sum0*sum0;
double den2b = sum12*n - sum1*sum1;
cc = (nom)/(sqrt(den2a*den2b));
}
return cc;
}
// calcCC : specializations
template TValueCC calcCC<TSampleI16>(
const TWaveformViewT<TSampleI16> & waveform0,
const TWaveformViewT<TSampleI16> & waveform1,
int64_t sum0, int64_t sum02);
//
// findBestCC
//
std::tuple<TValueCC, TOffset> findBestCC(
const TKeyWaveformF & waveform0,
const TKeyWaveformF & waveform1,
int is0, int is1,
int alignWindow) {
TOffset besto = -1;
TValueCC bestcc = -1.0f;
int is00 = waveform0.size()/2 - (is1 - is0)/2;
auto ret = calcSum(waveform0, is00, is00 + is1 - is0);
auto sum0 = std::get<0>(ret);
auto sum02 = std::get<1>(ret);
#ifdef __EMSCRIPTEN__
int nWorkers = std::min(4, std::max(1, int(std::thread::hardware_concurrency()) - 2));
#else
int nWorkers = std::min(4u, std::thread::hardware_concurrency());
std::mutex mutex;
std::vector<std::thread> workers(nWorkers);
for (int i = 0; i < (int) workers.size(); ++i) {
auto & worker = workers[i];
worker = std::thread([&, sum0 = sum0, sum02 = sum02, i]() {
TOffset cbesto = -1;
TValueCC cbestcc = -1.0f;
for (int o = -alignWindow + i; o <= alignWindow; o += nWorkers) {
auto cc = calcCC(waveform0, waveform1, sum0, sum02, is00, is0 + o, is1 + o);
if (cc > cbestcc) {
cbesto = o;
cbestcc = cc;
}
}
{
std::lock_guard<std::mutex> lock(mutex);
if (cbestcc > bestcc) {
bestcc = cbestcc;
besto = cbesto;
}
}
});
}
for (auto & worker : workers) worker.join();
#endif
return std::tuple<TValueCC, TOffset>(bestcc, besto);
}
template<typename T>
std::tuple<TValueCC, TOffset> findBestCC(
const TWaveformViewT<T> & waveform0,
const TWaveformViewT<T> & waveform1,
int64_t alignWindow) {
TValueCC bestcc = -1.0;
TOffset besto = -1;
//auto samples0 = waveform0.samples;
auto n0 = waveform0.n;
auto samples1 = waveform1.samples;
#ifdef MY_DEBUG
auto n1 = waveform1.n;
if (n0 + 2*alignWindow != n1) {
printf("BUG 924830jm92, n0 = %d, a = %d\n", (int) n0, (int) alignWindow);
}
#endif
auto ret = calcSum(waveform0);
auto sum0 = std::get<0>(ret);
auto sum02 = std::get<1>(ret);
for (int o = 0; o <= 2*alignWindow; ++o) {
auto cc = calcCC(waveform0, { samples1 + o, n0 }, sum0, sum02);
if (cc > bestcc) {
besto = o - alignWindow;
bestcc = cc;
}
}
return std::tuple<TValueCC, TOffset>(bestcc, besto);
}
// findBestCC : specializations
template std::tuple<TValueCC, TOffset> findBestCC<TSampleI16>(
const TWaveformViewT<TSampleI16> & waveform0,
const TWaveformViewT<TSampleI16> & waveform1,
int64_t alignWindow);
//
// calculateSimilarityMap
//
template<>
bool calculateSimilartyMap(
const int32_t keyPressWidth_samples,
const int32_t alignWindow_samples,
const int32_t offsetFromPeak_samples,
TKeyPressCollectionT<TSampleMI16> & keyPresses,
TSimilarityMap & res) {
int nPresses = keyPresses.size();
int w = keyPressWidth_samples;
int a = alignWindow_samples;
res.clear();
res.resize(nPresses);
for (auto & x : res) x.resize(nPresses);
#ifdef __EMSCRIPTEN__
int nWorkers = std::min(kMaxThreads, std::max(1, int(std::thread::hardware_concurrency()) - 2));
#else
int nWorkers = std::thread::hardware_concurrency();
#endif
std::vector<std::thread> workers(nWorkers);
for (int iw = 0; iw < (int) workers.size(); ++iw) {
auto & worker = workers[iw];
worker = std::thread([&](int ith) {
for (int i = ith; i < nPresses; i += nWorkers) {
res[i][i].cc = 1.0f;
res[i][i].offset = 0;
const auto & waveform0 = keyPresses[i].waveform;
const auto & pos0 = keyPresses[i].pos;
auto & avgcc = keyPresses[i].ccAvg;
const auto samples0 = waveform0.samples;
for (int j = i + 1; j < nPresses; ++j) {
if (i == j) continue;
const auto waveform1 = keyPresses[j].waveform;
const auto pos1 = keyPresses[j].pos;
const auto samples1 = waveform1.samples;
const auto ret = findBestCC(TWaveformViewT<TSampleMI16> { samples0 + pos0 + offsetFromPeak_samples - w, 2*w },
TWaveformViewT<TSampleMI16> { samples1 + pos1 + offsetFromPeak_samples - w - a, 2*w + 2*a }, a);
const auto bestcc = std::get<0>(ret);
const auto bestoffset = std::get<1>(ret);
res[i][j].cc = bestcc;
res[i][j].offset = bestoffset;
res[j][i].cc = bestcc;
res[j][i].offset = -bestoffset;
avgcc += bestcc;
}
avgcc /= (nPresses - 1);
}
}, iw);
}
for (auto & worker : workers) worker.join();
return true;
}
template<typename T>
bool calculateSimilartyMap(
const int32_t keyPressWidth_samples,
const int32_t alignWindow_samples,
const int32_t offsetFromPeak_samples,
TKeyPressCollectionT<T> & keyPresses,
TSimilarityMap & res) {
int nPresses = keyPresses.size();
int w = keyPressWidth_samples;
int a = alignWindow_samples;
res.clear();
res.resize(nPresses);
for (auto & x : res) x.resize(nPresses);
#ifdef __EMSCRIPTEN__
int nWorkers = std::min(kMaxThreads, std::max(1, int(std::thread::hardware_concurrency()) - 2));
#else
int nWorkers = std::thread::hardware_concurrency();
#endif
std::vector<std::thread> workers(nWorkers);
for (int iw = 0; iw < (int) workers.size(); ++iw) {
auto & worker = workers[iw];
worker = std::thread([&](int ith) {
for (int i = ith; i < nPresses; i += nWorkers) {
res[i][i].cc = 1.0f;
res[i][i].offset = 0;
const auto & waveform0 = keyPresses[i].waveform;
const auto & pos0 = keyPresses[i].pos;
auto & avgcc = keyPresses[i].ccAvg;
const auto samples0 = waveform0.samples;
for (int j = i + 1; j < nPresses; ++j) {
if (i == j) continue;
const auto waveform1 = keyPresses[j].waveform;
const auto pos1 = keyPresses[j].pos;
const auto samples1 = waveform1.samples;
const auto ret = findBestCC(TWaveformViewT<T> { samples0 + pos0 + offsetFromPeak_samples - w, 2*w },
TWaveformViewT<T> { samples1 + pos1 + offsetFromPeak_samples - w - a, 2*w + 2*a }, a);
const auto bestcc = std::get<0>(ret);
const auto bestoffset = std::get<1>(ret);
res[i][j].cc = bestcc;
res[i][j].offset = bestoffset;
res[j][i].cc = bestcc;
res[j][i].offset = -bestoffset;
avgcc += bestcc;
}
avgcc /= (nPresses - 1);
}
}, iw);
}
for (auto & worker : workers) worker.join();
return true;
}
template bool calculateSimilartyMap<TSampleI16>(
const int32_t keyPressWidth_samples,
const int32_t alignWindow_samples,
const int32_t offsetFromPeak_samples,
TKeyPressCollectionT<TSampleI16> & keyPresses,
TSimilarityMap & res);
//
// findKeyPresses
//
template<>
bool findKeyPresses(
const TWaveformViewT<TSampleMI16> & waveform,
TKeyPressCollectionT<TSampleMI16> & res,
TWaveformT<TSampleMI16> & waveformThreshold,
TWaveformT<TSampleMI16> & waveformMax,
double thresholdBackground,
int historySize,
int historySizeReset,
bool removeLowPower) {
res.clear();
waveformThreshold.resize(waveform.n);
waveformMax.resize(waveform.n);
int rbBegin = 0;
double rbAverage = 0.0;
std::vector<double> rbSamples(8*historySize, 0.0);
int k = historySize;
std::deque<int64_t> que(k);
auto samples = waveform.samples;
auto n = waveform.n;
TWaveformT<TSampleMI16> waveformAbs(n);
for (int64_t i = 0; i < n; ++i) {
waveformAbs[i][0] = std::abs(samples[i][0]);
}
for (int64_t i = 0; i < n; ++i) {
{
int64_t ii = i - k/2;
if (ii >= 0) {
rbAverage *= rbSamples.size();
rbAverage -= rbSamples[rbBegin];
double acur = waveformAbs[i][0];
rbSamples[rbBegin] = acur;
rbAverage += acur;
rbAverage /= rbSamples.size();
if (++rbBegin >= (int) rbSamples.size()) {
rbBegin = 0;
}
}
}
if (i < k) {
while((!que.empty()) && waveformAbs[i] >= waveformAbs[que.back()]) {
que.pop_back();
}
que.push_back(i);
} else {
while((!que.empty()) && que.front() <= i - k) {
que.pop_front();
}
while((!que.empty()) && waveformAbs[i] >= waveformAbs[que.back()]) {
que.pop_back();
}
que.push_back(i);
int64_t itest = i - k/2;
if (itest >= 2*k && itest < n - 2*k && que.front() == itest) {
double acur = waveformAbs[itest][0];
if (acur > thresholdBackground*rbAverage) {
res.emplace_back(TKeyPressDataT<TSampleMI16> { std::move(waveform), itest, 0.0, -1, -1, '?' });
}
}
waveformThreshold[itest][0] = thresholdBackground*rbAverage;
waveformMax[itest] = waveformAbs[que.front()];
}
}
if (removeLowPower) {
while (true) {
auto oldn = res.size();
double avgPower = 0.0;
for (const auto & kp : res) {
avgPower += waveformAbs[kp.pos][0];
}
avgPower /= res.size();
auto tmp = std::move(res);
for (const auto & kp : tmp) {
if (waveformAbs[kp.pos][0] > 0.3*avgPower) {
res.push_back(kp);
}
}
if (res.size() == oldn) break;
}
}
if (res.size() > 1) {
TKeyPressCollectionT<TSampleMI16> res2;
res2.push_back(res.front());
for (int i = 1; i < (int) res.size(); ++i) {
if (res[i].pos - res2.back().pos > historySizeReset || waveformMax[res[i].pos] > waveformMax[res2.back().pos]) {
res2.push_back(res[i]);
}
}
std::swap(res, res2);
}
return true;
}
template<typename T>
bool findKeyPresses(
const TWaveformViewT<T> & waveform,
TKeyPressCollectionT<T> & res,
TWaveformT<T> & waveformThreshold,
TWaveformT<T> & waveformMax,
double thresholdBackground,
int historySize,
int historySizeReset,
bool removeLowPower) {
res.clear();
waveformThreshold.resize(waveform.n);
waveformMax.resize(waveform.n);
int rbBegin = 0;
double rbAverage = 0.0;
std::vector<double> rbSamples(8*historySize, 0.0);
int k = historySize;
std::deque<int64_t> que(k);
auto samples = waveform.samples;
auto n = waveform.n;
TWaveformT<T> waveformAbs(n);
for (int64_t i = 0; i < n; ++i) {
waveformAbs[i] = std::abs(samples[i]);
}
for (int64_t i = 0; i < n; ++i) {
{
int64_t ii = i - k/2;
if (ii >= 0) {
rbAverage *= rbSamples.size();
rbAverage -= rbSamples[rbBegin];
double acur = waveformAbs[i];
rbSamples[rbBegin] = acur;
rbAverage += acur;
rbAverage /= rbSamples.size();
if (++rbBegin >= (int) rbSamples.size()) {
rbBegin = 0;
}
}
}
if (i < k) {
while((!que.empty()) && waveformAbs[i] >= waveformAbs[que.back()]) {
que.pop_back();
}
que.push_back(i);
} else {
while((!que.empty()) && que.front() <= i - k) {
que.pop_front();
}
while((!que.empty()) && waveformAbs[i] >= waveformAbs[que.back()]) {
que.pop_back();
}
que.push_back(i);
int64_t itest = i - k/2;
if (itest >= 2*k && itest < n - 2*k && que.front() == itest) {
double acur = waveformAbs[itest];
if (acur > thresholdBackground*rbAverage) {
res.emplace_back(TKeyPressDataT<T> { std::move(waveform), itest, 0.0, -1, -1, '?' });
}
}
waveformThreshold[itest] = thresholdBackground*rbAverage;
waveformMax[itest] = waveformAbs[que.front()];
}
}
if (removeLowPower) {
while (true) {
auto oldn = res.size();
double avgPower = 0.0;
for (const auto & kp : res) {
avgPower += waveformAbs[kp.pos];
}
avgPower /= res.size();
auto tmp = std::move(res);
for (const auto & kp : tmp) {
if (waveformAbs[kp.pos] > 0.3*avgPower) {
res.push_back(kp);
}
}
if (res.size() == oldn) break;
}
}
if (res.size() > 1) {
TKeyPressCollectionT<T> res2;
res2.push_back(res.front());
for (int i = 1; i < (int) res.size(); ++i) {
if (res[i].pos - res2.back().pos > historySizeReset || waveformMax[res[i].pos] > waveformMax[res2.back().pos]) {
res2.push_back(res[i]);
}
}
std::swap(res, res2);
}
return true;
}
template bool findKeyPresses<TSampleI16>(
const TWaveformViewT<TSampleI16> & waveform,
TKeyPressCollectionT<TSampleI16> & res,
TWaveformT<TSampleI16> & waveformThreshold,
TWaveformT<TSampleI16> & waveformMax,
double thresholdBackground,
int historySize,
int historySizeReset,
bool removeLowPower);
template<typename T>
bool saveKeyPresses(const std::string & fname, const TKeyPressCollectionT<T> & keyPresses) {
std::ofstream fout(fname, std::ios::binary);
int n = keyPresses.size();
fout.write((char *)(&n), sizeof(n));
for (int i = 0; i < n; ++i) {
fout.write((char *)(&keyPresses[i].pos), sizeof(keyPresses[i].pos));
}
fout.close();
return true;
}
template bool saveKeyPresses<TSampleI16>(const std::string & fname, const TKeyPressCollectionT<TSampleI16> & keyPresses);
template<typename T>
bool loadKeyPresses(const std::string & fname, const TWaveformViewT<T> & waveform, TKeyPressCollectionT<T> & keyPresses) {
keyPresses.clear();
std::ifstream fin(fname, std::ios::binary);
int n = 0;
fin.read((char *)(&n), sizeof(n));
keyPresses.resize(n);
for (int i = 0; i < n; ++i) {
keyPresses[i].waveform = waveform;
fin.read((char *)(&keyPresses[i].pos), sizeof(keyPresses[i].pos));
}
fin.close();
return true;
}