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
|
// create a node command line listener that allows the user to press any key , and averages the distances between the key presses to determine the BPM (with a window of 4 key presses). If the user presses q, the program should exit and print the final BPM.
// Import the necessary modules
import * as readline from "readline";
export function tapBpm() {
// Set up readline interface to listen for keypresses
readline.emitKeypressEvents(process.stdin);
process.stdin.setRawMode(true);
// accept SIGINT on stdin
// Array to store the time differences between the last 4 key presses
const timeDifferences: number[] = [];
let lastPressTime: number | null = null;
// Function to calculate BPM based on average time between keypresses
function calculateBPM() {
if (timeDifferences.length < 1) {
return 0;
}
const averageTimeDiff =
timeDifferences.reduce((acc, curr) => acc + curr, 0) /
timeDifferences.length;
return (60 * 1000) / averageTimeDiff;
}
// Handle the SIGINT (Ctrl+C) event manually
process.on("SIGINT", () => {
console.log(
"\nExiting via SIGINT (Ctrl+C)... Final BPM:",
calculateBPM().toFixed(2)
);
process.exit();
});
// Listen for keypress events
process.stdin.on("keypress", (str, key) => {
// Exit if 'q' is pressed
if (key.name === "q") {
console.log("Exiting... Final BPM:", calculateBPM().toFixed(2));
process.exit();
}
// Handle Ctrl+C (SIGINT)
if (key.sequence === "\u0003") {
// '\u0003' is the raw code for Ctrl+C
console.log(
"\nExiting via Ctrl+C... Final BPM:",
calculateBPM().toFixed(2)
);
process.exit();
}
// Capture the current time of the keypress
const currentTime = Date.now();
// If it's not the first keypress, calculate the time difference
if (lastPressTime !== null) {
const timeDiff = currentTime - lastPressTime;
// Add the time difference to the array (limit to last 10 key presses)
if (timeDifferences.length >= 10) {
timeDifferences.shift(); // Remove the oldest time difference
}
timeDifferences.push(timeDiff);
// Calculate and display the BPM
const bpm = calculateBPM();
console.log("Current BPM:", bpm.toFixed(2));
} else {
console.log("Waiting for more key presses to calculate BPM...");
}
// Update the lastPressTime to the current time
lastPressTime = currentTime;
});
console.log('Press any key to calculate BPM, press "q" to quit.');
}
|