AudioNodeInput.h 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  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. #ifndef AudioNodeInput_h
  25. #define AudioNodeInput_h
  26. #include "AudioBus.h"
  27. #include "AudioNode.h"
  28. #include "AudioSummingJunction.h"
  29. #include <wtf/HashSet.h>
  30. #include <wtf/Vector.h>
  31. namespace WebCore {
  32. class AudioNode;
  33. class AudioNodeOutput;
  34. // An AudioNodeInput represents an input to an AudioNode and can be connected from one or more AudioNodeOutputs.
  35. // In the case of multiple connections, the input will act as a unity-gain summing junction, mixing all the outputs.
  36. // The number of channels of the input's bus is the maximum of the number of channels of all its connections.
  37. class AudioNodeInput : public AudioSummingJunction {
  38. public:
  39. explicit AudioNodeInput(AudioNode*);
  40. // AudioSummingJunction
  41. virtual bool canUpdateState() OVERRIDE { return !node()->isMarkedForDeletion(); }
  42. virtual void didUpdate() OVERRIDE;
  43. // Can be called from any thread.
  44. AudioNode* node() const { return m_node; }
  45. // Must be called with the context's graph lock.
  46. void connect(AudioNodeOutput*);
  47. void disconnect(AudioNodeOutput*);
  48. // disable() will take the output out of the active connections list and set aside in a disabled list.
  49. // enable() will put the output back into the active connections list.
  50. // Must be called with the context's graph lock.
  51. void enable(AudioNodeOutput*);
  52. void disable(AudioNodeOutput*);
  53. // pull() processes all of the AudioNodes connected to us.
  54. // In the case of multiple connections it sums the result into an internal summing bus.
  55. // In the single connection case, it allows in-place processing where possible using inPlaceBus.
  56. // It returns the bus which it rendered into, returning inPlaceBus if in-place processing was performed.
  57. // Called from context's audio thread.
  58. AudioBus* pull(AudioBus* inPlaceBus, size_t framesToProcess);
  59. // bus() contains the rendered audio after pull() has been called for each time quantum.
  60. // Called from context's audio thread.
  61. AudioBus* bus();
  62. // updateInternalBus() updates m_internalSummingBus appropriately for the number of channels.
  63. // This must be called when we own the context's graph lock in the audio thread at the very start or end of the render quantum.
  64. void updateInternalBus();
  65. // The number of channels of the connection with the largest number of channels.
  66. unsigned numberOfChannels() const;
  67. private:
  68. AudioNode* m_node;
  69. // m_disabledOutputs contains the AudioNodeOutputs which are disabled (will not be processed) by the audio graph rendering.
  70. // But, from JavaScript's perspective, these outputs are still connected to us.
  71. // Generally, these represent disabled connections from "notes" which have finished playing but are not yet garbage collected.
  72. HashSet<AudioNodeOutput*> m_disabledOutputs;
  73. // Called from context's audio thread.
  74. AudioBus* internalSummingBus();
  75. void sumAllConnections(AudioBus* summingBus, size_t framesToProcess);
  76. RefPtr<AudioBus> m_internalSummingBus;
  77. };
  78. } // namespace WebCore
  79. #endif // AudioNodeInput_h