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 "ConvolverNode.h"
30
31#include "AudioBuffer.h"
32#include "AudioContext.h"
33#include "AudioNodeInput.h"
34#include "AudioNodeOutput.h"
35#include "Reverb.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    addInput(std::make_unique<AudioNodeInput>(this));
53    addOutput(std::make_unique<AudioNodeOutput>(this, 2));
54
55    // Node-specific default mixing rules.
56    m_channelCount = 2;
57    m_channelCountMode = ClampedMax;
58    m_channelInterpretation = AudioBus::Speakers;
59
60    setNodeType(NodeTypeConvolver);
61
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    std::unique_lock<std::mutex> lock(m_processMutex, std::try_to_lock);
77    if (!lock.owns_lock()) {
78        // Too bad - the try_lock() failed. We must be in the middle of setting a new impulse response.
79        outputBus->zero();
80        return;
81    }
82
83    if (!isInitialized() || !m_reverb.get())
84        outputBus->zero();
85    else {
86        // Process using the convolution engine.
87        // 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.
88        // 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
89        // we keep getting fed silence.
90        m_reverb->process(input(0)->bus(), outputBus, framesToProcess);
91    }
92}
93
94void ConvolverNode::reset()
95{
96    std::lock_guard<std::mutex> lock(m_processMutex);
97    if (m_reverb)
98        m_reverb->reset();
99}
100
101void ConvolverNode::initialize()
102{
103    if (isInitialized())
104        return;
105
106    AudioNode::initialize();
107}
108
109void ConvolverNode::uninitialize()
110{
111    if (!isInitialized())
112        return;
113
114    m_reverb = nullptr;
115    AudioNode::uninitialize();
116}
117
118void ConvolverNode::setBuffer(AudioBuffer* buffer)
119{
120    ASSERT(isMainThread());
121
122    if (!buffer)
123        return;
124
125    unsigned numberOfChannels = buffer->numberOfChannels();
126    size_t bufferLength = buffer->length();
127
128    // The current implementation supports up to four channel impulse responses, which are interpreted as true-stereo (see Reverb class).
129    bool isBufferGood = numberOfChannels > 0 && numberOfChannels <= 4 && bufferLength;
130    ASSERT(isBufferGood);
131    if (!isBufferGood)
132        return;
133
134    // Wrap the AudioBuffer by an AudioBus. It's an efficient pointer set and not a memcpy().
135    // This memory is simply used in the Reverb constructor and no reference to it is kept for later use in that class.
136    RefPtr<AudioBus> bufferBus = AudioBus::create(numberOfChannels, bufferLength, false);
137    for (unsigned i = 0; i < numberOfChannels; ++i)
138        bufferBus->setChannelMemory(i, buffer->getChannelData(i)->data(), bufferLength);
139
140    bufferBus->setSampleRate(buffer->sampleRate());
141
142    // Create the reverb with the given impulse response.
143    bool useBackgroundThreads = !context()->isOfflineContext();
144    auto reverb = std::make_unique<Reverb>(bufferBus.get(), AudioNode::ProcessingSizeInFrames, MaxFFTSize, 2, useBackgroundThreads, m_normalize);
145
146    {
147        // Synchronize with process().
148        std::lock_guard<std::mutex> lock(m_processMutex);
149        m_reverb = WTF::move(reverb);
150        m_buffer = buffer;
151    }
152}
153
154AudioBuffer* ConvolverNode::buffer()
155{
156    ASSERT(isMainThread());
157    return m_buffer.get();
158}
159
160double ConvolverNode::tailTime() const
161{
162    return m_reverb ? m_reverb->impulseResponseLength() / static_cast<double>(sampleRate()) : 0;
163}
164
165double ConvolverNode::latencyTime() const
166{
167    return m_reverb ? m_reverb->latencyFrames() / static_cast<double>(sampleRate()) : 0;
168}
169
170} // namespace WebCore
171
172#endif // ENABLE(WEB_AUDIO)
173