This source file includes following definitions.
- create
- m_detuneValues
- type
- setType
- setType
- calculateSampleAccuratePhaseIncrements
- process
- setPeriodicWave
- propagatesSilence
#include "config.h"
#if ENABLE(WEB_AUDIO)
#include "modules/webaudio/OscillatorNode.h"
#include "platform/audio/AudioUtilities.h"
#include "platform/audio/VectorMath.h"
#include "modules/webaudio/AudioContext.h"
#include "modules/webaudio/AudioNodeOutput.h"
#include "modules/webaudio/PeriodicWave.h"
#include "wtf/MathExtras.h"
#include "wtf/StdLibExtras.h"
#include <algorithm>
using namespace std;
namespace WebCore {
using namespace VectorMath;
PassRefPtr<OscillatorNode> OscillatorNode::create(AudioContext* context, float sampleRate)
{
return adoptRef(new OscillatorNode(context, sampleRate));
}
OscillatorNode::OscillatorNode(AudioContext* context, float sampleRate)
: AudioScheduledSourceNode(context, sampleRate)
, m_type(SINE)
, m_firstRender(true)
, m_virtualReadIndex(0)
, m_phaseIncrements(AudioNode::ProcessingSizeInFrames)
, m_detuneValues(AudioNode::ProcessingSizeInFrames)
{
ScriptWrappable::init(this);
setNodeType(NodeTypeOscillator);
m_frequency = AudioParam::create(context, "frequency", 440, 0, 100000);
m_detune = AudioParam::create(context, "detune", 0, -4800, 4800);
setType(m_type);
addOutput(adoptPtr(new AudioNodeOutput(this, 1)));
initialize();
}
OscillatorNode::~OscillatorNode()
{
uninitialize();
}
String OscillatorNode::type() const
{
switch (m_type) {
case SINE:
return "sine";
case SQUARE:
return "square";
case SAWTOOTH:
return "sawtooth";
case TRIANGLE:
return "triangle";
case CUSTOM:
return "custom";
default:
ASSERT_NOT_REACHED();
return "custom";
}
}
void OscillatorNode::setType(const String& type)
{
if (type == "sine")
setType(SINE);
else if (type == "square")
setType(SQUARE);
else if (type == "sawtooth")
setType(SAWTOOTH);
else if (type == "triangle")
setType(TRIANGLE);
else
ASSERT_NOT_REACHED();
}
bool OscillatorNode::setType(unsigned type)
{
PeriodicWave* periodicWave = 0;
float sampleRate = this->sampleRate();
switch (type) {
case SINE: {
DEFINE_STATIC_REF(PeriodicWave, periodicWaveSine, (PeriodicWave::createSine(sampleRate)));
periodicWave = periodicWaveSine;
break;
}
case SQUARE: {
DEFINE_STATIC_REF(PeriodicWave, periodicWaveSquare, (PeriodicWave::createSquare(sampleRate)));
periodicWave = periodicWaveSquare;
break;
}
case SAWTOOTH: {
DEFINE_STATIC_REF(PeriodicWave, periodicWaveSawtooth, (PeriodicWave::createSawtooth(sampleRate)));
periodicWave = periodicWaveSawtooth;
break;
}
case TRIANGLE: {
DEFINE_STATIC_REF(PeriodicWave, periodicWaveTriangle, (PeriodicWave::createTriangle(sampleRate)));
periodicWave = periodicWaveTriangle;
break;
}
case CUSTOM:
default:
return false;
}
setPeriodicWave(periodicWave);
m_type = type;
return true;
}
bool OscillatorNode::calculateSampleAccuratePhaseIncrements(size_t framesToProcess)
{
bool isGood = framesToProcess <= m_phaseIncrements.size() && framesToProcess <= m_detuneValues.size();
ASSERT(isGood);
if (!isGood)
return false;
if (m_firstRender) {
m_firstRender = false;
m_frequency->resetSmoothedValue();
m_detune->resetSmoothedValue();
}
bool hasSampleAccurateValues = false;
bool hasFrequencyChanges = false;
float* phaseIncrements = m_phaseIncrements.data();
float finalScale = m_periodicWave->rateScale();
if (m_frequency->hasSampleAccurateValues()) {
hasSampleAccurateValues = true;
hasFrequencyChanges = true;
m_frequency->calculateSampleAccurateValues(phaseIncrements, framesToProcess);
} else {
m_frequency->smooth();
float frequency = m_frequency->smoothedValue();
finalScale *= frequency;
}
if (m_detune->hasSampleAccurateValues()) {
hasSampleAccurateValues = true;
float* detuneValues = hasFrequencyChanges ? m_detuneValues.data() : phaseIncrements;
m_detune->calculateSampleAccurateValues(detuneValues, framesToProcess);
float k = 1.0 / 1200;
vsmul(detuneValues, 1, &k, detuneValues, 1, framesToProcess);
for (unsigned i = 0; i < framesToProcess; ++i)
detuneValues[i] = powf(2, detuneValues[i]);
if (hasFrequencyChanges) {
vmul(detuneValues, 1, phaseIncrements, 1, phaseIncrements, 1, framesToProcess);
}
} else {
m_detune->smooth();
float detune = m_detune->smoothedValue();
float detuneScale = powf(2, detune / 1200);
finalScale *= detuneScale;
}
if (hasSampleAccurateValues) {
vsmul(phaseIncrements, 1, &finalScale, phaseIncrements, 1, framesToProcess);
}
return hasSampleAccurateValues;
}
void OscillatorNode::process(size_t framesToProcess)
{
AudioBus* outputBus = output(0)->bus();
if (!isInitialized() || !outputBus->numberOfChannels()) {
outputBus->zero();
return;
}
ASSERT(framesToProcess <= m_phaseIncrements.size());
if (framesToProcess > m_phaseIncrements.size())
return;
MutexTryLocker tryLocker(m_processLock);
if (!tryLocker.locked()) {
outputBus->zero();
return;
}
if (!m_periodicWave.get()) {
outputBus->zero();
return;
}
size_t quantumFrameOffset;
size_t nonSilentFramesToProcess;
updateSchedulingInfo(framesToProcess, outputBus, quantumFrameOffset, nonSilentFramesToProcess);
if (!nonSilentFramesToProcess) {
outputBus->zero();
return;
}
unsigned periodicWaveSize = m_periodicWave->periodicWaveSize();
double invPeriodicWaveSize = 1.0 / periodicWaveSize;
float* destP = outputBus->channel(0)->mutableData();
ASSERT(quantumFrameOffset <= framesToProcess);
double virtualReadIndex = m_virtualReadIndex;
float rateScale = m_periodicWave->rateScale();
float invRateScale = 1 / rateScale;
bool hasSampleAccurateValues = calculateSampleAccuratePhaseIncrements(framesToProcess);
float frequency = 0;
float* higherWaveData = 0;
float* lowerWaveData = 0;
float tableInterpolationFactor;
if (!hasSampleAccurateValues) {
frequency = m_frequency->smoothedValue();
float detune = m_detune->smoothedValue();
float detuneScale = powf(2, detune / 1200);
frequency *= detuneScale;
m_periodicWave->waveDataForFundamentalFrequency(frequency, lowerWaveData, higherWaveData, tableInterpolationFactor);
}
float incr = frequency * rateScale;
float* phaseIncrements = m_phaseIncrements.data();
unsigned readIndexMask = periodicWaveSize - 1;
destP += quantumFrameOffset;
int n = nonSilentFramesToProcess;
while (n--) {
unsigned readIndex = static_cast<unsigned>(virtualReadIndex);
unsigned readIndex2 = readIndex + 1;
readIndex = readIndex & readIndexMask;
readIndex2 = readIndex2 & readIndexMask;
if (hasSampleAccurateValues) {
incr = *phaseIncrements++;
frequency = invRateScale * incr;
m_periodicWave->waveDataForFundamentalFrequency(frequency, lowerWaveData, higherWaveData, tableInterpolationFactor);
}
float sample1Lower = lowerWaveData[readIndex];
float sample2Lower = lowerWaveData[readIndex2];
float sample1Higher = higherWaveData[readIndex];
float sample2Higher = higherWaveData[readIndex2];
float interpolationFactor = static_cast<float>(virtualReadIndex) - readIndex;
float sampleHigher = (1 - interpolationFactor) * sample1Higher + interpolationFactor * sample2Higher;
float sampleLower = (1 - interpolationFactor) * sample1Lower + interpolationFactor * sample2Lower;
float sample = (1 - tableInterpolationFactor) * sampleHigher + tableInterpolationFactor * sampleLower;
*destP++ = sample;
virtualReadIndex += incr;
virtualReadIndex -= floor(virtualReadIndex * invPeriodicWaveSize) * periodicWaveSize;
}
m_virtualReadIndex = virtualReadIndex;
outputBus->clearSilentFlag();
}
void OscillatorNode::setPeriodicWave(PeriodicWave* periodicWave)
{
ASSERT(isMainThread());
MutexLocker processLocker(m_processLock);
m_periodicWave = periodicWave;
m_type = CUSTOM;
}
bool OscillatorNode::propagatesSilence() const
{
return !isPlayingOrScheduled() || hasFinished() || !m_periodicWave.get();
}
}
#endif