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
25#include "config.h"
26
27#if ENABLE(WEB_AUDIO)
28
29#include "modules/webaudio/ConvolverNode.h"
30
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"
39
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.
46const size_t MaxFFTSize = 32768;
47
48namespace blink {
49
50ConvolverNode::ConvolverNode(AudioContext* context, float sampleRate)
51    : AudioNode(context, sampleRate)
52    , m_normalize(true)
53{
54    addInput();
55    addOutput(AudioNodeOutput::create(this, 2));
56
57    // Node-specific default mixing rules.
58    m_channelCount = 2;
59    m_channelCountMode = ClampedMax;
60    m_channelInterpretation = AudioBus::Speakers;
61
62    setNodeType(NodeTypeConvolver);
63    initialize();
64}
65
66ConvolverNode::~ConvolverNode()
67{
68    ASSERT(!isInitialized());
69}
70
71void ConvolverNode::dispose()
72{
73    uninitialize();
74    AudioNode::dispose();
75}
76
77void ConvolverNode::process(size_t framesToProcess)
78{
79    AudioBus* outputBus = output(0)->bus();
80    ASSERT(outputBus);
81
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())
86            outputBus->zero();
87        else {
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);
93        }
94    } else {
95        // Too bad - the tryLock() failed.  We must be in the middle of setting a new impulse response.
96        outputBus->zero();
97    }
98}
99
100void ConvolverNode::initialize()
101{
102    if (isInitialized())
103        return;
104
105    AudioNode::initialize();
106}
107
108void ConvolverNode::uninitialize()
109{
110    if (!isInitialized())
111        return;
112
113    m_reverb.clear();
114    AudioNode::uninitialize();
115}
116
117void ConvolverNode::setBuffer(AudioBuffer* buffer, ExceptionState& exceptionState)
118{
119    ASSERT(isMainThread());
120
121    if (!buffer)
122        return;
123
124    if (buffer->sampleRate() != context()->sampleRate()) {
125        exceptionState.throwDOMException(
126            NotSupportedError,
127            "The buffer sample rate of " + String::number(buffer->sampleRate())
128            + " does not match the context rate of " + String::number(context()->sampleRate())
129            + " Hz.");
130    }
131
132    unsigned numberOfChannels = buffer->numberOfChannels();
133    size_t bufferLength = buffer->length();
134
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);
138    if (!isBufferGood)
139        return;
140
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);
146
147    bufferBus->setSampleRate(buffer->sampleRate());
148
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));
152
153    {
154        // Synchronize with process().
155        MutexLocker locker(m_processLock);
156        m_reverb = reverb.release();
157        m_buffer = buffer;
158    }
159}
160
161AudioBuffer* ConvolverNode::buffer()
162{
163    ASSERT(isMainThread());
164    return m_buffer.get();
165}
166
167double ConvolverNode::tailTime() const
168{
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();
175}
176
177double ConvolverNode::latencyTime() const
178{
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();
185}
186
187void ConvolverNode::trace(Visitor* visitor)
188{
189    visitor->trace(m_buffer);
190    AudioNode::trace(visitor);
191}
192
193} // namespace blink
194
195#endif // ENABLE(WEB_AUDIO)
196