forked from ggerganov/kbd-audio
-
Notifications
You must be signed in to change notification settings - Fork 0
/
common.cpp
648 lines (524 loc) · 18 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
/*! \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>
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 = 0.0f;
for (auto i = 0; i < (int) buf.size(); ++i) if (std::abs(buf[i]) > amax) amax = std::abs(buf[i]);
for (auto i = 0; i < (int) buf.size(); ++i) res[offset + i] = std::round(std::numeric_limits<TSampleI16>::max()*(buf[i]/amax));
} 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;
}
}
float frand() { return ((float)rand())/RAND_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;
}
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 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 <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);
//
// 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<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) {
auto 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<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) {
auto a0 = samples0[is];
auto 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__
TOffset cbesto = -1;
TValueCC cbestcc = -1.0f;
for (int o = -alignWindow; o < alignWindow; ++o) {
auto cc = calcCC(waveform0, waveform1, sum0, sum02, is00, is0 + o, is1 + o);
if (cc > cbestcc) {
cbesto = o;
cbestcc = cc;
}
}
if (cbestcc > bestcc) {
bestcc = cbestcc;
besto = cbesto;
}
#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<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);
for (int i = 0; i < nPresses; ++i) {
res[i][i].cc = 1.0f;
res[i][i].offset = 0;
auto & waveform0 = keyPresses[i].waveform;
auto & pos0 = keyPresses[i].pos;
auto & avgcc = keyPresses[i].ccAvg;
auto samples0 = waveform0.samples;
//auto n0 = waveform0.n;
for (int j = 0; j < nPresses; ++j) {
if (i == j) continue;
auto waveform1 = keyPresses[j].waveform;
auto pos1 = keyPresses[j].pos;
auto samples1 = waveform1.samples;
auto ret = findBestCC(TWaveformViewT<T> { samples0 + pos0 + offsetFromPeak_samples, 2*w },
TWaveformViewT<T> { samples1 + pos1 + offsetFromPeak_samples - a, 2*w + 2*a }, a);
auto bestcc = std::get<0>(ret);
auto bestoffset = std::get<1>(ret);
res[i][j].cc = bestcc;
res[i][j].offset = bestoffset;
avgcc += bestcc;
}
avgcc /= (nPresses - 1);
}
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<typename T>
bool findKeyPresses(
const TWaveformViewT<T> & waveform,
TKeyPressCollectionT<T> & res,
TWaveformT<T> & waveformThreshold,
double thresholdBackground,
int historySize) {
res.clear();
waveformThreshold.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] = waveformAbs[que.front()];
}
}
return true;
}
template bool findKeyPresses<TSampleI16>(
const TWaveformViewT<TSampleI16> & waveform,
TKeyPressCollectionT<TSampleI16> & res,
TWaveformT<TSampleI16> & waveformThreshold,
double thresholdBackground,
int historySize);
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;
}
template bool loadKeyPresses<TSampleI16>(const std::string & fname, const TWaveformViewT<TSampleI16> & waveform, TKeyPressCollectionT<TSampleI16> & keyPresses);
template<typename T>
bool dumpKeyPresses(const std::string & fname, const TKeyPressCollectionT<T> & data) {
std::ofstream fout(fname);
for (auto & k : data) {
fout << k.pos << " 1" << std::endl;
}
fout.close();
return true;
}
template bool dumpKeyPresses<TSampleI16>(const std::string & fname, const TKeyPressCollectionT<TSampleI16> & data);
template<typename TSample>
void cbPlayback(void * userData, uint8_t * stream, int len) {
TPlaybackDataT<TSample> * data = (TPlaybackDataT<TSample> *)(userData);
if (data->playing == false) {
int offset = 0;
TSample a = 0;
while (len > 0) {
memcpy(stream + offset*sizeof(a), &a, sizeof(a));
len -= sizeof(a);
++offset;
}
return;
}
auto end = std::min(data->idx + TPlaybackDataT<TSample>::kSamples/data->slowDown, data->waveform.n);
auto idx = data->idx;
auto sidx = 0;
for (; idx < end; ++idx) {
TSample a = data->waveform.samples[idx];
memcpy(stream + (sidx)*sizeof(a), &a, sizeof(a));
len -= sizeof(a);
++sidx;
if (data->slowDown == 2) {
TSample a2 = data->waveform.samples[idx + 1];
a = 0.5*a + 0.5*a2;
memcpy(stream + (sidx)*sizeof(a), &a, sizeof(a));
len -= sizeof(a);
++sidx;
}
}
while (len > 0) {
TSample a = 0;
memcpy(stream + (idx - data->idx)*sizeof(a), &a, sizeof(a));
len -= sizeof(a);
++idx;
}
data->idx = idx;
}
template void cbPlayback<TSampleI16>(void * userData, uint8_t * stream, int len);
template<typename T>
bool generateLowResWaveform(const TWaveformViewT<T> & waveform, TWaveformT<T> & waveformLowRes, int nWindow) {
waveformLowRes.resize(waveform.n);
int k = nWindow;
std::deque<int64_t> que(k);
//auto [samples, n] = waveform;
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) {
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;
waveformLowRes[itest] = waveformAbs[que.front()];
}
}
return true;
}
template bool generateLowResWaveform<TSampleI16>(const TWaveformViewT<TSampleI16> & waveform, TWaveformT<TSampleI16> & waveformLowRes, int nWindow);