ChannelMergerNode.cpp 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  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. *
  8. * 1. Redistributions of source code must retain the above copyright
  9. * notice, this list of conditions and the following disclaimer.
  10. * 2. Redistributions in binary form must reproduce the above copyright
  11. * notice, this list of conditions and the following disclaimer in the
  12. * documentation and/or other materials provided with the distribution.
  13. * 3. Neither the name of Apple Computer, Inc. ("Apple") nor the names of
  14. * its contributors may be used to endorse or promote products derived
  15. * from this software without specific prior written permission.
  16. *
  17. * THIS SOFTWARE IS PROVIDED BY APPLE AND ITS CONTRIBUTORS "AS IS" AND ANY
  18. * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  19. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  20. * DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR ANY
  21. * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  22. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  23. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  24. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  25. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
  26. * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  27. */
  28. #include "config.h"
  29. #if ENABLE(WEB_AUDIO)
  30. #include "ChannelMergerNode.h"
  31. #include "AudioContext.h"
  32. #include "AudioNodeInput.h"
  33. #include "AudioNodeOutput.h"
  34. const unsigned DefaultNumberOfOutputChannels = 1;
  35. namespace WebCore {
  36. PassRefPtr<ChannelMergerNode> ChannelMergerNode::create(AudioContext* context, float sampleRate, unsigned numberOfInputs)
  37. {
  38. if (!numberOfInputs || numberOfInputs > AudioContext::maxNumberOfChannels())
  39. return 0;
  40. return adoptRef(new ChannelMergerNode(context, sampleRate, numberOfInputs));
  41. }
  42. ChannelMergerNode::ChannelMergerNode(AudioContext* context, float sampleRate, unsigned numberOfInputs)
  43. : AudioNode(context, sampleRate)
  44. , m_desiredNumberOfOutputChannels(DefaultNumberOfOutputChannels)
  45. {
  46. // Create the requested number of inputs.
  47. for (unsigned i = 0; i < numberOfInputs; ++i)
  48. addInput(adoptPtr(new AudioNodeInput(this)));
  49. addOutput(adoptPtr(new AudioNodeOutput(this, 1)));
  50. setNodeType(NodeTypeChannelMerger);
  51. initialize();
  52. }
  53. void ChannelMergerNode::process(size_t framesToProcess)
  54. {
  55. AudioNodeOutput* output = this->output(0);
  56. ASSERT(output);
  57. ASSERT_UNUSED(framesToProcess, framesToProcess == output->bus()->length());
  58. // Output bus not updated yet, so just output silence.
  59. if (m_desiredNumberOfOutputChannels != output->numberOfChannels()) {
  60. output->bus()->zero();
  61. return;
  62. }
  63. // Merge all the channels from all the inputs into one output.
  64. unsigned outputChannelIndex = 0;
  65. for (unsigned i = 0; i < numberOfInputs(); ++i) {
  66. AudioNodeInput* input = this->input(i);
  67. if (input->isConnected()) {
  68. unsigned numberOfInputChannels = input->bus()->numberOfChannels();
  69. // Merge channels from this particular input.
  70. for (unsigned j = 0; j < numberOfInputChannels; ++j) {
  71. AudioChannel* inputChannel = input->bus()->channel(j);
  72. AudioChannel* outputChannel = output->bus()->channel(outputChannelIndex);
  73. outputChannel->copyFrom(inputChannel);
  74. ++outputChannelIndex;
  75. }
  76. }
  77. }
  78. ASSERT(outputChannelIndex == output->numberOfChannels());
  79. }
  80. void ChannelMergerNode::reset()
  81. {
  82. }
  83. // Any time a connection or disconnection happens on any of our inputs, we potentially need to change the
  84. // number of channels of our output.
  85. void ChannelMergerNode::checkNumberOfChannelsForInput(AudioNodeInput* input)
  86. {
  87. ASSERT(context()->isAudioThread() && context()->isGraphOwner());
  88. // Count how many channels we have all together from all of the inputs.
  89. unsigned numberOfOutputChannels = 0;
  90. for (unsigned i = 0; i < numberOfInputs(); ++i) {
  91. AudioNodeInput* input = this->input(i);
  92. if (input->isConnected())
  93. numberOfOutputChannels += input->numberOfChannels();
  94. }
  95. // Set the correct number of channels on the output
  96. AudioNodeOutput* output = this->output(0);
  97. ASSERT(output);
  98. output->setNumberOfChannels(numberOfOutputChannels);
  99. // There can in rare cases be a slight delay before the output bus is updated to the new number of
  100. // channels because of tryLocks() in the context's updating system. So record the new number of
  101. // output channels here.
  102. m_desiredNumberOfOutputChannels = numberOfOutputChannels;
  103. AudioNode::checkNumberOfChannelsForInput(input);
  104. }
  105. } // namespace WebCore
  106. #endif // ENABLE(WEB_AUDIO)