Wed, 31 Dec 2014 06:09:35 +0100
Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.
michael@0 | 1 | /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ |
michael@0 | 2 | /* vim:set ts=2 sw=2 sts=2 et cindent: */ |
michael@0 | 3 | /* This Source Code Form is subject to the terms of the Mozilla Public |
michael@0 | 4 | * License, v. 2.0. If a copy of the MPL was not distributed with this |
michael@0 | 5 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
michael@0 | 6 | |
michael@0 | 7 | #ifndef WebAudioUtils_h_ |
michael@0 | 8 | #define WebAudioUtils_h_ |
michael@0 | 9 | |
michael@0 | 10 | #include <cmath> |
michael@0 | 11 | #include <limits> |
michael@0 | 12 | #include "mozilla/TypeTraits.h" |
michael@0 | 13 | #include "mozilla/FloatingPoint.h" |
michael@0 | 14 | #include "MediaSegment.h" |
michael@0 | 15 | |
michael@0 | 16 | // Forward declaration |
michael@0 | 17 | typedef struct SpeexResamplerState_ SpeexResamplerState; |
michael@0 | 18 | |
michael@0 | 19 | namespace mozilla { |
michael@0 | 20 | |
michael@0 | 21 | class AudioNodeStream; |
michael@0 | 22 | |
michael@0 | 23 | namespace dom { |
michael@0 | 24 | |
michael@0 | 25 | class AudioParamTimeline; |
michael@0 | 26 | |
michael@0 | 27 | namespace WebAudioUtils { |
michael@0 | 28 | // 32 is the minimum required by the spec for createBuffer() and |
michael@0 | 29 | // createScriptProcessor() and matches what is used by Blink. The limit |
michael@0 | 30 | // protects against large memory allocations. |
michael@0 | 31 | const uint32_t MaxChannelCount = 32; |
michael@0 | 32 | // AudioContext::CreateBuffer() "must support sample-rates in at least the |
michael@0 | 33 | // range 22050 to 96000." |
michael@0 | 34 | const uint32_t MinSampleRate = 8000; |
michael@0 | 35 | const uint32_t MaxSampleRate = 192000; |
michael@0 | 36 | |
michael@0 | 37 | inline bool FuzzyEqual(float v1, float v2) |
michael@0 | 38 | { |
michael@0 | 39 | using namespace std; |
michael@0 | 40 | return fabsf(v1 - v2) < 1e-7f; |
michael@0 | 41 | } |
michael@0 | 42 | inline bool FuzzyEqual(double v1, double v2) |
michael@0 | 43 | { |
michael@0 | 44 | using namespace std; |
michael@0 | 45 | return fabs(v1 - v2) < 1e-7; |
michael@0 | 46 | } |
michael@0 | 47 | |
michael@0 | 48 | /** |
michael@0 | 49 | * Computes an exponential smoothing rate for a time based variable |
michael@0 | 50 | * over aDuration seconds. |
michael@0 | 51 | */ |
michael@0 | 52 | inline double ComputeSmoothingRate(double aDuration, double aSampleRate) |
michael@0 | 53 | { |
michael@0 | 54 | return 1.0 - std::exp(-1.0 / (aDuration * aSampleRate)); |
michael@0 | 55 | } |
michael@0 | 56 | |
michael@0 | 57 | /** |
michael@0 | 58 | * Converts AudioParamTimeline floating point time values to tick values |
michael@0 | 59 | * with respect to a source and a destination AudioNodeStream. |
michael@0 | 60 | * |
michael@0 | 61 | * This needs to be called for each AudioParamTimeline that gets sent to an |
michael@0 | 62 | * AudioNodeEngine on the engine side where the AudioParamTimeline is |
michael@0 | 63 | * received. This means that such engines need to be aware of their source |
michael@0 | 64 | * and destination streams as well. |
michael@0 | 65 | */ |
michael@0 | 66 | void ConvertAudioParamToTicks(AudioParamTimeline& aParam, |
michael@0 | 67 | AudioNodeStream* aSource, |
michael@0 | 68 | AudioNodeStream* aDest); |
michael@0 | 69 | |
michael@0 | 70 | /** |
michael@0 | 71 | * Converts a linear value to decibels. Returns aMinDecibels if the linear |
michael@0 | 72 | * value is 0. |
michael@0 | 73 | */ |
michael@0 | 74 | inline float ConvertLinearToDecibels(float aLinearValue, float aMinDecibels) |
michael@0 | 75 | { |
michael@0 | 76 | return aLinearValue ? 20.0f * std::log10(aLinearValue) : aMinDecibels; |
michael@0 | 77 | } |
michael@0 | 78 | |
michael@0 | 79 | /** |
michael@0 | 80 | * Converts a decibel value to a linear value. |
michael@0 | 81 | */ |
michael@0 | 82 | inline float ConvertDecibelsToLinear(float aDecibels) |
michael@0 | 83 | { |
michael@0 | 84 | return std::pow(10.0f, 0.05f * aDecibels); |
michael@0 | 85 | } |
michael@0 | 86 | |
michael@0 | 87 | /** |
michael@0 | 88 | * Converts a decibel to a linear value. |
michael@0 | 89 | */ |
michael@0 | 90 | inline float ConvertDecibelToLinear(float aDecibel) |
michael@0 | 91 | { |
michael@0 | 92 | return std::pow(10.0f, 0.05f * aDecibel); |
michael@0 | 93 | } |
michael@0 | 94 | |
michael@0 | 95 | inline void FixNaN(double& aDouble) |
michael@0 | 96 | { |
michael@0 | 97 | if (IsNaN(aDouble) || IsInfinite(aDouble)) { |
michael@0 | 98 | aDouble = 0.0; |
michael@0 | 99 | } |
michael@0 | 100 | } |
michael@0 | 101 | |
michael@0 | 102 | inline double DiscreteTimeConstantForSampleRate(double timeConstant, double sampleRate) |
michael@0 | 103 | { |
michael@0 | 104 | return 1.0 - std::exp(-1.0 / (sampleRate * timeConstant)); |
michael@0 | 105 | } |
michael@0 | 106 | |
michael@0 | 107 | inline bool IsTimeValid(double aTime) |
michael@0 | 108 | { |
michael@0 | 109 | return aTime >= 0 && aTime <= (MEDIA_TIME_MAX >> MEDIA_TIME_FRAC_BITS); |
michael@0 | 110 | } |
michael@0 | 111 | |
michael@0 | 112 | /** |
michael@0 | 113 | * Converts a floating point value to an integral type in a safe and |
michael@0 | 114 | * platform agnostic way. The following program demonstrates the kinds |
michael@0 | 115 | * of ways things can go wrong depending on the CPU architecture you're |
michael@0 | 116 | * compiling for: |
michael@0 | 117 | * |
michael@0 | 118 | * #include <stdio.h> |
michael@0 | 119 | * volatile float r; |
michael@0 | 120 | * int main() |
michael@0 | 121 | * { |
michael@0 | 122 | * unsigned int q; |
michael@0 | 123 | * r = 1e100; |
michael@0 | 124 | * q = r; |
michael@0 | 125 | * printf("%f %d\n", r, q); |
michael@0 | 126 | * r = -1e100; |
michael@0 | 127 | * q = r; |
michael@0 | 128 | * printf("%f %d\n", r, q); |
michael@0 | 129 | * r = 1e15; |
michael@0 | 130 | * q = r; |
michael@0 | 131 | * printf("%f %x\n", r, q); |
michael@0 | 132 | * r = 0/0.; |
michael@0 | 133 | * q = r; |
michael@0 | 134 | * printf("%f %d\n", r, q); |
michael@0 | 135 | * } |
michael@0 | 136 | * |
michael@0 | 137 | * This program, when compiled for unsigned int, generates the following |
michael@0 | 138 | * results depending on the architecture: |
michael@0 | 139 | * |
michael@0 | 140 | * x86 and x86-64 |
michael@0 | 141 | * --- |
michael@0 | 142 | * inf 0 |
michael@0 | 143 | * -inf 0 |
michael@0 | 144 | * 999999995904.000000 -727384064 d4a50000 |
michael@0 | 145 | * nan 0 |
michael@0 | 146 | * |
michael@0 | 147 | * ARM |
michael@0 | 148 | * --- |
michael@0 | 149 | * inf -1 |
michael@0 | 150 | * -inf 0 |
michael@0 | 151 | * 999999995904.000000 -1 |
michael@0 | 152 | * nan 0 |
michael@0 | 153 | * |
michael@0 | 154 | * When compiled for int, this program generates the following results: |
michael@0 | 155 | * |
michael@0 | 156 | * x86 and x86-64 |
michael@0 | 157 | * --- |
michael@0 | 158 | * inf -2147483648 |
michael@0 | 159 | * -inf -2147483648 |
michael@0 | 160 | * 999999995904.000000 -2147483648 |
michael@0 | 161 | * nan -2147483648 |
michael@0 | 162 | * |
michael@0 | 163 | * ARM |
michael@0 | 164 | * --- |
michael@0 | 165 | * inf 2147483647 |
michael@0 | 166 | * -inf -2147483648 |
michael@0 | 167 | * 999999995904.000000 2147483647 |
michael@0 | 168 | * nan 0 |
michael@0 | 169 | * |
michael@0 | 170 | * Note that the caller is responsible to make sure that the value |
michael@0 | 171 | * passed to this function is not a NaN. This function will abort if |
michael@0 | 172 | * it sees a NaN. |
michael@0 | 173 | */ |
michael@0 | 174 | template <typename IntType, typename FloatType> |
michael@0 | 175 | IntType TruncateFloatToInt(FloatType f) |
michael@0 | 176 | { |
michael@0 | 177 | using namespace std; |
michael@0 | 178 | |
michael@0 | 179 | static_assert(mozilla::IsIntegral<IntType>::value == true, |
michael@0 | 180 | "IntType must be an integral type"); |
michael@0 | 181 | static_assert(mozilla::IsFloatingPoint<FloatType>::value == true, |
michael@0 | 182 | "FloatType must be a floating point type"); |
michael@0 | 183 | |
michael@0 | 184 | if (f != f) { |
michael@0 | 185 | // It is the responsibility of the caller to deal with NaN values. |
michael@0 | 186 | // If we ever get to this point, we have a serious bug to fix. |
michael@0 | 187 | NS_RUNTIMEABORT("We should never see a NaN here"); |
michael@0 | 188 | } |
michael@0 | 189 | |
michael@0 | 190 | if (f > FloatType(numeric_limits<IntType>::max())) { |
michael@0 | 191 | // If the floating point value is outside of the range of maximum |
michael@0 | 192 | // integral value for this type, just clamp to the maximum value. |
michael@0 | 193 | return numeric_limits<IntType>::max(); |
michael@0 | 194 | } |
michael@0 | 195 | |
michael@0 | 196 | if (f < FloatType(numeric_limits<IntType>::min())) { |
michael@0 | 197 | // If the floating point value is outside of the range of minimum |
michael@0 | 198 | // integral value for this type, just clamp to the minimum value. |
michael@0 | 199 | return numeric_limits<IntType>::min(); |
michael@0 | 200 | } |
michael@0 | 201 | |
michael@0 | 202 | // Otherwise, this conversion must be well defined. |
michael@0 | 203 | return IntType(f); |
michael@0 | 204 | } |
michael@0 | 205 | |
michael@0 | 206 | void Shutdown(); |
michael@0 | 207 | |
michael@0 | 208 | int |
michael@0 | 209 | SpeexResamplerProcess(SpeexResamplerState* aResampler, |
michael@0 | 210 | uint32_t aChannel, |
michael@0 | 211 | const float* aIn, uint32_t* aInLen, |
michael@0 | 212 | float* aOut, uint32_t* aOutLen); |
michael@0 | 213 | |
michael@0 | 214 | int |
michael@0 | 215 | SpeexResamplerProcess(SpeexResamplerState* aResampler, |
michael@0 | 216 | uint32_t aChannel, |
michael@0 | 217 | const int16_t* aIn, uint32_t* aInLen, |
michael@0 | 218 | float* aOut, uint32_t* aOutLen); |
michael@0 | 219 | |
michael@0 | 220 | int |
michael@0 | 221 | SpeexResamplerProcess(SpeexResamplerState* aResampler, |
michael@0 | 222 | uint32_t aChannel, |
michael@0 | 223 | const int16_t* aIn, uint32_t* aInLen, |
michael@0 | 224 | int16_t* aOut, uint32_t* aOutLen); |
michael@0 | 225 | } |
michael@0 | 226 | |
michael@0 | 227 | } |
michael@0 | 228 | } |
michael@0 | 229 | |
michael@0 | 230 | #endif |
michael@0 | 231 |