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 void AudioDSPKernelProcessor::initialize()
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());
62 m_hasJustReset
= true;
65 void AudioDSPKernelProcessor::uninitialize()
70 MutexLocker
locker(m_processLock
);
73 m_initialized
= false;
76 void AudioDSPKernelProcessor::process(const AudioBus
* source
, AudioBus
* destination
, size_t framesToProcess
)
78 ASSERT(source
&& destination
);
79 if (!source
|| !destination
)
82 if (!isInitialized()) {
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
)
94 for (unsigned i
= 0; i
< m_kernels
.size(); ++i
)
95 m_kernels
[i
]->process(source
->channel(i
)->data(), destination
->channel(i
)->mutableData(), framesToProcess
);
97 // Unfortunately, the kernel is being processed by another thread.
98 // See also ConvolverNode::process().
103 // Resets filter state
104 void AudioDSPKernelProcessor::reset()
106 ASSERT(isMainThread());
107 if (!isInitialized())
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
)
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();
157 #endif // ENABLE(WEB_AUDIO)