2 * Copyright (C) 2010, Google Inc. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
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.
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.
25 #ifndef ScriptProcessorNode_h
26 #define ScriptProcessorNode_h
28 #include "platform/audio/AudioBus.h"
29 #include "modules/webaudio/AudioNode.h"
30 #include "wtf/Forward.h"
31 #include "wtf/PassRefPtr.h"
32 #include "wtf/RefPtr.h"
33 #include "wtf/Vector.h"
39 class AudioProcessingEvent;
41 // ScriptProcessorNode is an AudioNode which allows for arbitrary synthesis or processing directly using JavaScript.
42 // The API allows for a variable number of inputs and outputs, although it must have at least one input or output.
43 // This basic implementation supports no more than one input and output.
44 // The "onaudioprocess" attribute is an event listener which will get called periodically with an AudioProcessingEvent which has
45 // AudioBuffers for each input and output.
47 class ScriptProcessorNode FINAL : public AudioNode {
48 DEFINE_WRAPPERTYPEINFO();
50 // bufferSize must be one of the following values: 256, 512, 1024, 2048, 4096, 8192, 16384.
51 // This value controls how frequently the onaudioprocess event handler is called and how many sample-frames need to be processed each call.
52 // Lower numbers for bufferSize will result in a lower (better) latency. Higher numbers will be necessary to avoid audio breakup and glitches.
53 // The value chosen must carefully balance between latency and audio quality.
54 static ScriptProcessorNode* create(AudioContext*, float sampleRate, size_t bufferSize, unsigned numberOfInputChannels, unsigned numberOfOutputChannels);
56 virtual ~ScriptProcessorNode();
59 virtual void dispose() OVERRIDE;
60 virtual void process(size_t framesToProcess) OVERRIDE;
61 virtual void initialize() OVERRIDE;
62 virtual void uninitialize() OVERRIDE;
64 size_t bufferSize() const { return m_bufferSize; }
66 DEFINE_ATTRIBUTE_EVENT_LISTENER(audioprocess);
71 virtual double tailTime() const OVERRIDE;
72 virtual double latencyTime() const OVERRIDE;
74 ScriptProcessorNode(AudioContext*, float sampleRate, size_t bufferSize, unsigned numberOfInputChannels, unsigned numberOfOutputChannels);
76 void fireProcessEvent();
79 unsigned doubleBufferIndex() const { return m_doubleBufferIndex; }
80 void swapBuffers() { m_doubleBufferIndex = 1 - m_doubleBufferIndex; }
81 unsigned m_doubleBufferIndex;
82 unsigned m_doubleBufferIndexForEvent;
83 HeapVector<Member<AudioBuffer> > m_inputBuffers;
84 HeapVector<Member<AudioBuffer> > m_outputBuffers;
87 unsigned m_bufferReadWriteIndex;
89 unsigned m_numberOfInputChannels;
90 unsigned m_numberOfOutputChannels;
92 RefPtr<AudioBus> m_internalInputBus;
93 // Synchronize process() with fireProcessEvent().
94 mutable Mutex m_processEventLock;
99 #endif // ScriptProcessorNode_h