initial import
[vuplus_webkit] / Source / WebCore / webaudio / ConvolverNode.cpp
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.
44 const size_t MaxFFTSize = 32768;
45
46 namespace WebCore {
47
48 ConvolverNode::ConvolverNode(AudioContext* context, double sampleRate)
49     : AudioNode(context, sampleRate)
50 {
51     addInput(adoptPtr(new AudioNodeInput(this)));
52     addOutput(adoptPtr(new AudioNodeOutput(this, 2)));
53     
54     setType(NodeTypeConvolver);
55     
56     initialize();
57 }
58
59 ConvolverNode::~ConvolverNode()
60 {
61     uninitialize();
62 }
63
64 void ConvolverNode::process(size_t framesToProcess)
65 {
66     AudioBus* outputBus = output(0)->bus();
67     ASSERT(outputBus);
68
69     // Synchronize with possible dynamic changes to the impulse response.
70     if (m_processLock.tryLock()) {
71         if (!isInitialized() || !m_reverb.get())
72             outputBus->zero();
73         else {
74             // Process using the convolution engine.
75             // 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.
76             // 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
77             // we keep getting fed silence.
78             m_reverb->process(input(0)->bus(), outputBus, framesToProcess);
79         }
80         
81         m_processLock.unlock();
82     } else {
83         // Too bad - the tryLock() failed.  We must be in the middle of setting a new impulse response.
84         outputBus->zero();
85     }
86 }
87
88 void ConvolverNode::reset()
89 {
90     MutexLocker locker(m_processLock);
91     if (m_reverb.get())
92         m_reverb->reset();
93 }
94
95 void ConvolverNode::initialize()
96 {
97     if (isInitialized())
98         return;
99         
100     AudioNode::initialize();
101 }
102
103 void ConvolverNode::uninitialize()
104 {
105     if (!isInitialized())
106         return;
107
108     m_reverb.clear();
109     AudioNode::uninitialize();
110 }
111
112 void ConvolverNode::setBuffer(AudioBuffer* buffer)
113 {
114     ASSERT(isMainThread());
115     
116     ASSERT(buffer);
117     if (!buffer)
118         return;
119
120     unsigned numberOfChannels = buffer->numberOfChannels();
121     size_t bufferLength = buffer->length();
122
123     // The current implementation supports up to four channel impulse responses, which are interpreted as true-stereo (see Reverb class).
124     bool isBufferGood = numberOfChannels > 0 && numberOfChannels <= 4 && bufferLength;
125     ASSERT(isBufferGood);
126     if (!isBufferGood)
127         return;
128
129     // Wrap the AudioBuffer by an AudioBus. It's an efficient pointer set and not a memcpy().
130     // This memory is simply used in the Reverb constructor and no reference to it is kept for later use in that class.
131     AudioBus bufferBus(numberOfChannels, bufferLength, false);
132     for (unsigned i = 0; i < numberOfChannels; ++i)
133         bufferBus.setChannelMemory(i, buffer->getChannelData(i)->data(), bufferLength);
134     
135     // Create the reverb with the given impulse response.
136     bool useBackgroundThreads = !context()->isOfflineContext();
137     OwnPtr<Reverb> reverb = adoptPtr(new Reverb(&bufferBus, AudioNode::ProcessingSizeInFrames, MaxFFTSize, 2, useBackgroundThreads));
138
139     {
140         // Synchronize with process().
141         MutexLocker locker(m_processLock);
142         m_reverb = reverb.release();
143         m_buffer = buffer;
144     }
145 }
146
147 AudioBuffer* ConvolverNode::buffer()
148 {
149     ASSERT(isMainThread());
150     return m_buffer.get();
151 }
152
153 } // namespace WebCore
154
155 #endif // ENABLE(WEB_AUDIO)