This source file includes following definitions.
- m_channelInterpretation
- initialize
- uninitialize
- nodeTypeName
- setNodeType
- addInput
- addOutput
- input
- output
- connect
- connect
- disconnect
- channelCount
- setChannelCount
- channelCountMode
- setChannelCountMode
- channelInterpretation
- setChannelInterpretation
- updateChannelsForInputs
- interfaceName
- executionContext
- processIfNecessary
- checkNumberOfChannelsForInput
- propagatesSilence
- pullInputs
- inputsAreSilent
- silenceOutputs
- unsilenceOutputs
- enableOutputsIfNecessary
- disableOutputsIfNecessary
- ref
- deref
- finishDeref
- printNodeCounts
#include "config.h"
#if ENABLE(WEB_AUDIO)
#include "modules/webaudio/AudioNode.h"
#include "bindings/v8/ExceptionState.h"
#include "core/dom/ExceptionCode.h"
#include "modules/webaudio/AudioContext.h"
#include "modules/webaudio/AudioNodeInput.h"
#include "modules/webaudio/AudioNodeOutput.h"
#include "modules/webaudio/AudioParam.h"
#include "wtf/Atomics.h"
#include "wtf/MainThread.h"
#if DEBUG_AUDIONODE_REFERENCES
#include <stdio.h>
#endif
namespace WebCore {
AudioNode::AudioNode(AudioContext* context, float sampleRate)
: m_isInitialized(false)
, m_nodeType(NodeTypeUnknown)
, m_context(context)
, m_sampleRate(sampleRate)
, m_lastProcessingTime(-1)
, m_lastNonSilentTime(-1)
, m_normalRefCount(1)
, m_connectionRefCount(0)
, m_isMarkedForDeletion(false)
, m_isDisabled(false)
, m_channelCount(2)
, m_channelCountMode(Max)
, m_channelInterpretation(AudioBus::Speakers)
{
ScriptWrappable::init(this);
context->lazyInitialize();
#if DEBUG_AUDIONODE_REFERENCES
if (!s_isNodeCountInitialized) {
s_isNodeCountInitialized = true;
atexit(AudioNode::printNodeCounts);
}
#endif
}
AudioNode::~AudioNode()
{
#if DEBUG_AUDIONODE_REFERENCES
--s_nodeCount[nodeType()];
fprintf(stderr, "%p: %d: AudioNode::~AudioNode() %d %d\n", this, nodeType(), m_normalRefCount, m_connectionRefCount);
#endif
}
void AudioNode::initialize()
{
m_isInitialized = true;
}
void AudioNode::uninitialize()
{
m_isInitialized = false;
}
String AudioNode::nodeTypeName() const
{
switch (m_nodeType) {
case NodeTypeDestination:
return "AudioDestinationNode";
case NodeTypeOscillator:
return "OscillatorNode";
case NodeTypeAudioBufferSource:
return "AudioBufferSourceNode";
case NodeTypeMediaElementAudioSource:
return "MediaElementAudioSourceNode";
case NodeTypeMediaStreamAudioDestination:
return "MediaStreamAudioDestinationNode";
case NodeTypeMediaStreamAudioSource:
return "MediaStreamAudioSourceNode";
case NodeTypeJavaScript:
return "ScriptProcessorNode";
case NodeTypeBiquadFilter:
return "BiquadFilterNode";
case NodeTypePanner:
return "PannerNode";
case NodeTypeConvolver:
return "ConvolverNode";
case NodeTypeDelay:
return "DelayNode";
case NodeTypeGain:
return "GainNode";
case NodeTypeChannelSplitter:
return "ChannelSplitterNode";
case NodeTypeChannelMerger:
return "ChannelMergerNode";
case NodeTypeAnalyser:
return "AnalyserNode";
case NodeTypeDynamicsCompressor:
return "DynamicsCompressorNode";
case NodeTypeWaveShaper:
return "WaveShaperNode";
case NodeTypeUnknown:
case NodeTypeEnd:
default:
ASSERT_NOT_REACHED();
return "UnknownNode";
}
}
void AudioNode::setNodeType(NodeType type)
{
m_nodeType = type;
#if DEBUG_AUDIONODE_REFERENCES
++s_nodeCount[type];
#endif
}
void AudioNode::addInput(PassOwnPtr<AudioNodeInput> input)
{
m_inputs.append(input);
}
void AudioNode::addOutput(PassOwnPtr<AudioNodeOutput> output)
{
m_outputs.append(output);
}
AudioNodeInput* AudioNode::input(unsigned i)
{
if (i < m_inputs.size())
return m_inputs[i].get();
return 0;
}
AudioNodeOutput* AudioNode::output(unsigned i)
{
if (i < m_outputs.size())
return m_outputs[i].get();
return 0;
}
void AudioNode::connect(AudioNode* destination, unsigned outputIndex, unsigned inputIndex, ExceptionState& exceptionState)
{
ASSERT(isMainThread());
AudioContext::AutoLocker locker(context());
if (!destination) {
exceptionState.throwDOMException(
SyntaxError,
"invalid destination node.");
return;
}
if (outputIndex >= numberOfOutputs()) {
exceptionState.throwDOMException(
IndexSizeError,
"output index (" + String::number(outputIndex) + ") exceeds number of outputs (" + String::number(numberOfOutputs()) + ").");
return;
}
if (destination && inputIndex >= destination->numberOfInputs()) {
exceptionState.throwDOMException(
IndexSizeError,
"input index (" + String::number(inputIndex) + ") exceeds number of inputs (" + String::number(destination->numberOfInputs()) + ").");
return;
}
if (context() != destination->context()) {
exceptionState.throwDOMException(
SyntaxError,
"cannot connect to a destination belonging to a different audio context.");
return;
}
AudioNodeInput* input = destination->input(inputIndex);
AudioNodeOutput* output = this->output(outputIndex);
input->connect(output);
context()->incrementConnectionCount();
}
void AudioNode::connect(AudioParam* param, unsigned outputIndex, ExceptionState& exceptionState)
{
ASSERT(isMainThread());
AudioContext::AutoLocker locker(context());
if (!param) {
exceptionState.throwDOMException(
SyntaxError,
"invalid AudioParam.");
return;
}
if (outputIndex >= numberOfOutputs()) {
exceptionState.throwDOMException(
IndexSizeError,
"output index (" + String::number(outputIndex) + ") exceeds number of outputs (" + String::number(numberOfOutputs()) + ").");
return;
}
if (context() != param->context()) {
exceptionState.throwDOMException(
SyntaxError,
"cannot connect to an AudioParam belonging to a different audio context.");
return;
}
AudioNodeOutput* output = this->output(outputIndex);
param->connect(output);
}
void AudioNode::disconnect(unsigned outputIndex, ExceptionState& exceptionState)
{
ASSERT(isMainThread());
AudioContext::AutoLocker locker(context());
if (outputIndex >= numberOfOutputs()) {
exceptionState.throwDOMException(
IndexSizeError,
"output index (" + String::number(outputIndex) + ") exceeds number of outputs (" + String::number(numberOfOutputs()) + ").");
return;
}
AudioNodeOutput* output = this->output(outputIndex);
output->disconnectAll();
}
unsigned long AudioNode::channelCount()
{
return m_channelCount;
}
void AudioNode::setChannelCount(unsigned long channelCount, ExceptionState& exceptionState)
{
ASSERT(isMainThread());
AudioContext::AutoLocker locker(context());
if (channelCount > 0 && channelCount <= AudioContext::maxNumberOfChannels()) {
if (m_channelCount != channelCount) {
m_channelCount = channelCount;
if (m_channelCountMode != Max)
updateChannelsForInputs();
}
} else {
exceptionState.throwDOMException(
NotSupportedError,
"channel count (" + String::number(channelCount) + ") must be between 1 and " + String::number(AudioContext::maxNumberOfChannels()) + ".");
}
}
String AudioNode::channelCountMode()
{
switch (m_channelCountMode) {
case Max:
return "max";
case ClampedMax:
return "clamped-max";
case Explicit:
return "explicit";
}
ASSERT_NOT_REACHED();
return "";
}
void AudioNode::setChannelCountMode(const String& mode, ExceptionState& exceptionState)
{
ASSERT(isMainThread());
AudioContext::AutoLocker locker(context());
ChannelCountMode oldMode = m_channelCountMode;
if (mode == "max") {
m_channelCountMode = Max;
} else if (mode == "clamped-max") {
m_channelCountMode = ClampedMax;
} else if (mode == "explicit") {
m_channelCountMode = Explicit;
} else {
exceptionState.throwDOMException(
InvalidStateError,
"invalid mode '" + mode + "'; must be 'max', 'clamped-max', or 'explicit'.");
}
if (m_channelCountMode != oldMode)
updateChannelsForInputs();
}
String AudioNode::channelInterpretation()
{
switch (m_channelInterpretation) {
case AudioBus::Speakers:
return "speakers";
case AudioBus::Discrete:
return "discrete";
}
ASSERT_NOT_REACHED();
return "";
}
void AudioNode::setChannelInterpretation(const String& interpretation, ExceptionState& exceptionState)
{
ASSERT(isMainThread());
AudioContext::AutoLocker locker(context());
if (interpretation == "speakers") {
m_channelInterpretation = AudioBus::Speakers;
} else if (interpretation == "discrete") {
m_channelInterpretation = AudioBus::Discrete;
} else {
exceptionState.throwDOMException(
InvalidStateError,
"invalid interpretation '" + interpretation + "'; must be 'speakers' or 'discrete'.");
}
}
void AudioNode::updateChannelsForInputs()
{
for (unsigned i = 0; i < m_inputs.size(); ++i)
input(i)->changedOutputs();
}
const AtomicString& AudioNode::interfaceName() const
{
return EventTargetNames::AudioNode;
}
ExecutionContext* AudioNode::executionContext() const
{
return const_cast<AudioNode*>(this)->context()->executionContext();
}
void AudioNode::processIfNecessary(size_t framesToProcess)
{
ASSERT(context()->isAudioThread());
if (!isInitialized())
return;
double currentTime = context()->currentTime();
if (m_lastProcessingTime != currentTime) {
m_lastProcessingTime = currentTime;
pullInputs(framesToProcess);
bool silentInputs = inputsAreSilent();
if (!silentInputs)
m_lastNonSilentTime = (context()->currentSampleFrame() + framesToProcess) / static_cast<double>(m_sampleRate);
if (silentInputs && propagatesSilence())
silenceOutputs();
else {
process(framesToProcess);
unsilenceOutputs();
}
}
}
void AudioNode::checkNumberOfChannelsForInput(AudioNodeInput* input)
{
ASSERT(context()->isAudioThread() && context()->isGraphOwner());
ASSERT(m_inputs.contains(input));
if (!m_inputs.contains(input))
return;
input->updateInternalBus();
}
bool AudioNode::propagatesSilence() const
{
return m_lastNonSilentTime + latencyTime() + tailTime() < context()->currentTime();
}
void AudioNode::pullInputs(size_t framesToProcess)
{
ASSERT(context()->isAudioThread());
for (unsigned i = 0; i < m_inputs.size(); ++i)
input(i)->pull(0, framesToProcess);
}
bool AudioNode::inputsAreSilent()
{
for (unsigned i = 0; i < m_inputs.size(); ++i) {
if (!input(i)->bus()->isSilent())
return false;
}
return true;
}
void AudioNode::silenceOutputs()
{
for (unsigned i = 0; i < m_outputs.size(); ++i)
output(i)->bus()->zero();
}
void AudioNode::unsilenceOutputs()
{
for (unsigned i = 0; i < m_outputs.size(); ++i)
output(i)->bus()->clearSilentFlag();
}
void AudioNode::enableOutputsIfNecessary()
{
if (m_isDisabled && m_connectionRefCount > 0) {
ASSERT(isMainThread());
AudioContext::AutoLocker locker(context());
m_isDisabled = false;
for (unsigned i = 0; i < m_outputs.size(); ++i)
output(i)->enable();
}
}
void AudioNode::disableOutputsIfNecessary()
{
if (m_connectionRefCount <= 1 && !m_isDisabled) {
if (nodeType() != NodeTypeConvolver && nodeType() != NodeTypeDelay) {
m_isDisabled = true;
for (unsigned i = 0; i < m_outputs.size(); ++i)
output(i)->disable();
}
}
}
void AudioNode::ref(RefType refType)
{
switch (refType) {
case RefTypeNormal:
atomicIncrement(&m_normalRefCount);
break;
case RefTypeConnection:
atomicIncrement(&m_connectionRefCount);
break;
default:
ASSERT_NOT_REACHED();
}
#if DEBUG_AUDIONODE_REFERENCES
fprintf(stderr, "%p: %d: AudioNode::ref(%d) %d %d\n", this, nodeType(), refType, m_normalRefCount, m_connectionRefCount);
#endif
if (refType == RefTypeConnection)
enableOutputsIfNecessary();
}
void AudioNode::deref(RefType refType)
{
bool hasLock = false;
bool mustReleaseLock = false;
if (context()->isAudioThread()) {
hasLock = context()->tryLock(mustReleaseLock);
} else {
context()->lock(mustReleaseLock);
hasLock = true;
}
if (hasLock) {
finishDeref(refType);
if (mustReleaseLock)
context()->unlock();
} else {
ASSERT(context()->isAudioThread());
ASSERT(refType == RefTypeConnection);
context()->addDeferredFinishDeref(this);
}
if (context()->isAudioThreadFinished())
context()->deleteMarkedNodes();
}
void AudioNode::finishDeref(RefType refType)
{
ASSERT(context()->isGraphOwner());
switch (refType) {
case RefTypeNormal:
ASSERT(m_normalRefCount > 0);
atomicDecrement(&m_normalRefCount);
break;
case RefTypeConnection:
ASSERT(m_connectionRefCount > 0);
atomicDecrement(&m_connectionRefCount);
break;
default:
ASSERT_NOT_REACHED();
}
#if DEBUG_AUDIONODE_REFERENCES
fprintf(stderr, "%p: %d: AudioNode::deref(%d) %d %d\n", this, nodeType(), refType, m_normalRefCount, m_connectionRefCount);
#endif
if (!m_connectionRefCount) {
if (!m_normalRefCount) {
if (!m_isMarkedForDeletion) {
for (unsigned i = 0; i < m_outputs.size(); ++i)
output(i)->disconnectAll();
context()->markForDeletion(this);
m_isMarkedForDeletion = true;
}
} else if (refType == RefTypeConnection)
disableOutputsIfNecessary();
}
}
#if DEBUG_AUDIONODE_REFERENCES
bool AudioNode::s_isNodeCountInitialized = false;
int AudioNode::s_nodeCount[NodeTypeEnd];
void AudioNode::printNodeCounts()
{
fprintf(stderr, "\n\n");
fprintf(stderr, "===========================\n");
fprintf(stderr, "AudioNode: reference counts\n");
fprintf(stderr, "===========================\n");
for (unsigned i = 0; i < NodeTypeEnd; ++i)
fprintf(stderr, "%d: %d\n", i, s_nodeCount[i]);
fprintf(stderr, "===========================\n\n\n");
}
#endif
}
#endif