2 * Copyright (C) 2010, Google Inc. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
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.
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.
29 #include "modules/webaudio/ConvolverNode.h"
31 #include "bindings/core/v8/ExceptionState.h"
32 #include "core/dom/ExceptionCode.h"
33 #include "platform/audio/Reverb.h"
34 #include "modules/webaudio/AudioBuffer.h"
35 #include "modules/webaudio/AudioContext.h"
36 #include "modules/webaudio/AudioNodeInput.h"
37 #include "modules/webaudio/AudioNodeOutput.h"
38 #include "wtf/MainThread.h"
40 // Note about empirical tuning:
41 // The maximum FFT size affects reverb performance and accuracy.
42 // If the reverb is single-threaded and processes entirely in the real-time audio thread,
43 // it's important not to make this too high. In this case 8192 is a good value.
44 // But, the Reverb object is multi-threaded, so we want this as high as possible without losing too much accuracy.
45 // Very large FFTs will have worse phase errors. Given these constraints 32768 is a good compromise.
46 const size_t MaxFFTSize = 32768;
50 ConvolverNode::ConvolverNode(AudioContext* context, float sampleRate)
51 : AudioNode(context, sampleRate)
55 addOutput(AudioNodeOutput::create(this, 2));
57 // Node-specific default mixing rules.
59 m_channelCountMode = ClampedMax;
60 m_channelInterpretation = AudioBus::Speakers;
62 setNodeType(NodeTypeConvolver);
66 ConvolverNode::~ConvolverNode()
68 ASSERT(!isInitialized());
71 void ConvolverNode::dispose()
77 void ConvolverNode::process(size_t framesToProcess)
79 AudioBus* outputBus = output(0)->bus();
82 // Synchronize with possible dynamic changes to the impulse response.
83 MutexTryLocker tryLocker(m_processLock);
84 if (tryLocker.locked()) {
85 if (!isInitialized() || !m_reverb.get())
88 // Process using the convolution engine.
89 // 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.
90 // 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
91 // we keep getting fed silence.
92 m_reverb->process(input(0)->bus(), outputBus, framesToProcess);
95 // Too bad - the tryLock() failed. We must be in the middle of setting a new impulse response.
100 void ConvolverNode::initialize()
105 AudioNode::initialize();
108 void ConvolverNode::uninitialize()
110 if (!isInitialized())
114 AudioNode::uninitialize();
117 void ConvolverNode::setBuffer(AudioBuffer* buffer, ExceptionState& exceptionState)
119 ASSERT(isMainThread());
124 if (buffer->sampleRate() != context()->sampleRate()) {
125 exceptionState.throwDOMException(
127 "The buffer sample rate of " + String::number(buffer->sampleRate())
128 + " does not match the context rate of " + String::number(context()->sampleRate())
132 unsigned numberOfChannels = buffer->numberOfChannels();
133 size_t bufferLength = buffer->length();
135 // The current implementation supports up to four channel impulse responses, which are interpreted as true-stereo (see Reverb class).
136 bool isBufferGood = numberOfChannels > 0 && numberOfChannels <= 4 && bufferLength;
137 ASSERT(isBufferGood);
141 // Wrap the AudioBuffer by an AudioBus. It's an efficient pointer set and not a memcpy().
142 // This memory is simply used in the Reverb constructor and no reference to it is kept for later use in that class.
143 RefPtr<AudioBus> bufferBus = AudioBus::create(numberOfChannels, bufferLength, false);
144 for (unsigned i = 0; i < numberOfChannels; ++i)
145 bufferBus->setChannelMemory(i, buffer->getChannelData(i)->data(), bufferLength);
147 bufferBus->setSampleRate(buffer->sampleRate());
149 // Create the reverb with the given impulse response.
150 bool useBackgroundThreads = !context()->isOfflineContext();
151 OwnPtr<Reverb> reverb = adoptPtr(new Reverb(bufferBus.get(), AudioNode::ProcessingSizeInFrames, MaxFFTSize, 2, useBackgroundThreads, m_normalize));
154 // Synchronize with process().
155 MutexLocker locker(m_processLock);
156 m_reverb = reverb.release();
161 AudioBuffer* ConvolverNode::buffer()
163 ASSERT(isMainThread());
164 return m_buffer.get();
167 double ConvolverNode::tailTime() const
169 MutexTryLocker tryLocker(m_processLock);
170 if (tryLocker.locked())
171 return m_reverb ? m_reverb->impulseResponseLength() / static_cast<double>(sampleRate()) : 0;
172 // Since we don't want to block the Audio Device thread, we return a large value
173 // instead of trying to acquire the lock.
174 return std::numeric_limits<double>::infinity();
177 double ConvolverNode::latencyTime() const
179 MutexTryLocker tryLocker(m_processLock);
180 if (tryLocker.locked())
181 return m_reverb ? m_reverb->latencyFrames() / static_cast<double>(sampleRate()) : 0;
182 // Since we don't want to block the Audio Device thread, we return a large value
183 // instead of trying to acquire the lock.
184 return std::numeric_limits<double>::infinity();
187 void ConvolverNode::trace(Visitor* visitor)
189 visitor->trace(m_buffer);
190 AudioNode::trace(visitor);
195 #endif // ENABLE(WEB_AUDIO)