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 "AudioGainNode.h"
30
31 #include "AudioBus.h"
32 #include "AudioNodeInput.h"
33 #include "AudioNodeOutput.h"
34
35 namespace WebCore {
36
AudioGainNode(AudioContext * context,double sampleRate)37 AudioGainNode::AudioGainNode(AudioContext* context, double sampleRate)
38 : AudioNode(context, sampleRate)
39 , m_lastGain(1.0)
40 {
41 m_gain = AudioGain::create("gain", 1.0, 0.0, 1.0);
42
43 addInput(adoptPtr(new AudioNodeInput(this)));
44 addOutput(adoptPtr(new AudioNodeOutput(this, 1)));
45
46 setType(NodeTypeGain);
47
48 initialize();
49 }
50
process(size_t)51 void AudioGainNode::process(size_t /*framesToProcess*/)
52 {
53 // FIXME: there is a nice optimization to avoid processing here, and let the gain change
54 // happen in the summing junction input of the AudioNode we're connected to.
55 // Then we can avoid all of the following:
56
57 AudioBus* outputBus = output(0)->bus();
58 ASSERT(outputBus);
59
60 // The realtime thread can't block on this lock, so we call tryLock() instead.
61 if (m_processLock.tryLock()) {
62 if (!isInitialized() || !input(0)->isConnected())
63 outputBus->zero();
64 else {
65 AudioBus* inputBus = input(0)->bus();
66
67 // Apply the gain with de-zippering into the output bus.
68 outputBus->copyWithGainFrom(*inputBus, &m_lastGain, gain()->value());
69 }
70
71 m_processLock.unlock();
72 } else {
73 // Too bad - the tryLock() failed. We must be in the middle of re-connecting and were already outputting silence anyway...
74 outputBus->zero();
75 }
76 }
77
reset()78 void AudioGainNode::reset()
79 {
80 // Snap directly to desired gain.
81 m_lastGain = gain()->value();
82 }
83
84 // FIXME: this can go away when we do mixing with gain directly in summing junction of AudioNodeInput
85 //
86 // As soon as we know the channel count of our input, we can lazily initialize.
87 // Sometimes this may be called more than once with different channel counts, in which case we must safely
88 // uninitialize and then re-initialize with the new channel count.
checkNumberOfChannelsForInput(AudioNodeInput * input)89 void AudioGainNode::checkNumberOfChannelsForInput(AudioNodeInput* input)
90 {
91 ASSERT(input && input == this->input(0));
92 if (input != this->input(0))
93 return;
94
95 unsigned numberOfChannels = input->numberOfChannels();
96
97 if (isInitialized() && numberOfChannels != output(0)->numberOfChannels()) {
98 // We're already initialized but the channel count has changed.
99 // We need to be careful since we may be actively processing right now, so synchronize with process().
100 MutexLocker locker(m_processLock);
101 uninitialize();
102 }
103
104 if (!isInitialized()) {
105 // This will propagate the channel count to any nodes connected further downstream in the graph.
106 output(0)->setNumberOfChannels(numberOfChannels);
107 initialize();
108 }
109 }
110
111 } // namespace WebCore
112
113 #endif // ENABLE(WEB_AUDIO)
114