Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(893)

Side by Side Diff: Source/modules/webaudio/StereoPannerNode.cpp

Issue 691143007: Implement StereoPannerNode for robust stereo panning (Closed) Base URL: https://chromium.googlesource.com/chromium/blink.git@master
Patch Set: Created 6 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
1 // Copyright 2014 The Chromium Authors. All rights reserved.
2 //
3 // Redistribution and use in source and binary forms, with or without
4 // modification, are permitted provided that the following conditions are
5 // met:
6 //
7 // * Redistributions of source code must retain the above copyright
8 // notice, this list of conditions and the following disclaimer.
9 // * Redistributions in binary form must reproduce the above
10 // copyright notice, this list of conditions and the following disclaimer
11 // in the documentation and/or other materials provided with the
12 // distribution.
13 // * Neither the name of Google Inc. nor the names of its
14 // contributors may be used to endorse or promote products derived from
15 // this software without specific prior written permission.
16 //
17 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
20 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
21 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
22 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
23 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
27 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28
29 #include "config.h"
30
31 #if ENABLE(WEB_AUDIO)
32
33 #include "modules/webaudio/StereoPannerNode.h"
34
35 #include "bindings/core/v8/ExceptionMessages.h"
36 #include "bindings/core/v8/ExceptionState.h"
37 #include "core/dom/ExecutionContext.h"
38 #include "modules/webaudio/AudioContext.h"
39 #include "modules/webaudio/AudioNodeInput.h"
40 #include "modules/webaudio/AudioNodeOutput.h"
41 #include "platform/audio/StereoPanner.h"
42 #include "wtf/MathExtras.h"
43
44 namespace blink {
45
46 StereoPannerNode::StereoPannerNode(AudioContext* context, float sampleRate)
47 : AudioNode(context, sampleRate)
48 , m_sampleAccuratePanValues(AudioNode::ProcessingSizeInFrames)
49 {
50 m_pan = AudioParam::create(context, 0);
51
52 addInput();
53 addOutput(AudioNodeOutput::create(this, 2));
54
55 // This node-specific default mixing rules declare that StereoPannerNode
Raymond Toy 2014/11/12 18:06:52 Typo: "This" -> "The"
hongchan 2014/11/12 22:01:42 Done.
56 // can handle mono to stereo and stereo to stereo conversion.
57 m_channelCount = 2;
58 m_channelCountMode = ClampedMax;
59 m_channelInterpretation = AudioBus::Speakers;
60
61 setNodeType(NodeTypeStereoPanner);
62
63 initialize();
64 }
65
66 StereoPannerNode::~StereoPannerNode()
67 {
68 ASSERT(!isInitialized());
69 }
70
71 void StereoPannerNode::dispose()
72 {
73 uninitialize();
74 AudioNode::dispose();
75 }
76
77 void StereoPannerNode::process(size_t framesToProcess)
78 {
79 AudioBus* outputBus = output(0)->bus();
80
81 if (!isInitialized() || !input(0)->isConnected() || !m_stereoPanner.get()) {
82 outputBus->zero();
83 return;
84 }
85
86 AudioBus* inputBus = input(0)->bus();
87 if (!inputBus) {
88 outputBus->zero();
89 return;
90 }
91
92 if (pan()->hasSampleAccurateValues()) {
93 // Apply sample-accurate panning specified by AudioParam automation.
94 ASSERT(framesToProcess <= m_sampleAccuratePanValues.size());
95 if (framesToProcess <= m_sampleAccuratePanValues.size()) {
96 float* panValues = m_sampleAccuratePanValues.data();
97 pan()->calculateSampleAccurateValues(panValues, framesToProcess);
98 m_stereoPanner->panWithSampleAccurateValues(inputBus, outputBus, pan Values, framesToProcess);
99 }
100 } else {
101 m_stereoPanner->panToTargetValue(inputBus, outputBus, pan()->value(), fr amesToProcess);
102 }
103 }
104
105 void StereoPannerNode::initialize()
106 {
107 if (isInitialized())
108 return;
109
110 m_stereoPanner = Spatializer::create(Spatializer::PanningModelEqualPower, sa mpleRate());
111
112 AudioNode::initialize();
113 }
114
115 void StereoPannerNode::uninitialize()
116 {
117 if (!isInitialized())
118 return;
119
120 m_stereoPanner.clear();
121
122 AudioNode::uninitialize();
123 }
124
125 void StereoPannerNode::setChannelCount(unsigned long channelCount, ExceptionStat e& exceptionState)
126 {
127 ASSERT(isMainThread());
128 AudioContext::AutoLocker locker(context());
129
130 // A PannerNode only supports 1 or 2 channels
131 if (channelCount > 0 && channelCount <= 2) {
132 if (m_channelCount != channelCount) {
133 m_channelCount = channelCount;
134 if (m_channelCountMode != Max)
135 updateChannelsForInputs();
136 }
137 } else {
138 exceptionState.throwDOMException(
139 NotSupportedError,
140 ExceptionMessages::indexOutsideRange<unsigned long>(
141 "channelCount",
142 channelCount,
143 1,
144 ExceptionMessages::InclusiveBound,
145 2,
146 ExceptionMessages::InclusiveBound));
147 }
148 }
149
150 void StereoPannerNode::setChannelCountMode(const String& mode, ExceptionState& e xceptionState)
151 {
152 ASSERT(isMainThread());
153 AudioContext::AutoLocker locker(context());
154
155 ChannelCountMode oldMode = m_channelCountMode;
156
157 if (mode == "clamped-max") {
158 m_newChannelCountMode = ClampedMax;
159 } else if (mode == "explicit") {
160 m_newChannelCountMode = Explicit;
161 } else if (mode == "max") {
162 // This is not supported for a StereoPannerNode, which can only handle
163 // 1 or 2 channels.
164 exceptionState.throwDOMException(
165 NotSupportedError,
166 ExceptionMessages::failedToSet(
167 "channelCountMode",
168 "StereoPannerNode",
169 "'max' is not allowed"));
170 m_newChannelCountMode = oldMode;
171 } else {
172 // Do nothing for other invalid values.
173 m_newChannelCountMode = oldMode;
174 }
175
176 if (m_newChannelCountMode != oldMode)
177 context()->addChangedChannelCountMode(this);
178 }
179
180 void StereoPannerNode::trace(Visitor* visitor)
181 {
182 visitor->trace(m_stereoPanner);
183 visitor->trace(m_pan);
184 AudioNode::trace(visitor);
185 }
186
187 } // namespace blink
188
189 #endif // ENABLE(WEB_AUDIO)
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698