libmixed
Loading...
Searching...
No Matches
mixed_encoding.h
Go to the documentation of this file.
1#include <math.h>
2
3#define INT24_MAX 8388607
4#define INT24_MIN -8388608
5#define UINT24_MAX 16777215
6typedef int32_t int24_t;
7typedef uint32_t uint24_t;
8
9__attribute__((always_inline))
10MIXED_EXPORT inline float mixed_from_float(float sample){
11 return (1.0f<sample)? 1.0f
12 : (-1.0f<sample)? sample
13 : -1.0f;
14}
15
16__attribute__((always_inline))
17MIXED_EXPORT inline float mixed_from_double(double sample){
18 return (1.0<sample)? 1.0f
19 : (-1.0<sample)? (float)sample
20 : -1.0f;
21}
22
23__attribute__((always_inline))
24MIXED_EXPORT inline float mixed_from_int8(int8_t sample){
25 return (sample < 0)
26 ? -(sample/(float)INT8_MIN)
27 : +(sample/(float)INT8_MAX);
28}
29
30__attribute__((always_inline))
31MIXED_EXPORT inline float mixed_from_uint8(uint8_t sample){
32 return ((float)sample)/((float)UINT8_MAX/2)-1;
33}
34
35__attribute__((always_inline))
36MIXED_EXPORT inline float mixed_from_int16(int16_t sample){
37 return (sample < 0)
38 ? -(sample/(float)INT16_MIN)
39 : +(sample/(float)INT16_MAX);
40}
41
42__attribute__((always_inline))
43MIXED_EXPORT inline float mixed_from_uint16(uint16_t sample){
44 return ((float)sample)/((float)UINT16_MAX/2)-1;
45}
46
47__attribute__((always_inline))
48MIXED_EXPORT inline float mixed_from_int24(int24_t sample){
49 return (sample < 0)
50 ? -(sample/(float)INT24_MIN)
51 : +(sample/(float)INT24_MAX);
52}
53
54__attribute__((always_inline))
55MIXED_EXPORT inline float mixed_from_uint24(uint24_t sample){
56 return ((double)sample)/((double)UINT24_MAX/2)-1;
57}
58
59__attribute__((always_inline))
60MIXED_EXPORT inline float mixed_from_int32(int32_t sample){
61 return (sample < 0)
62 ? -(sample/(double)INT32_MIN)
63 : +(sample/(double)INT32_MAX);
64}
65
66__attribute__((always_inline))
67MIXED_EXPORT inline float mixed_from_uint32(uint32_t sample){
68 return mixed_from_double(((double)sample-0x80000000L)/((double)0x80000000L));
69}
70
71__attribute__((always_inline))
72MIXED_EXPORT inline float mixed_to_float(float sample){
73 return (1.0f<=sample)? 1.0f
74 : (-1.0f<=sample)? sample
75 : -1.0f;
76}
77
78__attribute__((always_inline))
79MIXED_EXPORT inline double mixed_to_double(float sample){
80 return (1.0f<=sample)? 1.0
81 : (-1.0f<=sample)? (double)sample
82 : -1.0;
83}
84
85__attribute__((always_inline))
86MIXED_EXPORT inline int8_t mixed_to_int8(float sample){
87 return (1.0f<=sample)? INT8_MAX
88 : (-1.0f<=sample)? sample*0x80
89 : INT8_MIN;
90}
91
92__attribute__((always_inline))
93MIXED_EXPORT inline uint8_t mixed_to_uint8(float sample){
94 return (1.0f<=sample)? UINT8_MAX
95 : (-1.0f<=sample)? (sample+1)*0x80
96 : 0;
97}
98
99__attribute__((always_inline))
100MIXED_EXPORT inline int16_t mixed_to_int16(float sample){
101 return (1.0f<=sample)? INT16_MAX
102 : (-1.0f<=sample)? sample*0x8000
103 : INT16_MIN;
104}
105
106__attribute__((always_inline))
107MIXED_EXPORT inline uint16_t mixed_to_uint16(float sample){
108 return (1.0f<=sample)? UINT16_MAX
109 : (-1.0f<=sample)? (sample+1)*0x8000
110 : 0;
111}
112
113__attribute__((always_inline))
114MIXED_EXPORT inline int24_t mixed_to_int24(float sample){
115 return (1.0f<=sample)? INT24_MAX
116 : (-1.0f<=sample)? sample*0x800000
117 : INT24_MIN;
118}
119
120__attribute__((always_inline))
121MIXED_EXPORT inline uint24_t mixed_to_uint24(float sample){
122 return (1.0f<=sample)? UINT24_MAX
123 : (-1.0f<=sample)? round((sample+1)*((float)UINT24_MAX/2))
124 : 0;
125}
126
127__attribute__((always_inline))
128MIXED_EXPORT inline int32_t mixed_to_int32(float sample){
129 return (1.0f<=sample)? INT32_MAX
130 : (-1.0f<=sample)? sample*0x80000000L
131 : INT32_MIN;
132}
133
134__attribute__((always_inline))
135MIXED_EXPORT inline uint32_t mixed_to_uint32(float sample){
136 return (1.0f<=sample)? UINT32_MAX
137 : (-1.0f<=sample)? (sample+1)*0x80000000L
138 : 0;
139}
#define MIXED_EXPORT
Definition mixed.h:22
int32_t int24_t
Definition mixed_encoding.h:6
#define INT24_MAX
Definition mixed_encoding.h:3
#define UINT24_MAX
Definition mixed_encoding.h:5
#define INT24_MIN
Definition mixed_encoding.h:4
__attribute__((always_inline)) MIXED_EXPORT inline float mixed_from_float(float sample)
Definition mixed_encoding.h:9
uint32_t uint24_t
Definition mixed_encoding.h:7