Move parseFontFaceDescriptor to CSSPropertyParser.cpp
[chromium-blink-merge.git] / third_party / WebKit / Source / platform / audio / AudioDSPKernelProcessor.cpp
blobdfc3297a4fcf9829f65a84a837d2cc601f523d85
1 /*
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
6 * met:
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
13 * distribution.
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.
31 #include "config.h"
33 #if ENABLE(WEB_AUDIO)
35 #include "platform/audio/AudioDSPKernelProcessor.h"
37 #include "platform/audio/AudioDSPKernel.h"
38 #include "wtf/MainThread.h"
40 namespace blink {
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 void AudioDSPKernelProcessor::initialize()
51 if (isInitialized())
52 return;
54 MutexLocker locker(m_processLock);
55 ASSERT(!m_kernels.size());
57 // Create processing kernels, one per channel.
58 for (unsigned i = 0; i < numberOfChannels(); ++i)
59 m_kernels.append(createKernel());
61 m_initialized = true;
62 m_hasJustReset = true;
65 void AudioDSPKernelProcessor::uninitialize()
67 if (!isInitialized())
68 return;
70 MutexLocker locker(m_processLock);
71 m_kernels.clear();
73 m_initialized = false;
76 void AudioDSPKernelProcessor::process(const AudioBus* source, AudioBus* destination, size_t framesToProcess)
78 ASSERT(source && destination);
79 if (!source || !destination)
80 return;
82 if (!isInitialized()) {
83 destination->zero();
84 return;
87 MutexTryLocker tryLocker(m_processLock);
88 if (tryLocker.locked()) {
89 bool channelCountMatches = source->numberOfChannels() == destination->numberOfChannels() && source->numberOfChannels() == m_kernels.size();
90 ASSERT(channelCountMatches);
91 if (!channelCountMatches)
92 return;
94 for (unsigned i = 0; i < m_kernels.size(); ++i)
95 m_kernels[i]->process(source->channel(i)->data(), destination->channel(i)->mutableData(), framesToProcess);
96 } else {
97 // Unfortunately, the kernel is being processed by another thread.
98 // See also ConvolverNode::process().
99 destination->zero();
103 // Resets filter state
104 void AudioDSPKernelProcessor::reset()
106 ASSERT(isMainThread());
107 if (!isInitialized())
108 return;
110 // Forces snap to parameter values - first time.
111 // Any processing depending on this value must set it to false at the appropriate time.
112 m_hasJustReset = true;
114 MutexLocker locker(m_processLock);
115 for (unsigned i = 0; i < m_kernels.size(); ++i)
116 m_kernels[i]->reset();
119 void AudioDSPKernelProcessor::setNumberOfChannels(unsigned numberOfChannels)
121 if (numberOfChannels == m_numberOfChannels)
122 return;
124 ASSERT(!isInitialized());
125 if (!isInitialized())
126 m_numberOfChannels = numberOfChannels;
129 double AudioDSPKernelProcessor::tailTime() const
131 ASSERT(!isMainThread());
132 MutexTryLocker tryLocker(m_processLock);
133 if (tryLocker.locked()) {
134 // It is expected that all the kernels have the same tailTime.
135 return !m_kernels.isEmpty() ? m_kernels.first()->tailTime() : 0;
137 // Since we don't want to block the Audio Device thread, we return a large value
138 // instead of trying to acquire the lock.
139 return std::numeric_limits<double>::infinity();
142 double AudioDSPKernelProcessor::latencyTime() const
144 ASSERT(!isMainThread());
145 MutexTryLocker tryLocker(m_processLock);
146 if (tryLocker.locked()) {
147 // It is expected that all the kernels have the same latencyTime.
148 return !m_kernels.isEmpty() ? m_kernels.first()->latencyTime() : 0;
150 // Since we don't want to block the Audio Device thread, we return a large value
151 // instead of trying to acquire the lock.
152 return std::numeric_limits<double>::infinity();
155 } // namespace blink
157 #endif // ENABLE(WEB_AUDIO)