#include "config.h"
#if ENABLE(WEB_AUDIO)
#include "AudioParam.h"
#include "AudioNode.h"
#include "AudioNodeOutput.h"
#include "AudioUtilities.h"
#include "FloatConversion.h"
#include <wtf/MathExtras.h>
namespace WebCore {
const double AudioParam::DefaultSmoothingConstant = 0.05;
const double AudioParam::SnapThreshold = 0.001;
float AudioParam::value()
{
if (context() && context()->isAudioThread()) {
bool hasValue;
float timelineValue = m_timeline.valueForContextTime(context(), narrowPrecisionToFloat(m_value), hasValue);
if (hasValue)
m_value = timelineValue;
}
return narrowPrecisionToFloat(m_value);
}
void AudioParam::setValue(float value)
{
if (!isnan(value) && !isinf(value))
m_value = value;
}
float AudioParam::smoothedValue()
{
return narrowPrecisionToFloat(m_smoothedValue);
}
bool AudioParam::smooth()
{
bool useTimelineValue = false;
if (context())
m_value = m_timeline.valueForContextTime(context(), narrowPrecisionToFloat(m_value), useTimelineValue);
if (m_smoothedValue == m_value) {
return true;
}
if (useTimelineValue)
m_smoothedValue = m_value;
else {
m_smoothedValue += (m_value - m_smoothedValue) * m_smoothingConstant;
if (fabs(m_smoothedValue - m_value) < SnapThreshold) m_smoothedValue = m_value;
}
return false;
}
void AudioParam::calculateSampleAccurateValues(float* values, unsigned numberOfValues)
{
bool isSafe = context() && context()->isAudioThread() && values && numberOfValues;
ASSERT(isSafe);
if (!isSafe)
return;
if (m_audioRateSignal)
calculateAudioRateSignalValues(values, numberOfValues);
else
calculateTimelineValues(values, numberOfValues);
}
void AudioParam::calculateAudioRateSignalValues(float* values, unsigned numberOfValues)
{
ASSERT(m_audioRateSignal);
AudioBus* bus = m_audioRateSignal->pull(0, numberOfValues);
bool isBusGood = bus && bus->numberOfChannels() && bus->length() >= numberOfValues;
ASSERT(isBusGood);
if (!isBusGood)
return;
if (bus->numberOfChannels() == 1) {
memcpy(values, bus->channel(0)->data(), sizeof(float) * numberOfValues);
} else {
AudioBus wrapperBus(1, numberOfValues, false);
wrapperBus.setChannelMemory(0, values, numberOfValues);
wrapperBus.copyFrom(*bus); }
m_value = values[0]; }
void AudioParam::calculateTimelineValues(float* values, unsigned numberOfValues)
{
float sampleRate = context()->sampleRate();
float startTime = narrowPrecisionToFloat(context()->currentTime());
float endTime = startTime + numberOfValues / sampleRate;
m_value = m_timeline.valuesForTimeRange(startTime, endTime, narrowPrecisionToFloat(m_value), values, numberOfValues, sampleRate, sampleRate);
}
void AudioParam::connect(AudioNodeOutput* audioRateSignal)
{
ASSERT(context()->isGraphOwner());
ASSERT(audioRateSignal);
if (!audioRateSignal)
return;
if (m_audioRateSignal && m_audioRateSignal != audioRateSignal) {
m_audioRateSignal->removeParam(this);
}
audioRateSignal->addParam(this);
m_audioRateSignal = audioRateSignal;
}
void AudioParam::disconnect(AudioNodeOutput* audioRateSignal)
{
ASSERT(context()->isGraphOwner());
ASSERT(audioRateSignal);
if (!audioRateSignal)
return;
if (m_audioRateSignal == audioRateSignal)
m_audioRateSignal = 0;
}
}
#endif // ENABLE(WEB_AUDIO)