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

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

Issue 386343004: WebAudio: Use references instead of pointers in AudioNodeInput and AudioNodeOutput. (Closed) Base URL: svn://svn.chromium.org/blink/trunk
Patch Set: Created 6 years, 5 months 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 | Annotate | Revision Log
« no previous file with comments | « Source/modules/webaudio/AudioNodeInput.h ('k') | Source/modules/webaudio/AudioNodeOutput.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 /* 1 /*
2 * Copyright (C) 2010, Google Inc. All rights reserved. 2 * Copyright (C) 2010, Google Inc. All rights reserved.
3 * 3 *
4 * Redistribution and use in source and binary forms, with or without 4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions 5 * modification, are permitted provided that the following conditions
6 * are met: 6 * are met:
7 * 1. Redistributions of source code must retain the above copyright 7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer. 8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright 9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the 10 * notice, this list of conditions and the following disclaimer in the
(...skipping 16 matching lines...) Expand all
27 #if ENABLE(WEB_AUDIO) 27 #if ENABLE(WEB_AUDIO)
28 28
29 #include "modules/webaudio/AudioNodeInput.h" 29 #include "modules/webaudio/AudioNodeInput.h"
30 30
31 #include "modules/webaudio/AudioContext.h" 31 #include "modules/webaudio/AudioContext.h"
32 #include "modules/webaudio/AudioNodeOutput.h" 32 #include "modules/webaudio/AudioNodeOutput.h"
33 #include <algorithm> 33 #include <algorithm>
34 34
35 namespace WebCore { 35 namespace WebCore {
36 36
37 inline AudioNodeInput::AudioNodeInput(AudioNode* node) 37 inline AudioNodeInput::AudioNodeInput(AudioNode& node)
38 : AudioSummingJunction(node->context()) 38 : AudioSummingJunction(node.context())
39 , m_node(node) 39 , m_node(node)
40 { 40 {
41 // Set to mono by default. 41 // Set to mono by default.
42 m_internalSummingBus = AudioBus::create(1, AudioNode::ProcessingSizeInFrames ); 42 m_internalSummingBus = AudioBus::create(1, AudioNode::ProcessingSizeInFrames );
43 } 43 }
44 44
45 PassOwnPtr<AudioNodeInput> AudioNodeInput::create(AudioNode* node) 45 PassOwnPtr<AudioNodeInput> AudioNodeInput::create(AudioNode& node)
46 { 46 {
47 return adoptPtr(new AudioNodeInput(node)); 47 return adoptPtr(new AudioNodeInput(node));
48 } 48 }
49 49
50 void AudioNodeInput::connect(AudioNodeOutput* output) 50 void AudioNodeInput::connect(AudioNodeOutput& output)
51 { 51 {
52 ASSERT(context()->isGraphOwner()); 52 ASSERT(context()->isGraphOwner());
53 53
54 ASSERT(output && node()); 54 // Check if we're already connected to this output.
55 if (!output || !node()) 55 if (m_outputs.contains(&output))
56 return; 56 return;
57 57
58 // Check if we're already connected to this output. 58 output.addInput(*this);
59 if (m_outputs.contains(output)) 59 m_outputs.add(&output);
60 return;
61
62 output->addInput(this);
63 m_outputs.add(output);
64 changedOutputs(); 60 changedOutputs();
65 } 61 }
66 62
67 void AudioNodeInput::disconnect(AudioNodeOutput* output) 63 void AudioNodeInput::disconnect(AudioNodeOutput& output)
68 { 64 {
69 ASSERT(context()->isGraphOwner()); 65 ASSERT(context()->isGraphOwner());
70 66
71 ASSERT(output && node());
72 if (!output || !node())
73 return;
74
75 // First try to disconnect from "active" connections. 67 // First try to disconnect from "active" connections.
76 if (m_outputs.contains(output)) { 68 if (m_outputs.contains(&output)) {
77 m_outputs.remove(output); 69 m_outputs.remove(&output);
78 changedOutputs(); 70 changedOutputs();
79 output->removeInput(this); 71 output.removeInput(*this);
80 // Note: it's important to return immediately after removeInput() calls 72 // Note: it's important to return immediately after removeInput() calls
81 // since the node may be deleted. 73 // since the node may be deleted.
82 return; 74 return;
83 } 75 }
84 76
85 // Otherwise, try to disconnect from disabled connections. 77 // Otherwise, try to disconnect from disabled connections.
86 if (m_disabledOutputs.contains(output)) { 78 if (m_disabledOutputs.contains(&output)) {
87 m_disabledOutputs.remove(output); 79 m_disabledOutputs.remove(&output);
88 output->removeInput(this); 80 output.removeInput(*this);
89 // Note: it's important to return immediately after all removeInput() ca lls 81 // Note: it's important to return immediately after all removeInput() ca lls
90 // since the node may be deleted. 82 // since the node may be deleted.
91 return; 83 return;
92 } 84 }
93 85
94 ASSERT_NOT_REACHED(); 86 ASSERT_NOT_REACHED();
95 } 87 }
96 88
97 void AudioNodeInput::disable(AudioNodeOutput* output) 89 void AudioNodeInput::disable(AudioNodeOutput& output)
98 { 90 {
99 ASSERT(context()->isGraphOwner()); 91 ASSERT(context()->isGraphOwner());
92 ASSERT(m_outputs.contains(&output));
100 93
101 ASSERT(output && node()); 94 m_disabledOutputs.add(&output);
102 if (!output || !node()) 95 m_outputs.remove(&output);
103 return;
104
105 ASSERT(m_outputs.contains(output));
106
107 m_disabledOutputs.add(output);
108 m_outputs.remove(output);
109 changedOutputs(); 96 changedOutputs();
110 97
111 // Propagate disabled state to outputs. 98 // Propagate disabled state to outputs.
112 node()->disableOutputsIfNecessary(); 99 node().disableOutputsIfNecessary();
113 } 100 }
114 101
115 void AudioNodeInput::enable(AudioNodeOutput* output) 102 void AudioNodeInput::enable(AudioNodeOutput& output)
116 { 103 {
117 ASSERT(context()->isGraphOwner()); 104 ASSERT(context()->isGraphOwner());
118 105 ASSERT(m_disabledOutputs.contains(&output));
119 ASSERT(output && node());
120 if (!output || !node())
121 return;
122
123 ASSERT(m_disabledOutputs.contains(output));
124 106
125 // Move output from disabled list to active list. 107 // Move output from disabled list to active list.
126 m_outputs.add(output); 108 m_outputs.add(&output);
127 m_disabledOutputs.remove(output); 109 m_disabledOutputs.remove(&output);
128 changedOutputs(); 110 changedOutputs();
129 111
130 // Propagate enabled state to outputs. 112 // Propagate enabled state to outputs.
131 node()->enableOutputsIfNecessary(); 113 node().enableOutputsIfNecessary();
132 } 114 }
133 115
134 void AudioNodeInput::didUpdate() 116 void AudioNodeInput::didUpdate()
135 { 117 {
136 node()->checkNumberOfChannelsForInput(this); 118 node().checkNumberOfChannelsForInput(this);
137 } 119 }
138 120
139 void AudioNodeInput::updateInternalBus() 121 void AudioNodeInput::updateInternalBus()
140 { 122 {
141 ASSERT(context()->isAudioThread() && context()->isGraphOwner()); 123 ASSERT(context()->isAudioThread() && context()->isGraphOwner());
142 124
143 unsigned numberOfInputChannels = numberOfChannels(); 125 unsigned numberOfInputChannels = numberOfChannels();
144 126
145 if (numberOfInputChannels == m_internalSummingBus->numberOfChannels()) 127 if (numberOfInputChannels == m_internalSummingBus->numberOfChannels())
146 return; 128 return;
147 129
148 m_internalSummingBus = AudioBus::create(numberOfInputChannels, AudioNode::Pr ocessingSizeInFrames); 130 m_internalSummingBus = AudioBus::create(numberOfInputChannels, AudioNode::Pr ocessingSizeInFrames);
149 } 131 }
150 132
151 unsigned AudioNodeInput::numberOfChannels() const 133 unsigned AudioNodeInput::numberOfChannels() const
152 { 134 {
153 AudioNode::ChannelCountMode mode = node()->internalChannelCountMode(); 135 AudioNode::ChannelCountMode mode = node().internalChannelCountMode();
154 if (mode == AudioNode::Explicit) 136 if (mode == AudioNode::Explicit)
155 return node()->channelCount(); 137 return node().channelCount();
156 138
157 // Find the number of channels of the connection with the largest number of channels. 139 // Find the number of channels of the connection with the largest number of channels.
158 unsigned maxChannels = 1; // one channel is the minimum allowed 140 unsigned maxChannels = 1; // one channel is the minimum allowed
159 141
160 for (HashSet<AudioNodeOutput*>::iterator i = m_outputs.begin(); i != m_outpu ts.end(); ++i) { 142 for (HashSet<AudioNodeOutput*>::iterator i = m_outputs.begin(); i != m_outpu ts.end(); ++i) {
161 AudioNodeOutput* output = *i; 143 AudioNodeOutput* output = *i;
162 // Use output()->numberOfChannels() instead of output->bus()->numberOfCh annels(), 144 // Use output()->numberOfChannels() instead of output->bus()->numberOfCh annels(),
163 // because the calling of AudioNodeOutput::bus() is not safe here. 145 // because the calling of AudioNodeOutput::bus() is not safe here.
164 maxChannels = std::max(maxChannels, output->numberOfChannels()); 146 maxChannels = std::max(maxChannels, output->numberOfChannels());
165 } 147 }
166 148
167 if (mode == AudioNode::ClampedMax) 149 if (mode == AudioNode::ClampedMax)
168 maxChannels = std::min(maxChannels, static_cast<unsigned>(node()->channe lCount())); 150 maxChannels = std::min(maxChannels, static_cast<unsigned>(node().channel Count()));
169 151
170 return maxChannels; 152 return maxChannels;
171 } 153 }
172 154
173 AudioBus* AudioNodeInput::bus() 155 AudioBus* AudioNodeInput::bus()
174 { 156 {
175 ASSERT(context()->isAudioThread()); 157 ASSERT(context()->isAudioThread());
176 158
177 // Handle single connection specially to allow for in-place processing. 159 // Handle single connection specially to allow for in-place processing.
178 if (numberOfRenderingConnections() == 1 && node()->internalChannelCountMode( ) == AudioNode::Max) 160 if (numberOfRenderingConnections() == 1 && node().internalChannelCountMode() == AudioNode::Max)
179 return renderingOutput(0)->bus(); 161 return renderingOutput(0)->bus();
180 162
181 // Multiple connections case or complex ChannelCountMode (or no connections) . 163 // Multiple connections case or complex ChannelCountMode (or no connections) .
182 return internalSummingBus(); 164 return internalSummingBus();
183 } 165 }
184 166
185 AudioBus* AudioNodeInput::internalSummingBus() 167 AudioBus* AudioNodeInput::internalSummingBus()
186 { 168 {
187 ASSERT(context()->isAudioThread()); 169 ASSERT(context()->isAudioThread());
188 170
189 return m_internalSummingBus.get(); 171 return m_internalSummingBus.get();
190 } 172 }
191 173
192 void AudioNodeInput::sumAllConnections(AudioBus* summingBus, size_t framesToProc ess) 174 void AudioNodeInput::sumAllConnections(AudioBus* summingBus, size_t framesToProc ess)
193 { 175 {
194 ASSERT(context()->isAudioThread()); 176 ASSERT(context()->isAudioThread());
195 177
196 // We shouldn't be calling this method if there's only one connection, since it's less efficient. 178 // We shouldn't be calling this method if there's only one connection, since it's less efficient.
197 ASSERT(numberOfRenderingConnections() > 1 || node()->internalChannelCountMod e() != AudioNode::Max); 179 ASSERT(numberOfRenderingConnections() > 1 || node().internalChannelCountMode () != AudioNode::Max);
198 180
199 ASSERT(summingBus); 181 ASSERT(summingBus);
200 if (!summingBus) 182 if (!summingBus)
201 return; 183 return;
202 184
203 summingBus->zero(); 185 summingBus->zero();
204 186
205 AudioBus::ChannelInterpretation interpretation = node()->internalChannelInte rpretation(); 187 AudioBus::ChannelInterpretation interpretation = node().internalChannelInter pretation();
206 188
207 for (unsigned i = 0; i < numberOfRenderingConnections(); ++i) { 189 for (unsigned i = 0; i < numberOfRenderingConnections(); ++i) {
208 AudioNodeOutput* output = renderingOutput(i); 190 AudioNodeOutput* output = renderingOutput(i);
209 ASSERT(output); 191 ASSERT(output);
210 192
211 // Render audio from this output. 193 // Render audio from this output.
212 AudioBus* connectionBus = output->pull(0, framesToProcess); 194 AudioBus* connectionBus = output->pull(0, framesToProcess);
213 195
214 // Sum, with unity-gain. 196 // Sum, with unity-gain.
215 summingBus->sumFrom(*connectionBus, interpretation); 197 summingBus->sumFrom(*connectionBus, interpretation);
216 } 198 }
217 } 199 }
218 200
219 AudioBus* AudioNodeInput::pull(AudioBus* inPlaceBus, size_t framesToProcess) 201 AudioBus* AudioNodeInput::pull(AudioBus* inPlaceBus, size_t framesToProcess)
220 { 202 {
221 ASSERT(context()->isAudioThread()); 203 ASSERT(context()->isAudioThread());
222 204
223 // Handle single connection case. 205 // Handle single connection case.
224 if (numberOfRenderingConnections() == 1 && node()->internalChannelCountMode( ) == AudioNode::Max) { 206 if (numberOfRenderingConnections() == 1 && node().internalChannelCountMode() == AudioNode::Max) {
225 // The output will optimize processing using inPlaceBus if it's able. 207 // The output will optimize processing using inPlaceBus if it's able.
226 AudioNodeOutput* output = this->renderingOutput(0); 208 AudioNodeOutput* output = this->renderingOutput(0);
227 return output->pull(inPlaceBus, framesToProcess); 209 return output->pull(inPlaceBus, framesToProcess);
228 } 210 }
229 211
230 AudioBus* internalSummingBus = this->internalSummingBus(); 212 AudioBus* internalSummingBus = this->internalSummingBus();
231 213
232 if (!numberOfRenderingConnections()) { 214 if (!numberOfRenderingConnections()) {
233 // At least, generate silence if we're not connected to anything. 215 // At least, generate silence if we're not connected to anything.
234 // FIXME: if we wanted to get fancy, we could propagate a 'silent hint' here to optimize the downstream graph processing. 216 // FIXME: if we wanted to get fancy, we could propagate a 'silent hint' here to optimize the downstream graph processing.
235 internalSummingBus->zero(); 217 internalSummingBus->zero();
236 return internalSummingBus; 218 return internalSummingBus;
237 } 219 }
238 220
239 // Handle multiple connections case. 221 // Handle multiple connections case.
240 sumAllConnections(internalSummingBus, framesToProcess); 222 sumAllConnections(internalSummingBus, framesToProcess);
241 223
242 return internalSummingBus; 224 return internalSummingBus;
243 } 225 }
244 226
245 } // namespace WebCore 227 } // namespace WebCore
246 228
247 #endif // ENABLE(WEB_AUDIO) 229 #endif // ENABLE(WEB_AUDIO)
OLDNEW
« no previous file with comments | « Source/modules/webaudio/AudioNodeInput.h ('k') | Source/modules/webaudio/AudioNodeOutput.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698