This source file includes following definitions.
- chooseBufferSize
- create
- m_internalInputBus
- initialize
- uninitialize
- process
- fireProcessEventDispatch
- fireProcessEvent
- tailTime
- latencyTime
#include "config.h"
#if ENABLE(WEB_AUDIO)
#include "modules/webaudio/ScriptProcessorNode.h"
#include "core/dom/Document.h"
#include "modules/webaudio/AudioBuffer.h"
#include "modules/webaudio/AudioContext.h"
#include "modules/webaudio/AudioNodeInput.h"
#include "modules/webaudio/AudioNodeOutput.h"
#include "modules/webaudio/AudioProcessingEvent.h"
#include "public/platform/Platform.h"
#include "wtf/Float32Array.h"
#include "wtf/MainThread.h"
namespace WebCore {
static size_t chooseBufferSize()
{
size_t hardwareBufferSize = blink::Platform::current()->audioHardwareBufferSize();
size_t bufferSize = 1 << static_cast<unsigned>(log2(4 * hardwareBufferSize) + 0.5);
if (bufferSize < 256)
return 256;
if (bufferSize > 16384)
return 16384;
return bufferSize;
}
PassRefPtr<ScriptProcessorNode> ScriptProcessorNode::create(AudioContext* context, float sampleRate, size_t bufferSize, unsigned numberOfInputChannels, unsigned numberOfOutputChannels)
{
switch (bufferSize) {
case 0:
bufferSize = chooseBufferSize();
break;
case 256:
case 512:
case 1024:
case 2048:
case 4096:
case 8192:
case 16384:
break;
default:
return nullptr;
}
if (!numberOfInputChannels && !numberOfOutputChannels)
return nullptr;
if (numberOfInputChannels > AudioContext::maxNumberOfChannels())
return nullptr;
if (numberOfOutputChannels > AudioContext::maxNumberOfChannels())
return nullptr;
return adoptRef(new ScriptProcessorNode(context, sampleRate, bufferSize, numberOfInputChannels, numberOfOutputChannels));
}
ScriptProcessorNode::ScriptProcessorNode(AudioContext* context, float sampleRate, size_t bufferSize, unsigned numberOfInputChannels, unsigned numberOfOutputChannels)
: AudioNode(context, sampleRate)
, m_doubleBufferIndex(0)
, m_doubleBufferIndexForEvent(0)
, m_bufferSize(bufferSize)
, m_bufferReadWriteIndex(0)
, m_numberOfInputChannels(numberOfInputChannels)
, m_numberOfOutputChannels(numberOfOutputChannels)
, m_internalInputBus(AudioBus::create(numberOfInputChannels, AudioNode::ProcessingSizeInFrames, false))
{
ScriptWrappable::init(this);
if (m_bufferSize < AudioNode::ProcessingSizeInFrames)
m_bufferSize = AudioNode::ProcessingSizeInFrames;
ASSERT(numberOfInputChannels <= AudioContext::maxNumberOfChannels());
addInput(adoptPtr(new AudioNodeInput(this)));
addOutput(adoptPtr(new AudioNodeOutput(this, numberOfOutputChannels)));
setNodeType(NodeTypeJavaScript);
initialize();
}
ScriptProcessorNode::~ScriptProcessorNode()
{
uninitialize();
}
void ScriptProcessorNode::initialize()
{
if (isInitialized())
return;
float sampleRate = context()->sampleRate();
for (unsigned i = 0; i < 2; ++i) {
RefPtr<AudioBuffer> inputBuffer = m_numberOfInputChannels ? AudioBuffer::create(m_numberOfInputChannels, bufferSize(), sampleRate) : nullptr;
RefPtr<AudioBuffer> outputBuffer = m_numberOfOutputChannels ? AudioBuffer::create(m_numberOfOutputChannels, bufferSize(), sampleRate) : nullptr;
m_inputBuffers.append(inputBuffer);
m_outputBuffers.append(outputBuffer);
}
AudioNode::initialize();
}
void ScriptProcessorNode::uninitialize()
{
if (!isInitialized())
return;
m_inputBuffers.clear();
m_outputBuffers.clear();
AudioNode::uninitialize();
}
void ScriptProcessorNode::process(size_t framesToProcess)
{
AudioBus* inputBus = this->input(0)->bus();
AudioBus* outputBus = this->output(0)->bus();
unsigned doubleBufferIndex = this->doubleBufferIndex();
bool isDoubleBufferIndexGood = doubleBufferIndex < 2 && doubleBufferIndex < m_inputBuffers.size() && doubleBufferIndex < m_outputBuffers.size();
ASSERT(isDoubleBufferIndexGood);
if (!isDoubleBufferIndexGood)
return;
AudioBuffer* inputBuffer = m_inputBuffers[doubleBufferIndex].get();
AudioBuffer* outputBuffer = m_outputBuffers[doubleBufferIndex].get();
unsigned numberOfInputChannels = m_internalInputBus->numberOfChannels();
bool buffersAreGood = outputBuffer && bufferSize() == outputBuffer->length() && m_bufferReadWriteIndex + framesToProcess <= bufferSize();
if (m_internalInputBus->numberOfChannels())
buffersAreGood = buffersAreGood && inputBuffer && bufferSize() == inputBuffer->length();
ASSERT(buffersAreGood);
if (!buffersAreGood)
return;
bool isFramesToProcessGood = framesToProcess && bufferSize() >= framesToProcess && !(bufferSize() % framesToProcess);
ASSERT(isFramesToProcessGood);
if (!isFramesToProcessGood)
return;
unsigned numberOfOutputChannels = outputBus->numberOfChannels();
bool channelsAreGood = (numberOfInputChannels == m_numberOfInputChannels) && (numberOfOutputChannels == m_numberOfOutputChannels);
ASSERT(channelsAreGood);
if (!channelsAreGood)
return;
for (unsigned i = 0; i < numberOfInputChannels; i++)
m_internalInputBus->setChannelMemory(i, inputBuffer->getChannelData(i)->data() + m_bufferReadWriteIndex, framesToProcess);
if (numberOfInputChannels)
m_internalInputBus->copyFrom(*inputBus);
for (unsigned i = 0; i < numberOfOutputChannels; ++i)
memcpy(outputBus->channel(i)->mutableData(), outputBuffer->getChannelData(i)->data() + m_bufferReadWriteIndex, sizeof(float) * framesToProcess);
m_bufferReadWriteIndex = (m_bufferReadWriteIndex + framesToProcess) % bufferSize();
if (!m_bufferReadWriteIndex) {
MutexTryLocker tryLocker(m_processEventLock);
if (!tryLocker.locked()) {
outputBuffer->zero();
} else {
ref();
m_doubleBufferIndexForEvent = m_doubleBufferIndex;
callOnMainThread(fireProcessEventDispatch, this);
}
swapBuffers();
}
}
void ScriptProcessorNode::fireProcessEventDispatch(void* userData)
{
ScriptProcessorNode* jsAudioNode = static_cast<ScriptProcessorNode*>(userData);
ASSERT(jsAudioNode);
if (!jsAudioNode)
return;
jsAudioNode->fireProcessEvent();
jsAudioNode->deref();
}
void ScriptProcessorNode::fireProcessEvent()
{
ASSERT(isMainThread());
bool isIndexGood = m_doubleBufferIndexForEvent < 2;
ASSERT(isIndexGood);
if (!isIndexGood)
return;
AudioBuffer* inputBuffer = m_inputBuffers[m_doubleBufferIndexForEvent].get();
AudioBuffer* outputBuffer = m_outputBuffers[m_doubleBufferIndexForEvent].get();
ASSERT(outputBuffer);
if (!outputBuffer)
return;
if (context()->executionContext()) {
MutexLocker processLocker(m_processEventLock);
double playbackTime = (context()->currentSampleFrame() + m_bufferSize) / static_cast<double>(context()->sampleRate());
dispatchEvent(AudioProcessingEvent::create(inputBuffer, outputBuffer, playbackTime));
}
}
double ScriptProcessorNode::tailTime() const
{
return std::numeric_limits<double>::infinity();
}
double ScriptProcessorNode::latencyTime() const
{
return std::numeric_limits<double>::infinity();
}
}
#endif