uMIDI
The swiss army knife for quick and easy developement of MIDI applications.
math.h
Go to the documentation of this file.
1 
4 /*
5  * Copyright 2015 Simon Gansen
6  *
7  * This file is part of the uMIDI firmware.
8  *
9  * the uMIDI firmware is free software: you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation, either version 3 of the License, or
12  * (at your option) any later version.
13  *
14  * the uMIDI firmware is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with the uMIDI firmware. If not, see <http://www.gnu.org/licenses/>.
21  */
22 
23 #ifndef _MATH_H
24 #define _MATH_H
25 
26 //---------------- includes -----------------//
27 #include <stdbool.h>
28 #include <stdint.h>
29 
30 #include "midi.h"
31 
32 
33 //---------------- constants ----------------//
35 #define FIXED_POINT 16
36 
37 
38 //---------------- data types ---------------//
40 typedef uint32_t fixed_t;
41 
43 typedef uint64_t fixed_accu_t;
44 
47 {
48  uint16_t from;
49  uint16_t to;
51 };
52 
53 
54 //---------------- prototypes ----------------//
55 
59 void init_linear_from_midi(struct linear_range* config);
60 
64 void init_linear_to_midi(struct linear_range* config);
65 
72 uint16_t linear_from_midi(const struct linear_range* config, midi_value_t midi_value);
73 
80 midi_value_t linear_to_midi(const struct linear_range* config, uint16_t input);
81 
82 
83 //---------------- inline functions ----------------//
84 
89 static inline fixed_t fixed_from_int(const uint16_t value)
90 {
91  fixed_t result = value;
92  result <<= FIXED_POINT;
93  return result;
94 }
95 
105 static inline fixed_t fixed_div(const fixed_t a, const fixed_t b)
106 {
107  fixed_accu_t accu = a;
108  accu <<= FIXED_POINT;
109  accu /= b;
110  return (fixed_t) accu;
111 }
112 
122 static inline fixed_t fixed_mpy(const fixed_t a, const fixed_t b)
123 {
124  fixed_accu_t accu = a;
125  accu *= b;
126  accu >>= FIXED_POINT;
127  return (fixed_t) accu;
128 }
129 
134 static inline uint16_t fixed_to_int(fixed_t value)
135 {
136  // Round
137  if (value & 0x00008000) {
138  value += 0x00008000;
139  }
140 
141  // Drop fracional part
142  value >>= FIXED_POINT;
143  return (uint16_t) value;
144 }
145 
146 #endif // _MATH_H
static fixed_t fixed_from_int(const uint16_t value)
Converts an integer to a fixed point number.
Definition: math.h:89
static uint16_t fixed_to_int(fixed_t value)
Converts fixed point number to an integer.
Definition: math.h:134
uint8_t midi_value_t
Type for valid MIDI values [0..127].
Definition: midi.h:56
uint16_t to
The highest input (linear_to_midi) / output (linear_from_midi) value.
Definition: math.h:49
static fixed_t fixed_mpy(const fixed_t a, const fixed_t b)
Multiplies two fixed point numbers.
Definition: math.h:122
MIDI message transceiver.
void init_linear_to_midi(struct linear_range *config)
Initializes a linear function to map from an arbitrary integer range to MIDI values.
Definition: math.c:44
static fixed_t fixed_div(const fixed_t a, const fixed_t b)
Performs a division of two fixed point numbers.
Definition: math.h:105
Configuration for a linear scaling function.
Definition: math.h:46
fixed_t slope
The slope of the linear function (computed in init_linear_...())
Definition: math.h:50
#define FIXED_POINT
Width of the fractional part of fixed point numbers.
Definition: math.h:35
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.
Definition: math.c:55
uint64_t fixed_accu_t
Datatype for accumulators in fixed point number arithmetic functions.
Definition: math.h:43
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.
Definition: math.c:50
uint32_t fixed_t
Datatype for fixed point numbers.
Definition: math.h:40
void init_linear_from_midi(struct linear_range *config)
Initializes a linear function to map from MIDI values to an arbitrary integer range.
Definition: math.c:38
uint16_t from
The lowest input (linear_to_midi) / output (linear_from_midi) value.
Definition: math.h:48