Tempo Tapper  1.0.0
A simple library to implement a tempo tapper
tempo_tapper_common.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 
32 #include <stdlib.h>
33 #include <stddef.h>
34 
35 #include <tempo_tapper.h>
36 
37 unsigned long tt_period_us(tempo_tapper *tapper)
38 {
39  if (tapper->taps < 1)
40  return 0;
41 
42  unsigned long us = time_to_us(&tapper->prd_sum);
43  return us/tapper->taps;
44 }
45 
46 void tt_tap(tempo_tapper *tapper)
47 {
48  tt_time_t c_time;
49  current_time(&c_time);
50 
51  if (tapper->taps >= 0) {
52  tt_time_t tdiff;
53  sub_time(&c_time, &tapper->lst_t, &tdiff);
54  add_time(&tapper->prd_sum, &tdiff, &tapper->prd_sum);
55  }
56 
57  tapper->taps++;
58  tapper->lst_t = c_time;
59 }
60 
62 {
63  tempo_tapper *tapper = (tempo_tapper *) malloc(sizeof(tempo_tapper));
64 
65  if (tapper == NULL)
66  return NULL;
67 
68  tt_reset(tapper);
69  return tapper;
70 }
71 
72 BPM_t tt_bpm(tempo_tapper *tapper)
73 {
74  unsigned long us = tt_period_us(tapper);
75 
76  if (us == 0)
77  return 0;
78 
79 
80  return (60 * S_TO_US)/(BPM_t)tt_period_us(tapper);
81 }
82 
83 void tt_reset(tempo_tapper *tapper)
84 {
85  tapper->taps = -1;
86  reset_time(&tapper->prd_sum);
87 }
tt_tap
void tt_tap(tempo_tapper *tapper)
"Taps" the tempo tapper
Definition: tempo_tapper_common.cxx:46
reset_time
void reset_time(tt_time_t *time)
Resets a time var to 0.
add_time
void add_time(tt_time_t *a, tt_time_t *b, tt_time_t *res)
Adds two time values.
tt_reset
void tt_reset(tempo_tapper *tapper)
Resets the tempo tapper.
Definition: tempo_tapper_common.cxx:83
tempo_tapper::prd_sum
tt_time_t prd_sum
Holds the sum of all measured/"tapped" periods.
Definition: tempo_tapper.h:89
tempo_tapper::lst_t
tt_time_t lst_t
Hold the clock time of the last tap.
Definition: tempo_tapper.h:90
tempo_tapper::taps
int taps
Number of taps. The inital val is -1, meaning the 1st tap does not count.
Definition: tempo_tapper.h:91
tt_new
tempo_tapper * tt_new()
Creates a new tempo tapper instance.
Definition: tempo_tapper_common.cxx:61
sub_time
void sub_time(tt_time_t *a, tt_time_t *b, tt_time_t *res)
Subtracts two time values.
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
time_to_us
unsigned long time_to_us(tt_time_t *time)
Returns a time value in microseconds.
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
current_time
void current_time(tt_time_t *time)
Receives the current clock time.
tempo_tapper.h
Provides all necessary structs and functions to implement a tempo tapper.