Tempo Tapper  1.0.0
A simple library to implement a tempo tapper
Classes | Macros | Typedefs | Functions
tempo_tapper.h File Reference

Provides all necessary structs and functions to implement a tempo tapper. More...

#include <stdint.h>
Include dependency graph for tempo_tapper.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Classes

struct  tempo_tapper
 Tempo tapper struct. More...
 

Macros

#define S_TO_US   1000000
 

Typedefs

typedef float BPM_t
 
typedef struct tempo_tapper tempo_tapper
 Tempo tapper struct. More...
 

Functions

void current_time (tt_time_t *time)
 Receives the current clock time. More...
 
void add_time (tt_time_t *a, tt_time_t *b, tt_time_t *res)
 Adds two time values. More...
 
void sub_time (tt_time_t *a, tt_time_t *b, tt_time_t *res)
 Subtracts two time values. More...
 
void reset_time (tt_time_t *time)
 Resets a time var to 0. More...
 
unsigned long time_to_us (tt_time_t *time)
 Returns a time value in microseconds. More...
 
tempo_tappertt_new ()
 Creates a new tempo tapper instance. More...
 
unsigned long tt_period_us (tempo_tapper *tapper)
 Returns the period of a tempo in microseconds. More...
 
void tt_tap (tempo_tapper *tapper)
 "Taps" the tempo tapper More...
 
void tt_reset (tempo_tapper *tapper)
 Resets the tempo tapper. More...
 
BPM_t tt_bpm (tempo_tapper *tapper)
 Returns the tempo in BPM. More...
 

Detailed Description

Provides all necessary structs and functions to implement a tempo tapper.

Author
Patrick Pedersen
Date
2021-08-05

The following file Provides all necessary strucs and functions to implement a tempo tapper, i.e. the tempo tapper struct and prototypes for all functions that interact with a tempo tapper struct instance.

The tempo tapper libraries is supported by multiple target platforms. Define one of the following preprocessors to select your target platform:

Definition in file tempo_tapper.h.

Typedef Documentation

◆ tempo_tapper

typedef struct tempo_tapper tempo_tapper

Tempo tapper struct.

The following struct represents a virtual tempo tapper, and stores the necessary information to set, alter and receive a tempo value.

To create and interface with a tempo tapper struct, use the following functions:

To store time values, the platform varying tt_time_t typedef is used, as each platform offers its own preferred data type or struct to store time values (Ex. timeval on posix). This means that time arithmetic is implemented differently on every platform (see current_time(), add_time(), sub_time(), time_to_us(), reset_time()). For the library user, this is irrelevant as platform specific code is handled by the library internally. The only noticable external difference may be a variation in speed and precision.

Function Documentation

◆ add_time()

void add_time ( tt_time_t *  a,
tt_time_t *  b,
tt_time_t *  res 
)

Adds two time values.

The following function adds two time values, a, and b and stores the result in res.

Note
The implementation of this function is platform specific.

Referenced by tt_tap().

◆ current_time()

void current_time ( tt_time_t *  time)

Receives the current clock time.

The following function sets the parsed time var to the current clock time. The clock time may be the time that has passed since the start of the program, (ex. micros() on arduino platforms), but may also be the current time of the day (ex. gettimeofday() on posix).

Note
The implementation of this function is platform specific.

Referenced by tt_tap().

◆ reset_time()

void reset_time ( tt_time_t *  time)

Resets a time var to 0.

The following function resets a time var to 0

Note
The implementation of this function is platform specific.

Referenced by tt_reset().

◆ sub_time()

void sub_time ( tt_time_t *  a,
tt_time_t *  b,
tt_time_t *  res 
)

Subtracts two time values.

The following function subtracts the time value of b from a and stores the result in res.

Note
The implementation of this function is platform specific.

Referenced by tt_tap().

◆ time_to_us()

unsigned long time_to_us ( tt_time_t *  time)

Returns a time value in microseconds.

The following function converts and returns a time value in microseconds.

Returns
Time in microseconds
Note
The implementation of this function is platform specific.

Referenced by tt_period_us().

◆ tt_bpm()

BPM_t tt_bpm ( tempo_tapper tapper)

Returns the tempo in BPM.

The following function returns the tempo of the tempo tapper in BPM. The decimal precision is platform dependant.

Returns
Tempo in BPM

Definition at line 72 of file tempo_tapper_common.cxx.

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 }

References tt_period_us().

Referenced by tempo_tapper_cpp::bpm().

◆ tt_new()

tempo_tapper* tt_new ( )

Creates a new tempo tapper instance.

The following function creates and initializes a tempo tapper struct instance.

Returns
A initialized tempo_tapper struct instance or NULL on failure

Definition at line 61 of file tempo_tapper_common.cxx.

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 }

References tt_reset().

Referenced by tempo_tapper_cpp::tempo_tapper_cpp().

◆ tt_period_us()

unsigned long tt_period_us ( tempo_tapper tapper)

Returns the period of a tempo in microseconds.

The following function returns the time in microseconds for a period of the current tempo set by the tempo tapper struct.

Returns
Period time in microseconds

Definition at line 37 of file tempo_tapper_common.cxx.

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 }

References tempo_tapper::prd_sum, tempo_tapper::taps, and time_to_us().

Referenced by tempo_tapper_cpp::period_us(), and tt_bpm().

◆ tt_reset()

void tt_reset ( tempo_tapper tapper)

Resets the tempo tapper.

The following function resets the tempo tapper to its iniital values.

Definition at line 83 of file tempo_tapper_common.cxx.

84 {
85  tapper->taps = -1;
86  reset_time(&tapper->prd_sum);
87 }

References tempo_tapper::prd_sum, reset_time(), and tempo_tapper::taps.

Referenced by tempo_tapper_cpp::reset(), and tt_new().

◆ tt_tap()

void tt_tap ( tempo_tapper tapper)

"Taps" the tempo tapper

The following function is used to "tap" a tempo tapper struct instance. Calling this function on a tempo tapper struct will increase the value of taps, add the time that has passed since the last tap to prd_sum and update lst_t to the current clock time.

Definition at line 46 of file tempo_tapper_common.cxx.

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 }

References add_time(), current_time(), tempo_tapper::lst_t, tempo_tapper::prd_sum, sub_time(), and tempo_tapper::taps.

Referenced by tempo_tapper_cpp::tap().

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
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
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.