ConvolverNode.cpp 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  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 "ConvolverNode.h"
  27. #include "AudioBuffer.h"
  28. #include "AudioContext.h"
  29. #include "AudioNodeInput.h"
  30. #include "AudioNodeOutput.h"
  31. #include "Reverb.h"
  32. #include <wtf/MainThread.h>
  33. // Note about empirical tuning:
  34. // The maximum FFT size affects reverb performance and accuracy.
  35. // If the reverb is single-threaded and processes entirely in the real-time audio thread,
  36. // it's important not to make this too high. In this case 8192 is a good value.
  37. // But, the Reverb object is multi-threaded, so we want this as high as possible without losing too much accuracy.
  38. // Very large FFTs will have worse phase errors. Given these constraints 32768 is a good compromise.
  39. const size_t MaxFFTSize = 32768;
  40. namespace WebCore {
  41. ConvolverNode::ConvolverNode(AudioContext* context, float sampleRate)
  42. : AudioNode(context, sampleRate)
  43. , m_normalize(true)
  44. {
  45. addInput(adoptPtr(new AudioNodeInput(this)));
  46. addOutput(adoptPtr(new AudioNodeOutput(this, 2)));
  47. // Node-specific default mixing rules.
  48. m_channelCount = 2;
  49. m_channelCountMode = ClampedMax;
  50. m_channelInterpretation = AudioBus::Speakers;
  51. setNodeType(NodeTypeConvolver);
  52. initialize();
  53. }
  54. ConvolverNode::~ConvolverNode()
  55. {
  56. uninitialize();
  57. }
  58. void ConvolverNode::process(size_t framesToProcess)
  59. {
  60. AudioBus* outputBus = output(0)->bus();
  61. ASSERT(outputBus);
  62. // Synchronize with possible dynamic changes to the impulse response.
  63. MutexTryLocker tryLocker(m_processLock);
  64. if (tryLocker.locked()) {
  65. if (!isInitialized() || !m_reverb.get())
  66. outputBus->zero();
  67. else {
  68. // Process using the convolution engine.
  69. // Note that we can handle the case where nothing is connected to the input, in which case we'll just feed silence into the convolver.
  70. // FIXME: If we wanted to get fancy we could try to factor in the 'tail time' and stop processing once the tail dies down if
  71. // we keep getting fed silence.
  72. m_reverb->process(input(0)->bus(), outputBus, framesToProcess);
  73. }
  74. } else {
  75. // Too bad - the tryLock() failed. We must be in the middle of setting a new impulse response.
  76. outputBus->zero();
  77. }
  78. }
  79. void ConvolverNode::reset()
  80. {
  81. MutexLocker locker(m_processLock);
  82. if (m_reverb.get())
  83. m_reverb->reset();
  84. }
  85. void ConvolverNode::initialize()
  86. {
  87. if (isInitialized())
  88. return;
  89. AudioNode::initialize();
  90. }
  91. void ConvolverNode::uninitialize()
  92. {
  93. if (!isInitialized())
  94. return;
  95. m_reverb.clear();
  96. AudioNode::uninitialize();
  97. }
  98. void ConvolverNode::setBuffer(AudioBuffer* buffer)
  99. {
  100. ASSERT(isMainThread());
  101. if (!buffer)
  102. return;
  103. unsigned numberOfChannels = buffer->numberOfChannels();
  104. size_t bufferLength = buffer->length();
  105. // The current implementation supports up to four channel impulse responses, which are interpreted as true-stereo (see Reverb class).
  106. bool isBufferGood = numberOfChannels > 0 && numberOfChannels <= 4 && bufferLength;
  107. ASSERT(isBufferGood);
  108. if (!isBufferGood)
  109. return;
  110. // Wrap the AudioBuffer by an AudioBus. It's an efficient pointer set and not a memcpy().
  111. // This memory is simply used in the Reverb constructor and no reference to it is kept for later use in that class.
  112. RefPtr<AudioBus> bufferBus = AudioBus::create(numberOfChannels, bufferLength, false);
  113. for (unsigned i = 0; i < numberOfChannels; ++i)
  114. bufferBus->setChannelMemory(i, buffer->getChannelData(i)->data(), bufferLength);
  115. bufferBus->setSampleRate(buffer->sampleRate());
  116. // Create the reverb with the given impulse response.
  117. bool useBackgroundThreads = !context()->isOfflineContext();
  118. OwnPtr<Reverb> reverb = adoptPtr(new Reverb(bufferBus.get(), AudioNode::ProcessingSizeInFrames, MaxFFTSize, 2, useBackgroundThreads, m_normalize));
  119. {
  120. // Synchronize with process().
  121. MutexLocker locker(m_processLock);
  122. m_reverb = reverb.release();
  123. m_buffer = buffer;
  124. }
  125. }
  126. AudioBuffer* ConvolverNode::buffer()
  127. {
  128. ASSERT(isMainThread());
  129. return m_buffer.get();
  130. }
  131. double ConvolverNode::tailTime() const
  132. {
  133. return m_reverb ? m_reverb->impulseResponseLength() / static_cast<double>(sampleRate()) : 0;
  134. }
  135. double ConvolverNode::latencyTime() const
  136. {
  137. return m_reverb ? m_reverb->latencyFrames() / static_cast<double>(sampleRate()) : 0;
  138. }
  139. } // namespace WebCore
  140. #endif // ENABLE(WEB_AUDIO)