2013-03-21 00:12:07 +00:00
|
|
|
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
|
|
|
/* vim:set ts=2 sw=2 sts=2 et cindent: */
|
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
|
|
|
|
|
|
|
#ifndef WebAudioUtils_h_
|
|
|
|
#define WebAudioUtils_h_
|
|
|
|
|
|
|
|
#include <cmath>
|
2013-05-06 15:34:03 +00:00
|
|
|
#include <limits>
|
|
|
|
#include "mozilla/TypeTraits.h"
|
2013-08-15 19:44:14 +00:00
|
|
|
#include "mozilla/FloatingPoint.h"
|
2013-04-14 01:37:04 +00:00
|
|
|
#include "MediaSegment.h"
|
2013-03-21 00:12:07 +00:00
|
|
|
|
2013-10-01 06:22:54 +00:00
|
|
|
// Forward declaration
|
|
|
|
typedef struct SpeexResamplerState_ SpeexResamplerState;
|
|
|
|
|
2013-03-21 00:12:07 +00:00
|
|
|
namespace mozilla {
|
|
|
|
|
2013-03-22 04:01:28 +00:00
|
|
|
class AudioNodeStream;
|
|
|
|
|
2015-11-23 23:48:17 +00:00
|
|
|
extern LazyLogModule gWebAudioAPILog;
|
|
|
|
#define WEB_AUDIO_API_LOG(...) \
|
|
|
|
MOZ_LOG(gWebAudioAPILog, LogLevel::Debug, (__VA_ARGS__))
|
|
|
|
|
2013-03-21 00:12:07 +00:00
|
|
|
namespace dom {
|
|
|
|
|
2015-09-25 13:57:55 +00:00
|
|
|
struct AudioTimelineEvent;
|
2013-08-15 19:44:14 +00:00
|
|
|
|
2014-05-15 20:44:17 +00:00
|
|
|
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;
|
|
|
|
|
|
|
|
inline bool FuzzyEqual(float v1, float v2)
|
2013-03-21 00:12:07 +00:00
|
|
|
{
|
|
|
|
using namespace std;
|
|
|
|
return fabsf(v1 - v2) < 1e-7f;
|
|
|
|
}
|
2014-05-15 20:44:17 +00:00
|
|
|
inline bool FuzzyEqual(double v1, double v2)
|
2013-03-21 00:12:07 +00:00
|
|
|
{
|
|
|
|
using namespace std;
|
|
|
|
return fabs(v1 - v2) < 1e-7;
|
|
|
|
}
|
2013-03-22 04:01:28 +00:00
|
|
|
|
2013-03-25 12:34:59 +00:00
|
|
|
/**
|
|
|
|
* Computes an exponential smoothing rate for a time based variable
|
|
|
|
* over aDuration seconds.
|
|
|
|
*/
|
2014-05-15 20:44:17 +00:00
|
|
|
inline double ComputeSmoothingRate(double aDuration, double aSampleRate)
|
2013-03-25 12:34:59 +00:00
|
|
|
{
|
|
|
|
return 1.0 - std::exp(-1.0 / (aDuration * aSampleRate));
|
|
|
|
}
|
|
|
|
|
2013-03-22 04:01:28 +00:00
|
|
|
/**
|
2015-09-25 13:57:55 +00:00
|
|
|
* Converts an AudioTimelineEvent's floating point time values to tick values
|
|
|
|
* with respect to a destination AudioNodeStream.
|
2013-03-22 04:01:28 +00:00
|
|
|
*
|
2015-09-25 13:57:55 +00:00
|
|
|
* This needs to be called for each AudioTimelineEvent that gets sent to an
|
|
|
|
* AudioNodeEngine, on the engine side where the AudioTimlineEvent is
|
|
|
|
* received. This means that such engines need to be aware of their
|
|
|
|
* destination streams as well.
|
2013-03-22 04:01:28 +00:00
|
|
|
*/
|
2015-09-25 13:57:55 +00:00
|
|
|
void ConvertAudioTimelineEventToTicks(AudioTimelineEvent& aEvent,
|
|
|
|
AudioNodeStream* aDest);
|
2013-04-01 23:26:17 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Converts a linear value to decibels. Returns aMinDecibels if the linear
|
|
|
|
* value is 0.
|
|
|
|
*/
|
2014-05-15 20:44:17 +00:00
|
|
|
inline float ConvertLinearToDecibels(float aLinearValue, float aMinDecibels)
|
2013-04-01 23:26:17 +00:00
|
|
|
{
|
|
|
|
return aLinearValue ? 20.0f * std::log10(aLinearValue) : aMinDecibels;
|
|
|
|
}
|
|
|
|
|
2013-04-21 03:12:00 +00:00
|
|
|
/**
|
|
|
|
* Converts a decibel value to a linear value.
|
|
|
|
*/
|
2014-05-15 20:44:17 +00:00
|
|
|
inline float ConvertDecibelsToLinear(float aDecibels)
|
2013-04-21 03:12:00 +00:00
|
|
|
{
|
|
|
|
return std::pow(10.0f, 0.05f * aDecibels);
|
|
|
|
}
|
|
|
|
|
2013-04-01 23:26:17 +00:00
|
|
|
/**
|
|
|
|
* Converts a decibel to a linear value.
|
|
|
|
*/
|
2014-05-15 20:44:17 +00:00
|
|
|
inline float ConvertDecibelToLinear(float aDecibel)
|
2013-04-01 23:26:17 +00:00
|
|
|
{
|
|
|
|
return std::pow(10.0f, 0.05f * aDecibel);
|
|
|
|
}
|
2013-04-11 12:47:57 +00:00
|
|
|
|
2014-05-15 20:44:17 +00:00
|
|
|
inline void FixNaN(double& aDouble)
|
2013-04-11 12:47:57 +00:00
|
|
|
{
|
2013-05-01 20:55:13 +00:00
|
|
|
if (IsNaN(aDouble) || IsInfinite(aDouble)) {
|
2013-04-11 12:47:57 +00:00
|
|
|
aDouble = 0.0;
|
|
|
|
}
|
|
|
|
}
|
2013-04-14 01:37:04 +00:00
|
|
|
|
2014-05-15 20:44:17 +00:00
|
|
|
inline double DiscreteTimeConstantForSampleRate(double timeConstant, double sampleRate)
|
2013-04-21 03:12:00 +00:00
|
|
|
{
|
|
|
|
return 1.0 - std::exp(-1.0 / (sampleRate * timeConstant));
|
|
|
|
}
|
|
|
|
|
2014-05-15 20:44:17 +00:00
|
|
|
inline bool IsTimeValid(double aTime)
|
2013-05-31 00:53:15 +00:00
|
|
|
{
|
2014-06-12 04:45:00 +00:00
|
|
|
return aTime >= 0 && aTime <= (MEDIA_TIME_MAX >> TRACK_RATE_MAX_BITS);
|
2013-05-31 00:53:15 +00:00
|
|
|
}
|
|
|
|
|
2013-05-06 15:34:03 +00:00
|
|
|
/**
|
|
|
|
* Converts a floating point value to an integral type in a safe and
|
|
|
|
* platform agnostic way. The following program demonstrates the kinds
|
|
|
|
* of ways things can go wrong depending on the CPU architecture you're
|
|
|
|
* compiling for:
|
|
|
|
*
|
|
|
|
* #include <stdio.h>
|
|
|
|
* volatile float r;
|
|
|
|
* int main()
|
|
|
|
* {
|
|
|
|
* unsigned int q;
|
|
|
|
* r = 1e100;
|
|
|
|
* q = r;
|
|
|
|
* printf("%f %d\n", r, q);
|
|
|
|
* r = -1e100;
|
|
|
|
* q = r;
|
|
|
|
* printf("%f %d\n", r, q);
|
|
|
|
* r = 1e15;
|
|
|
|
* q = r;
|
|
|
|
* printf("%f %x\n", r, q);
|
|
|
|
* r = 0/0.;
|
|
|
|
* q = r;
|
|
|
|
* printf("%f %d\n", r, q);
|
|
|
|
* }
|
|
|
|
*
|
|
|
|
* This program, when compiled for unsigned int, generates the following
|
|
|
|
* results depending on the architecture:
|
|
|
|
*
|
|
|
|
* x86 and x86-64
|
|
|
|
* ---
|
|
|
|
* inf 0
|
|
|
|
* -inf 0
|
|
|
|
* 999999995904.000000 -727384064 d4a50000
|
|
|
|
* nan 0
|
|
|
|
*
|
|
|
|
* ARM
|
|
|
|
* ---
|
|
|
|
* inf -1
|
|
|
|
* -inf 0
|
|
|
|
* 999999995904.000000 -1
|
|
|
|
* nan 0
|
|
|
|
*
|
|
|
|
* When compiled for int, this program generates the following results:
|
|
|
|
*
|
|
|
|
* x86 and x86-64
|
|
|
|
* ---
|
|
|
|
* inf -2147483648
|
|
|
|
* -inf -2147483648
|
|
|
|
* 999999995904.000000 -2147483648
|
|
|
|
* nan -2147483648
|
|
|
|
*
|
|
|
|
* ARM
|
|
|
|
* ---
|
|
|
|
* inf 2147483647
|
|
|
|
* -inf -2147483648
|
|
|
|
* 999999995904.000000 2147483647
|
|
|
|
* nan 0
|
|
|
|
*
|
|
|
|
* Note that the caller is responsible to make sure that the value
|
|
|
|
* passed to this function is not a NaN. This function will abort if
|
|
|
|
* it sees a NaN.
|
|
|
|
*/
|
|
|
|
template <typename IntType, typename FloatType>
|
2014-05-15 20:44:17 +00:00
|
|
|
IntType TruncateFloatToInt(FloatType f)
|
2013-05-06 15:34:03 +00:00
|
|
|
{
|
|
|
|
using namespace std;
|
|
|
|
|
2013-07-18 17:59:53 +00:00
|
|
|
static_assert(mozilla::IsIntegral<IntType>::value == true,
|
|
|
|
"IntType must be an integral type");
|
|
|
|
static_assert(mozilla::IsFloatingPoint<FloatType>::value == true,
|
|
|
|
"FloatType must be a floating point type");
|
2013-05-06 15:34:03 +00:00
|
|
|
|
2014-12-22 13:54:55 +00:00
|
|
|
if (mozilla::IsNaN(f)) {
|
2013-05-06 15:34:03 +00:00
|
|
|
// It is the responsibility of the caller to deal with NaN values.
|
|
|
|
// If we ever get to this point, we have a serious bug to fix.
|
|
|
|
NS_RUNTIMEABORT("We should never see a NaN here");
|
|
|
|
}
|
|
|
|
|
|
|
|
if (f > FloatType(numeric_limits<IntType>::max())) {
|
|
|
|
// If the floating point value is outside of the range of maximum
|
|
|
|
// integral value for this type, just clamp to the maximum value.
|
|
|
|
return numeric_limits<IntType>::max();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (f < FloatType(numeric_limits<IntType>::min())) {
|
|
|
|
// If the floating point value is outside of the range of minimum
|
|
|
|
// integral value for this type, just clamp to the minimum value.
|
|
|
|
return numeric_limits<IntType>::min();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Otherwise, this conversion must be well defined.
|
|
|
|
return IntType(f);
|
|
|
|
}
|
2013-08-08 22:07:42 +00:00
|
|
|
|
2014-05-15 20:44:17 +00:00
|
|
|
void Shutdown();
|
2013-10-01 06:22:54 +00:00
|
|
|
|
2014-05-15 20:44:17 +00:00
|
|
|
int
|
2013-10-01 06:22:54 +00:00
|
|
|
SpeexResamplerProcess(SpeexResamplerState* aResampler,
|
|
|
|
uint32_t aChannel,
|
|
|
|
const float* aIn, uint32_t* aInLen,
|
|
|
|
float* aOut, uint32_t* aOutLen);
|
|
|
|
|
2014-05-15 20:44:17 +00:00
|
|
|
int
|
2013-10-01 06:22:54 +00:00
|
|
|
SpeexResamplerProcess(SpeexResamplerState* aResampler,
|
|
|
|
uint32_t aChannel,
|
|
|
|
const int16_t* aIn, uint32_t* aInLen,
|
|
|
|
float* aOut, uint32_t* aOutLen);
|
2014-03-24 10:06:05 +00:00
|
|
|
|
2014-05-15 20:44:17 +00:00
|
|
|
int
|
2014-03-24 10:06:05 +00:00
|
|
|
SpeexResamplerProcess(SpeexResamplerState* aResampler,
|
|
|
|
uint32_t aChannel,
|
|
|
|
const int16_t* aIn, uint32_t* aInLen,
|
|
|
|
int16_t* aOut, uint32_t* aOutLen);
|
2016-06-03 18:23:11 +00:00
|
|
|
|
|
|
|
void
|
|
|
|
LogToDeveloperConsole(uint64_t aWindowID, const char* aKey);
|
|
|
|
|
2015-07-13 15:25:42 +00:00
|
|
|
} // namespace WebAudioUtils
|
2013-03-21 00:12:07 +00:00
|
|
|
|
2015-07-13 15:25:42 +00:00
|
|
|
} // namespace dom
|
|
|
|
} // namespace mozilla
|
2013-03-21 00:12:07 +00:00
|
|
|
|
|
|
|
#endif
|
|
|
|
|