From df408748acce00c2e4ff4940d5f7095da449f892 Mon Sep 17 00:00:00 2001 From: Karl Tomlinson Date: Fri, 16 May 2014 08:44:17 +1200 Subject: [PATCH] b=991533 move sample rate limits to WebAudioUtils r=padenot Also change WebAudioUtils from a class to a namespace, so that constant variables can be defined inline with internal linkage. static class variables cannot be defined inline because this violates the one definition rule, even though some compilers may not notice. --HG-- extra : transplant_source : %9F4%2Ct%BA%D2%BD%8A1Xev%92%C0%A1%AD%88IH%BF --- content/media/webaudio/AudioContext.cpp | 4 +- content/media/webaudio/WebAudioUtils.cpp | 4 -- content/media/webaudio/WebAudioUtils.h | 47 +++++++++++-------- .../blink/DynamicsCompressorKernel.cpp | 2 +- 4 files changed, 31 insertions(+), 26 deletions(-) diff --git a/content/media/webaudio/AudioContext.cpp b/content/media/webaudio/AudioContext.cpp index beaa28708ef..9ed4b91ad50 100644 --- a/content/media/webaudio/AudioContext.cpp +++ b/content/media/webaudio/AudioContext.cpp @@ -204,7 +204,9 @@ AudioContext::CreateBuffer(JSContext* aJSContext, uint32_t aNumberOfChannels, uint32_t aLength, float aSampleRate, ErrorResult& aRv) { - if (aSampleRate < 8000 || aSampleRate > 192000 || !aLength || !aNumberOfChannels) { + if (aSampleRate < WebAudioUtils::MinSampleRate || + aSampleRate > WebAudioUtils::MaxSampleRate || + !aLength || !aNumberOfChannels) { aRv.Throw(NS_ERROR_DOM_INDEX_SIZE_ERR); return nullptr; } diff --git a/content/media/webaudio/WebAudioUtils.cpp b/content/media/webaudio/WebAudioUtils.cpp index 4d5f7cd40cc..47ecabb7d6d 100644 --- a/content/media/webaudio/WebAudioUtils.cpp +++ b/content/media/webaudio/WebAudioUtils.cpp @@ -14,10 +14,6 @@ namespace mozilla { namespace dom { -// 32 is the minimum required by the spec and matches what is used by blink. -// The limit protects against large memory allocations. -const size_t WebAudioUtils::MaxChannelCount = 32; - struct ConvertTimeToTickHelper { AudioNodeStream* mSourceStream; diff --git a/content/media/webaudio/WebAudioUtils.h b/content/media/webaudio/WebAudioUtils.h index 771af3888e1..19abe1123fe 100644 --- a/content/media/webaudio/WebAudioUtils.h +++ b/content/media/webaudio/WebAudioUtils.h @@ -24,15 +24,22 @@ namespace dom { class AudioParamTimeline; -struct WebAudioUtils { - static const size_t MaxChannelCount; +namespace WebAudioUtils { + // 32 is the minimum required by the spec for createBuffer() and + // createScriptProcessor() and matches what is used by Blink. The limit + // protects against large memory allocations. + const size_t MaxChannelCount = 32; + // AudioContext::CreateBuffer() "must support sample-rates in at least the + // range 22050 to 96000." + const uint32_t MinSampleRate = 8000; + const uint32_t MaxSampleRate = 192000; - static bool FuzzyEqual(float v1, float v2) + inline bool FuzzyEqual(float v1, float v2) { using namespace std; return fabsf(v1 - v2) < 1e-7f; } - static bool FuzzyEqual(double v1, double v2) + inline bool FuzzyEqual(double v1, double v2) { using namespace std; return fabs(v1 - v2) < 1e-7; @@ -42,7 +49,7 @@ struct WebAudioUtils { * Computes an exponential smoothing rate for a time based variable * over aDuration seconds. */ - static double ComputeSmoothingRate(double aDuration, double aSampleRate) + inline double ComputeSmoothingRate(double aDuration, double aSampleRate) { return 1.0 - std::exp(-1.0 / (aDuration * aSampleRate)); } @@ -56,15 +63,15 @@ struct WebAudioUtils { * received. This means that such engines need to be aware of their source * and destination streams as well. */ - static void ConvertAudioParamToTicks(AudioParamTimeline& aParam, - AudioNodeStream* aSource, - AudioNodeStream* aDest); + void ConvertAudioParamToTicks(AudioParamTimeline& aParam, + AudioNodeStream* aSource, + AudioNodeStream* aDest); /** * Converts a linear value to decibels. Returns aMinDecibels if the linear * value is 0. */ - static float ConvertLinearToDecibels(float aLinearValue, float aMinDecibels) + inline float ConvertLinearToDecibels(float aLinearValue, float aMinDecibels) { return aLinearValue ? 20.0f * std::log10(aLinearValue) : aMinDecibels; } @@ -72,7 +79,7 @@ struct WebAudioUtils { /** * Converts a decibel value to a linear value. */ - static float ConvertDecibelsToLinear(float aDecibels) + inline float ConvertDecibelsToLinear(float aDecibels) { return std::pow(10.0f, 0.05f * aDecibels); } @@ -80,24 +87,24 @@ struct WebAudioUtils { /** * Converts a decibel to a linear value. */ - static float ConvertDecibelToLinear(float aDecibel) + inline float ConvertDecibelToLinear(float aDecibel) { return std::pow(10.0f, 0.05f * aDecibel); } - static void FixNaN(double& aDouble) + inline void FixNaN(double& aDouble) { if (IsNaN(aDouble) || IsInfinite(aDouble)) { aDouble = 0.0; } } - static double DiscreteTimeConstantForSampleRate(double timeConstant, double sampleRate) + inline double DiscreteTimeConstantForSampleRate(double timeConstant, double sampleRate) { return 1.0 - std::exp(-1.0 / (sampleRate * timeConstant)); } - static bool IsTimeValid(double aTime) + inline bool IsTimeValid(double aTime) { return aTime >= 0 && aTime <= (MEDIA_TIME_MAX >> MEDIA_TIME_FRAC_BITS); } @@ -165,7 +172,7 @@ struct WebAudioUtils { * it sees a NaN. */ template - static IntType TruncateFloatToInt(FloatType f) + IntType TruncateFloatToInt(FloatType f) { using namespace std; @@ -196,26 +203,26 @@ struct WebAudioUtils { return IntType(f); } - static void Shutdown(); + void Shutdown(); - static int + int SpeexResamplerProcess(SpeexResamplerState* aResampler, uint32_t aChannel, const float* aIn, uint32_t* aInLen, float* aOut, uint32_t* aOutLen); - static int + int SpeexResamplerProcess(SpeexResamplerState* aResampler, uint32_t aChannel, const int16_t* aIn, uint32_t* aInLen, float* aOut, uint32_t* aOutLen); - static int + int SpeexResamplerProcess(SpeexResamplerState* aResampler, uint32_t aChannel, const int16_t* aIn, uint32_t* aInLen, int16_t* aOut, uint32_t* aOutLen); - }; + } } } diff --git a/content/media/webaudio/blink/DynamicsCompressorKernel.cpp b/content/media/webaudio/blink/DynamicsCompressorKernel.cpp index 57a71ed8ded..c6f9256e3c2 100644 --- a/content/media/webaudio/blink/DynamicsCompressorKernel.cpp +++ b/content/media/webaudio/blink/DynamicsCompressorKernel.cpp @@ -37,7 +37,7 @@ using namespace std; -using mozilla::dom::WebAudioUtils; +using namespace mozilla::dom; // for WebAudioUtils using mozilla::IsInfinite; using mozilla::IsNaN;