-
Notifications
You must be signed in to change notification settings - Fork 0
/
px_globals.h
110 lines (87 loc) · 2.49 KB
/
px_globals.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
#include <stdarg.h>
#include <math.h>
#include <stdlib.h>
#include <stdbool.h>
#include <assert.h>
#include <string.h>
#include <stdio.h>
#ifndef PX_GLOBALS_H
#define PX_GLOBALS_H
// Math Constants
// ---------------------------------
#define DC_OFFSET 1.0E-25
#define PI 3.141592653589793
#define sgn(val) ((0 < val) - (val < 0))
// ---------------------------------
// Gain
// linear -> dB conversion
static inline float lin2dB(float lin) {
static const float LOG_2_DB = 8.6858896380650365530225783783321; // 20 / ln( 10 )
return log(lin) * LOG_2_DB;
}
// dB -> linear conversion
static inline float dB2lin(float dB) {
static const float DB_2_LOG = 0.11512925464970228420089957273422; // ln( 10 ) / 20
return exp(dB * DB_2_LOG);
}
// -------------------------------------
//
// Mid Side
//
//
typedef enum
{
BOTH = 0,
LEFT,
RIGHT,
MID,
SIDE
} CHANNEL_FLAG;
typedef struct
{
float left;
float right;
} px_ms_decoded;
typedef struct
{
float mid;
float side;
} px_ms_encoded;
static inline px_ms_encoded px_ms_encode(px_ms_decoded decoded)
{
px_ms_encoded encoded;
encoded.mid = 0.5f * (decoded.left + decoded.right);
encoded.side = 0.5f * (decoded.left - decoded.right);
return encoded;
}
static inline px_ms_decoded px_ms_decode(px_ms_encoded encoded)
{
px_ms_decoded decoded;
decoded.left = encoded.mid + encoded.side;
decoded.right = encoded.mid - encoded.side;
return decoded;
}
// assert for process functions
// ------------------------------------------------------------------------------------------------------
static void px_assert_mono(void* control_pointer, float* value_pointer)
{
// check if DSP object passed is NULL or uninitialized
assert(control_pointer);
// check if value is valid float pointer
assert(value_pointer);
}
static void px_assert_stereo(void* control_pointer, float* channel_one_pointer, float* channel_two_pointer)
{
// check if DSP object passed is NULL or uninitialized
assert(control_pointer);
// check if value is valid float pointer
assert(channel_one_pointer);
assert(channel_two_pointer);
}
#define EXPAND(x) x
#define GET_ASSERT_MACRO(_1, _2, _3, NAME, ...) NAME
#define px_assert(...) EXPAND(GET_ASSERT_MACRO(__VA_ARGS__, px_assert_stereo, px_assert_mono)(__VA_ARGS__))
#define px_map(value, inputMin, inputMax, outputMin, outputMax) \
((inputMin > inputMax) ? (outputMin) : \
((outputMin) + ((outputMax) - (outputMin)) * (((float)(value) - (float)(inputMin)) / ((float)(inputMax) - (float)(inputMin)))))
#endif