AudioBasicProcessorNode.cpp 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  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 "AudioBasicProcessorNode.h"
  27. #include "AudioBus.h"
  28. #include "AudioContext.h"
  29. #include "AudioNodeInput.h"
  30. #include "AudioNodeOutput.h"
  31. #include "AudioProcessor.h"
  32. namespace WebCore {
  33. AudioBasicProcessorNode::AudioBasicProcessorNode(AudioContext* context, float sampleRate)
  34. : AudioNode(context, sampleRate)
  35. {
  36. addInput(adoptPtr(new AudioNodeInput(this)));
  37. addOutput(adoptPtr(new AudioNodeOutput(this, 1)));
  38. // The subclass must create m_processor.
  39. }
  40. void AudioBasicProcessorNode::initialize()
  41. {
  42. if (isInitialized())
  43. return;
  44. ASSERT(processor());
  45. processor()->initialize();
  46. AudioNode::initialize();
  47. }
  48. void AudioBasicProcessorNode::uninitialize()
  49. {
  50. if (!isInitialized())
  51. return;
  52. ASSERT(processor());
  53. processor()->uninitialize();
  54. AudioNode::uninitialize();
  55. }
  56. void AudioBasicProcessorNode::process(size_t framesToProcess)
  57. {
  58. AudioBus* destinationBus = output(0)->bus();
  59. if (!isInitialized() || !processor() || processor()->numberOfChannels() != numberOfChannels())
  60. destinationBus->zero();
  61. else {
  62. AudioBus* sourceBus = input(0)->bus();
  63. // FIXME: if we take "tail time" into account, then we can avoid calling processor()->process() once the tail dies down.
  64. if (!input(0)->isConnected())
  65. sourceBus->zero();
  66. processor()->process(sourceBus, destinationBus, framesToProcess);
  67. }
  68. }
  69. // Nice optimization in the very common case allowing for "in-place" processing
  70. void AudioBasicProcessorNode::pullInputs(size_t framesToProcess)
  71. {
  72. // Render input stream - suggest to the input to render directly into output bus for in-place processing in process() if possible.
  73. input(0)->pull(output(0)->bus(), framesToProcess);
  74. }
  75. void AudioBasicProcessorNode::reset()
  76. {
  77. if (processor())
  78. processor()->reset();
  79. }
  80. // As soon as we know the channel count of our input, we can lazily initialize.
  81. // Sometimes this may be called more than once with different channel counts, in which case we must safely
  82. // uninitialize and then re-initialize with the new channel count.
  83. void AudioBasicProcessorNode::checkNumberOfChannelsForInput(AudioNodeInput* input)
  84. {
  85. ASSERT(context()->isAudioThread() && context()->isGraphOwner());
  86. ASSERT(input == this->input(0));
  87. if (input != this->input(0))
  88. return;
  89. ASSERT(processor());
  90. if (!processor())
  91. return;
  92. unsigned numberOfChannels = input->numberOfChannels();
  93. if (isInitialized() && numberOfChannels != output(0)->numberOfChannels()) {
  94. // We're already initialized but the channel count has changed.
  95. uninitialize();
  96. }
  97. if (!isInitialized()) {
  98. // This will propagate the channel count to any nodes connected further down the chain...
  99. output(0)->setNumberOfChannels(numberOfChannels);
  100. // Re-initialize the processor with the new channel count.
  101. processor()->setNumberOfChannels(numberOfChannels);
  102. initialize();
  103. }
  104. AudioNode::checkNumberOfChannelsForInput(input);
  105. }
  106. unsigned AudioBasicProcessorNode::numberOfChannels()
  107. {
  108. return output(0)->numberOfChannels();
  109. }
  110. double AudioBasicProcessorNode::tailTime() const
  111. {
  112. return m_processor->tailTime();
  113. }
  114. double AudioBasicProcessorNode::latencyTime() const
  115. {
  116. return m_processor->latencyTime();
  117. }
  118. } // namespace WebCore
  119. #endif // ENABLE(WEB_AUDIO)