-
Notifications
You must be signed in to change notification settings - Fork 1
/
raw2real.h
163 lines (160 loc) · 4.85 KB
/
raw2real.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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
/*
* (c) Copyright 2001 - 2004, 2013 -- Anders Torger
*
* This program is open source. For license terms, see the LICENSE file.
*
*/
static void
RAW2REAL_NAME(void *_realbuf,
void *_rawbuf,
int bytes,
bool_t isfloat,
int spacing,
bool_t swap,
int n_samples)
{
numunion_t *realbuf, *rawbuf, sample;
int n, i;
realbuf = (numunion_t *)_realbuf;
rawbuf = (numunion_t *)_rawbuf;
if (isfloat) {
#if REALSIZE == 4
#define RXX r32
#define REAL_T float
switch (bytes) {
case 4:
if (swap) {
for (n = i = 0; n < n_samples; n++, i += spacing) {
realbuf->u32[n] = SWAP32(rawbuf->u32[i]);
}
} else {
for (n = i = 0; n < n_samples; n++, i += spacing) {
realbuf->r32[n] = rawbuf->r32[i];
}
}
break;
case 8:
if (swap) {
for (n = i = 0; n < n_samples; n++, i += spacing) {
sample.u64[0] = SWAP64(rawbuf->u64[i]);
realbuf->r32[n] = (float)sample.r64[0];
}
} else {
for (n = i = 0; n < n_samples; n++, i += spacing) {
realbuf->r32[n] = (float)rawbuf->r64[i];
}
}
break;
default:
goto raw2real_invalid_byte_size;
}
#elif REALSIZE == 8
#define RXX r64
#define REAL_T double
switch (bytes) {
case 4:
if (swap) {
for (n = i = 0; n < n_samples; n++, i += spacing) {
sample.u32[0] = SWAP32(rawbuf->u32[i]);
realbuf->r64[n] = (double)sample.r32[0];
}
} else {
for (n = i = 0; n < n_samples; n++, i += spacing) {
realbuf->r64[n] = (double)rawbuf->r32[i];
}
}
break;
case 8:
if (swap) {
for (n = i = 0; n < n_samples; n++, i += spacing) {
realbuf->u64[n] = SWAP64(rawbuf->u64[i]);
}
} else {
for (n = i = 0; n < n_samples; n++, i += spacing) {
realbuf->r64[n] = rawbuf->r64[i];
}
}
break;
default:
goto raw2real_invalid_byte_size;
}
#else
#error invalid REALSIZE
#endif
return;
}
sample.u64[0] = 0;
switch (bytes) {
case 1:
for (n = i = 0; n < n_samples; n++, i += spacing) {
realbuf->RXX[n] = (REAL_T)rawbuf->i8[i];
}
break;
case 2:
if (swap) {
for (n = i = 0; n < n_samples; n++, i += spacing) {
realbuf->RXX[n] = (REAL_T)((int16_t)SWAP16(rawbuf->u16[i]));
}
} else {
for (n = i = 0; n < n_samples; n++, i += spacing) {
realbuf->RXX[n] = (REAL_T)rawbuf->i16[i];
}
}
break;
case 3:
spacing = spacing * 3 - 3;
#ifdef __BIG_ENDIAN__
if (swap) {
for (n = i = 0; n < n_samples; n++, i += spacing) {
sample.u8[2] = rawbuf->u8[i++];
sample.u8[1] = rawbuf->u8[i++];
sample.u8[0] = rawbuf->u8[i++];
realbuf->RXX[n] = (REAL_T)(sample.i32[0] >> 8);
}
} else {
for (n = i = 0; n < n_samples; n++, i += spacing) {
sample.u8[0] = rawbuf->u8[i++];
sample.u8[1] = rawbuf->u8[i++];
sample.u8[2] = rawbuf->u8[i++];
realbuf->RXX[n] = (REAL_T)(sample.i32[0] >> 8);
}
}
#endif
#ifdef __LITTLE_ENDIAN__
if (swap) {
for (n = i = 0; n < n_samples; n++, i += spacing) {
sample.u8[3] = rawbuf->u8[i++];
sample.u8[2] = rawbuf->u8[i++];
sample.u8[1] = rawbuf->u8[i++];
realbuf->RXX[n] = (REAL_T)(sample.i32[0] >> 8);
}
} else {
for (n = i = 0; n < n_samples; n++, i += spacing) {
sample.u8[1] = rawbuf->u8[i++];
sample.u8[2] = rawbuf->u8[i++];
sample.u8[3] = rawbuf->u8[i++];
realbuf->RXX[n] = (REAL_T)(sample.i32[0] >> 8);
}
}
#endif
break;
case 4:
if (swap) {
for (n = i = 0; n < n_samples; n++, i += spacing) {
realbuf->RXX[n] = (REAL_T)((int32_t)SWAP32(rawbuf->u32[i]));
}
} else {
for (n = i = 0; n < n_samples; n++, i += spacing) {
realbuf->RXX[n] = (REAL_T)rawbuf->i32[i];
}
}
break;
default:
raw2real_invalid_byte_size:
fprintf(stderr, "Sample byte size %d is not suppported.\n", bytes);
bf_exit(BF_EXIT_OTHER);
break;
}
}
#undef RXX
#undef REAL_T