PageRenderTime 42ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/src/3rdparty/webkit/Source/WebCore/platform/audio/HRTFKernel.cpp

https://bitbucket.org/ariya/qt4-retrofit
C++ | 144 lines | 77 code | 30 blank | 37 comment | 11 complexity | 6dada731083bc0a93a783a788e64378e MD5 | raw file
Possible License(s): LGPL-2.0, LGPL-3.0, CC-BY-SA-4.0, GPL-3.0, LGPL-2.1, BSD-3-Clause, Apache-2.0, GPL-2.0
  1. /*
  2. * Copyright (C) 2010 Google Inc. All rights reserved.
  3. *
  4. * Redistribution and use in source and binary forms, with or without
  5. * modification, are permitted provided that the following conditions
  6. * are met:
  7. *
  8. * 1. Redistributions of source code must retain the above copyright
  9. * notice, this list of conditions and the following disclaimer.
  10. * 2. Redistributions in binary form must reproduce the above copyright
  11. * notice, this list of conditions and the following disclaimer in the
  12. * documentation and/or other materials provided with the distribution.
  13. * 3. Neither the name of Apple Computer, Inc. ("Apple") nor the names of
  14. * its contributors may be used to endorse or promote products derived
  15. * from this software without specific prior written permission.
  16. *
  17. * THIS SOFTWARE IS PROVIDED BY APPLE AND ITS CONTRIBUTORS "AS IS" AND ANY
  18. * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  19. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  20. * DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR ANY
  21. * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  22. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  23. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  24. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  25. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
  26. * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  27. */
  28. #include "config.h"
  29. #if ENABLE(WEB_AUDIO)
  30. #include "HRTFKernel.h"
  31. #include "AudioChannel.h"
  32. #include "Biquad.h"
  33. #include "FFTFrame.h"
  34. #include "FloatConversion.h"
  35. #include "PlatformMemoryInstrumentation.h"
  36. #include <wtf/MathExtras.h>
  37. using namespace std;
  38. namespace WebCore {
  39. // Takes the input AudioChannel as an input impulse response and calculates the average group delay.
  40. // This represents the initial delay before the most energetic part of the impulse response.
  41. // The sample-frame delay is removed from the impulseP impulse response, and this value is returned.
  42. // the length of the passed in AudioChannel must be a power of 2.
  43. static float extractAverageGroupDelay(AudioChannel* channel, size_t analysisFFTSize)
  44. {
  45. ASSERT(channel);
  46. float* impulseP = channel->mutableData();
  47. bool isSizeGood = channel->length() >= analysisFFTSize;
  48. ASSERT(isSizeGood);
  49. if (!isSizeGood)
  50. return 0;
  51. // Check for power-of-2.
  52. ASSERT(1UL << static_cast<unsigned>(log2(analysisFFTSize)) == analysisFFTSize);
  53. FFTFrame estimationFrame(analysisFFTSize);
  54. estimationFrame.doFFT(impulseP);
  55. float frameDelay = narrowPrecisionToFloat(estimationFrame.extractAverageGroupDelay());
  56. estimationFrame.doInverseFFT(impulseP);
  57. return frameDelay;
  58. }
  59. HRTFKernel::HRTFKernel(AudioChannel* channel, size_t fftSize, float sampleRate)
  60. : m_frameDelay(0)
  61. , m_sampleRate(sampleRate)
  62. {
  63. ASSERT(channel);
  64. // Determine the leading delay (average group delay) for the response.
  65. m_frameDelay = extractAverageGroupDelay(channel, fftSize / 2);
  66. float* impulseResponse = channel->mutableData();
  67. size_t responseLength = channel->length();
  68. // We need to truncate to fit into 1/2 the FFT size (with zero padding) in order to do proper convolution.
  69. size_t truncatedResponseLength = min(responseLength, fftSize / 2); // truncate if necessary to max impulse response length allowed by FFT
  70. // Quick fade-out (apply window) at truncation point
  71. unsigned numberOfFadeOutFrames = static_cast<unsigned>(sampleRate / 4410); // 10 sample-frames @44.1KHz sample-rate
  72. ASSERT(numberOfFadeOutFrames < truncatedResponseLength);
  73. if (numberOfFadeOutFrames < truncatedResponseLength) {
  74. for (unsigned i = truncatedResponseLength - numberOfFadeOutFrames; i < truncatedResponseLength; ++i) {
  75. float x = 1.0f - static_cast<float>(i - (truncatedResponseLength - numberOfFadeOutFrames)) / numberOfFadeOutFrames;
  76. impulseResponse[i] *= x;
  77. }
  78. }
  79. m_fftFrame = adoptPtr(new FFTFrame(fftSize));
  80. m_fftFrame->doPaddedFFT(impulseResponse, truncatedResponseLength);
  81. }
  82. PassOwnPtr<AudioChannel> HRTFKernel::createImpulseResponse()
  83. {
  84. OwnPtr<AudioChannel> channel = adoptPtr(new AudioChannel(fftSize()));
  85. FFTFrame fftFrame(*m_fftFrame);
  86. // Add leading delay back in.
  87. fftFrame.addConstantGroupDelay(m_frameDelay);
  88. fftFrame.doInverseFFT(channel->mutableData());
  89. return channel.release();
  90. }
  91. // Interpolates two kernels with x: 0 -> 1 and returns the result.
  92. PassRefPtr<HRTFKernel> HRTFKernel::createInterpolatedKernel(HRTFKernel* kernel1, HRTFKernel* kernel2, float x)
  93. {
  94. ASSERT(kernel1 && kernel2);
  95. if (!kernel1 || !kernel2)
  96. return 0;
  97. ASSERT(x >= 0.0 && x < 1.0);
  98. x = min(1.0f, max(0.0f, x));
  99. float sampleRate1 = kernel1->sampleRate();
  100. float sampleRate2 = kernel2->sampleRate();
  101. ASSERT(sampleRate1 == sampleRate2);
  102. if (sampleRate1 != sampleRate2)
  103. return 0;
  104. float frameDelay = (1 - x) * kernel1->frameDelay() + x * kernel2->frameDelay();
  105. OwnPtr<FFTFrame> interpolatedFrame = FFTFrame::createInterpolatedFrame(*kernel1->fftFrame(), *kernel2->fftFrame(), x);
  106. return HRTFKernel::create(interpolatedFrame.release(), frameDelay, sampleRate1);
  107. }
  108. void HRTFKernel::reportMemoryUsage(MemoryObjectInfo* memoryObjectInfo) const
  109. {
  110. MemoryClassInfo info(memoryObjectInfo, this, PlatformMemoryTypes::AudioSharedData);
  111. info.addMember(m_fftFrame);
  112. }
  113. } // namespace WebCore
  114. #endif // ENABLE(WEB_AUDIO)