/src/3rdparty/webkit/Source/WebCore/webaudio/AudioNodeInput.cpp

https://bitbucket.org/ultra_iter/qt-vtl · C++ · 270 lines · 167 code · 60 blank · 43 comment · 33 complexity · 60e846446929f2bce8313f77b85c81e3 MD5 · raw file

  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. #include "config.h"
  25. #if ENABLE(WEB_AUDIO)
  26. #include "AudioNodeInput.h"
  27. #include "AudioContext.h"
  28. #include "AudioNode.h"
  29. #include "AudioNodeOutput.h"
  30. #include <algorithm>
  31. using namespace std;
  32. namespace WebCore {
  33. AudioNodeInput::AudioNodeInput(AudioNode* node)
  34. : m_node(node)
  35. , m_renderingStateNeedUpdating(false)
  36. {
  37. m_monoSummingBus = adoptPtr(new AudioBus(1, AudioNode::ProcessingSizeInFrames));
  38. m_stereoSummingBus = adoptPtr(new AudioBus(2, AudioNode::ProcessingSizeInFrames));
  39. }
  40. void AudioNodeInput::connect(AudioNodeOutput* output)
  41. {
  42. ASSERT(context()->isGraphOwner());
  43. ASSERT(output && node());
  44. if (!output || !node())
  45. return;
  46. // Check if we're already connected to this output.
  47. if (m_outputs.contains(output))
  48. return;
  49. output->addInput(this);
  50. m_outputs.add(output);
  51. changedOutputs();
  52. // Sombody has just connected to us, so count it as a reference.
  53. node()->ref(AudioNode::RefTypeConnection);
  54. }
  55. void AudioNodeInput::disconnect(AudioNodeOutput* output)
  56. {
  57. ASSERT(context()->isGraphOwner());
  58. ASSERT(output && node());
  59. if (!output || !node())
  60. return;
  61. // First try to disconnect from "active" connections.
  62. if (m_outputs.contains(output)) {
  63. m_outputs.remove(output);
  64. changedOutputs();
  65. output->removeInput(this);
  66. node()->deref(AudioNode::RefTypeConnection); // Note: it's important to return immediately after all deref() calls since the node may be deleted.
  67. return;
  68. }
  69. // Otherwise, try to disconnect from disabled connections.
  70. if (m_disabledOutputs.contains(output)) {
  71. m_disabledOutputs.remove(output);
  72. output->removeInput(this);
  73. node()->deref(AudioNode::RefTypeDisabled); // Note: it's important to return immediately after all deref() calls since the node may be deleted.
  74. return;
  75. }
  76. ASSERT_NOT_REACHED();
  77. }
  78. void AudioNodeInput::disable(AudioNodeOutput* output)
  79. {
  80. ASSERT(context()->isGraphOwner());
  81. ASSERT(output && node());
  82. if (!output || !node())
  83. return;
  84. ASSERT(m_outputs.contains(output));
  85. m_disabledOutputs.add(output);
  86. m_outputs.remove(output);
  87. changedOutputs();
  88. node()->ref(AudioNode::RefTypeDisabled);
  89. node()->deref(AudioNode::RefTypeConnection); // Note: it's important to return immediately after all deref() calls since the node may be deleted.
  90. }
  91. void AudioNodeInput::enable(AudioNodeOutput* output)
  92. {
  93. ASSERT(context()->isGraphOwner());
  94. ASSERT(output && node());
  95. if (!output || !node())
  96. return;
  97. ASSERT(m_disabledOutputs.contains(output));
  98. // Move output from disabled list to active list.
  99. m_outputs.add(output);
  100. m_disabledOutputs.remove(output);
  101. changedOutputs();
  102. node()->ref(AudioNode::RefTypeConnection);
  103. node()->deref(AudioNode::RefTypeDisabled); // Note: it's important to return immediately after all deref() calls since the node may be deleted.
  104. }
  105. void AudioNodeInput::changedOutputs()
  106. {
  107. ASSERT(context()->isGraphOwner());
  108. if (!m_renderingStateNeedUpdating && !node()->isMarkedForDeletion()) {
  109. context()->markAudioNodeInputDirty(this);
  110. m_renderingStateNeedUpdating = true;
  111. }
  112. }
  113. void AudioNodeInput::updateRenderingState()
  114. {
  115. ASSERT(context()->isAudioThread() && context()->isGraphOwner());
  116. if (m_renderingStateNeedUpdating && !node()->isMarkedForDeletion()) {
  117. // Copy from m_outputs to m_renderingOutputs.
  118. m_renderingOutputs.resize(m_outputs.size());
  119. unsigned j = 0;
  120. for (HashSet<AudioNodeOutput*>::iterator i = m_outputs.begin(); i != m_outputs.end(); ++i, ++j) {
  121. AudioNodeOutput* output = *i;
  122. m_renderingOutputs[j] = output;
  123. output->updateRenderingState();
  124. }
  125. node()->checkNumberOfChannelsForInput(this);
  126. m_renderingStateNeedUpdating = false;
  127. }
  128. }
  129. unsigned AudioNodeInput::numberOfChannels() const
  130. {
  131. // Find the number of channels of the connection with the largest number of channels.
  132. unsigned maxChannels = 1; // one channel is the minimum allowed
  133. for (HashSet<AudioNodeOutput*>::iterator i = m_outputs.begin(); i != m_outputs.end(); ++i) {
  134. AudioNodeOutput* output = *i;
  135. maxChannels = max(maxChannels, output->bus()->numberOfChannels());
  136. }
  137. return maxChannels;
  138. }
  139. unsigned AudioNodeInput::numberOfRenderingChannels()
  140. {
  141. ASSERT(context()->isAudioThread());
  142. // Find the number of channels of the rendering connection with the largest number of channels.
  143. unsigned maxChannels = 1; // one channel is the minimum allowed
  144. for (unsigned i = 0; i < numberOfRenderingConnections(); ++i)
  145. maxChannels = max(maxChannels, renderingOutput(i)->bus()->numberOfChannels());
  146. return maxChannels;
  147. }
  148. AudioBus* AudioNodeInput::bus()
  149. {
  150. ASSERT(context()->isAudioThread());
  151. // Handle single connection specially to allow for in-place processing.
  152. if (numberOfRenderingConnections() == 1)
  153. return renderingOutput(0)->bus();
  154. // Multiple connections case (or no connections).
  155. return internalSummingBus();
  156. }
  157. AudioBus* AudioNodeInput::internalSummingBus()
  158. {
  159. ASSERT(context()->isAudioThread());
  160. // We must pick a summing bus which is the right size to handle the largest connection.
  161. switch (numberOfRenderingChannels()) {
  162. case 1:
  163. return m_monoSummingBus.get();
  164. case 2:
  165. return m_stereoSummingBus.get();
  166. // FIXME: could implement more than just mono and stereo mixing in the future
  167. }
  168. ASSERT_NOT_REACHED();
  169. return 0;
  170. }
  171. void AudioNodeInput::sumAllConnections(AudioBus* summingBus, size_t framesToProcess)
  172. {
  173. ASSERT(context()->isAudioThread());
  174. // We shouldn't be calling this method if there's only one connection, since it's less efficient.
  175. ASSERT(numberOfRenderingConnections() > 1);
  176. ASSERT(summingBus);
  177. if (!summingBus)
  178. return;
  179. summingBus->zero();
  180. for (unsigned i = 0; i < numberOfRenderingConnections(); ++i) {
  181. AudioNodeOutput* output = renderingOutput(i);
  182. ASSERT(output);
  183. // Render audio from this output.
  184. AudioBus* connectionBus = output->pull(0, framesToProcess);
  185. // Sum, with unity-gain.
  186. summingBus->sumFrom(*connectionBus);
  187. }
  188. }
  189. AudioBus* AudioNodeInput::pull(AudioBus* inPlaceBus, size_t framesToProcess)
  190. {
  191. ASSERT(context()->isAudioThread());
  192. // Handle single connection case.
  193. if (numberOfRenderingConnections() == 1) {
  194. // The output will optimize processing using inPlaceBus if it's able.
  195. AudioNodeOutput* output = this->renderingOutput(0);
  196. return output->pull(inPlaceBus, framesToProcess);
  197. }
  198. AudioBus* internalSummingBus = this->internalSummingBus();
  199. if (!numberOfRenderingConnections()) {
  200. // At least, generate silence if we're not connected to anything.
  201. // FIXME: if we wanted to get fancy, we could propagate a 'silent hint' here to optimize the downstream graph processing.
  202. internalSummingBus->zero();
  203. return internalSummingBus;
  204. }
  205. // Handle multiple connections case.
  206. sumAllConnections(internalSummingBus, framesToProcess);
  207. return internalSummingBus;
  208. }
  209. } // namespace WebCore
  210. #endif // ENABLE(WEB_AUDIO)