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 "core/platform/audio/Reverb.h"
32#include "modules/webaudio/AudioBuffer.h"
33#include "modules/webaudio/AudioContext.h"
34#include "modules/webaudio/AudioNodeInput.h"
35#include "modules/webaudio/AudioNodeOutput.h"
36#include "wtf/MainThread.h"
37
38// Note about empirical tuning:
39// The maximum FFT size affects reverb performance and accuracy.
40// If the reverb is single-threaded and processes entirely in the real-time audio thread,
41// it's important not to make this too high.  In this case 8192 is a good value.
42// But, the Reverb object is multi-threaded, so we want this as high as possible without losing too much accuracy.
43// Very large FFTs will have worse phase errors. Given these constraints 32768 is a good compromise.
44const size_t MaxFFTSize = 32768;
45
46namespace WebCore {
47
48ConvolverNode::ConvolverNode(AudioContext* context, float sampleRate)
49    : AudioNode(context, sampleRate)
50    , m_normalize(true)
51{
52    ScriptWrappable::init(this);
53    addInput(adoptPtr(new AudioNodeInput(this)));
54    addOutput(adoptPtr(new AudioNodeOutput(this, 2)));
55
56    // Node-specific default mixing rules.
57    m_channelCount = 2;
58    m_channelCountMode = ClampedMax;
59    m_channelInterpretation = AudioBus::Speakers;
60
61    setNodeType(NodeTypeConvolver);
62    initialize();
63}
64
65ConvolverNode::~ConvolverNode()
66{
67    uninitialize();
68}
69
70void ConvolverNode::process(size_t framesToProcess)
71{
72    AudioBus* outputBus = output(0)->bus();
73    ASSERT(outputBus);
74
75    // Synchronize with possible dynamic changes to the impulse response.
76    MutexTryLocker tryLocker(m_processLock);
77    if (tryLocker.locked()) {
78        if (!isInitialized() || !m_reverb.get())
79            outputBus->zero();
80        else {
81            // Process using the convolution engine.
82            // 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.
83            // 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
84            // we keep getting fed silence.
85            m_reverb->process(input(0)->bus(), outputBus, framesToProcess);
86        }
87    } else {
88        // Too bad - the tryLock() failed.  We must be in the middle of setting a new impulse response.
89        outputBus->zero();
90    }
91}
92
93void ConvolverNode::reset()
94{
95    MutexLocker locker(m_processLock);
96    if (m_reverb.get())
97        m_reverb->reset();
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)
118{
119    ASSERT(isMainThread());
120
121    if (!buffer)
122        return;
123
124    unsigned numberOfChannels = buffer->numberOfChannels();
125    size_t bufferLength = buffer->length();
126
127    // The current implementation supports up to four channel impulse responses, which are interpreted as true-stereo (see Reverb class).
128    bool isBufferGood = numberOfChannels > 0 && numberOfChannels <= 4 && bufferLength;
129    ASSERT(isBufferGood);
130    if (!isBufferGood)
131        return;
132
133    // Wrap the AudioBuffer by an AudioBus. It's an efficient pointer set and not a memcpy().
134    // This memory is simply used in the Reverb constructor and no reference to it is kept for later use in that class.
135    RefPtr<AudioBus> bufferBus = AudioBus::create(numberOfChannels, bufferLength, false);
136    for (unsigned i = 0; i < numberOfChannels; ++i)
137        bufferBus->setChannelMemory(i, buffer->getChannelData(i)->data(), bufferLength);
138
139    bufferBus->setSampleRate(buffer->sampleRate());
140
141    // Create the reverb with the given impulse response.
142    bool useBackgroundThreads = !context()->isOfflineContext();
143    OwnPtr<Reverb> reverb = adoptPtr(new Reverb(bufferBus.get(), AudioNode::ProcessingSizeInFrames, MaxFFTSize, 2, useBackgroundThreads, m_normalize));
144
145    {
146        // Synchronize with process().
147        MutexLocker locker(m_processLock);
148        m_reverb = reverb.release();
149        m_buffer = buffer;
150    }
151}
152
153AudioBuffer* ConvolverNode::buffer()
154{
155    ASSERT(isMainThread());
156    return m_buffer.get();
157}
158
159double ConvolverNode::tailTime() const
160{
161    MutexTryLocker tryLocker(m_processLock);
162    if (tryLocker.locked())
163        return m_reverb ? m_reverb->impulseResponseLength() / static_cast<double>(sampleRate()) : 0;
164    // Since we don't want to block the Audio Device thread, we return a large value
165    // instead of trying to acquire the lock.
166    return std::numeric_limits<double>::infinity();
167}
168
169double ConvolverNode::latencyTime() const
170{
171    MutexTryLocker tryLocker(m_processLock);
172    if (tryLocker.locked())
173        return m_reverb ? m_reverb->latencyFrames() / static_cast<double>(sampleRate()) : 0;
174    // Since we don't want to block the Audio Device thread, we return a large value
175    // instead of trying to acquire the lock.
176    return std::numeric_limits<double>::infinity();
177}
178
179} // namespace WebCore
180
181#endif // ENABLE(WEB_AUDIO)
182