summaryrefslogtreecommitdiffstats
path: root/chromium/third_party/blink/renderer/modules/webaudio/realtime_analyser.cc
blob: 325f61e14ac97a257280cda40aa93d3469643b6f (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
/*
 * Copyright (C) 2010, Google Inc. All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1.  Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2.  Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS'' AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
 * DAMAGE.
 */

#include <limits.h>
#include <algorithm>
#include <complex>
#include "third_party/blink/renderer/modules/webaudio/realtime_analyser.h"
#include "third_party/blink/renderer/platform/audio/audio_bus.h"
#include "third_party/blink/renderer/platform/audio/audio_utilities.h"
#include "third_party/blink/renderer/platform/audio/vector_math.h"
#include "third_party/blink/renderer/platform/wtf/math_extras.h"

namespace blink {

const double RealtimeAnalyser::kDefaultSmoothingTimeConstant = 0.8;
const double RealtimeAnalyser::kDefaultMinDecibels = -100;
const double RealtimeAnalyser::kDefaultMaxDecibels = -30;

const unsigned RealtimeAnalyser::kDefaultFFTSize = 2048;
// All FFT implementations are expected to handle power-of-two sizes
// MinFFTSize <= size <= MaxFFTSize.
const unsigned RealtimeAnalyser::kMinFFTSize = 32;
const unsigned RealtimeAnalyser::kMaxFFTSize = 32768;
const unsigned RealtimeAnalyser::kInputBufferSize =
    RealtimeAnalyser::kMaxFFTSize * 2;

RealtimeAnalyser::RealtimeAnalyser()
    : input_buffer_(kInputBufferSize),
      down_mix_bus_(AudioBus::Create(1, audio_utilities::kRenderQuantumFrames)),
      fft_size_(kDefaultFFTSize),
      magnitude_buffer_(kDefaultFFTSize / 2),
      smoothing_time_constant_(kDefaultSmoothingTimeConstant),
      min_decibels_(kDefaultMinDecibels),
      max_decibels_(kDefaultMaxDecibels),
      last_analysis_time_(-1) {
  analysis_frame_ = std::make_unique<FFTFrame>(kDefaultFFTSize);
}

bool RealtimeAnalyser::SetFftSize(uint32_t size) {
  DCHECK(IsMainThread());

  // Only allow powers of two within the allowed range.
  if (size > kMaxFFTSize || size < kMinFFTSize ||
      !audio_utilities::IsPowerOfTwo(size))
    return false;

  if (fft_size_ != size) {
    analysis_frame_ = std::make_unique<FFTFrame>(size);
    // m_magnitudeBuffer has size = fftSize / 2 because it contains floats
    // reduced from complex values in m_analysisFrame.
    magnitude_buffer_.Allocate(size / 2);
    fft_size_ = size;
  }

  return true;
}

void RealtimeAnalyser::WriteInput(AudioBus* bus, uint32_t frames_to_process) {
  DCHECK(bus);
  DCHECK_GT(bus->NumberOfChannels(), 0u);
  DCHECK_GE(bus->Channel(0)->length(), frames_to_process);

  unsigned write_index = GetWriteIndex();
  // FIXME : allow to work with non-FFTSize divisible chunking
  DCHECK_LT(write_index, input_buffer_.size());
  DCHECK_LE(write_index + frames_to_process, input_buffer_.size());

  // Perform real-time analysis
  float* dest = input_buffer_.Data() + write_index;

  // Clear the bus and downmix the input according to the down mixing rules.
  // Then save the result in the m_inputBuffer at the appropriate place.
  down_mix_bus_->Zero();
  down_mix_bus_->SumFrom(*bus);
  memcpy(dest, down_mix_bus_->Channel(0)->Data(),
         frames_to_process * sizeof(*dest));

  write_index += frames_to_process;
  if (write_index >= kInputBufferSize)
    write_index = 0;
  SetWriteIndex(write_index);
}

namespace {

void ApplyWindow(float* p, size_t n) {
  DCHECK(IsMainThread());

  // Blackman window
  double alpha = 0.16;
  double a0 = 0.5 * (1 - alpha);
  double a1 = 0.5;
  double a2 = 0.5 * alpha;

  for (unsigned i = 0; i < n; ++i) {
    double x = static_cast<double>(i) / static_cast<double>(n);
    double window =
        a0 - a1 * cos(kTwoPiDouble * x) + a2 * cos(kTwoPiDouble * 2.0 * x);
    p[i] *= float(window);
  }
}

}  // namespace

void RealtimeAnalyser::DoFFTAnalysis() {
  DCHECK(IsMainThread());

  // Unroll the input buffer into a temporary buffer, where we'll apply an
  // analysis window followed by an FFT.
  uint32_t fft_size = this->FftSize();

  AudioFloatArray temporary_buffer(fft_size);
  float* input_buffer = input_buffer_.Data();
  float* temp_p = temporary_buffer.Data();

  // Take the previous fftSize values from the input buffer and copy into the
  // temporary buffer.
  unsigned write_index = GetWriteIndex();
  if (write_index < fft_size) {
    memcpy(temp_p, input_buffer + write_index - fft_size + kInputBufferSize,
           sizeof(*temp_p) * (fft_size - write_index));
    memcpy(temp_p + fft_size - write_index, input_buffer,
           sizeof(*temp_p) * write_index);
  } else {
    memcpy(temp_p, input_buffer + write_index - fft_size,
           sizeof(*temp_p) * fft_size);
  }

  // Window the input samples.
  ApplyWindow(temp_p, fft_size);

  // Do the analysis.
  analysis_frame_->DoFFT(temp_p);

  const AudioFloatArray& real = analysis_frame_->RealData();
  AudioFloatArray& imag = analysis_frame_->ImagData();

  // Blow away the packed nyquist component.
  imag[0] = 0;

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

  // A value of 0 does no averaging with the previous result.  Larger values
  // produce slower, but smoother changes.
  double k = smoothing_time_constant_;
  k = std::max(0.0, k);
  k = std::min(1.0, k);

  // Convert the analysis data from complex to magnitude and average with the
  // previous result.
  float* destination = MagnitudeBuffer().Data();
  size_t n = MagnitudeBuffer().size();
  DCHECK_GE(real.size(), n);
  const float* real_p_data = real.Data();
  DCHECK_GE(imag.size(), n);
  const float* imag_p_data = imag.Data();
  for (size_t i = 0; i < n; ++i) {
    std::complex<double> c(real_p_data[i], imag_p_data[i]);
    double scalar_magnitude = abs(c) * magnitude_scale;
    destination[i] = float(k * destination[i] + (1 - k) * scalar_magnitude);
  }
}

void RealtimeAnalyser::ConvertFloatToDb(DOMFloat32Array* destination_array) {
  // Convert from linear magnitude to floating-point decibels.
  size_t source_length = MagnitudeBuffer().size();
  size_t len = std::min(source_length, destination_array->lengthAsSizeT());
  if (len > 0) {
    const float* source = MagnitudeBuffer().Data();
    float* destination = destination_array->Data();

    for (unsigned i = 0; i < len; ++i) {
      float linear_value = source[i];
      double db_mag = audio_utilities::LinearToDecibels(linear_value);
      destination[i] = float(db_mag);
    }
  }
}

void RealtimeAnalyser::GetFloatFrequencyData(DOMFloat32Array* destination_array,
                                             double current_time) {
  DCHECK(IsMainThread());
  DCHECK(destination_array);

  if (current_time <= last_analysis_time_) {
    ConvertFloatToDb(destination_array);
    return;
  }

  // Time has advanced since the last call; update the FFT data.
  last_analysis_time_ = current_time;
  DoFFTAnalysis();

  ConvertFloatToDb(destination_array);
}

void RealtimeAnalyser::ConvertToByteData(DOMUint8Array* destination_array) {
  // Convert from linear magnitude to unsigned-byte decibels.
  size_t source_length = MagnitudeBuffer().size();
  size_t len = std::min(source_length, destination_array->lengthAsSizeT());
  if (len > 0) {
    const double range_scale_factor = max_decibels_ == min_decibels_
                                          ? 1
                                          : 1 / (max_decibels_ - min_decibels_);
    const double min_decibels = min_decibels_;

    const float* source = MagnitudeBuffer().Data();
    unsigned char* destination = destination_array->Data();

    for (unsigned i = 0; i < len; ++i) {
      float linear_value = source[i];
      double db_mag = audio_utilities::LinearToDecibels(linear_value);

      // The range m_minDecibels to m_maxDecibels will be scaled to byte values
      // from 0 to UCHAR_MAX.
      double scaled_value =
          UCHAR_MAX * (db_mag - min_decibels) * range_scale_factor;

      // Clip to valid range.
      if (scaled_value < 0)
        scaled_value = 0;
      if (scaled_value > UCHAR_MAX)
        scaled_value = UCHAR_MAX;

      destination[i] = static_cast<unsigned char>(scaled_value);
    }
  }
}

void RealtimeAnalyser::GetByteFrequencyData(DOMUint8Array* destination_array,
                                            double current_time) {
  DCHECK(IsMainThread());
  DCHECK(destination_array);

  if (current_time <= last_analysis_time_) {
    // FIXME: Is it worth caching the data so we don't have to do the conversion
    // every time?  Perhaps not, since we expect many calls in the same
    // rendering quantum.
    ConvertToByteData(destination_array);
    return;
  }

  // Time has advanced since the last call; update the FFT data.
  last_analysis_time_ = current_time;
  DoFFTAnalysis();

  ConvertToByteData(destination_array);
}

void RealtimeAnalyser::GetFloatTimeDomainData(
    DOMFloat32Array* destination_array) {
  DCHECK(IsMainThread());
  DCHECK(destination_array);

  unsigned fft_size = this->FftSize();
  size_t len = std::min(static_cast<size_t>(fft_size),
                        destination_array->lengthAsSizeT());
  if (len > 0) {
    DCHECK_EQ(input_buffer_.size(), kInputBufferSize);
    DCHECK_GT(input_buffer_.size(), fft_size);

    float* input_buffer = input_buffer_.Data();
    float* destination = destination_array->Data();

    unsigned write_index = GetWriteIndex();

    for (unsigned i = 0; i < len; ++i) {
      // Buffer access is protected due to modulo operation.
      float value =
          input_buffer[(i + write_index - fft_size + kInputBufferSize) %
                       kInputBufferSize];

      destination[i] = value;
    }
  }
}

void RealtimeAnalyser::GetByteTimeDomainData(DOMUint8Array* destination_array) {
  DCHECK(IsMainThread());
  DCHECK(destination_array);

  unsigned fft_size = this->FftSize();
  size_t len = std::min(static_cast<size_t>(fft_size),
                        destination_array->lengthAsSizeT());
  if (len > 0) {
    DCHECK_EQ(input_buffer_.size(), kInputBufferSize);
    DCHECK_GT(input_buffer_.size(), fft_size);

    float* input_buffer = input_buffer_.Data();
    unsigned char* destination = destination_array->Data();

    unsigned write_index = GetWriteIndex();

    for (unsigned i = 0; i < len; ++i) {
      // Buffer access is protected due to modulo operation.
      float value =
          input_buffer[(i + write_index - fft_size + kInputBufferSize) %
                       kInputBufferSize];

      // Scale from nominal -1 -> +1 to unsigned byte.
      double scaled_value = 128 * (value + 1);

      // Clip to valid range.
      if (scaled_value < 0)
        scaled_value = 0;
      if (scaled_value > UCHAR_MAX)
        scaled_value = UCHAR_MAX;

      destination[i] = static_cast<unsigned char>(scaled_value);
    }
  }
}

}  // namespace blink