1 | /* |
2 | * Copyright (C) 2010, Google Inc. All rights reserved. |
3 | * Copyright (C) 2016, Apple Inc. All rights reserved. |
4 | * |
5 | * Redistribution and use in source and binary forms, with or without |
6 | * modification, are permitted provided that the following conditions |
7 | * are met: |
8 | * 1. Redistributions of source code must retain the above copyright |
9 | * notice, this list of conditions and the following disclaimer. |
10 | * 2. Redistributions in binary form must reproduce the above copyright |
11 | * notice, this list of conditions and the following disclaimer in the |
12 | * documentation and/or other materials provided with the distribution. |
13 | * |
14 | * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS'' AND ANY |
15 | * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED |
16 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
17 | * DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS BE LIABLE FOR ANY |
18 | * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES |
19 | * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; |
20 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON |
21 | * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
22 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
23 | * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
24 | */ |
25 | |
26 | #include "config.h" |
27 | |
28 | #if ENABLE(WEB_AUDIO) |
29 | |
30 | #include "ConvolverNode.h" |
31 | |
32 | #include "AudioBuffer.h" |
33 | #include "AudioNodeInput.h" |
34 | #include "AudioNodeOutput.h" |
35 | #include "Reverb.h" |
36 | #include <wtf/IsoMallocInlines.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. |
44 | const size_t MaxFFTSize = 32768; |
45 | |
46 | namespace WebCore { |
47 | |
48 | WTF_MAKE_ISO_ALLOCATED_IMPL(ConvolverNode); |
49 | |
50 | ConvolverNode::ConvolverNode(AudioContext& context, float sampleRate) |
51 | : AudioNode(context, sampleRate) |
52 | { |
53 | setNodeType(NodeTypeConvolver); |
54 | |
55 | addInput(std::make_unique<AudioNodeInput>(this)); |
56 | addOutput(std::make_unique<AudioNodeOutput>(this, 2)); |
57 | |
58 | // Node-specific default mixing rules. |
59 | m_channelCount = 2; |
60 | m_channelCountMode = ClampedMax; |
61 | m_channelInterpretation = AudioBus::Speakers; |
62 | |
63 | initialize(); |
64 | } |
65 | |
66 | ConvolverNode::~ConvolverNode() |
67 | { |
68 | uninitialize(); |
69 | } |
70 | |
71 | void ConvolverNode::process(size_t framesToProcess) |
72 | { |
73 | AudioBus* outputBus = output(0)->bus(); |
74 | ASSERT(outputBus); |
75 | |
76 | // Synchronize with possible dynamic changes to the impulse response. |
77 | std::unique_lock<Lock> lock(m_processMutex, std::try_to_lock); |
78 | if (!lock.owns_lock()) { |
79 | // Too bad - the try_lock() failed. We must be in the middle of setting a new impulse response. |
80 | outputBus->zero(); |
81 | return; |
82 | } |
83 | |
84 | if (!isInitialized() || !m_reverb.get()) |
85 | outputBus->zero(); |
86 | else { |
87 | // Process using the convolution engine. |
88 | // 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. |
89 | // 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 |
90 | // we keep getting fed silence. |
91 | m_reverb->process(input(0)->bus(), outputBus, framesToProcess); |
92 | } |
93 | } |
94 | |
95 | void ConvolverNode::reset() |
96 | { |
97 | std::lock_guard<Lock> lock(m_processMutex); |
98 | if (m_reverb) |
99 | m_reverb->reset(); |
100 | } |
101 | |
102 | void ConvolverNode::initialize() |
103 | { |
104 | if (isInitialized()) |
105 | return; |
106 | |
107 | AudioNode::initialize(); |
108 | } |
109 | |
110 | void ConvolverNode::uninitialize() |
111 | { |
112 | if (!isInitialized()) |
113 | return; |
114 | |
115 | m_reverb = nullptr; |
116 | AudioNode::uninitialize(); |
117 | } |
118 | |
119 | ExceptionOr<void> ConvolverNode::setBuffer(AudioBuffer* buffer) |
120 | { |
121 | ASSERT(isMainThread()); |
122 | |
123 | if (!buffer) |
124 | return { }; |
125 | |
126 | if (buffer->sampleRate() != context().sampleRate()) |
127 | return Exception { NotSupportedError }; |
128 | |
129 | unsigned numberOfChannels = buffer->numberOfChannels(); |
130 | size_t bufferLength = buffer->length(); |
131 | |
132 | // The current implementation supports only 1-, 2-, or 4-channel impulse responses, with the |
133 | // 4-channel response being interpreted as true-stereo (see Reverb class). |
134 | bool isChannelCountGood = (numberOfChannels == 1 || numberOfChannels == 2 || numberOfChannels == 4) && bufferLength; |
135 | |
136 | if (!isChannelCountGood) |
137 | return Exception { NotSupportedError }; |
138 | |
139 | // Wrap the AudioBuffer by an AudioBus. It's an efficient pointer set and not a memcpy(). |
140 | // This memory is simply used in the Reverb constructor and no reference to it is kept for later use in that class. |
141 | auto bufferBus = AudioBus::create(numberOfChannels, bufferLength, false); |
142 | for (unsigned i = 0; i < numberOfChannels; ++i) |
143 | bufferBus->setChannelMemory(i, buffer->channelData(i)->data(), bufferLength); |
144 | |
145 | bufferBus->setSampleRate(buffer->sampleRate()); |
146 | |
147 | // Create the reverb with the given impulse response. |
148 | bool useBackgroundThreads = !context().isOfflineContext(); |
149 | auto reverb = std::make_unique<Reverb>(bufferBus.get(), AudioNode::ProcessingSizeInFrames, MaxFFTSize, 2, useBackgroundThreads, m_normalize); |
150 | |
151 | { |
152 | // Synchronize with process(). |
153 | std::lock_guard<Lock> lock(m_processMutex); |
154 | m_reverb = WTFMove(reverb); |
155 | m_buffer = buffer; |
156 | } |
157 | |
158 | return { }; |
159 | } |
160 | |
161 | AudioBuffer* ConvolverNode::buffer() |
162 | { |
163 | ASSERT(isMainThread()); |
164 | return m_buffer.get(); |
165 | } |
166 | |
167 | double ConvolverNode::tailTime() const |
168 | { |
169 | return m_reverb ? m_reverb->impulseResponseLength() / static_cast<double>(sampleRate()) : 0; |
170 | } |
171 | |
172 | double ConvolverNode::latencyTime() const |
173 | { |
174 | return m_reverb ? m_reverb->latencyFrames() / static_cast<double>(sampleRate()) : 0; |
175 | } |
176 | |
177 | } // namespace WebCore |
178 | |
179 | #endif // ENABLE(WEB_AUDIO) |
180 | |