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#pragma once
26
27#include "AudioBus.h"
28#include "AudioNode.h"
29#include "EventListener.h"
30#include "EventTarget.h"
31#include <wtf/Forward.h>
32#include <wtf/RefPtr.h>
33#include <wtf/Vector.h>
34
35namespace WebCore {
36
37class AudioBuffer;
38class AudioContext;
39class 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
47class ScriptProcessorNode final : public AudioNode {
48 WTF_MAKE_ISO_ALLOCATED(ScriptProcessorNode);
49public:
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 Ref<ScriptProcessorNode> create(AudioContext&, float sampleRate, size_t bufferSize, unsigned numberOfInputChannels, unsigned numberOfOutputChannels);
55
56 virtual ~ScriptProcessorNode();
57
58 // AudioNode
59 void process(size_t framesToProcess) override;
60 void reset() override;
61 void initialize() override;
62 void uninitialize() override;
63
64 size_t bufferSize() const { return m_bufferSize; }
65
66private:
67 double tailTime() const override;
68 double latencyTime() const override;
69
70 ScriptProcessorNode(AudioContext&, float sampleRate, size_t bufferSize, unsigned numberOfInputChannels, unsigned numberOfOutputChannels);
71
72 void fireProcessEvent();
73
74 bool addEventListener(const AtomString& eventType, Ref<EventListener>&&, const AddEventListenerOptions&) override;
75 bool removeEventListener(const AtomString& eventType, EventListener&, const ListenerOptions&) override;
76 void removeAllEventListeners() override;
77
78 // Double buffering
79 unsigned doubleBufferIndex() const { return m_doubleBufferIndex; }
80 void swapBuffers() { m_doubleBufferIndex = 1 - m_doubleBufferIndex; }
81 unsigned m_doubleBufferIndex;
82 unsigned m_doubleBufferIndexForEvent;
83 Vector<RefPtr<AudioBuffer>> m_inputBuffers;
84 Vector<RefPtr<AudioBuffer>> m_outputBuffers;
85
86 size_t m_bufferSize;
87 unsigned m_bufferReadWriteIndex;
88 volatile bool m_isRequestOutstanding;
89
90 unsigned m_numberOfInputChannels;
91 unsigned m_numberOfOutputChannels;
92
93 RefPtr<AudioBus> m_internalInputBus;
94 bool m_hasAudioProcessListener;
95};
96
97} // namespace WebCore
98