michael@0: /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ michael@0: /* vim:set ts=2 sw=2 sts=2 et cindent: */ michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: #ifndef WebAudioUtils_h_ michael@0: #define WebAudioUtils_h_ michael@0: michael@0: #include michael@0: #include michael@0: #include "mozilla/TypeTraits.h" michael@0: #include "mozilla/FloatingPoint.h" michael@0: #include "MediaSegment.h" michael@0: michael@0: // Forward declaration michael@0: typedef struct SpeexResamplerState_ SpeexResamplerState; michael@0: michael@0: namespace mozilla { michael@0: michael@0: class AudioNodeStream; michael@0: michael@0: namespace dom { michael@0: michael@0: class AudioParamTimeline; michael@0: michael@0: namespace WebAudioUtils { michael@0: // 32 is the minimum required by the spec for createBuffer() and michael@0: // createScriptProcessor() and matches what is used by Blink. The limit michael@0: // protects against large memory allocations. michael@0: const uint32_t MaxChannelCount = 32; michael@0: // AudioContext::CreateBuffer() "must support sample-rates in at least the michael@0: // range 22050 to 96000." michael@0: const uint32_t MinSampleRate = 8000; michael@0: const uint32_t MaxSampleRate = 192000; michael@0: michael@0: inline bool FuzzyEqual(float v1, float v2) michael@0: { michael@0: using namespace std; michael@0: return fabsf(v1 - v2) < 1e-7f; michael@0: } michael@0: inline bool FuzzyEqual(double v1, double v2) michael@0: { michael@0: using namespace std; michael@0: return fabs(v1 - v2) < 1e-7; michael@0: } michael@0: michael@0: /** michael@0: * Computes an exponential smoothing rate for a time based variable michael@0: * over aDuration seconds. michael@0: */ michael@0: inline double ComputeSmoothingRate(double aDuration, double aSampleRate) michael@0: { michael@0: return 1.0 - std::exp(-1.0 / (aDuration * aSampleRate)); michael@0: } michael@0: michael@0: /** michael@0: * Converts AudioParamTimeline floating point time values to tick values michael@0: * with respect to a source and a destination AudioNodeStream. michael@0: * michael@0: * This needs to be called for each AudioParamTimeline that gets sent to an michael@0: * AudioNodeEngine on the engine side where the AudioParamTimeline is michael@0: * received. This means that such engines need to be aware of their source michael@0: * and destination streams as well. michael@0: */ michael@0: void ConvertAudioParamToTicks(AudioParamTimeline& aParam, michael@0: AudioNodeStream* aSource, michael@0: AudioNodeStream* aDest); michael@0: michael@0: /** michael@0: * Converts a linear value to decibels. Returns aMinDecibels if the linear michael@0: * value is 0. michael@0: */ michael@0: inline float ConvertLinearToDecibels(float aLinearValue, float aMinDecibels) michael@0: { michael@0: return aLinearValue ? 20.0f * std::log10(aLinearValue) : aMinDecibels; michael@0: } michael@0: michael@0: /** michael@0: * Converts a decibel value to a linear value. michael@0: */ michael@0: inline float ConvertDecibelsToLinear(float aDecibels) michael@0: { michael@0: return std::pow(10.0f, 0.05f * aDecibels); michael@0: } michael@0: michael@0: /** michael@0: * Converts a decibel to a linear value. michael@0: */ michael@0: inline float ConvertDecibelToLinear(float aDecibel) michael@0: { michael@0: return std::pow(10.0f, 0.05f * aDecibel); michael@0: } michael@0: michael@0: inline void FixNaN(double& aDouble) michael@0: { michael@0: if (IsNaN(aDouble) || IsInfinite(aDouble)) { michael@0: aDouble = 0.0; michael@0: } michael@0: } michael@0: michael@0: inline double DiscreteTimeConstantForSampleRate(double timeConstant, double sampleRate) michael@0: { michael@0: return 1.0 - std::exp(-1.0 / (sampleRate * timeConstant)); michael@0: } michael@0: michael@0: inline bool IsTimeValid(double aTime) michael@0: { michael@0: return aTime >= 0 && aTime <= (MEDIA_TIME_MAX >> MEDIA_TIME_FRAC_BITS); michael@0: } michael@0: michael@0: /** michael@0: * Converts a floating point value to an integral type in a safe and michael@0: * platform agnostic way. The following program demonstrates the kinds michael@0: * of ways things can go wrong depending on the CPU architecture you're michael@0: * compiling for: michael@0: * michael@0: * #include michael@0: * volatile float r; michael@0: * int main() michael@0: * { michael@0: * unsigned int q; michael@0: * r = 1e100; michael@0: * q = r; michael@0: * printf("%f %d\n", r, q); michael@0: * r = -1e100; michael@0: * q = r; michael@0: * printf("%f %d\n", r, q); michael@0: * r = 1e15; michael@0: * q = r; michael@0: * printf("%f %x\n", r, q); michael@0: * r = 0/0.; michael@0: * q = r; michael@0: * printf("%f %d\n", r, q); michael@0: * } michael@0: * michael@0: * This program, when compiled for unsigned int, generates the following michael@0: * results depending on the architecture: michael@0: * michael@0: * x86 and x86-64 michael@0: * --- michael@0: * inf 0 michael@0: * -inf 0 michael@0: * 999999995904.000000 -727384064 d4a50000 michael@0: * nan 0 michael@0: * michael@0: * ARM michael@0: * --- michael@0: * inf -1 michael@0: * -inf 0 michael@0: * 999999995904.000000 -1 michael@0: * nan 0 michael@0: * michael@0: * When compiled for int, this program generates the following results: michael@0: * michael@0: * x86 and x86-64 michael@0: * --- michael@0: * inf -2147483648 michael@0: * -inf -2147483648 michael@0: * 999999995904.000000 -2147483648 michael@0: * nan -2147483648 michael@0: * michael@0: * ARM michael@0: * --- michael@0: * inf 2147483647 michael@0: * -inf -2147483648 michael@0: * 999999995904.000000 2147483647 michael@0: * nan 0 michael@0: * michael@0: * Note that the caller is responsible to make sure that the value michael@0: * passed to this function is not a NaN. This function will abort if michael@0: * it sees a NaN. michael@0: */ michael@0: template michael@0: IntType TruncateFloatToInt(FloatType f) michael@0: { michael@0: using namespace std; michael@0: michael@0: static_assert(mozilla::IsIntegral::value == true, michael@0: "IntType must be an integral type"); michael@0: static_assert(mozilla::IsFloatingPoint::value == true, michael@0: "FloatType must be a floating point type"); michael@0: michael@0: if (f != f) { michael@0: // It is the responsibility of the caller to deal with NaN values. michael@0: // If we ever get to this point, we have a serious bug to fix. michael@0: NS_RUNTIMEABORT("We should never see a NaN here"); michael@0: } michael@0: michael@0: if (f > FloatType(numeric_limits::max())) { michael@0: // If the floating point value is outside of the range of maximum michael@0: // integral value for this type, just clamp to the maximum value. michael@0: return numeric_limits::max(); michael@0: } michael@0: michael@0: if (f < FloatType(numeric_limits::min())) { michael@0: // If the floating point value is outside of the range of minimum michael@0: // integral value for this type, just clamp to the minimum value. michael@0: return numeric_limits::min(); michael@0: } michael@0: michael@0: // Otherwise, this conversion must be well defined. michael@0: return IntType(f); michael@0: } michael@0: michael@0: void Shutdown(); michael@0: michael@0: int michael@0: SpeexResamplerProcess(SpeexResamplerState* aResampler, michael@0: uint32_t aChannel, michael@0: const float* aIn, uint32_t* aInLen, michael@0: float* aOut, uint32_t* aOutLen); michael@0: michael@0: int michael@0: SpeexResamplerProcess(SpeexResamplerState* aResampler, michael@0: uint32_t aChannel, michael@0: const int16_t* aIn, uint32_t* aInLen, michael@0: float* aOut, uint32_t* aOutLen); michael@0: michael@0: int michael@0: SpeexResamplerProcess(SpeexResamplerState* aResampler, michael@0: uint32_t aChannel, michael@0: const int16_t* aIn, uint32_t* aInLen, michael@0: int16_t* aOut, uint32_t* aOutLen); michael@0: } michael@0: michael@0: } michael@0: } michael@0: michael@0: #endif michael@0: