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 are
6 * met:
7 *
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.
17 *
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.
29 */
30
31 #include "config.h"
32
33 #if ENABLE(WEB_AUDIO)
34
35 #include "platform/audio/AudioDSPKernelProcessor.h"
36
37 #include "platform/audio/AudioDSPKernel.h"
38 #include "wtf/MainThread.h"
39
40 namespace blink {
41
42 // setNumberOfChannels() may later be called if the object is not yet in an "initialized" state.
AudioDSPKernelProcessor(float sampleRate,unsigned numberOfChannels)43 AudioDSPKernelProcessor::AudioDSPKernelProcessor(float sampleRate, unsigned numberOfChannels)
44 : AudioProcessor(sampleRate, numberOfChannels)
45 , m_hasJustReset(true)
46 {
47 }
48
~AudioDSPKernelProcessor()49 AudioDSPKernelProcessor::~AudioDSPKernelProcessor()
50 {
51 }
52
initialize()53 void AudioDSPKernelProcessor::initialize()
54 {
55 if (isInitialized())
56 return;
57
58 MutexLocker locker(m_processLock);
59 ASSERT(!m_kernels.size());
60
61 // Create processing kernels, one per channel.
62 for (unsigned i = 0; i < numberOfChannels(); ++i)
63 m_kernels.append(createKernel());
64
65 m_initialized = true;
66 m_hasJustReset = true;
67 }
68
uninitialize()69 void AudioDSPKernelProcessor::uninitialize()
70 {
71 if (!isInitialized())
72 return;
73
74 MutexLocker locker(m_processLock);
75 m_kernels.clear();
76
77 m_initialized = false;
78 }
79
process(const AudioBus * source,AudioBus * destination,size_t framesToProcess)80 void AudioDSPKernelProcessor::process(const AudioBus* source, AudioBus* destination, size_t framesToProcess)
81 {
82 ASSERT(source && destination);
83 if (!source || !destination)
84 return;
85
86 if (!isInitialized()) {
87 destination->zero();
88 return;
89 }
90
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)
96 return;
97
98 for (unsigned i = 0; i < m_kernels.size(); ++i)
99 m_kernels[i]->process(source->channel(i)->data(), destination->channel(i)->mutableData(), framesToProcess);
100 } else {
101 // Unfortunately, the kernel is being processed by another thread.
102 // See also ConvolverNode::process().
103 destination->zero();
104 }
105 }
106
107 // Resets filter state
reset()108 void AudioDSPKernelProcessor::reset()
109 {
110 ASSERT(isMainThread());
111 if (!isInitialized())
112 return;
113
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;
117
118 MutexLocker locker(m_processLock);
119 for (unsigned i = 0; i < m_kernels.size(); ++i)
120 m_kernels[i]->reset();
121 }
122
setNumberOfChannels(unsigned numberOfChannels)123 void AudioDSPKernelProcessor::setNumberOfChannels(unsigned numberOfChannels)
124 {
125 if (numberOfChannels == m_numberOfChannels)
126 return;
127
128 ASSERT(!isInitialized());
129 if (!isInitialized())
130 m_numberOfChannels = numberOfChannels;
131 }
132
tailTime() const133 double AudioDSPKernelProcessor::tailTime() const
134 {
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;
140 }
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();
144 }
145
latencyTime() const146 double AudioDSPKernelProcessor::latencyTime() const
147 {
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;
153 }
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();
157 }
158
159 } // namespace blink
160
161 #endif // ENABLE(WEB_AUDIO)
162