BiquadProcessor.cpp 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  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. * 1. Redistributions of source code must retain the above copyright
  8. * notice, this list of conditions and the following disclaimer.
  9. * 2. Redistributions in binary form must reproduce the above copyright
  10. * notice, this list of conditions and the following disclaimer in the
  11. * documentation and/or other materials provided with the distribution.
  12. *
  13. * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS'' AND ANY
  14. * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  15. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  16. * DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS BE LIABLE FOR ANY
  17. * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  18. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  19. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
  20. * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  21. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  22. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  23. */
  24. #include "config.h"
  25. #if ENABLE(WEB_AUDIO)
  26. #include "BiquadProcessor.h"
  27. #include "BiquadDSPKernel.h"
  28. namespace WebCore {
  29. BiquadProcessor::BiquadProcessor(AudioContext* context, float sampleRate, size_t numberOfChannels, bool autoInitialize)
  30. : AudioDSPKernelProcessor(sampleRate, numberOfChannels)
  31. , m_type(LowPass)
  32. , m_parameter1(0)
  33. , m_parameter2(0)
  34. , m_parameter3(0)
  35. , m_parameter4(0)
  36. , m_filterCoefficientsDirty(true)
  37. , m_hasSampleAccurateValues(false)
  38. {
  39. double nyquist = 0.5 * this->sampleRate();
  40. // Create parameters for BiquadFilterNode.
  41. m_parameter1 = AudioParam::create(context, "frequency", 350.0, 10.0, nyquist);
  42. m_parameter2 = AudioParam::create(context, "Q", 1, 0.0001, 1000.0);
  43. m_parameter3 = AudioParam::create(context, "gain", 0.0, -40, 40);
  44. m_parameter4 = AudioParam::create(context, "detune", 0.0, -4800, 4800);
  45. if (autoInitialize)
  46. initialize();
  47. }
  48. BiquadProcessor::~BiquadProcessor()
  49. {
  50. if (isInitialized())
  51. uninitialize();
  52. }
  53. PassOwnPtr<AudioDSPKernel> BiquadProcessor::createKernel()
  54. {
  55. return adoptPtr(new BiquadDSPKernel(this));
  56. }
  57. void BiquadProcessor::checkForDirtyCoefficients()
  58. {
  59. // Deal with smoothing / de-zippering. Start out assuming filter parameters are not changing.
  60. // The BiquadDSPKernel objects rely on this value to see if they need to re-compute their internal filter coefficients.
  61. m_filterCoefficientsDirty = false;
  62. m_hasSampleAccurateValues = false;
  63. if (m_parameter1->hasSampleAccurateValues() || m_parameter2->hasSampleAccurateValues() || m_parameter3->hasSampleAccurateValues() || m_parameter4->hasSampleAccurateValues()) {
  64. m_filterCoefficientsDirty = true;
  65. m_hasSampleAccurateValues = true;
  66. } else {
  67. if (m_hasJustReset) {
  68. // Snap to exact values first time after reset, then smooth for subsequent changes.
  69. m_parameter1->resetSmoothedValue();
  70. m_parameter2->resetSmoothedValue();
  71. m_parameter3->resetSmoothedValue();
  72. m_parameter4->resetSmoothedValue();
  73. m_filterCoefficientsDirty = true;
  74. m_hasJustReset = false;
  75. } else {
  76. // Smooth all of the filter parameters. If they haven't yet converged to their target value then mark coefficients as dirty.
  77. bool isStable1 = m_parameter1->smooth();
  78. bool isStable2 = m_parameter2->smooth();
  79. bool isStable3 = m_parameter3->smooth();
  80. bool isStable4 = m_parameter4->smooth();
  81. if (!(isStable1 && isStable2 && isStable3 && isStable4))
  82. m_filterCoefficientsDirty = true;
  83. }
  84. }
  85. }
  86. void BiquadProcessor::process(const AudioBus* source, AudioBus* destination, size_t framesToProcess)
  87. {
  88. if (!isInitialized()) {
  89. destination->zero();
  90. return;
  91. }
  92. checkForDirtyCoefficients();
  93. // For each channel of our input, process using the corresponding BiquadDSPKernel into the output channel.
  94. for (unsigned i = 0; i < m_kernels.size(); ++i)
  95. m_kernels[i]->process(source->channel(i)->data(), destination->channel(i)->mutableData(), framesToProcess);
  96. }
  97. void BiquadProcessor::setType(FilterType type)
  98. {
  99. if (type != m_type) {
  100. m_type = type;
  101. reset(); // The filter state must be reset only if the type has changed.
  102. }
  103. }
  104. void BiquadProcessor::getFrequencyResponse(int nFrequencies,
  105. const float* frequencyHz,
  106. float* magResponse,
  107. float* phaseResponse)
  108. {
  109. // Compute the frequency response on a separate temporary kernel
  110. // to avoid interfering with the processing running in the audio
  111. // thread on the main kernels.
  112. OwnPtr<BiquadDSPKernel> responseKernel = adoptPtr(new BiquadDSPKernel(this));
  113. responseKernel->getFrequencyResponse(nFrequencies, frequencyHz, magResponse, phaseResponse);
  114. }
  115. } // namespace WebCore
  116. #endif // ENABLE(WEB_AUDIO)