summaryrefslogtreecommitdiffstats
path: root/dom/media/webaudio/AnalyserNode.cpp
blob: d8b547477e55e269abf6af73c1dd9372059d3e7f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
/* -*- 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/. */

#include "mozilla/dom/AnalyserNode.h"
#include "mozilla/dom/AnalyserNodeBinding.h"
#include "AudioNodeEngine.h"
#include "AudioNodeTrack.h"
#include "mozilla/Mutex.h"
#include "mozilla/PodOperations.h"
#include "nsMathUtils.h"
#include "Tracing.h"

namespace mozilla {

static const uint32_t MAX_FFT_SIZE = 32768;
static const size_t CHUNK_COUNT = MAX_FFT_SIZE >> WEBAUDIO_BLOCK_SIZE_BITS;
static_assert(MAX_FFT_SIZE == CHUNK_COUNT * WEBAUDIO_BLOCK_SIZE,
              "MAX_FFT_SIZE must be a multiple of WEBAUDIO_BLOCK_SIZE");
static_assert((CHUNK_COUNT & (CHUNK_COUNT - 1)) == 0,
              "CHUNK_COUNT must be power of 2 for remainder behavior");

namespace dom {

class AnalyserNodeEngine final : public AudioNodeEngine {
  class TransferBuffer final : public Runnable {
   public:
    TransferBuffer(AudioNodeTrack* aTrack, const AudioChunk& aChunk)
        : Runnable("dom::AnalyserNodeEngine::TransferBuffer"),
          mTrack(aTrack),
          mChunk(aChunk) {}

    NS_IMETHOD Run() override {
      RefPtr<AnalyserNode> node =
          static_cast<AnalyserNode*>(mTrack->Engine()->NodeMainThread());
      if (node) {
        node->AppendChunk(mChunk);
      }
      return NS_OK;
    }

   private:
    RefPtr<AudioNodeTrack> mTrack;
    AudioChunk mChunk;
  };

 public:
  explicit AnalyserNodeEngine(AnalyserNode* aNode) : AudioNodeEngine(aNode) {
    MOZ_ASSERT(NS_IsMainThread());
  }

  virtual void ProcessBlock(AudioNodeTrack* aTrack, GraphTime aFrom,
                            const AudioBlock& aInput, AudioBlock* aOutput,
                            bool* aFinished) override {
    TRACE("AnalyserNodeEngine::ProcessBlock");
    *aOutput = aInput;

    if (aInput.IsNull()) {
      // If AnalyserNode::mChunks has only null chunks, then there is no need
      // to send further null chunks.
      if (mChunksToProcess == 0) {
        return;
      }

      --mChunksToProcess;
      if (mChunksToProcess == 0) {
        aTrack->ScheduleCheckForInactive();
      }

    } else {
      // This many null chunks will be required to empty AnalyserNode::mChunks.
      mChunksToProcess = CHUNK_COUNT;
    }

    RefPtr<TransferBuffer> transfer =
        new TransferBuffer(aTrack, aInput.AsAudioChunk());
    AbstractThread::MainThread()->Dispatch(transfer.forget());
  }

  virtual bool IsActive() const override { return mChunksToProcess != 0; }

  virtual size_t SizeOfIncludingThis(
      MallocSizeOf aMallocSizeOf) const override {
    return aMallocSizeOf(this) + SizeOfExcludingThis(aMallocSizeOf);
  }

  uint32_t mChunksToProcess = 0;
};

/* static */
already_AddRefed<AnalyserNode> AnalyserNode::Create(
    AudioContext& aAudioContext, const AnalyserOptions& aOptions,
    ErrorResult& aRv) {
  RefPtr<AnalyserNode> analyserNode = new AnalyserNode(&aAudioContext);

  analyserNode->Initialize(aOptions, aRv);
  if (NS_WARN_IF(aRv.Failed())) {
    return nullptr;
  }

  analyserNode->SetFftSize(aOptions.mFftSize, aRv);
  if (NS_WARN_IF(aRv.Failed())) {
    return nullptr;
  }

  analyserNode->SetMinAndMaxDecibels(aOptions.mMinDecibels,
                                     aOptions.mMaxDecibels, aRv);
  if (NS_WARN_IF(aRv.Failed())) {
    return nullptr;
  }

  analyserNode->SetSmoothingTimeConstant(aOptions.mSmoothingTimeConstant, aRv);
  if (NS_WARN_IF(aRv.Failed())) {
    return nullptr;
  }

  return analyserNode.forget();
}

AnalyserNode::AnalyserNode(AudioContext* aContext)
    : AudioNode(aContext, 2, ChannelCountMode::Max,
                ChannelInterpretation::Speakers),
      mAnalysisBlock(2048),
      mMinDecibels(-100.),
      mMaxDecibels(-30.),
      mSmoothingTimeConstant(.8) {
  mTrack =
      AudioNodeTrack::Create(aContext, new AnalyserNodeEngine(this),
                             AudioNodeTrack::NO_TRACK_FLAGS, aContext->Graph());

  // Enough chunks must be recorded to handle the case of fftSize being
  // increased to maximum immediately before getFloatTimeDomainData() is
  // called, for example.
  Unused << mChunks.SetLength(CHUNK_COUNT, fallible);

  AllocateBuffer();
}

size_t AnalyserNode::SizeOfExcludingThis(MallocSizeOf aMallocSizeOf) const {
  size_t amount = AudioNode::SizeOfExcludingThis(aMallocSizeOf);
  amount += mAnalysisBlock.SizeOfExcludingThis(aMallocSizeOf);
  amount += mChunks.ShallowSizeOfExcludingThis(aMallocSizeOf);
  amount += mOutputBuffer.ShallowSizeOfExcludingThis(aMallocSizeOf);
  return amount;
}

size_t AnalyserNode::SizeOfIncludingThis(MallocSizeOf aMallocSizeOf) const {
  return aMallocSizeOf(this) + SizeOfExcludingThis(aMallocSizeOf);
}

JSObject* AnalyserNode::WrapObject(JSContext* aCx,
                                   JS::Handle<JSObject*> aGivenProto) {
  return AnalyserNode_Binding::Wrap(aCx, this, aGivenProto);
}

void AnalyserNode::SetFftSize(uint32_t aValue, ErrorResult& aRv) {
  // Disallow values that are not a power of 2 and outside the [32,32768] range
  if (aValue < 32 || aValue > MAX_FFT_SIZE || (aValue & (aValue - 1)) != 0) {
    aRv.ThrowIndexSizeError(nsPrintfCString(
        "FFT size %u is not a power of two in the range 32 to 32768", aValue));
    return;
  }
  if (FftSize() != aValue) {
    mAnalysisBlock.SetFFTSize(aValue);
    AllocateBuffer();
  }
}

void AnalyserNode::SetMinDecibels(double aValue, ErrorResult& aRv) {
  if (aValue >= mMaxDecibels) {
    aRv.ThrowIndexSizeError(nsPrintfCString(
        "%g is not strictly smaller than current maxDecibels (%g)", aValue,
        mMaxDecibels));
    return;
  }
  mMinDecibels = aValue;
}

void AnalyserNode::SetMaxDecibels(double aValue, ErrorResult& aRv) {
  if (aValue <= mMinDecibels) {
    aRv.ThrowIndexSizeError(nsPrintfCString(
        "%g is not strictly larger than current minDecibels (%g)", aValue,
        mMinDecibels));
    return;
  }
  mMaxDecibels = aValue;
}

void AnalyserNode::SetMinAndMaxDecibels(double aMinValue, double aMaxValue,
                                        ErrorResult& aRv) {
  if (aMinValue >= aMaxValue) {
    aRv.ThrowIndexSizeError(nsPrintfCString(
        "minDecibels value (%g) must be smaller than maxDecibels value (%g)",
        aMinValue, aMaxValue));
    return;
  }
  mMinDecibels = aMinValue;
  mMaxDecibels = aMaxValue;
}

void AnalyserNode::SetSmoothingTimeConstant(double aValue, ErrorResult& aRv) {
  if (aValue < 0 || aValue > 1) {
    aRv.ThrowIndexSizeError(
        nsPrintfCString("%g is not in the range [0, 1]", aValue));
    return;
  }
  mSmoothingTimeConstant = aValue;
}

void AnalyserNode::GetFloatFrequencyData(const Float32Array& aArray) {
  if (!FFTAnalysis()) {
    // Might fail to allocate memory
    return;
  }

  aArray.ProcessData([&](const Span<float>& aData, JS::AutoCheckCannotGC&&) {
    size_t length = std::min(size_t(aData.Length()), mOutputBuffer.Length());

    for (size_t i = 0; i < length; ++i) {
      aData[i] = WebAudioUtils::ConvertLinearToDecibels(
          mOutputBuffer[i], -std::numeric_limits<float>::infinity());
    }
  });
}

void AnalyserNode::GetByteFrequencyData(const Uint8Array& aArray) {
  if (!FFTAnalysis()) {
    // Might fail to allocate memory
    return;
  }

  const double rangeScaleFactor = 1.0 / (mMaxDecibels - mMinDecibels);

  aArray.ProcessData([&](const Span<uint8_t>& aData, JS::AutoCheckCannotGC&&) {
    size_t length = std::min(size_t(aData.Length()), mOutputBuffer.Length());

    for (size_t i = 0; i < length; ++i) {
      const double decibels = WebAudioUtils::ConvertLinearToDecibels(
          mOutputBuffer[i], mMinDecibels);
      // scale down the value to the range of [0, UCHAR_MAX]
      const double scaled = std::max(
          0.0,
          std::min(double(UCHAR_MAX),
                   UCHAR_MAX * (decibels - mMinDecibels) * rangeScaleFactor));
      aData[i] = static_cast<unsigned char>(scaled);
    }
  });
}

void AnalyserNode::GetFloatTimeDomainData(const Float32Array& aArray) {
  aArray.ProcessData([&](const Span<float>& aData, JS::AutoCheckCannotGC&&) {
    size_t length = std::min(aData.Length(), size_t(FftSize()));

    GetTimeDomainData(aData.Elements(), length);
  });
}

void AnalyserNode::GetByteTimeDomainData(const Uint8Array& aArray) {
  aArray.ProcessData([&](const Span<uint8_t>& aData, JS::AutoCheckCannotGC&&) {
    size_t length = std::min(aData.Length(), size_t(FftSize()));

    AlignedTArray<float> tmpBuffer;
    if (!tmpBuffer.SetLength(length, fallible)) {
      return;
    }

    GetTimeDomainData(tmpBuffer.Elements(), length);

    unsigned char* buffer = aData.Elements();
    for (size_t i = 0; i < length; ++i) {
      const float value = tmpBuffer[i];
      // scale the value to the range of [0, UCHAR_MAX]
      const float scaled =
          std::max(0.0f, std::min(float(UCHAR_MAX), 128.0f * (value + 1.0f)));
      buffer[i] = static_cast<unsigned char>(scaled);
    }
  });
}

bool AnalyserNode::FFTAnalysis() {
  AlignedTArray<float> tmpBuffer;
  size_t fftSize = FftSize();
  if (!tmpBuffer.SetLength(fftSize, fallible)) {
    return false;
  }

  float* inputBuffer = tmpBuffer.Elements();
  GetTimeDomainData(inputBuffer, fftSize);
  ApplyBlackmanWindow(inputBuffer, fftSize);
  mAnalysisBlock.PerformFFT(inputBuffer);

  // Normalize so than an input sine wave at 0dBfs registers as 0dBfs (undo FFT
  // scaling factor).
  const double magnitudeScale = 1.0 / fftSize;

  for (uint32_t i = 0; i < mOutputBuffer.Length(); ++i) {
    double scalarMagnitude =
        fdlibm_hypot(mAnalysisBlock.RealData(i), mAnalysisBlock.ImagData(i)) *
        magnitudeScale;
    mOutputBuffer[i] = mSmoothingTimeConstant * mOutputBuffer[i] +
                       (1.0 - mSmoothingTimeConstant) * scalarMagnitude;
  }

  return true;
}

void AnalyserNode::ApplyBlackmanWindow(float* aBuffer, uint32_t aSize) {
  double alpha = 0.16;
  double a0 = 0.5 * (1.0 - alpha);
  double a1 = 0.5;
  double a2 = 0.5 * alpha;

  for (uint32_t i = 0; i < aSize; ++i) {
    double x = double(i) / aSize;
    double window =
        a0 - a1 * fdlibm_cos(2 * M_PI * x) + a2 * fdlibm_cos(4 * M_PI * x);
    aBuffer[i] *= window;
  }
}

bool AnalyserNode::AllocateBuffer() {
  bool result = true;
  if (mOutputBuffer.Length() != FrequencyBinCount()) {
    if (!mOutputBuffer.SetLength(FrequencyBinCount(), fallible)) {
      return false;
    }
    memset(mOutputBuffer.Elements(), 0, sizeof(float) * FrequencyBinCount());
  }
  return result;
}

void AnalyserNode::AppendChunk(const AudioChunk& aChunk) {
  if (mChunks.Length() == 0) {
    return;
  }

  ++mCurrentChunk;
  mChunks[mCurrentChunk & (CHUNK_COUNT - 1)] = aChunk;
}

// Reads into aData the oldest aLength samples of the fftSize most recent
// samples.
void AnalyserNode::GetTimeDomainData(float* aData, size_t aLength) {
  size_t fftSize = FftSize();
  MOZ_ASSERT(aLength <= fftSize);

  if (mChunks.Length() == 0) {
    PodZero(aData, aLength);
    return;
  }

  size_t readChunk =
      mCurrentChunk - ((fftSize - 1) >> WEBAUDIO_BLOCK_SIZE_BITS);
  size_t readIndex = (0 - fftSize) & (WEBAUDIO_BLOCK_SIZE - 1);
  MOZ_ASSERT(readIndex == 0 || readIndex + fftSize == WEBAUDIO_BLOCK_SIZE);

  for (size_t writeIndex = 0; writeIndex < aLength;) {
    const AudioChunk& chunk = mChunks[readChunk & (CHUNK_COUNT - 1)];
    const size_t channelCount = chunk.ChannelCount();
    size_t copyLength =
        std::min<size_t>(aLength - writeIndex, WEBAUDIO_BLOCK_SIZE);
    float* dataOut = &aData[writeIndex];

    if (channelCount == 0) {
      PodZero(dataOut, copyLength);
    } else {
      float scale = chunk.mVolume / channelCount;
      {  // channel 0
        auto channelData =
            static_cast<const float*>(chunk.mChannelData[0]) + readIndex;
        AudioBufferCopyWithScale(channelData, scale, dataOut, copyLength);
      }
      for (uint32_t i = 1; i < channelCount; ++i) {
        auto channelData =
            static_cast<const float*>(chunk.mChannelData[i]) + readIndex;
        AudioBufferAddWithScale(channelData, scale, dataOut, copyLength);
      }
    }

    readChunk++;
    writeIndex += copyLength;
  }
}

}  // namespace dom
}  // namespace mozilla