2 * Copyright (C) 2010 Google Inc. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are
8 * * Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * * Redistributions in binary form must reproduce the above
11 * copyright notice, this list of conditions and the following disclaimer
12 * in the documentation and/or other materials provided with the
14 * * Neither the name of Google Inc. nor the names of its
15 * contributors may be used to endorse or promote products derived from
16 * this software without specific prior written permission.
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
35 #include "platform/audio/AudioDSPKernelProcessor.h"
37 #include "platform/audio/AudioDSPKernel.h"
38 #include "wtf/MainThread.h"
42 // setNumberOfChannels() may later be called if the object is not yet in an "initialized" state.
43 AudioDSPKernelProcessor::AudioDSPKernelProcessor(float sampleRate, unsigned numberOfChannels)
44 : AudioProcessor(sampleRate, numberOfChannels)
45 , m_hasJustReset(true)
49 AudioDSPKernelProcessor::~AudioDSPKernelProcessor()
53 void AudioDSPKernelProcessor::initialize()
58 MutexLocker locker(m_processLock);
59 ASSERT(!m_kernels.size());
61 // Create processing kernels, one per channel.
62 for (unsigned i = 0; i < numberOfChannels(); ++i)
63 m_kernels.append(createKernel());
66 m_hasJustReset = true;
69 void AudioDSPKernelProcessor::uninitialize()
74 MutexLocker locker(m_processLock);
77 m_initialized = false;
80 void AudioDSPKernelProcessor::process(const AudioBus* source, AudioBus* destination, size_t framesToProcess)
82 ASSERT(source && destination);
83 if (!source || !destination)
86 if (!isInitialized()) {
91 MutexTryLocker tryLocker(m_processLock);
92 if (tryLocker.locked()) {
93 bool channelCountMatches = source->numberOfChannels() == destination->numberOfChannels() && source->numberOfChannels() == m_kernels.size();
94 ASSERT(channelCountMatches);
95 if (!channelCountMatches)
98 for (unsigned i = 0; i < m_kernels.size(); ++i)
99 m_kernels[i]->process(source->channel(i)->data(), destination->channel(i)->mutableData(), framesToProcess);
101 // Unfortunately, the kernel is being processed by another thread.
102 // See also ConvolverNode::process().
107 // Resets filter state
108 void AudioDSPKernelProcessor::reset()
110 ASSERT(isMainThread());
111 if (!isInitialized())
114 // Forces snap to parameter values - first time.
115 // Any processing depending on this value must set it to false at the appropriate time.
116 m_hasJustReset = true;
118 MutexLocker locker(m_processLock);
119 for (unsigned i = 0; i < m_kernels.size(); ++i)
120 m_kernels[i]->reset();
123 void AudioDSPKernelProcessor::setNumberOfChannels(unsigned numberOfChannels)
125 if (numberOfChannels == m_numberOfChannels)
128 ASSERT(!isInitialized());
129 if (!isInitialized())
130 m_numberOfChannels = numberOfChannels;
133 double AudioDSPKernelProcessor::tailTime() const
135 ASSERT(!isMainThread());
136 MutexTryLocker tryLocker(m_processLock);
137 if (tryLocker.locked()) {
138 // It is expected that all the kernels have the same tailTime.
139 return !m_kernels.isEmpty() ? m_kernels.first()->tailTime() : 0;
141 // Since we don't want to block the Audio Device thread, we return a large value
142 // instead of trying to acquire the lock.
143 return std::numeric_limits<double>::infinity();
146 double AudioDSPKernelProcessor::latencyTime() const
148 ASSERT(!isMainThread());
149 MutexTryLocker tryLocker(m_processLock);
150 if (tryLocker.locked()) {
151 // It is expected that all the kernels have the same latencyTime.
152 return !m_kernels.isEmpty() ? m_kernels.first()->latencyTime() : 0;
154 // Since we don't want to block the Audio Device thread, we return a large value
155 // instead of trying to acquire the lock.
156 return std::numeric_limits<double>::infinity();
161 #endif // ENABLE(WEB_AUDIO)