This source file includes following definitions.
- create
- m_pannerNode
- process
- renderSilenceAndFinishIfNotLooping
- renderFromBuffer
- setBuffer
- numberOfChannels
- start
- start
- start
- noteGrainOn
- totalPitchRate
- propagatesSilence
- setPannerNode
- clearPannerNode
- finish
#include "config.h"
#if ENABLE(WEB_AUDIO)
#include "modules/webaudio/AudioBufferSourceNode.h"
#include "bindings/v8/ExceptionState.h"
#include "core/dom/ExceptionCode.h"
#include "core/frame/PageConsole.h"
#include "platform/audio/AudioUtilities.h"
#include "modules/webaudio/AudioContext.h"
#include "modules/webaudio/AudioNodeOutput.h"
#include "platform/FloatConversion.h"
#include "wtf/MainThread.h"
#include "wtf/MathExtras.h"
#include <algorithm>
using namespace std;
namespace WebCore {
const double DefaultGrainDuration = 0.020;
const double MaxRate = 1024;
PassRefPtr<AudioBufferSourceNode> AudioBufferSourceNode::create(AudioContext* context, float sampleRate)
{
return adoptRef(new AudioBufferSourceNode(context, sampleRate));
}
AudioBufferSourceNode::AudioBufferSourceNode(AudioContext* context, float sampleRate)
: AudioScheduledSourceNode(context, sampleRate)
, m_buffer(nullptr)
, m_isLooping(false)
, m_loopStart(0)
, m_loopEnd(0)
, m_virtualReadIndex(0)
, m_isGrain(false)
, m_grainOffset(0.0)
, m_grainDuration(DefaultGrainDuration)
, m_lastGain(1.0)
, m_pannerNode(0)
{
ScriptWrappable::init(this);
setNodeType(NodeTypeAudioBufferSource);
m_gain = AudioParam::create(context, "gain", 1.0, 0.0, 1.0);
m_playbackRate = AudioParam::create(context, "playbackRate", 1.0, 0.0, MaxRate);
addOutput(adoptPtr(new AudioNodeOutput(this, 1)));
initialize();
}
AudioBufferSourceNode::~AudioBufferSourceNode()
{
clearPannerNode();
uninitialize();
}
void AudioBufferSourceNode::process(size_t framesToProcess)
{
AudioBus* outputBus = output(0)->bus();
if (!isInitialized()) {
outputBus->zero();
return;
}
MutexTryLocker tryLocker(m_processLock);
if (tryLocker.locked()) {
if (!buffer()) {
outputBus->zero();
return;
}
if (numberOfChannels() != buffer()->numberOfChannels()) {
outputBus->zero();
return;
}
size_t quantumFrameOffset;
size_t bufferFramesToProcess;
updateSchedulingInfo(framesToProcess,
outputBus,
quantumFrameOffset,
bufferFramesToProcess);
if (!bufferFramesToProcess) {
outputBus->zero();
return;
}
for (unsigned i = 0; i < outputBus->numberOfChannels(); ++i)
m_destinationChannels[i] = outputBus->channel(i)->mutableData();
if (!renderFromBuffer(outputBus, quantumFrameOffset, bufferFramesToProcess)) {
outputBus->zero();
return;
}
float totalGain = gain()->value() * m_buffer->gain();
outputBus->copyWithGainFrom(*outputBus, &m_lastGain, totalGain);
outputBus->clearSilentFlag();
} else {
outputBus->zero();
}
}
bool AudioBufferSourceNode::renderSilenceAndFinishIfNotLooping(AudioBus*, unsigned index, size_t framesToProcess)
{
if (!loop()) {
if (framesToProcess > 0) {
for (unsigned i = 0; i < numberOfChannels(); ++i)
memset(m_destinationChannels[i] + index, 0, sizeof(float) * framesToProcess);
}
finish();
return true;
}
return false;
}
bool AudioBufferSourceNode::renderFromBuffer(AudioBus* bus, unsigned destinationFrameOffset, size_t numberOfFrames)
{
ASSERT(context()->isAudioThread());
ASSERT(bus);
ASSERT(buffer());
if (!bus || !buffer())
return false;
unsigned numberOfChannels = this->numberOfChannels();
unsigned busNumberOfChannels = bus->numberOfChannels();
bool channelCountGood = numberOfChannels && numberOfChannels == busNumberOfChannels;
ASSERT(channelCountGood);
if (!channelCountGood)
return false;
size_t destinationLength = bus->length();
bool isLengthGood = destinationLength <= 4096 && numberOfFrames <= 4096;
ASSERT(isLengthGood);
if (!isLengthGood)
return false;
bool isOffsetGood = destinationFrameOffset <= destinationLength && destinationFrameOffset + numberOfFrames <= destinationLength;
ASSERT(isOffsetGood);
if (!isOffsetGood)
return false;
if (destinationFrameOffset) {
for (unsigned i = 0; i < numberOfChannels; ++i)
memset(m_destinationChannels[i], 0, sizeof(float) * destinationFrameOffset);
}
unsigned writeIndex = destinationFrameOffset;
size_t bufferLength = buffer()->length();
double bufferSampleRate = buffer()->sampleRate();
unsigned endFrame = m_isGrain ? AudioUtilities::timeToSampleFrame(m_grainOffset + m_grainDuration, bufferSampleRate) : bufferLength;
if (m_isGrain)
endFrame += 512;
if (endFrame > bufferLength)
endFrame = bufferLength;
if (m_virtualReadIndex >= endFrame)
m_virtualReadIndex = 0;
double virtualEndFrame = endFrame;
double virtualDeltaFrames = endFrame;
if (loop() && (m_loopStart || m_loopEnd) && m_loopStart >= 0 && m_loopEnd > 0 && m_loopStart < m_loopEnd) {
double loopStartFrame = m_loopStart * buffer()->sampleRate();
double loopEndFrame = m_loopEnd * buffer()->sampleRate();
virtualEndFrame = min(loopEndFrame, virtualEndFrame);
virtualDeltaFrames = virtualEndFrame - loopStartFrame;
}
double pitchRate = totalPitchRate();
if (pitchRate >= virtualDeltaFrames)
return false;
double virtualReadIndex = m_virtualReadIndex;
int framesToProcess = numberOfFrames;
const float** sourceChannels = m_sourceChannels.get();
float** destinationChannels = m_destinationChannels.get();
if (pitchRate == 1 && virtualReadIndex == floor(virtualReadIndex)
&& virtualDeltaFrames == floor(virtualDeltaFrames)
&& virtualEndFrame == floor(virtualEndFrame)) {
unsigned readIndex = static_cast<unsigned>(virtualReadIndex);
unsigned deltaFrames = static_cast<unsigned>(virtualDeltaFrames);
endFrame = static_cast<unsigned>(virtualEndFrame);
while (framesToProcess > 0) {
int framesToEnd = endFrame - readIndex;
int framesThisTime = min(framesToProcess, framesToEnd);
framesThisTime = max(0, framesThisTime);
for (unsigned i = 0; i < numberOfChannels; ++i)
memcpy(destinationChannels[i] + writeIndex, sourceChannels[i] + readIndex, sizeof(float) * framesThisTime);
writeIndex += framesThisTime;
readIndex += framesThisTime;
framesToProcess -= framesThisTime;
if (readIndex >= endFrame) {
readIndex -= deltaFrames;
if (renderSilenceAndFinishIfNotLooping(bus, writeIndex, framesToProcess))
break;
}
}
virtualReadIndex = readIndex;
} else {
while (framesToProcess--) {
unsigned readIndex = static_cast<unsigned>(virtualReadIndex);
double interpolationFactor = virtualReadIndex - readIndex;
unsigned readIndex2 = readIndex + 1;
if (readIndex2 >= bufferLength) {
if (loop()) {
readIndex2 = static_cast<unsigned>(virtualReadIndex + 1 - virtualDeltaFrames);
} else
readIndex2 = readIndex;
}
if (readIndex >= bufferLength || readIndex2 >= bufferLength)
break;
for (unsigned i = 0; i < numberOfChannels; ++i) {
float* destination = destinationChannels[i];
const float* source = sourceChannels[i];
double sample1 = source[readIndex];
double sample2 = source[readIndex2];
double sample = (1.0 - interpolationFactor) * sample1 + interpolationFactor * sample2;
destination[writeIndex] = narrowPrecisionToFloat(sample);
}
writeIndex++;
virtualReadIndex += pitchRate;
if (virtualReadIndex >= virtualEndFrame) {
virtualReadIndex -= virtualDeltaFrames;
if (renderSilenceAndFinishIfNotLooping(bus, writeIndex, framesToProcess))
break;
}
}
}
bus->clearSilentFlag();
m_virtualReadIndex = virtualReadIndex;
return true;
}
void AudioBufferSourceNode::setBuffer(AudioBuffer* buffer, ExceptionState& exceptionState)
{
ASSERT(isMainThread());
if (!buffer) {
exceptionState.throwTypeError("buffer cannot be null");
return;
}
AudioContext::AutoLocker contextLocker(context());
MutexLocker processLocker(m_processLock);
if (buffer) {
unsigned numberOfChannels = buffer->numberOfChannels();
if (numberOfChannels > AudioContext::maxNumberOfChannels()) {
exceptionState.throwTypeError("number of input channels (" + String::number(numberOfChannels)
+ ") exceeds maximum ("
+ String::number(AudioContext::maxNumberOfChannels()) + ").");
return;
}
output(0)->setNumberOfChannels(numberOfChannels);
m_sourceChannels = adoptArrayPtr(new const float* [numberOfChannels]);
m_destinationChannels = adoptArrayPtr(new float* [numberOfChannels]);
for (unsigned i = 0; i < numberOfChannels; ++i)
m_sourceChannels[i] = buffer->getChannelData(i)->data();
}
m_virtualReadIndex = 0;
m_buffer = buffer;
}
unsigned AudioBufferSourceNode::numberOfChannels()
{
return output(0)->numberOfChannels();
}
void AudioBufferSourceNode::start(double when, ExceptionState& exceptionState)
{
AudioScheduledSourceNode::start(when, exceptionState);
}
void AudioBufferSourceNode::start(double when, double grainOffset, ExceptionState& exceptionState)
{
start(when, grainOffset, buffer() ? buffer()->duration() : 0, exceptionState);
}
void AudioBufferSourceNode::start(double when, double grainOffset, double grainDuration, ExceptionState& exceptionState)
{
ASSERT(isMainThread());
if (m_playbackState != UNSCHEDULED_STATE) {
exceptionState.throwDOMException(
InvalidStateError,
"cannot call start more than once.");
return;
}
if (!buffer())
return;
double bufferDuration = buffer()->duration();
grainOffset = max(0.0, grainOffset);
grainOffset = min(bufferDuration, grainOffset);
m_grainOffset = grainOffset;
double maxDuration = bufferDuration - grainOffset;
grainDuration = max(0.0, grainDuration);
grainDuration = min(maxDuration, grainDuration);
m_grainDuration = grainDuration;
m_isGrain = true;
m_startTime = when;
m_virtualReadIndex = AudioUtilities::timeToSampleFrame(m_grainOffset, buffer()->sampleRate());
m_playbackState = SCHEDULED_STATE;
}
void AudioBufferSourceNode::noteGrainOn(double when, double grainOffset, double grainDuration, ExceptionState& exceptionState)
{
if (!grainDuration && buffer())
grainDuration = buffer()->duration();
start(when, grainOffset, grainDuration, exceptionState);
}
double AudioBufferSourceNode::totalPitchRate()
{
double dopplerRate = 1.0;
if (m_pannerNode)
dopplerRate = m_pannerNode->dopplerRate();
double sampleRateFactor = 1.0;
if (buffer())
sampleRateFactor = buffer()->sampleRate() / sampleRate();
double basePitchRate = playbackRate()->value();
double totalRate = dopplerRate * sampleRateFactor * basePitchRate;
totalRate = max(0.0, totalRate);
if (!totalRate)
totalRate = 1;
totalRate = min(MaxRate, totalRate);
bool isTotalRateValid = !std::isnan(totalRate) && !std::isinf(totalRate);
ASSERT(isTotalRateValid);
if (!isTotalRateValid)
totalRate = 1.0;
return totalRate;
}
bool AudioBufferSourceNode::propagatesSilence() const
{
return !isPlayingOrScheduled() || hasFinished() || !m_buffer;
}
void AudioBufferSourceNode::setPannerNode(PannerNode* pannerNode)
{
if (m_pannerNode != pannerNode && !hasFinished()) {
if (pannerNode)
pannerNode->ref(AudioNode::RefTypeConnection);
if (m_pannerNode)
m_pannerNode->deref(AudioNode::RefTypeConnection);
m_pannerNode = pannerNode;
}
}
void AudioBufferSourceNode::clearPannerNode()
{
if (m_pannerNode) {
m_pannerNode->deref(AudioNode::RefTypeConnection);
m_pannerNode = 0;
}
}
void AudioBufferSourceNode::finish()
{
clearPannerNode();
ASSERT(!m_pannerNode);
AudioScheduledSourceNode::finish();
}
}
#endif