uMIDI
The swiss army knife for quick and easy developement of MIDI applications.
Data Structures | Macros | Typedefs | Functions
math.h File Reference

API for a small collection of mathematical functions. More...

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

Go to the source code of this file.

Data Structures

struct  linear_range
 Configuration for a linear scaling function. More...
 

Macros

#define FIXED_POINT   16
 Width of the fractional part of fixed point numbers. More...
 

Typedefs

typedef uint32_t fixed_t
 Datatype for fixed point numbers. More...
 
typedef uint64_t fixed_accu_t
 Datatype for accumulators in fixed point number arithmetic functions. More...
 

Functions

void init_linear_from_midi (struct linear_range *config)
 Initializes a linear function to map from MIDI values to an arbitrary integer range. More...
 
void init_linear_to_midi (struct linear_range *config)
 Initializes a linear function to map from an arbitrary integer range to MIDI values. More...
 
uint16_t linear_from_midi (const struct linear_range *config, midi_value_t midi_value)
 Yields an integer in the configured range that corresponds to the given MIDI value. More...
 
midi_value_t linear_to_midi (const struct linear_range *config, uint16_t input)
 Yields the MIDI value [0,127] from a given integer in the configured range. More...
 
static fixed_t fixed_from_int (const uint16_t value)
 Converts an integer to a fixed point number. More...
 
static fixed_t fixed_div (const fixed_t a, const fixed_t b)
 Performs a division of two fixed point numbers. More...
 
static fixed_t fixed_mpy (const fixed_t a, const fixed_t b)
 Multiplies two fixed point numbers. More...
 
static uint16_t fixed_to_int (fixed_t value)
 Converts fixed point number to an integer. More...
 

Detailed Description

API for a small collection of mathematical functions.

Macro Definition Documentation

◆ FIXED_POINT

#define FIXED_POINT   16

Width of the fractional part of fixed point numbers.

Typedef Documentation

◆ fixed_accu_t

typedef uint64_t fixed_accu_t

Datatype for accumulators in fixed point number arithmetic functions.

◆ fixed_t

typedef uint32_t fixed_t

Datatype for fixed point numbers.

Function Documentation

◆ fixed_div()

static fixed_t fixed_div ( const fixed_t  a,
const fixed_t  b 
)
inlinestatic

Performs a division of two fixed point numbers.

Note: You do not have to use this function when you want to divide a fixed point number by a "normal" integer, because the division done in this function cancels out the multiplication in fixed_from_int().

Parameters
athe dividend
bthe divisor
Returns
the quotient as fixed point number

◆ fixed_from_int()

static fixed_t fixed_from_int ( const uint16_t  value)
inlinestatic

Converts an integer to a fixed point number.

Parameters
valuethe integer value to convert
Returns
the value as fixed point number

◆ fixed_mpy()

static fixed_t fixed_mpy ( const fixed_t  a,
const fixed_t  b 
)
inlinestatic

Multiplies two fixed point numbers.

Note: You do not have to use this function when one of the two operands you want to multiply is a "normal" integer, because the division done in this function and the multiplication in fixed_from_int() cancel out.

Parameters
athe first operand
bthe second operand
Returns
the product as fixed point number

◆ fixed_to_int()

static uint16_t fixed_to_int ( fixed_t  value)
inlinestatic

Converts fixed point number to an integer.

Parameters
valuethe fixed point number to convert
Returns
the rounded integer part of the fixed point number

◆ init_linear_from_midi()

void init_linear_from_midi ( struct linear_range config)

Initializes a linear function to map from MIDI values to an arbitrary integer range.

Parameters
configthe configuration of the linear function to initialize

◆ init_linear_to_midi()

void init_linear_to_midi ( struct linear_range config)

Initializes a linear function to map from an arbitrary integer range to MIDI values.

Parameters
configthe configuration of the linear function to initialize

◆ linear_from_midi()

uint16_t linear_from_midi ( const struct linear_range config,
midi_value_t  midi_value 
)

Yields an integer in the configured range that corresponds to the given MIDI value.

Parameters
configthe configuration of the linear function
midi_valuethe MIDI value to convert [0,127]
Returns
the input scaled to the configured range

◆ linear_to_midi()

midi_value_t linear_to_midi ( const struct linear_range config,
uint16_t  input 
)

Yields the MIDI value [0,127] from a given integer in the configured range.

Parameters
configthe configuration of the linear function
inputthe integer to convert to a MIDI value
Returns
the input scaled to the MIDI range [0,127]