Tempo Tapper  1.0.0
A simple library to implement a tempo tapper
term_tt_posix.cxx
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2021 Patrick Pedersen
3 
4  * This program is free software: you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation, either version 3 of the License, or
7  * (at your option) any later version.
8 
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU General Public License for more details.
13 
14  * You should have received a copy of the GNU General Public License
15  * along with this program. If not, see <https://www.gnu.org/licenses/>.
16  *
17  */
18 
49 #include <unistd.h>
50 #include <stdlib.h>
51 #include <stdio.h>
52 #include <curses.h>
53 
54 #include <tempo_tapper.h>
55 
56 int main()
57 {
58  tempo_tapper *tt = tt_new(); // Create new tempo tapper instance
59 
60  // Check if malloc failed
61  if (tt == NULL) {
62  free(tt);
63  printf("term_tt: Failed to create a new tempo tapper instance!\n");
64  return EXIT_FAILURE;
65  }
66 
67  // Initialize ncurses
68  initscr();
69  timeout(-1);
70  curs_set(0);
71 
72  char input; // Keyboard input
73 
74  do {
75  clear();
76  printw("Use the enter key to tap a tempo. Press q to quit.\n");
77  refresh();
78 
79  while (1) {
80  input = getchar();
81  clear();
82 
83  if (input == 'q' || input == 'r')
84  break;
85  else if (input == '\r' || input == '\n')
86  tt_tap(tt); // Newline received, tap!
87  else
88  printw("Invalid input!\n");
89 
90  // Get tempo, using tt_bpm(), and tempo period using tt_preiod_us(), and print it out!
91  printw("Tempo: %.2f BPM, Period: %.2fms\n", tt_bpm(tt), float(tt_period_us(tt))/1000);
92  printw("Press r to reset, press q to quit.\n");
93  refresh();
94  }
95 
96  tt_reset(tt); // Reset tempo tapper
97  } while(input != 'q');
98 
99  endwin();
100  free(tt);
101  return 0;
102 }
tt_tap
void tt_tap(tempo_tapper *tapper)
"Taps" the tempo tapper
Definition: tempo_tapper_common.cxx:46
tt_reset
void tt_reset(tempo_tapper *tapper)
Resets the tempo tapper.
Definition: tempo_tapper_common.cxx:83
tt_new
tempo_tapper * tt_new()
Creates a new tempo tapper instance.
Definition: tempo_tapper_common.cxx:61
tempo_tapper
Tempo tapper struct.
Definition: tempo_tapper.h:87
tt_bpm
BPM_t tt_bpm(tempo_tapper *tapper)
Returns the tempo in BPM.
Definition: tempo_tapper_common.cxx:72
tt_period_us
unsigned long tt_period_us(tempo_tapper *tapper)
Returns the period of a tempo in microseconds.
Definition: tempo_tapper_common.cxx:37
tempo_tapper.h
Provides all necessary structs and functions to implement a tempo tapper.