AudioNodeInput.cpp 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  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. : AudioSummingJunction(node->context())
  35. , m_node(node)
  36. {
  37. // Set to mono by default.
  38. m_internalSummingBus = AudioBus::create(1, 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::RefTypeConnection); // 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. // Propagate disabled state to outputs.
  89. node()->disableOutputsIfNecessary();
  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. // Propagate enabled state to outputs.
  103. node()->enableOutputsIfNecessary();
  104. }
  105. void AudioNodeInput::didUpdate()
  106. {
  107. node()->checkNumberOfChannelsForInput(this);
  108. }
  109. void AudioNodeInput::updateInternalBus()
  110. {
  111. ASSERT(context()->isAudioThread() && context()->isGraphOwner());
  112. unsigned numberOfInputChannels = numberOfChannels();
  113. if (numberOfInputChannels == m_internalSummingBus->numberOfChannels())
  114. return;
  115. m_internalSummingBus = AudioBus::create(numberOfInputChannels, AudioNode::ProcessingSizeInFrames);
  116. }
  117. unsigned AudioNodeInput::numberOfChannels() const
  118. {
  119. AudioNode::ChannelCountMode mode = node()->internalChannelCountMode();
  120. if (mode == AudioNode::Explicit)
  121. return node()->channelCount();
  122. // Find the number of channels of the connection with the largest number of channels.
  123. unsigned maxChannels = 1; // one channel is the minimum allowed
  124. for (HashSet<AudioNodeOutput*>::iterator i = m_outputs.begin(); i != m_outputs.end(); ++i) {
  125. AudioNodeOutput* output = *i;
  126. // Use output()->numberOfChannels() instead of output->bus()->numberOfChannels(),
  127. // because the calling of AudioNodeOutput::bus() is not safe here.
  128. maxChannels = max(maxChannels, output->numberOfChannels());
  129. }
  130. if (mode == AudioNode::ClampedMax)
  131. maxChannels = min(maxChannels, static_cast<unsigned>(node()->channelCount()));
  132. return maxChannels;
  133. }
  134. AudioBus* AudioNodeInput::bus()
  135. {
  136. ASSERT(context()->isAudioThread());
  137. // Handle single connection specially to allow for in-place processing.
  138. if (numberOfRenderingConnections() == 1 && node()->internalChannelCountMode() == AudioNode::Max)
  139. return renderingOutput(0)->bus();
  140. // Multiple connections case or complex ChannelCountMode (or no connections).
  141. return internalSummingBus();
  142. }
  143. AudioBus* AudioNodeInput::internalSummingBus()
  144. {
  145. ASSERT(context()->isAudioThread());
  146. return m_internalSummingBus.get();
  147. }
  148. void AudioNodeInput::sumAllConnections(AudioBus* summingBus, size_t framesToProcess)
  149. {
  150. ASSERT(context()->isAudioThread());
  151. // We shouldn't be calling this method if there's only one connection, since it's less efficient.
  152. ASSERT(numberOfRenderingConnections() > 1 || node()->internalChannelCountMode() != AudioNode::Max);
  153. ASSERT(summingBus);
  154. if (!summingBus)
  155. return;
  156. summingBus->zero();
  157. AudioBus::ChannelInterpretation interpretation = node()->internalChannelInterpretation();
  158. for (unsigned i = 0; i < numberOfRenderingConnections(); ++i) {
  159. AudioNodeOutput* output = renderingOutput(i);
  160. ASSERT(output);
  161. // Render audio from this output.
  162. AudioBus* connectionBus = output->pull(0, framesToProcess);
  163. // Sum, with unity-gain.
  164. summingBus->sumFrom(*connectionBus, interpretation);
  165. }
  166. }
  167. AudioBus* AudioNodeInput::pull(AudioBus* inPlaceBus, size_t framesToProcess)
  168. {
  169. ASSERT(context()->isAudioThread());
  170. // Handle single connection case.
  171. if (numberOfRenderingConnections() == 1 && node()->internalChannelCountMode() == AudioNode::Max) {
  172. // The output will optimize processing using inPlaceBus if it's able.
  173. AudioNodeOutput* output = this->renderingOutput(0);
  174. return output->pull(inPlaceBus, framesToProcess);
  175. }
  176. AudioBus* internalSummingBus = this->internalSummingBus();
  177. if (!numberOfRenderingConnections()) {
  178. // At least, generate silence if we're not connected to anything.
  179. // FIXME: if we wanted to get fancy, we could propagate a 'silent hint' here to optimize the downstream graph processing.
  180. internalSummingBus->zero();
  181. return internalSummingBus;
  182. }
  183. // Handle multiple connections case.
  184. sumAllConnections(internalSummingBus, framesToProcess);
  185. return internalSummingBus;
  186. }
  187. } // namespace WebCore
  188. #endif // ENABLE(WEB_AUDIO)