ScriptProcessorNode.h 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  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 ScriptProcessorNode_h
  25. #define ScriptProcessorNode_h
  26. #include "ActiveDOMObject.h"
  27. #include "AudioBus.h"
  28. #include "AudioNode.h"
  29. #include "EventListener.h"
  30. #include "EventTarget.h"
  31. #include <wtf/Forward.h>
  32. #include <wtf/PassRefPtr.h>
  33. #include <wtf/RefPtr.h>
  34. #include <wtf/Vector.h>
  35. namespace WebCore {
  36. class AudioBuffer;
  37. class AudioContext;
  38. class AudioProcessingEvent;
  39. // ScriptProcessorNode is an AudioNode which allows for arbitrary synthesis or processing directly using JavaScript.
  40. // The API allows for a variable number of inputs and outputs, although it must have at least one input or output.
  41. // This basic implementation supports no more than one input and output.
  42. // The "onaudioprocess" attribute is an event listener which will get called periodically with an AudioProcessingEvent which has
  43. // AudioBuffers for each input and output.
  44. class ScriptProcessorNode : public AudioNode {
  45. public:
  46. // bufferSize must be one of the following values: 256, 512, 1024, 2048, 4096, 8192, 16384.
  47. // This value controls how frequently the onaudioprocess event handler is called and how many sample-frames need to be processed each call.
  48. // Lower numbers for bufferSize will result in a lower (better) latency. Higher numbers will be necessary to avoid audio breakup and glitches.
  49. // The value chosen must carefully balance between latency and audio quality.
  50. static PassRefPtr<ScriptProcessorNode> create(AudioContext*, float sampleRate, size_t bufferSize, unsigned numberOfInputChannels, unsigned numberOfOutputChannels);
  51. virtual ~ScriptProcessorNode();
  52. // AudioNode
  53. virtual void process(size_t framesToProcess);
  54. virtual void reset();
  55. virtual void initialize();
  56. virtual void uninitialize();
  57. size_t bufferSize() const { return m_bufferSize; }
  58. EventListener* onaudioprocess() { return getAttributeEventListener(eventNames().audioprocessEvent); }
  59. void setOnaudioprocess(PassRefPtr<EventListener>);
  60. private:
  61. virtual double tailTime() const OVERRIDE;
  62. virtual double latencyTime() const OVERRIDE;
  63. ScriptProcessorNode(AudioContext*, float sampleRate, size_t bufferSize, unsigned numberOfInputChannels, unsigned numberOfOutputChannels);
  64. static void fireProcessEventDispatch(void* userData);
  65. void fireProcessEvent();
  66. // Double buffering
  67. unsigned doubleBufferIndex() const { return m_doubleBufferIndex; }
  68. void swapBuffers() { m_doubleBufferIndex = 1 - m_doubleBufferIndex; }
  69. unsigned m_doubleBufferIndex;
  70. unsigned m_doubleBufferIndexForEvent;
  71. Vector<RefPtr<AudioBuffer> > m_inputBuffers;
  72. Vector<RefPtr<AudioBuffer> > m_outputBuffers;
  73. size_t m_bufferSize;
  74. unsigned m_bufferReadWriteIndex;
  75. volatile bool m_isRequestOutstanding;
  76. unsigned m_numberOfInputChannels;
  77. unsigned m_numberOfOutputChannels;
  78. RefPtr<AudioBus> m_internalInputBus;
  79. bool m_hasAudioProcessListener;
  80. };
  81. } // namespace WebCore
  82. #endif // ScriptProcessorNode_h