AudioNode.h 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  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 AudioNode_h
  25. #define AudioNode_h
  26. #include "AudioBus.h"
  27. #include "EventTarget.h"
  28. #include <wtf/Forward.h>
  29. #include <wtf/OwnPtr.h>
  30. #include <wtf/PassOwnPtr.h>
  31. #include <wtf/RefPtr.h>
  32. #include <wtf/Vector.h>
  33. #define DEBUG_AUDIONODE_REFERENCES 0
  34. namespace WebCore {
  35. class AudioContext;
  36. class AudioNodeInput;
  37. class AudioNodeOutput;
  38. class AudioParam;
  39. typedef int ExceptionCode;
  40. // An AudioNode is the basic building block for handling audio within an AudioContext.
  41. // It may be an audio source, an intermediate processing module, or an audio destination.
  42. // Each AudioNode can have inputs and/or outputs. An AudioSourceNode has no inputs and a single output.
  43. // An AudioDestinationNode has one input and no outputs and represents the final destination to the audio hardware.
  44. // Most processing nodes such as filters will have one input and one output, although multiple inputs and outputs are possible.
  45. class AudioNode : public EventTarget {
  46. public:
  47. enum { ProcessingSizeInFrames = 128 };
  48. AudioNode(AudioContext*, float sampleRate);
  49. virtual ~AudioNode();
  50. AudioContext* context() { return m_context.get(); }
  51. const AudioContext* context() const { return m_context.get(); }
  52. enum NodeType {
  53. NodeTypeUnknown,
  54. NodeTypeDestination,
  55. NodeTypeOscillator,
  56. NodeTypeAudioBufferSource,
  57. NodeTypeMediaElementAudioSource,
  58. NodeTypeMediaStreamAudioDestination,
  59. NodeTypeMediaStreamAudioSource,
  60. NodeTypeJavaScript,
  61. NodeTypeBiquadFilter,
  62. NodeTypePanner,
  63. NodeTypeConvolver,
  64. NodeTypeDelay,
  65. NodeTypeGain,
  66. NodeTypeChannelSplitter,
  67. NodeTypeChannelMerger,
  68. NodeTypeAnalyser,
  69. NodeTypeDynamicsCompressor,
  70. NodeTypeWaveShaper,
  71. NodeTypeEnd
  72. };
  73. enum ChannelCountMode {
  74. Max,
  75. ClampedMax,
  76. Explicit
  77. };
  78. NodeType nodeType() const { return m_nodeType; }
  79. void setNodeType(NodeType);
  80. // We handle our own ref-counting because of the threading issues and subtle nature of
  81. // how AudioNodes can continue processing (playing one-shot sound) after there are no more
  82. // JavaScript references to the object.
  83. enum RefType { RefTypeNormal, RefTypeConnection };
  84. // Can be called from main thread or context's audio thread.
  85. void ref(RefType refType = RefTypeNormal);
  86. void deref(RefType refType = RefTypeNormal);
  87. // Can be called from main thread or context's audio thread. It must be called while the context's graph lock is held.
  88. void finishDeref(RefType refType);
  89. // The AudioNodeInput(s) (if any) will already have their input data available when process() is called.
  90. // Subclasses will take this input data and put the results in the AudioBus(s) of its AudioNodeOutput(s) (if any).
  91. // Called from context's audio thread.
  92. virtual void process(size_t framesToProcess) = 0;
  93. // Resets DSP processing state (clears delay lines, filter memory, etc.)
  94. // Called from context's audio thread.
  95. virtual void reset() = 0;
  96. // No significant resources should be allocated until initialize() is called.
  97. // Processing may not occur until a node is initialized.
  98. virtual void initialize();
  99. virtual void uninitialize();
  100. bool isInitialized() const { return m_isInitialized; }
  101. void lazyInitialize();
  102. unsigned numberOfInputs() const { return m_inputs.size(); }
  103. unsigned numberOfOutputs() const { return m_outputs.size(); }
  104. AudioNodeInput* input(unsigned);
  105. AudioNodeOutput* output(unsigned);
  106. // Called from main thread by corresponding JavaScript methods.
  107. virtual void connect(AudioNode*, unsigned outputIndex, unsigned inputIndex, ExceptionCode&);
  108. void connect(AudioParam*, unsigned outputIndex, ExceptionCode&);
  109. virtual void disconnect(unsigned outputIndex, ExceptionCode&);
  110. virtual float sampleRate() const { return m_sampleRate; }
  111. // processIfNecessary() is called by our output(s) when the rendering graph needs this AudioNode to process.
  112. // This method ensures that the AudioNode will only process once per rendering time quantum even if it's called repeatedly.
  113. // This handles the case of "fanout" where an output is connected to multiple AudioNode inputs.
  114. // Called from context's audio thread.
  115. void processIfNecessary(size_t framesToProcess);
  116. // Called when a new connection has been made to one of our inputs or the connection number of channels has changed.
  117. // This potentially gives us enough information to perform a lazy initialization or, if necessary, a re-initialization.
  118. // Called from main thread.
  119. virtual void checkNumberOfChannelsForInput(AudioNodeInput*);
  120. #if DEBUG_AUDIONODE_REFERENCES
  121. static void printNodeCounts();
  122. #endif
  123. bool isMarkedForDeletion() const { return m_isMarkedForDeletion; }
  124. // tailTime() is the length of time (not counting latency time) where non-zero output may occur after continuous silent input.
  125. virtual double tailTime() const = 0;
  126. // latencyTime() is the length of time it takes for non-zero output to appear after non-zero input is provided. This only applies to
  127. // processing delay which is an artifact of the processing algorithm chosen and is *not* part of the intrinsic desired effect. For
  128. // example, a "delay" effect is expected to delay the signal, and thus would not be considered latency.
  129. virtual double latencyTime() const = 0;
  130. // propagatesSilence() should return true if the node will generate silent output when given silent input. By default, AudioNode
  131. // will take tailTime() and latencyTime() into account when determining whether the node will propagate silence.
  132. virtual bool propagatesSilence() const;
  133. bool inputsAreSilent();
  134. void silenceOutputs();
  135. void unsilenceOutputs();
  136. void enableOutputsIfNecessary();
  137. void disableOutputsIfNecessary();
  138. unsigned long channelCount();
  139. virtual void setChannelCount(unsigned long, ExceptionCode&);
  140. String channelCountMode();
  141. void setChannelCountMode(const String&, ExceptionCode&);
  142. String channelInterpretation();
  143. void setChannelInterpretation(const String&, ExceptionCode&);
  144. ChannelCountMode internalChannelCountMode() const { return m_channelCountMode; }
  145. AudioBus::ChannelInterpretation internalChannelInterpretation() const { return m_channelInterpretation; }
  146. // EventTarget
  147. virtual const AtomicString& interfaceName() const OVERRIDE;
  148. virtual ScriptExecutionContext* scriptExecutionContext() const OVERRIDE;
  149. virtual EventTargetData* eventTargetData() OVERRIDE { return &m_eventTargetData; }
  150. virtual EventTargetData* ensureEventTargetData() OVERRIDE { return &m_eventTargetData; }
  151. protected:
  152. // Inputs and outputs must be created before the AudioNode is initialized.
  153. void addInput(PassOwnPtr<AudioNodeInput>);
  154. void addOutput(PassOwnPtr<AudioNodeOutput>);
  155. // Called by processIfNecessary() to cause all parts of the rendering graph connected to us to process.
  156. // Each rendering quantum, the audio data for each of the AudioNode's inputs will be available after this method is called.
  157. // Called from context's audio thread.
  158. virtual void pullInputs(size_t framesToProcess);
  159. // Force all inputs to take any channel interpretation changes into account.
  160. void updateChannelsForInputs();
  161. private:
  162. volatile bool m_isInitialized;
  163. NodeType m_nodeType;
  164. RefPtr<AudioContext> m_context;
  165. float m_sampleRate;
  166. Vector<OwnPtr<AudioNodeInput> > m_inputs;
  167. Vector<OwnPtr<AudioNodeOutput> > m_outputs;
  168. EventTargetData m_eventTargetData;
  169. double m_lastProcessingTime;
  170. double m_lastNonSilentTime;
  171. // Ref-counting
  172. volatile int m_normalRefCount;
  173. volatile int m_connectionRefCount;
  174. bool m_isMarkedForDeletion;
  175. bool m_isDisabled;
  176. #if DEBUG_AUDIONODE_REFERENCES
  177. static bool s_isNodeCountInitialized;
  178. static int s_nodeCount[NodeTypeEnd];
  179. #endif
  180. virtual void refEventTarget() OVERRIDE { ref(); }
  181. virtual void derefEventTarget() OVERRIDE { deref(); }
  182. protected:
  183. unsigned m_channelCount;
  184. ChannelCountMode m_channelCountMode;
  185. AudioBus::ChannelInterpretation m_channelInterpretation;
  186. };
  187. } // namespace WebCore
  188. #endif // AudioNode_h