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