forked from ggerganov/kbd-audio
-
Notifications
You must be signed in to change notification settings - Fork 0
/
record.cpp
88 lines (70 loc) · 2.51 KB
/
record.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
/*! \file record.cpp
* \brief Enter description here.
* \author Georgi Gerganov
*/
#include "constants.h"
#include "common.h"
#include "audio_logger.h"
#include <stdio.h>
#include <termios.h>
#include <unistd.h>
#include <map>
#include <cstdio>
#include <chrono>
#include <thread>
#include <fstream>
int main(int argc, char ** argv) {
printf("Usage: %s output.kbd [-cN]\n", argv[0]);
printf(" -cN - select capture device N\n");
printf("\n");
if (argc < 2) {
return -127;
}
auto argm = parseCmdArguments(argc, argv);
int captureId = argm["c"].empty() ? 0 : std::stoi(argm["c"]);
auto tStart = std::chrono::high_resolution_clock::now();
auto tEnd = std::chrono::high_resolution_clock::now();
size_t totalSize_bytes = 0;
int keyPressed = -1;
std::map<int, int> nTimes;
printf("Recording %d frames per key press\n", kTrainBufferSize_frames);
std::ofstream fout(argv[1], std::ios::binary);
fout.write((char *)(&kTrainBufferSize_frames), sizeof(kTrainBufferSize_frames));
AudioLogger audioLogger;
AudioLogger::Callback cbAudio = [&](const auto & frames) {
tEnd = std::chrono::high_resolution_clock::now();
fout.write((char *)(&keyPressed), sizeof(keyPressed));
for (const auto & frame : frames) {
totalSize_bytes += sizeof(frame[0])*frame.size();
fout.write((char *)(frame.data()), sizeof(frame[0])*frame.size());
fout.flush();
}
++nTimes[keyPressed];
printf("Last recorded key - %3d '%s'. Total times recorded so far - %3d. Total data saved: %g MB\n",
keyPressed, kKeyText.at(keyPressed), nTimes[keyPressed], ((float)(totalSize_bytes)/1024.0f/1024.0f));
keyPressed = -1;
};
if (audioLogger.install(kSampleRate, cbAudio, captureId) == false) {
fprintf(stderr, "Failed to install audio logger\n");
return -1;
}
std::thread keyReader = std::thread([&]() {
struct termios oldt, newt;
tcgetattr ( STDIN_FILENO, &oldt );
newt = oldt;
newt.c_lflag &= ~( ICANON | ECHO );
tcsetattr ( STDIN_FILENO, TCSANOW, &newt );
while (true) {
int key = getchar();
tStart = std::chrono::high_resolution_clock::now();
if (keyPressed == -1) {
keyPressed = key;
audioLogger.record(kTrainBufferSize_s);
}
}
tcsetattr ( STDIN_FILENO, TCSANOW, &oldt );
});
while (true) {}
fout.close();
return 0;
}