1 /* 2 * Copyright (C) 2014 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 #ifndef ANDROID_AUDIO_MINIFLOAT_H 18 #define ANDROID_AUDIO_MINIFLOAT_H 19 20 #include <stdint.h> 21 #include <sys/cdefs.h> 22 23 __BEGIN_DECLS 24 25 /* A single gain expressed as minifloat */ 26 typedef uint16_t gain_minifloat_t; 27 28 /* A pair of gain_minifloat_t packed into a single word */ 29 typedef uint32_t gain_minifloat_packed_t; 30 31 /* The nominal range of a gain, expressed as a float */ 32 #define GAIN_FLOAT_ZERO 0.0f 33 #define GAIN_FLOAT_UNITY 1.0f 34 35 /* Unity gain expressed as a minifloat */ 36 #define GAIN_MINIFLOAT_UNITY 0xE000 37 38 /* Pack a pair of gain_mini_float_t into a combined gain_minifloat_packed_t */ 39 static inline gain_minifloat_packed_t gain_minifloat_pack(gain_minifloat_t left, 40 gain_minifloat_t right) 41 { 42 return (right << 16) | left; 43 } 44 45 /* Unpack a gain_minifloat_packed_t into the two gain_minifloat_t components */ 46 static inline gain_minifloat_t gain_minifloat_unpack_left(gain_minifloat_packed_t packed) 47 { 48 return packed & 0xFFFF; 49 } 50 51 static inline gain_minifloat_t gain_minifloat_unpack_right(gain_minifloat_packed_t packed) 52 { 53 return packed >> 16; 54 } 55 56 /* A pair of unity gains expressed as a gain_minifloat_packed_t */ 57 #define GAIN_MINIFLOAT_PACKED_UNITY gain_minifloat_pack(GAIN_MINIFLOAT_UNITY, GAIN_MINIFLOAT_UNITY) 58 59 /* Convert a float to the internal representation used for gains. 60 * The nominal range [0.0, 1.0], but the hard range is [0.0, 2.0). 61 * Negative and underflow values are converted to 0.0, 62 * and values larger than the hard maximum are truncated to the hard maximum. 63 * 64 * Minifloats are ordered, and standard comparisons may be used between them 65 * in the gain_minifloat_t representation. 66 * 67 * Details on internal representation of gains, based on mini-floats: 68 * The nominal maximum is 1.0 and the hard maximum is 1 ULP less than 2.0, or +6 dB. 69 * The minimum non-zero value is approximately 1.9e-6 or -114 dB. 70 * Negative numbers, infinity, and NaN are not supported. 71 * There are 13 significand bits specified, 1 implied hidden bit, 3 exponent bits, 72 * and no sign bit. Denormals are supported. 73 */ 74 gain_minifloat_t gain_from_float(float f); 75 76 /* Convert the internal representation used for gains to float */ 77 float float_from_gain(gain_minifloat_t gain); 78 79 __END_DECLS 80 81 #endif // ANDROID_AUDIO_MINIFLOAT_H 82