Overte C++ Documentation
AudioHelpers.h
1 //
2 // AudioHelpers.h
3 // libraries/shared/src
4 //
5 // Created by Ken Cooke on 1/4/17.
6 // Copyright 2017 High Fidelity, Inc.
7 //
8 // Distributed under the Apache License, Version 2.0.
9 // See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html
10 //
11 
12 #ifndef hifi_AudioHelpers_h
13 #define hifi_AudioHelpers_h
14 
15 #include <stdint.h>
16 
17 #include <NumericalConstants.h>
18 
19 const int IEEE754_MANT_BITS = 23;
20 const int IEEE754_EXPN_BIAS = 127;
21 
22 //
23 // for x > 0.0f, returns log2(x)
24 // for x <= 0.0f, returns large negative value
25 //
26 // abs |error| < 2e-4, smooth (exact for x=2^N)
27 // rel |error| < 0.4 from precision loss very close to 1.0f
28 //
29 static inline float fastLog2f(float x) {
30 
31  union { float f; int32_t i; } mant, bits = { x };
32 
33  // split into mantissa and exponent
34  mant.i = (bits.i & ((1 << IEEE754_MANT_BITS) - 1)) | (IEEE754_EXPN_BIAS << IEEE754_MANT_BITS);
35  int32_t expn = (bits.i >> IEEE754_MANT_BITS) - IEEE754_EXPN_BIAS;
36 
37  mant.f -= 1.0f;
38 
39  // polynomial for log2(1+x) over x=[0,1]
40  x = (((-0.0821307180f * mant.f + 0.321188984f) * mant.f - 0.677784014f) * mant.f + 1.43872575f) * mant.f;
41 
42  return x + expn;
43 }
44 
45 //
46 // for -126 <= x < 128, returns exp2(x)
47 // for x < -126, returns 0
48 // for x >= 128, returns undefined
49 //
50 // rel |error| < 9e-6, smooth (exact for x=N)
51 //
52 static inline float fastExp2f(float x) {
53 
54  union { float f; int32_t i; } xi;
55 
56  // bias such that x > 0
57  x += IEEE754_EXPN_BIAS;
58 
59  // split into integer and fraction
60  xi.i = (int32_t)x;
61  x -= xi.i;
62 
63  // construct exp2(xi) as a float
64  xi.i &= ~(xi.i >> 31); // MAX(xi.i, 0)
65  xi.i <<= IEEE754_MANT_BITS;
66 
67  // polynomial for exp2(x) over x=[0,1]
68  x = (((0.0135557472f * x + 0.0520323690f) * x + 0.241379763f) * x + 0.693032121f) * x + 1.0f;
69 
70  return x * xi.f;
71 }
72 
73 //
74 // on x86 architecture, assume that SSE2 is present
75 //
76 #if defined(_M_IX86) || defined(_M_X64) || defined(__i386__) || defined(__x86_64__)
77 
78 #include <xmmintrin.h>
79 // inline sqrtss, without requiring /fp:fast
80 static inline float fastSqrtf(float x) {
81  return _mm_cvtss_f32(_mm_sqrt_ss(_mm_set_ss(x)));
82 }
83 
84 #else
85 
86 static inline float fastSqrtf(float x) {
87  return sqrtf(x);
88 }
89 
90 #endif
91 
92 //
93 // for -1 <= x <= 1, returns acos(x)
94 // otherwise, returns NaN
95 //
96 // abs |error| < 7e-5, smooth
97 //
98 static inline float fastAcosf(float x) {
99 
100  union { float f; int32_t i; } xi = { x };
101 
102  int32_t sign = xi.i & 0x80000000;
103  xi.i ^= sign; // fabs(x)
104 
105  // compute sqrt(1-x) in parallel
106  float r = fastSqrtf(1.0f - xi.f);
107 
108  // polynomial for acos(x)/sqrt(1-x) over x=[0,1]
109  xi.f = ((-0.0198439236f * xi.f + 0.0762021306f) * xi.f + -0.212940971f) * xi.f + 1.57079633f;
110 
111  xi.f *= r;
112  return (sign ? PI - xi.f : xi.f);
113 }
114 
115 //
116 // Quantize a non-negative gain value to the nearest 0.5dB, and pack to a byte.
117 //
118 // Values above +30dB are clamped to +30dB
119 // Values below -97dB are clamped to -inf
120 // Value of 1.0 (+0dB) is reconstructed exactly
121 //
122 const float GAIN_CONVERSION_RATIO = 2.0f * 6.02059991f; // scale log2 to 0.5dB
123 const float GAIN_CONVERSION_OFFSET = 255 - 60.0f; // translate +30dB to max
124 
125 static inline uint8_t packFloatGainToByte(float gain) {
126 
127  float f = fastLog2f(gain) * GAIN_CONVERSION_RATIO + GAIN_CONVERSION_OFFSET;
128  int32_t i = (int32_t)(f + 0.5f); // quantize
129 
130  uint8_t byte = (i < 0) ? 0 : ((i > 255) ? 255 : i); // clamp
131  return byte;
132 }
133 
134 static inline float unpackFloatGainFromByte(uint8_t byte) {
135 
136  float gain = (byte == 0) ? 0.0f : fastExp2f((byte - GAIN_CONVERSION_OFFSET) * (1.0f/GAIN_CONVERSION_RATIO));
137  return gain;
138 }
139 
140 #endif // hifi_AudioHelpers_h