-
Notifications
You must be signed in to change notification settings - Fork 5
/
float32.c
411 lines (351 loc) · 8.57 KB
/
float32.c
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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
/* $Id: float32.c,v 1.22 2009/09/11 16:51:07 toad32767 Exp $ */
/*-
* Copyright (c) 2005, 2006, 2007 Marco Trillo
*
* Permission is hereby granted, free of charge, to any
* person obtaining a copy of this software and associated
* documentation files (the "Software"), to deal in the
* Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish,
* distribute, sublicense, and/or sell copies of the
* Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice
* shall be included in all copies or substantial portions of
* the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY
* KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
* WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
* PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS
* OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
* OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
* OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
#define LIBAIFF 1
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <libaiff/libaiff.h>
#include <libaiff/endian.h>
#include "private.h"
#ifdef HAVE_INTEL_80x87
# define IEEE754_NATIVE 1
#endif
/*
* IEEE-754 32-bit single-precision floating point
*
* This is a conversor to linear 32-bit integer PCM
* using only integer arithmetic.
*
* Some IEEE-754 documentation and references on the WWW:
* -------------------------------------------------------------
* http://stevehollasch.com/cgindex/coding/ieeefloat.html
* http://www.answers.com/topic/ieee-floating-point-standard
* http://www.cs.berkeley.edu/~wkahan/ieee754status/IEEE754.PDF
* http://www.psc.edu/general/software/packages/ieee/ieee.html
* http://www.duke.edu/~twf/cps104/floating.html
* -------------------------------------------------------------
*/
static int32_t
float32dec(uint32_t in)
{
int sgn, exp;
uint32_t mantissa;
if (in == 0 || in == 0x80000000)
return (0);
sgn = in >> 31;
exp = (in >> 23) & 0xFF;
mantissa = in & 0x7FFFFF;
if (exp == 255) {
/*
* Infinity and NaN.
* XXX: what we should do with these?
*/
if (mantissa == 0) /* infinity */
return (int32_t) (0x7FFFFFFF);
else /* NaN */
return (0);
} else if (exp == 0) {
exp = -126; /* denormalized number */
} else {
/* Normalized numbers */
mantissa |= 0x800000; /* hidden bit */
exp -= 127; /* unbias exponent */
}
/*
* Quantization to linear PCM 31 bits.
*
* Multiply the mantissa by 2^(-23+exp) to get the floating
* point value, and then multiply by 2^31 to quantize
* the floating point to 31 bits. So:
*
* 2^(-23+exp) * 2^31 = 2^(8 + exp)
*/
exp += 8;
if (exp < 0) {
mantissa >>= -exp;
} else {
mantissa <<= exp;
}
if (sgn) {
if (mantissa == 0x80000000)
return (-2147483647 - 1); /* -(2^31) */
else {
return -((int32_t) mantissa);
}
} else {
/*
* if mantissa is 0x80000000 (for a 1.0 float),
* we should return +2147483648 (2^31), but in
* two's complement arithmetic the max. positive value
* is +2147483647, so clip the result.
*/
if (mantissa == 0x80000000)
return (2147483647);
else
return ((int32_t) mantissa);
}
}
static void
float32_decode(void *dst, void *src, int n)
{
uint32_t *srcSamples = (uint32_t *) src;
int32_t *dstSamples = (int32_t *) dst;
while (n-- > 0)
dstSamples[n] = float32dec(srcSamples[n]);
}
static void
float32_swap_samples(void *buffer, int n)
{
lpcm_swap32(buffer, buffer, n);
}
static size_t
float32_read_lpcm(AIFF_Ref r, void *buffer, size_t len)
{
int n;
uint32_t clen;
size_t slen;
size_t bytes_in;
size_t bytesToRead;
void *buf;
n = len >> 2;
len &= ~3;
slen = (size_t) (r->soundLen) - (size_t) (r->pos);
bytesToRead = MIN(len, slen);
if (0 == bytesToRead ||
NULL == (buf = AIFFBufAllocate(r, kAIFFBufConv, bytesToRead)))
return 0;
bytes_in = fread(buf, 1, bytesToRead, r->fd);
if (bytes_in > 0)
clen = (uint32_t) bytes_in;
else
clen = 0;
r->pos += clen;
if (r->flags & LPCM_NEED_SWAP)
float32_swap_samples(buf, n);
float32_decode(buffer, buf, n);
return bytes_in;
}
static int
float32_seek(AIFF_Ref r, uint64_t pos)
{
long of;
uint32_t b;
b = (uint32_t) pos * r->nChannels * 4;
if (b >= r->soundLen)
return 0;
of = (long) b;
if (fseek(r->fd, of, SEEK_CUR) < 0) {
return -1;
}
r->pos = b;
return 1;
}
/*
* Infinite & NAN values
* for non-IEEE systems
*/
#ifndef HUGE_VAL
# ifdef HUGE
# define INFINITE_VALUE HUGE
# define NAN_VALUE HUGE
# endif
#else
# define INFINITE_VALUE HUGE_VAL
# define NAN_VALUE HUGE_VAL
#endif
#ifndef IEEE754_NATIVE
/*
* Write IEEE Single Precision Numbers.
*/
static uint32_t
ieee754_write_single(float in)
{
uint32_t sgn, mantissa;
int exp;
double fraction;
if (in == 0.0)
return 0;
if (in < 0.0) {
in = (float) fabs(in);
sgn = 1;
} else {
sgn = 0;
}
/*
* Grab the exponent and mantissa. This function will return 24-bit
* mantissas of type 0.1F, having F 23 bits of precision.
*/
fraction = ldexp(frexp(in, &exp), 24);
mantissa = (uint32_t) floor(fraction);
if (exp > 128) {
mantissa = 0; /* infinity */
exp = 255;
goto done;
}
/*
* If the exponent is -126 or lssser, we use 'denormalised numbers'
* with 0.F mantissas, having F 23 bits of precision.
*/
if (exp <= -126) {
mantissa >>= 1 - exp - 126;
exp = 0;
goto done;
}
/*
* Any other number is stored as a 'normalised number', with a
* mantissa of type 1.F with 23 bits of precision for F.
*/
mantissa &= 0x7FFFFF; /* hidden bit */
exp += 127 - 1; /* biased exponent */
done:
return ((sgn << 31) | ((exp & 0xff) << 23) | mantissa);
}
# define IEEE754_TEST_VALUE -1.12877
/*
* ieee754_native(): check if host supports single-precision IEEE-754 natively.
*/
static int
ieee754_native(void)
{
union {
uint32_t w;
float f;
} u;
uint32_t t;
if (sizeof(u) != sizeof(uint32_t) ||
sizeof(u) != sizeof(float))
return (0);
u.f = IEEE754_TEST_VALUE;
t = ieee754_write_single(IEEE754_TEST_VALUE);
return (u.w == t);
}
/*
* Read IEEE Single Precision Numbers.
*/
static float
ieee754_read_single(uint32_t in)
{
uint32_t mantissa;
int sgn, exp;
double fraction;
float out;
if (in == 0)
return 0.0;
else if (in == 0x80000000)
return -0.0;
sgn = in >> 31;
exp = (in >> 23) & 0xFF;
mantissa = in & 0x7FFFFF;
switch (exp) {
case 255:
if (mantissa == 0) {
return (sgn ? -INFINITE_VALUE : INFINITE_VALUE);
} else {
return (sgn ? -NAN_VALUE : NAN_VALUE);
}
case 0:
/* Denormalised numbers */
exp = -126;
break;
default:
/* Normalised numbers */
mantissa |= 0x800000; /* hidden bit */
exp -= 127; /* unbias exponent */
}
fraction = (double) mantissa;
out = (float) ldexp(fraction, -23 + exp);
return (sgn ? -out : out);
}
#endif /* ! IEEE754_NATIVE */
static int
float32_read_float32(AIFF_Ref r, float *buffer, int n)
{
int nSamplesRead;
uint32_t clen;
size_t len, slen;
size_t bytes_in;
size_t bytesToRead;
len = n << 2;
slen = r->soundLen - r->pos;
bytesToRead = MIN(len, slen);
if (bytesToRead == 0)
return 0;
#ifndef IEEE754_NATIVE
/*
* Check if this host supports 32-bit IEEE floats
* natively and take note about it to avoid doing
* the test each time a buffer is to be read...
*/
if (!(r->flags & F_IEEE754_CHECKED)) {
r->flags |= F_IEEE754_CHECKED;
if (ieee754_native())
r->flags |= F_IEEE754_NATIVE;
}
if (r->flags & F_IEEE754_NATIVE) {
#endif
bytes_in = fread((void *) buffer, 1, bytesToRead, r->fd);
if (bytes_in > 0) {
nSamplesRead = (int) bytes_in >> 2;
if (r->flags & LPCM_NEED_SWAP)
float32_swap_samples((void *) buffer, nSamplesRead);
} else {
nSamplesRead = 0;
}
#ifndef IEEE754_NATIVE
} else {
void *buf = AIFFBufAllocate(r, kAIFFBufConv, bytesToRead);
if (buf == NULL)
return 0;
bytes_in = fread(buf, 1, bytesToRead, r->fd);
if (bytes_in > 0) {
uint32_t *dwords = buf;
nSamplesRead = (int) bytes_in >> 2;
if (r->flags & LPCM_NEED_SWAP)
float32_swap_samples(dwords, nSamplesRead);
while (nSamplesRead-- > 0)
buffer[nSamplesRead] = ieee754_read_single(dwords[nSamplesRead]);
} else {
nSamplesRead = 0;
}
}
#endif
if (bytes_in > 0)
clen = (uint32_t) bytes_in;
else
clen = 0;
r->pos += clen;
return nSamplesRead;
}
struct codec float32 = {
AUDIO_FORMAT_FL32,
NULL,
float32_read_lpcm,
float32_read_float32,
NULL,
float32_seek,
NULL
};