content/media/webaudio/AnalyserNode.cpp

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

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 #include "mozilla/dom/AnalyserNode.h"
michael@0 8 #include "mozilla/dom/AnalyserNodeBinding.h"
michael@0 9 #include "AudioNodeEngine.h"
michael@0 10 #include "AudioNodeStream.h"
michael@0 11 #include "mozilla/Mutex.h"
michael@0 12 #include "mozilla/PodOperations.h"
michael@0 13
michael@0 14 namespace mozilla {
michael@0 15 namespace dom {
michael@0 16
michael@0 17 NS_IMPL_ISUPPORTS_INHERITED0(AnalyserNode, AudioNode)
michael@0 18
michael@0 19 class AnalyserNodeEngine : public AudioNodeEngine
michael@0 20 {
michael@0 21 class TransferBuffer : public nsRunnable
michael@0 22 {
michael@0 23 public:
michael@0 24 TransferBuffer(AudioNodeStream* aStream,
michael@0 25 const AudioChunk& aChunk)
michael@0 26 : mStream(aStream)
michael@0 27 , mChunk(aChunk)
michael@0 28 {
michael@0 29 }
michael@0 30
michael@0 31 NS_IMETHOD Run()
michael@0 32 {
michael@0 33 nsRefPtr<AnalyserNode> node;
michael@0 34 {
michael@0 35 // No need to keep holding the lock for the whole duration of this
michael@0 36 // function, since we're holding a strong reference to it, so if
michael@0 37 // we can obtain the reference, we will hold the node alive in
michael@0 38 // this function.
michael@0 39 MutexAutoLock lock(mStream->Engine()->NodeMutex());
michael@0 40 node = static_cast<AnalyserNode*>(mStream->Engine()->Node());
michael@0 41 }
michael@0 42 if (node) {
michael@0 43 node->AppendChunk(mChunk);
michael@0 44 }
michael@0 45 return NS_OK;
michael@0 46 }
michael@0 47
michael@0 48 private:
michael@0 49 nsRefPtr<AudioNodeStream> mStream;
michael@0 50 AudioChunk mChunk;
michael@0 51 };
michael@0 52
michael@0 53 public:
michael@0 54 explicit AnalyserNodeEngine(AnalyserNode* aNode)
michael@0 55 : AudioNodeEngine(aNode)
michael@0 56 {
michael@0 57 MOZ_ASSERT(NS_IsMainThread());
michael@0 58 }
michael@0 59
michael@0 60 virtual void ProcessBlock(AudioNodeStream* aStream,
michael@0 61 const AudioChunk& aInput,
michael@0 62 AudioChunk* aOutput,
michael@0 63 bool* aFinished) MOZ_OVERRIDE
michael@0 64 {
michael@0 65 *aOutput = aInput;
michael@0 66
michael@0 67 MutexAutoLock lock(NodeMutex());
michael@0 68
michael@0 69 if (Node() &&
michael@0 70 aInput.mChannelData.Length() > 0) {
michael@0 71 nsRefPtr<TransferBuffer> transfer = new TransferBuffer(aStream, aInput);
michael@0 72 NS_DispatchToMainThread(transfer);
michael@0 73 }
michael@0 74 }
michael@0 75
michael@0 76 virtual size_t SizeOfIncludingThis(MallocSizeOf aMallocSizeOf) const MOZ_OVERRIDE
michael@0 77 {
michael@0 78 return aMallocSizeOf(this) + SizeOfExcludingThis(aMallocSizeOf);
michael@0 79 }
michael@0 80 };
michael@0 81
michael@0 82 AnalyserNode::AnalyserNode(AudioContext* aContext)
michael@0 83 : AudioNode(aContext,
michael@0 84 1,
michael@0 85 ChannelCountMode::Explicit,
michael@0 86 ChannelInterpretation::Speakers)
michael@0 87 , mAnalysisBlock(2048)
michael@0 88 , mMinDecibels(-100.)
michael@0 89 , mMaxDecibels(-30.)
michael@0 90 , mSmoothingTimeConstant(.8)
michael@0 91 , mWriteIndex(0)
michael@0 92 {
michael@0 93 mStream = aContext->Graph()->CreateAudioNodeStream(new AnalyserNodeEngine(this),
michael@0 94 MediaStreamGraph::INTERNAL_STREAM);
michael@0 95 AllocateBuffer();
michael@0 96 }
michael@0 97
michael@0 98 size_t
michael@0 99 AnalyserNode::SizeOfExcludingThis(MallocSizeOf aMallocSizeOf) const
michael@0 100 {
michael@0 101 size_t amount = AudioNode::SizeOfExcludingThis(aMallocSizeOf);
michael@0 102 amount += mAnalysisBlock.SizeOfExcludingThis(aMallocSizeOf);
michael@0 103 amount += mBuffer.SizeOfExcludingThis(aMallocSizeOf);
michael@0 104 amount += mOutputBuffer.SizeOfExcludingThis(aMallocSizeOf);
michael@0 105 return amount;
michael@0 106 }
michael@0 107
michael@0 108 size_t
michael@0 109 AnalyserNode::SizeOfIncludingThis(MallocSizeOf aMallocSizeOf) const
michael@0 110 {
michael@0 111 return aMallocSizeOf(this) + SizeOfExcludingThis(aMallocSizeOf);
michael@0 112 }
michael@0 113
michael@0 114 JSObject*
michael@0 115 AnalyserNode::WrapObject(JSContext* aCx)
michael@0 116 {
michael@0 117 return AnalyserNodeBinding::Wrap(aCx, this);
michael@0 118 }
michael@0 119
michael@0 120 void
michael@0 121 AnalyserNode::SetFftSize(uint32_t aValue, ErrorResult& aRv)
michael@0 122 {
michael@0 123 // Disallow values that are not a power of 2 and outside the [32,2048] range
michael@0 124 if (aValue < 32 ||
michael@0 125 aValue > 2048 ||
michael@0 126 (aValue & (aValue - 1)) != 0) {
michael@0 127 aRv.Throw(NS_ERROR_DOM_INDEX_SIZE_ERR);
michael@0 128 return;
michael@0 129 }
michael@0 130 if (FftSize() != aValue) {
michael@0 131 mAnalysisBlock.SetFFTSize(aValue);
michael@0 132 AllocateBuffer();
michael@0 133 }
michael@0 134 }
michael@0 135
michael@0 136 void
michael@0 137 AnalyserNode::SetMinDecibels(double aValue, ErrorResult& aRv)
michael@0 138 {
michael@0 139 if (aValue >= mMaxDecibels) {
michael@0 140 aRv.Throw(NS_ERROR_DOM_INDEX_SIZE_ERR);
michael@0 141 return;
michael@0 142 }
michael@0 143 mMinDecibels = aValue;
michael@0 144 }
michael@0 145
michael@0 146 void
michael@0 147 AnalyserNode::SetMaxDecibels(double aValue, ErrorResult& aRv)
michael@0 148 {
michael@0 149 if (aValue <= mMinDecibels) {
michael@0 150 aRv.Throw(NS_ERROR_DOM_INDEX_SIZE_ERR);
michael@0 151 return;
michael@0 152 }
michael@0 153 mMaxDecibels = aValue;
michael@0 154 }
michael@0 155
michael@0 156 void
michael@0 157 AnalyserNode::SetSmoothingTimeConstant(double aValue, ErrorResult& aRv)
michael@0 158 {
michael@0 159 if (aValue < 0 || aValue > 1) {
michael@0 160 aRv.Throw(NS_ERROR_DOM_INDEX_SIZE_ERR);
michael@0 161 return;
michael@0 162 }
michael@0 163 mSmoothingTimeConstant = aValue;
michael@0 164 }
michael@0 165
michael@0 166 void
michael@0 167 AnalyserNode::GetFloatFrequencyData(const Float32Array& aArray)
michael@0 168 {
michael@0 169 if (!FFTAnalysis()) {
michael@0 170 // Might fail to allocate memory
michael@0 171 return;
michael@0 172 }
michael@0 173
michael@0 174 aArray.ComputeLengthAndData();
michael@0 175
michael@0 176 float* buffer = aArray.Data();
michael@0 177 uint32_t length = std::min(aArray.Length(), mOutputBuffer.Length());
michael@0 178
michael@0 179 for (uint32_t i = 0; i < length; ++i) {
michael@0 180 buffer[i] = WebAudioUtils::ConvertLinearToDecibels(mOutputBuffer[i], mMinDecibels);
michael@0 181 }
michael@0 182 }
michael@0 183
michael@0 184 void
michael@0 185 AnalyserNode::GetByteFrequencyData(const Uint8Array& aArray)
michael@0 186 {
michael@0 187 if (!FFTAnalysis()) {
michael@0 188 // Might fail to allocate memory
michael@0 189 return;
michael@0 190 }
michael@0 191
michael@0 192 const double rangeScaleFactor = 1.0 / (mMaxDecibels - mMinDecibels);
michael@0 193
michael@0 194 aArray.ComputeLengthAndData();
michael@0 195
michael@0 196 unsigned char* buffer = aArray.Data();
michael@0 197 uint32_t length = std::min(aArray.Length(), mOutputBuffer.Length());
michael@0 198
michael@0 199 for (uint32_t i = 0; i < length; ++i) {
michael@0 200 const double decibels = WebAudioUtils::ConvertLinearToDecibels(mOutputBuffer[i], mMinDecibels);
michael@0 201 // scale down the value to the range of [0, UCHAR_MAX]
michael@0 202 const double scaled = std::max(0.0, std::min(double(UCHAR_MAX),
michael@0 203 UCHAR_MAX * (decibels - mMinDecibels) * rangeScaleFactor));
michael@0 204 buffer[i] = static_cast<unsigned char>(scaled);
michael@0 205 }
michael@0 206 }
michael@0 207
michael@0 208 void
michael@0 209 AnalyserNode::GetFloatTimeDomainData(const Float32Array& aArray)
michael@0 210 {
michael@0 211 aArray.ComputeLengthAndData();
michael@0 212
michael@0 213 float* buffer = aArray.Data();
michael@0 214 uint32_t length = std::min(aArray.Length(), mBuffer.Length());
michael@0 215
michael@0 216 for (uint32_t i = 0; i < length; ++i) {
michael@0 217 buffer[i] = mBuffer[(i + mWriteIndex) % mBuffer.Length()];;
michael@0 218 }
michael@0 219 }
michael@0 220
michael@0 221 void
michael@0 222 AnalyserNode::GetByteTimeDomainData(const Uint8Array& aArray)
michael@0 223 {
michael@0 224 aArray.ComputeLengthAndData();
michael@0 225
michael@0 226 unsigned char* buffer = aArray.Data();
michael@0 227 uint32_t length = std::min(aArray.Length(), mBuffer.Length());
michael@0 228
michael@0 229 for (uint32_t i = 0; i < length; ++i) {
michael@0 230 const float value = mBuffer[(i + mWriteIndex) % mBuffer.Length()];
michael@0 231 // scale the value to the range of [0, UCHAR_MAX]
michael@0 232 const float scaled = std::max(0.0f, std::min(float(UCHAR_MAX),
michael@0 233 128.0f * (value + 1.0f)));
michael@0 234 buffer[i] = static_cast<unsigned char>(scaled);
michael@0 235 }
michael@0 236 }
michael@0 237
michael@0 238 bool
michael@0 239 AnalyserNode::FFTAnalysis()
michael@0 240 {
michael@0 241 float* inputBuffer;
michael@0 242 bool allocated = false;
michael@0 243 if (mWriteIndex == 0) {
michael@0 244 inputBuffer = mBuffer.Elements();
michael@0 245 } else {
michael@0 246 inputBuffer = static_cast<float*>(moz_malloc(FftSize() * sizeof(float)));
michael@0 247 if (!inputBuffer) {
michael@0 248 return false;
michael@0 249 }
michael@0 250 memcpy(inputBuffer, mBuffer.Elements() + mWriteIndex, sizeof(float) * (FftSize() - mWriteIndex));
michael@0 251 memcpy(inputBuffer + FftSize() - mWriteIndex, mBuffer.Elements(), sizeof(float) * mWriteIndex);
michael@0 252 allocated = true;
michael@0 253 }
michael@0 254
michael@0 255 ApplyBlackmanWindow(inputBuffer, FftSize());
michael@0 256
michael@0 257 mAnalysisBlock.PerformFFT(inputBuffer);
michael@0 258
michael@0 259 // Normalize so than an input sine wave at 0dBfs registers as 0dBfs (undo FFT scaling factor).
michael@0 260 const double magnitudeScale = 1.0 / FftSize();
michael@0 261
michael@0 262 for (uint32_t i = 0; i < mOutputBuffer.Length(); ++i) {
michael@0 263 double scalarMagnitude = NS_hypot(mAnalysisBlock.RealData(i),
michael@0 264 mAnalysisBlock.ImagData(i)) *
michael@0 265 magnitudeScale;
michael@0 266 mOutputBuffer[i] = mSmoothingTimeConstant * mOutputBuffer[i] +
michael@0 267 (1.0 - mSmoothingTimeConstant) * scalarMagnitude;
michael@0 268 }
michael@0 269
michael@0 270 if (allocated) {
michael@0 271 moz_free(inputBuffer);
michael@0 272 }
michael@0 273 return true;
michael@0 274 }
michael@0 275
michael@0 276 void
michael@0 277 AnalyserNode::ApplyBlackmanWindow(float* aBuffer, uint32_t aSize)
michael@0 278 {
michael@0 279 double alpha = 0.16;
michael@0 280 double a0 = 0.5 * (1.0 - alpha);
michael@0 281 double a1 = 0.5;
michael@0 282 double a2 = 0.5 * alpha;
michael@0 283
michael@0 284 for (uint32_t i = 0; i < aSize; ++i) {
michael@0 285 double x = double(i) / aSize;
michael@0 286 double window = a0 - a1 * cos(2 * M_PI * x) + a2 * cos(4 * M_PI * x);
michael@0 287 aBuffer[i] *= window;
michael@0 288 }
michael@0 289 }
michael@0 290
michael@0 291 bool
michael@0 292 AnalyserNode::AllocateBuffer()
michael@0 293 {
michael@0 294 bool result = true;
michael@0 295 if (mBuffer.Length() != FftSize()) {
michael@0 296 result = mBuffer.SetLength(FftSize());
michael@0 297 if (result) {
michael@0 298 memset(mBuffer.Elements(), 0, sizeof(float) * FftSize());
michael@0 299 mWriteIndex = 0;
michael@0 300
michael@0 301 result = mOutputBuffer.SetLength(FrequencyBinCount());
michael@0 302 if (result) {
michael@0 303 memset(mOutputBuffer.Elements(), 0, sizeof(float) * FrequencyBinCount());
michael@0 304 }
michael@0 305 }
michael@0 306 }
michael@0 307 return result;
michael@0 308 }
michael@0 309
michael@0 310 void
michael@0 311 AnalyserNode::AppendChunk(const AudioChunk& aChunk)
michael@0 312 {
michael@0 313 const uint32_t bufferSize = mBuffer.Length();
michael@0 314 const uint32_t channelCount = aChunk.mChannelData.Length();
michael@0 315 uint32_t chunkDuration = aChunk.mDuration;
michael@0 316 MOZ_ASSERT((bufferSize & (bufferSize - 1)) == 0); // Must be a power of two!
michael@0 317 MOZ_ASSERT(channelCount > 0);
michael@0 318 MOZ_ASSERT(chunkDuration == WEBAUDIO_BLOCK_SIZE);
michael@0 319
michael@0 320 if (chunkDuration > bufferSize) {
michael@0 321 // Copy a maximum bufferSize samples.
michael@0 322 chunkDuration = bufferSize;
michael@0 323 }
michael@0 324
michael@0 325 PodCopy(mBuffer.Elements() + mWriteIndex, static_cast<const float*>(aChunk.mChannelData[0]), chunkDuration);
michael@0 326 for (uint32_t i = 1; i < channelCount; ++i) {
michael@0 327 AudioBlockAddChannelWithScale(static_cast<const float*>(aChunk.mChannelData[i]), 1.0f,
michael@0 328 mBuffer.Elements() + mWriteIndex);
michael@0 329 }
michael@0 330 if (channelCount > 1) {
michael@0 331 AudioBlockInPlaceScale(mBuffer.Elements() + mWriteIndex,
michael@0 332 1.0f / aChunk.mChannelData.Length());
michael@0 333 }
michael@0 334 mWriteIndex += chunkDuration;
michael@0 335 MOZ_ASSERT(mWriteIndex <= bufferSize);
michael@0 336 if (mWriteIndex >= bufferSize) {
michael@0 337 mWriteIndex = 0;
michael@0 338 }
michael@0 339 }
michael@0 340
michael@0 341 }
michael@0 342 }
michael@0 343

mercurial