File: HRTFElevation.cpp

package info (click to toggle)
chromium-browser 57.0.2987.98-1~deb8u1
  • links: PTS, VCS
  • area: main
  • in suites: jessie
  • size: 2,637,852 kB
  • ctags: 2,544,394
  • sloc: cpp: 12,815,961; ansic: 3,676,222; python: 1,147,112; asm: 526,608; java: 523,212; xml: 286,794; perl: 92,654; sh: 86,408; objc: 73,271; makefile: 27,698; cs: 18,487; yacc: 13,031; tcl: 12,957; pascal: 4,875; ml: 4,716; lex: 3,904; sql: 3,862; ruby: 1,982; lisp: 1,508; php: 1,368; exp: 404; awk: 325; csh: 117; jsp: 39; sed: 37
file content (384 lines) | stat: -rw-r--r-- 14,299 bytes parent folder | download
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
/*
 * 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.
 * 3.  Neither the name of Apple Computer, Inc. ("Apple") nor the names of
 *     its contributors may be used to endorse or promote products derived
 *     from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY APPLE 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 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 "platform/audio/AudioBus.h"
#include "platform/audio/HRTFElevation.h"
#include "platform/audio/HRTFPanner.h"
#include "wtf/PtrUtil.h"
#include "wtf/ThreadingPrimitives.h"
#include "wtf/text/StringHash.h"
#include <algorithm>
#include <math.h>
#include <memory>

namespace blink {

const unsigned HRTFElevation::AzimuthSpacing = 15;
const unsigned HRTFElevation::NumberOfRawAzimuths = 360 / AzimuthSpacing;
const unsigned HRTFElevation::InterpolationFactor = 8;
const unsigned HRTFElevation::NumberOfTotalAzimuths =
    NumberOfRawAzimuths * InterpolationFactor;

// Total number of components of an HRTF database.
const size_t TotalNumberOfResponses = 240;

// Number of frames in an individual impulse response.
const size_t ResponseFrameSize = 256;

// Sample-rate of the spatialization impulse responses as stored in the resource
// file.  The impulse responses may be resampled to a different sample-rate
// (depending on the audio hardware) when they are loaded.
const float ResponseSampleRate = 44100;

#if USE(CONCATENATED_IMPULSE_RESPONSES)

// This table maps the index into the elevation table with the corresponding
// angle. See https://bugs.webkit.org/show_bug.cgi?id=98294#c9 for the
// elevation angles and their order in the concatenated response.
const int ElevationIndexTableSize = 10;
const int ElevationIndexTable[ElevationIndexTableSize] = {
    0, 15, 30, 45, 60, 75, 90, 315, 330, 345};

// Lazily load a concatenated HRTF database for given subject and store it in a
// local hash table to ensure quick efficient future retrievals.
static PassRefPtr<AudioBus> getConcatenatedImpulseResponsesForSubject(
    const String& subjectName) {
  typedef HashMap<String, RefPtr<AudioBus>> AudioBusMap;
  DEFINE_THREAD_SAFE_STATIC_LOCAL(AudioBusMap, audioBusMap, new AudioBusMap());
  DEFINE_THREAD_SAFE_STATIC_LOCAL(Mutex, mutex, new Mutex());

  MutexLocker locker(mutex);
  RefPtr<AudioBus> bus;
  AudioBusMap::iterator iterator = audioBusMap.find(subjectName);
  if (iterator == audioBusMap.end()) {
    RefPtr<AudioBus> concatenatedImpulseResponses(
        AudioBus::loadPlatformResource(subjectName.utf8().data(),
                                       ResponseSampleRate));
    ASSERT(concatenatedImpulseResponses);
    if (!concatenatedImpulseResponses)
      return nullptr;

    bus = concatenatedImpulseResponses;
    audioBusMap.set(subjectName, bus);
  } else
    bus = iterator->value;

  size_t responseLength = bus->length();
  size_t expectedLength =
      static_cast<size_t>(TotalNumberOfResponses * ResponseFrameSize);

  // Check number of channels and length. For now these are fixed and known.
  bool isBusGood =
      responseLength == expectedLength && bus->numberOfChannels() == 2;
  ASSERT(isBusGood);
  if (!isBusGood)
    return nullptr;

  return bus;
}
#endif

bool HRTFElevation::calculateKernelsForAzimuthElevation(
    int azimuth,
    int elevation,
    float sampleRate,
    const String& subjectName,
    std::unique_ptr<HRTFKernel>& kernelL,
    std::unique_ptr<HRTFKernel>& kernelR) {
  // Valid values for azimuth are 0 -> 345 in 15 degree increments.
  // Valid values for elevation are -45 -> +90 in 15 degree increments.

  bool isAzimuthGood =
      azimuth >= 0 && azimuth <= 345 && (azimuth / 15) * 15 == azimuth;
  ASSERT(isAzimuthGood);
  if (!isAzimuthGood)
    return false;

  bool isElevationGood =
      elevation >= -45 && elevation <= 90 && (elevation / 15) * 15 == elevation;
  ASSERT(isElevationGood);
  if (!isElevationGood)
    return false;

  // Construct the resource name from the subject name, azimuth, and elevation,
  // for example:
  // "IRC_Composite_C_R0195_T015_P000"
  // Note: the passed in subjectName is not a string passed in via JavaScript or
  // the web.  It's passed in as an internal ASCII identifier and is an
  // implementation detail.
  int positiveElevation = elevation < 0 ? elevation + 360 : elevation;

#if USE(CONCATENATED_IMPULSE_RESPONSES)
  RefPtr<AudioBus> bus(getConcatenatedImpulseResponsesForSubject(subjectName));

  if (!bus)
    return false;

  // Just sequentially search the table to find the correct index.
  int elevationIndex = -1;

  for (int k = 0; k < ElevationIndexTableSize; ++k) {
    if (ElevationIndexTable[k] == positiveElevation) {
      elevationIndex = k;
      break;
    }
  }

  bool isElevationIndexGood =
      (elevationIndex >= 0) && (elevationIndex < ElevationIndexTableSize);
  ASSERT(isElevationIndexGood);
  if (!isElevationIndexGood)
    return false;

  // The concatenated impulse response is a bus containing all
  // the elevations per azimuth, for all azimuths by increasing
  // order. So for a given azimuth and elevation we need to compute
  // the index of the wanted audio frames in the concatenated table.
  unsigned index =
      ((azimuth / AzimuthSpacing) * HRTFDatabase::NumberOfRawElevations) +
      elevationIndex;
  bool isIndexGood = index < TotalNumberOfResponses;
  ASSERT(isIndexGood);
  if (!isIndexGood)
    return false;

  // Extract the individual impulse response from the concatenated
  // responses and potentially sample-rate convert it to the desired
  // (hardware) sample-rate.
  unsigned startFrame = index * ResponseFrameSize;
  unsigned stopFrame = startFrame + ResponseFrameSize;
  RefPtr<AudioBus> preSampleRateConvertedResponse(
      AudioBus::createBufferFromRange(bus.get(), startFrame, stopFrame));
  RefPtr<AudioBus> response(AudioBus::createBySampleRateConverting(
      preSampleRateConvertedResponse.get(), false, sampleRate));
  AudioChannel* leftEarImpulseResponse =
      response->channel(AudioBus::ChannelLeft);
  AudioChannel* rightEarImpulseResponse =
      response->channel(AudioBus::ChannelRight);
#else
  String resourceName =
      String::format("IRC_%s_C_R0195_T%03d_P%03d", subjectName.utf8().data(),
                     azimuth, positiveElevation);

  RefPtr<AudioBus> impulseResponse(
      AudioBus::loadPlatformResource(resourceName.utf8().data(), sampleRate));

  ASSERT(impulseResponse.get());
  if (!impulseResponse.get())
    return false;

  size_t responseLength = impulseResponse->length();
  size_t expectedLength = static_cast<size_t>(256 * (sampleRate / 44100.0));

  // Check number of channels and length.  For now these are fixed and known.
  bool isBusGood = responseLength == expectedLength &&
                   impulseResponse->numberOfChannels() == 2;
  ASSERT(isBusGood);
  if (!isBusGood)
    return false;

  AudioChannel* leftEarImpulseResponse =
      impulseResponse->channelByType(AudioBus::ChannelLeft);
  AudioChannel* rightEarImpulseResponse =
      impulseResponse->channelByType(AudioBus::ChannelRight);
#endif

  // Note that depending on the fftSize returned by the panner, we may be
  // truncating the impulse response we just loaded in.
  const size_t fftSize = HRTFPanner::fftSizeForSampleRate(sampleRate);
  kernelL = HRTFKernel::create(leftEarImpulseResponse, fftSize, sampleRate);
  kernelR = HRTFKernel::create(rightEarImpulseResponse, fftSize, sampleRate);

  return true;
}

// The range of elevations for the IRCAM impulse responses varies depending on
// azimuth, but the minimum elevation appears to always be -45.
//
// Here's how it goes:
static int maxElevations[] = {
    //  Azimuth
    //
    90,  // 0
    45,  // 15
    60,  // 30
    45,  // 45
    75,  // 60
    45,  // 75
    60,  // 90
    45,  // 105
    75,  // 120
    45,  // 135
    60,  // 150
    45,  // 165
    75,  // 180
    45,  // 195
    60,  // 210
    45,  // 225
    75,  // 240
    45,  // 255
    60,  // 270
    45,  // 285
    75,  // 300
    45,  // 315
    60,  // 330
    45   //  345
};

std::unique_ptr<HRTFElevation> HRTFElevation::createForSubject(
    const String& subjectName,
    int elevation,
    float sampleRate) {
  bool isElevationGood =
      elevation >= -45 && elevation <= 90 && (elevation / 15) * 15 == elevation;
  ASSERT(isElevationGood);
  if (!isElevationGood)
    return nullptr;

  std::unique_ptr<HRTFKernelList> kernelListL =
      WTF::makeUnique<HRTFKernelList>(NumberOfTotalAzimuths);
  std::unique_ptr<HRTFKernelList> kernelListR =
      WTF::makeUnique<HRTFKernelList>(NumberOfTotalAzimuths);

  // Load convolution kernels from HRTF files.
  int interpolatedIndex = 0;
  for (unsigned rawIndex = 0; rawIndex < NumberOfRawAzimuths; ++rawIndex) {
    // Don't let elevation exceed maximum for this azimuth.
    int maxElevation = maxElevations[rawIndex];
    int actualElevation = std::min(elevation, maxElevation);

    bool success = calculateKernelsForAzimuthElevation(
        rawIndex * AzimuthSpacing, actualElevation, sampleRate, subjectName,
        kernelListL->at(interpolatedIndex), kernelListR->at(interpolatedIndex));
    if (!success)
      return nullptr;

    interpolatedIndex += InterpolationFactor;
  }

  // Now go back and interpolate intermediate azimuth values.
  for (unsigned i = 0; i < NumberOfTotalAzimuths; i += InterpolationFactor) {
    int j = (i + InterpolationFactor) % NumberOfTotalAzimuths;

    // Create the interpolated convolution kernels and delays.
    for (unsigned jj = 1; jj < InterpolationFactor; ++jj) {
      float x =
          float(jj) / float(InterpolationFactor);  // interpolate from 0 -> 1

      (*kernelListL)[i + jj] = HRTFKernel::createInterpolatedKernel(
          kernelListL->at(i).get(), kernelListL->at(j).get(), x);
      (*kernelListR)[i + jj] = HRTFKernel::createInterpolatedKernel(
          kernelListR->at(i).get(), kernelListR->at(j).get(), x);
    }
  }

  std::unique_ptr<HRTFElevation> hrtfElevation = WTF::wrapUnique(
      new HRTFElevation(std::move(kernelListL), std::move(kernelListR),
                        elevation, sampleRate));
  return hrtfElevation;
}

std::unique_ptr<HRTFElevation> HRTFElevation::createByInterpolatingSlices(
    HRTFElevation* hrtfElevation1,
    HRTFElevation* hrtfElevation2,
    float x,
    float sampleRate) {
  ASSERT(hrtfElevation1 && hrtfElevation2);
  if (!hrtfElevation1 || !hrtfElevation2)
    return nullptr;

  ASSERT(x >= 0.0 && x < 1.0);

  std::unique_ptr<HRTFKernelList> kernelListL =
      WTF::makeUnique<HRTFKernelList>(NumberOfTotalAzimuths);
  std::unique_ptr<HRTFKernelList> kernelListR =
      WTF::makeUnique<HRTFKernelList>(NumberOfTotalAzimuths);

  HRTFKernelList* kernelListL1 = hrtfElevation1->kernelListL();
  HRTFKernelList* kernelListR1 = hrtfElevation1->kernelListR();
  HRTFKernelList* kernelListL2 = hrtfElevation2->kernelListL();
  HRTFKernelList* kernelListR2 = hrtfElevation2->kernelListR();

  // Interpolate kernels of corresponding azimuths of the two elevations.
  for (unsigned i = 0; i < NumberOfTotalAzimuths; ++i) {
    (*kernelListL)[i] = HRTFKernel::createInterpolatedKernel(
        kernelListL1->at(i).get(), kernelListL2->at(i).get(), x);
    (*kernelListR)[i] = HRTFKernel::createInterpolatedKernel(
        kernelListR1->at(i).get(), kernelListR2->at(i).get(), x);
  }

  // Interpolate elevation angle.
  double angle = (1.0 - x) * hrtfElevation1->elevationAngle() +
                 x * hrtfElevation2->elevationAngle();

  std::unique_ptr<HRTFElevation> hrtfElevation = WTF::wrapUnique(
      new HRTFElevation(std::move(kernelListL), std::move(kernelListR),
                        static_cast<int>(angle), sampleRate));
  return hrtfElevation;
}

void HRTFElevation::getKernelsFromAzimuth(double azimuthBlend,
                                          unsigned azimuthIndex,
                                          HRTFKernel*& kernelL,
                                          HRTFKernel*& kernelR,
                                          double& frameDelayL,
                                          double& frameDelayR) {
  bool checkAzimuthBlend = azimuthBlend >= 0.0 && azimuthBlend < 1.0;
  ASSERT(checkAzimuthBlend);
  if (!checkAzimuthBlend)
    azimuthBlend = 0.0;

  unsigned numKernels = m_kernelListL->size();

  bool isIndexGood = azimuthIndex < numKernels;
  ASSERT(isIndexGood);
  if (!isIndexGood) {
    kernelL = 0;
    kernelR = 0;
    return;
  }

  // Return the left and right kernels.
  kernelL = m_kernelListL->at(azimuthIndex).get();
  kernelR = m_kernelListR->at(azimuthIndex).get();

  frameDelayL = m_kernelListL->at(azimuthIndex)->frameDelay();
  frameDelayR = m_kernelListR->at(azimuthIndex)->frameDelay();

  int azimuthIndex2 = (azimuthIndex + 1) % numKernels;
  double frameDelay2L = m_kernelListL->at(azimuthIndex2)->frameDelay();
  double frameDelay2R = m_kernelListR->at(azimuthIndex2)->frameDelay();

  // Linearly interpolate delays.
  frameDelayL =
      (1.0 - azimuthBlend) * frameDelayL + azimuthBlend * frameDelay2L;
  frameDelayR =
      (1.0 - azimuthBlend) * frameDelayR + azimuthBlend * frameDelay2R;
}

}  // namespace blink