Tempo Tapper  1.0.0
A simple library to implement a tempo tapper
Functions
tempo_tapper_common.cxx File Reference

Defines code shared accross all target platform s. More...

#include <stdlib.h>
#include <stddef.h>
#include <tempo_tapper.h>
Include dependency graph for tempo_tapper_common.cxx:

Go to the source code of this file.

Functions

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...
 
tempo_tappertt_new ()
 Creates a new tempo tapper instance. More...
 
BPM_t tt_bpm (tempo_tapper *tapper)
 Returns the tempo in BPM. More...
 
void tt_reset (tempo_tapper *tapper)
 Resets the tempo tapper. More...
 

Detailed Description

Defines code shared accross all target platform s.

Author
Patrick Pedersen
Date
2021-08-05

The following file defines tempo tapper functions that are shared across all target platforms.

All function descriptions can be found in the tempo_tapper.h file.

Definition in file tempo_tapper_common.cxx.

Function Documentation

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