foobar2000 SDK  2015-08-03
audio_sample.cpp
Go to the documentation of this file.
1 #include "pfc.h"
2 
3 namespace pfc {
4  audio_sample audio_math::decodeFloat24ptr(const void * sourcePtr) {
5  PFC_STATIC_ASSERT(pfc::byte_order_is_little_endian);
6  union {
7  uint8_t bytes[4];
8  float v;
9  } u;
10  const uint8_t * s = reinterpret_cast<const uint8_t*>(sourcePtr);
11  u.bytes[0] = 0;
12  u.bytes[1] = s[0];
13  u.bytes[2] = s[1];
14  u.bytes[3] = s[2];
15  return u.v;
16  }
17  audio_sample audio_math::decodeFloat24ptrbs(const void * sourcePtr) {
18  PFC_STATIC_ASSERT(pfc::byte_order_is_little_endian);
19  union {
20  uint8_t bytes[4];
21  float v;
22  } u;
23  const uint8_t * s = reinterpret_cast<const uint8_t*>(sourcePtr);
24  u.bytes[0] = 0;
25  u.bytes[1] = s[2];
26  u.bytes[2] = s[1];
27  u.bytes[3] = s[0];
28  return u.v;
29  }
30 
32  const unsigned fractionBits = 10;
33  const unsigned widthBits = 16;
34  typedef uint16_t source_t;
35 
36  /* typedef uint64_t out_t; typedef double retval_t;
37  enum {
38  outExponent = 11,
39  outFraction = 52,
40  outExponentShift = (1 << (outExponent-1))-1
41  };*/
42 
43  typedef uint32_t out_t; typedef float retval_t;
44  enum {
45  outExponent = 8,
46  outFraction = 23,
47  outExponentShift = (1 << (outExponent-1))-1
48  };
49 
50  const unsigned exponentBits = widthBits - fractionBits - 1;
51  // 1 bit sign | exponent | fraction
52  source_t fraction = source & (((source_t)1 << fractionBits)-1);
53  source >>= fractionBits;
54  int exponent = (int)( source & (((source_t)1 << exponentBits)-1) ) - (int)((1 << (exponentBits-1))-1);
55  source >>= exponentBits;
56 
57  if (outExponent + outExponentShift <= 0) return 0;
58 
59  out_t output = (out_t)( source&1 );
60  output <<= outExponent;
61  output |= (unsigned) (exponent + outExponentShift) & ( (1<<outExponent) - 1 );
62  output <<= outFraction;
63  int shift = (int) outFraction - (int) fractionBits;
64  if (shift < 0) output |= (out_t) (fraction >> -shift);
65  else output |= (out_t) (fraction << shift);
66  return *(retval_t*)&output / pfc::audio_math::float16scale;
67  }
68 }
static const bool byte_order_is_little_endian
static audio_sample decodeFloat24ptr(const void *sourcePtr)
Definition: audio_sample.cpp:4
static const audio_sample float16scale
Definition: audio_sample.h:76
static audio_sample decodeFloat24ptrbs(const void *sourcePtr)
float audio_sample
Definition: audio_sample.h:6
static audio_sample decodeFloat16(uint16_t source)
Definition: output.h:95