Home | History | Annotate | Download | only in webaudio
      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 
     25 #ifndef ScriptProcessorNode_h
     26 #define ScriptProcessorNode_h
     27 
     28 #include "core/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"
     34 
     35 namespace WebCore {
     36 
     37 class AudioBuffer;
     38 class AudioContext;
     39 class AudioProcessingEvent;
     40 
     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.
     46 
     47 class ScriptProcessorNode : public AudioNode {
     48 public:
     49     // bufferSize must be one of the following values: 256, 512, 1024, 2048, 4096, 8192, 16384.
     50     // This value controls how frequently the onaudioprocess event handler is called and how many sample-frames need to be processed each call.
     51     // Lower numbers for bufferSize will result in a lower (better) latency. Higher numbers will be necessary to avoid audio breakup and glitches.
     52     // The value chosen must carefully balance between latency and audio quality.
     53     static PassRefPtr<ScriptProcessorNode> create(AudioContext*, float sampleRate, size_t bufferSize, unsigned numberOfInputChannels, unsigned numberOfOutputChannels);
     54 
     55     virtual ~ScriptProcessorNode();
     56 
     57     // AudioNode
     58     virtual void process(size_t framesToProcess);
     59     virtual void reset();
     60     virtual void initialize();
     61     virtual void uninitialize();
     62 
     63     size_t bufferSize() const { return m_bufferSize; }
     64 
     65     DEFINE_ATTRIBUTE_EVENT_LISTENER(audioprocess);
     66 
     67 
     68 private:
     69     virtual double tailTime() const OVERRIDE;
     70     virtual double latencyTime() const OVERRIDE;
     71 
     72     ScriptProcessorNode(AudioContext*, float sampleRate, size_t bufferSize, unsigned numberOfInputChannels, unsigned numberOfOutputChannels);
     73 
     74     static void fireProcessEventDispatch(void* userData);
     75     void fireProcessEvent();
     76 
     77     // Double buffering
     78     unsigned doubleBufferIndex() const { return m_doubleBufferIndex; }
     79     void swapBuffers() { m_doubleBufferIndex = 1 - m_doubleBufferIndex; }
     80     unsigned m_doubleBufferIndex;
     81     unsigned m_doubleBufferIndexForEvent;
     82     Vector<RefPtr<AudioBuffer> > m_inputBuffers;
     83     Vector<RefPtr<AudioBuffer> > m_outputBuffers;
     84 
     85     size_t m_bufferSize;
     86     unsigned m_bufferReadWriteIndex;
     87     volatile bool m_isRequestOutstanding;
     88 
     89     unsigned m_numberOfInputChannels;
     90     unsigned m_numberOfOutputChannels;
     91 
     92     RefPtr<AudioBus> m_internalInputBus;
     93 };
     94 
     95 } // namespace WebCore
     96 
     97 #endif // ScriptProcessorNode_h
     98