AudioBufferSourceNode.cpp [plain text]
#include "config.h"
#if ENABLE(WEB_AUDIO)
#include "AudioBufferSourceNode.h"
#include "AudioContext.h"
#include "AudioNodeOutput.h"
#include <algorithm>
#include <wtf/MathExtras.h>
using namespace std;
namespace WebCore {
const double DefaultGrainDuration = 0.020;
PassRefPtr<AudioBufferSourceNode> AudioBufferSourceNode::create(AudioContext* context, double sampleRate)
{
return adoptRef(new AudioBufferSourceNode(context, sampleRate));
}
AudioBufferSourceNode::AudioBufferSourceNode(AudioContext* context, double sampleRate)
: AudioSourceNode(context, sampleRate)
, m_buffer(0)
, m_isPlaying(false)
, m_isLooping(false)
, m_hasFinished(false)
, m_startTime(0.0)
, m_schedulingFrameDelay(0)
, m_readIndex(0)
, m_isGrain(false)
, m_grainOffset(0.0)
, m_grainDuration(DefaultGrainDuration)
, m_grainFrameCount(0)
, m_lastGain(1.0)
, m_pannerNode(0)
{
setType(NodeTypeAudioBufferSource);
m_gain = AudioGain::create("gain", 1.0, 0.0, 1.0);
m_playbackRate = AudioParam::create("playbackRate", 1.0, 0.0, AudioResampler::MaxRate);
addOutput(adoptPtr(new AudioNodeOutput(this, 1)));
initialize();
}
AudioBufferSourceNode::~AudioBufferSourceNode()
{
uninitialize();
}
void AudioBufferSourceNode::process(size_t framesToProcess)
{
AudioBus* outputBus = output(0)->bus();
if (!isInitialized()) {
outputBus->zero();
return;
}
if (m_processLock.tryLock()) {
double sampleRate = this->sampleRate();
double pitchRate = totalPitchRate();
double quantumStartTime = context()->currentTime();
double quantumEndTime = quantumStartTime + framesToProcess / sampleRate;
if (!m_isPlaying || m_hasFinished || !buffer() || m_startTime >= quantumEndTime) {
outputBus->zero();
m_processLock.unlock();
return;
}
m_schedulingFrameDelay = 0;
if (m_startTime >= quantumStartTime) {
ASSERT(m_startTime < quantumEndTime);
double startTimeInQuantum = m_startTime - quantumStartTime;
double startFrameInQuantum = startTimeInQuantum * sampleRate;
m_schedulingFrameDelay = static_cast<int>(pitchRate * startFrameInQuantum);
}
m_resampler.setRate(pitchRate);
m_resampler.process(this, outputBus, framesToProcess);
double totalGain = gain()->value() * m_buffer->gain();
outputBus->copyWithGainFrom(*outputBus, &m_lastGain, totalGain);
m_processLock.unlock();
} else {
outputBus->zero();
}
}
void AudioBufferSourceNode::provideInput(AudioBus* bus, size_t numberOfFrames)
{
ASSERT(context()->isAudioThread());
ASSERT(bus);
ASSERT(buffer());
if (!bus || !buffer())
return;
unsigned numberOfChannels = this->numberOfChannels();
unsigned busNumberOfChannels = bus->numberOfChannels();
bool channelCountGood = numberOfChannels == busNumberOfChannels && (numberOfChannels == 1 || numberOfChannels == 2);
ASSERT(channelCountGood);
if (!channelCountGood)
return;
float* destinationL = bus->channel(0)->data();
ASSERT(destinationL);
if (!destinationL)
return;
float* destinationR = (numberOfChannels < 2) ? 0 : bus->channel(1)->data();
size_t bufferLength = buffer()->length();
double bufferSampleRate = buffer()->sampleRate();
unsigned startFrame = m_isGrain ? static_cast<unsigned>(m_grainOffset * bufferSampleRate) : 0;
unsigned endFrame = m_isGrain ? static_cast<unsigned>(startFrame + m_grainDuration * bufferSampleRate) : bufferLength;
if (m_isGrain)
endFrame += 512;
if (startFrame >= bufferLength)
startFrame = !bufferLength ? 0 : bufferLength - 1;
if (endFrame > bufferLength)
endFrame = bufferLength;
if (m_readIndex >= endFrame)
m_readIndex = startFrame;
int framesToProcess = numberOfFrames;
if (m_schedulingFrameDelay > 0) {
ASSERT(m_schedulingFrameDelay <= framesToProcess);
if (m_schedulingFrameDelay <= framesToProcess) {
memset(destinationL, 0, sizeof(float) * m_schedulingFrameDelay);
destinationL += m_schedulingFrameDelay;
if (destinationR) {
memset(destinationR, 0, sizeof(float) * m_schedulingFrameDelay);
destinationR += m_schedulingFrameDelay;
}
framesToProcess -= m_schedulingFrameDelay;
}
}
while (framesToProcess > 0) {
ASSERT(m_readIndex <= endFrame);
if (m_readIndex > endFrame)
return;
int framesAvailable = endFrame - m_readIndex;
int framesThisTime = min(framesToProcess, framesAvailable);
AudioBus currentDestinationBus(busNumberOfChannels, framesThisTime, false);
currentDestinationBus.setChannelMemory(0, destinationL, framesThisTime);
if (busNumberOfChannels > 1)
currentDestinationBus.setChannelMemory(1, destinationR, framesThisTime);
readFromBuffer(¤tDestinationBus, framesThisTime);
destinationL += framesThisTime;
if (busNumberOfChannels > 1)
destinationR += framesThisTime;
framesToProcess -= framesThisTime;
if (m_readIndex >= endFrame) {
m_readIndex = startFrame;
m_grainFrameCount = 0;
if (!looping()) {
m_isPlaying = false;
if (framesToProcess > 0) {
memset(destinationL, 0, sizeof(float) * framesToProcess);
if (destinationR)
memset(destinationR, 0, sizeof(float) * framesToProcess);
}
if (!m_hasFinished) {
context()->notifyNodeFinishedProcessing(this);
m_hasFinished = true;
}
return;
}
}
}
}
void AudioBufferSourceNode::readFromBuffer(AudioBus* destinationBus, size_t framesToProcess)
{
bool isBusGood = destinationBus && destinationBus->length() == framesToProcess && destinationBus->numberOfChannels() == numberOfChannels();
ASSERT(isBusGood);
if (!isBusGood)
return;
unsigned numberOfChannels = this->numberOfChannels();
bool channelCountGood = numberOfChannels == 1 || numberOfChannels == 2;
ASSERT(channelCountGood);
if (!channelCountGood)
return;
float* sourceL = m_buffer->getChannelData(0)->data();
float* sourceR = m_buffer->numberOfChannels() == 2 ? m_buffer->getChannelData(1)->data() : 0;
bool isSourceGood = sourceL && (numberOfChannels == 1 || sourceR) && m_readIndex + framesToProcess <= m_buffer->length();
ASSERT(isSourceGood);
if (!isSourceGood)
return;
sourceL += m_readIndex;
sourceR += m_readIndex;
float* destinationL = destinationBus->channel(0)->data();
float* destinationR = numberOfChannels == 2 ? destinationBus->channel(1)->data() : 0;
bool isDestinationGood = destinationL && (numberOfChannels == 1 || destinationR);
ASSERT(isDestinationGood);
if (!isDestinationGood)
return;
if (m_isGrain)
readFromBufferWithGrainEnvelope(sourceL, sourceR, destinationL, destinationR, framesToProcess);
else {
memcpy(destinationL, sourceL, sizeof(float) * framesToProcess);
if (numberOfChannels == 2)
memcpy(destinationR, sourceR, sizeof(float) * framesToProcess);
}
m_readIndex += framesToProcess;
}
void AudioBufferSourceNode::readFromBufferWithGrainEnvelope(float* sourceL, float* sourceR, float* destinationL, float* destinationR, size_t framesToProcess)
{
ASSERT(sourceL && destinationL);
if (!sourceL || !destinationL)
return;
int grainFrameLength = static_cast<int>(m_grainDuration * m_buffer->sampleRate());
bool isStereo = sourceR && destinationR;
int n = framesToProcess;
while (n--) {
float x = static_cast<float>(m_grainFrameCount) / static_cast<float>(grainFrameLength);
m_grainFrameCount++;
x = min(1.0f, x);
float grainEnvelope = sinf(piFloat * x);
*destinationL++ = grainEnvelope * *sourceL++;
if (isStereo)
*destinationR++ = grainEnvelope * *sourceR++;
}
}
void AudioBufferSourceNode::reset()
{
m_resampler.reset();
m_readIndex = 0;
m_grainFrameCount = 0;
m_lastGain = gain()->value();
}
void AudioBufferSourceNode::setBuffer(AudioBuffer* buffer)
{
ASSERT(isMainThread());
AudioContext::AutoLocker contextLocker(context());
MutexLocker processLocker(m_processLock);
if (buffer) {
unsigned numberOfChannels = buffer->numberOfChannels();
m_resampler.configureChannels(numberOfChannels);
output(0)->setNumberOfChannels(numberOfChannels);
}
m_readIndex = 0;
m_buffer = buffer;
}
unsigned AudioBufferSourceNode::numberOfChannels()
{
return output(0)->numberOfChannels();
}
void AudioBufferSourceNode::noteOn(double when)
{
ASSERT(isMainThread());
if (m_isPlaying)
return;
m_isGrain = false;
m_startTime = when;
m_readIndex = 0;
m_isPlaying = true;
}
void AudioBufferSourceNode::noteGrainOn(double when, double grainOffset, double grainDuration)
{
ASSERT(isMainThread());
if (m_isPlaying)
return;
if (!buffer())
return;
double bufferDuration = buffer()->duration();
if (grainDuration > bufferDuration)
return;
double maxGrainOffset = bufferDuration - grainDuration;
maxGrainOffset = max(0.0, maxGrainOffset);
grainOffset = max(0.0, grainOffset);
grainOffset = min(maxGrainOffset, grainOffset);
m_grainOffset = grainOffset;
m_grainDuration = grainDuration;
m_grainFrameCount = 0;
m_isGrain = true;
m_startTime = when;
m_readIndex = static_cast<int>(m_grainOffset * buffer()->sampleRate());
m_isPlaying = true;
}
void AudioBufferSourceNode::noteOff(double)
{
ASSERT(isMainThread());
if (!m_isPlaying)
return;
m_isPlaying = false;
m_readIndex = 0;
}
double AudioBufferSourceNode::totalPitchRate()
{
double dopplerRate = 1.0;
if (m_pannerNode.get())
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);
totalRate = min(AudioResampler::MaxRate, totalRate);
bool isTotalRateValid = !isnan(totalRate) && !isinf(totalRate);
ASSERT(isTotalRateValid);
if (!isTotalRateValid)
totalRate = 1.0;
return totalRate;
}
}
#endif // ENABLE(WEB_AUDIO)