Bug 868864 - Correctly scale the time values of AudioParam when converting it to ticks, and correctly calculate SetTargetAtTime event values the first time they're encountered; r=roc

This commit is contained in:
Ehsan Akhgari 2013-05-06 14:59:43 -04:00
parent a9f136b2ec
commit c3c3b0028e
4 changed files with 80 additions and 2 deletions

View File

@ -226,6 +226,16 @@ public:
++i;
} while (i < mEvents.Length() &&
aTime == mEvents[i].template Time<TimeType>());
// SetTarget nodes can be handled no matter what their next node is (if they have one)
if (mEvents[i - 1].mType == AudioTimelineEvent::SetTarget) {
// Follow the curve, without regard to the next node
return ExponentialApproach(mEvents[i - 1].template Time<TimeType>(),
mValue, mEvents[i - 1].mValue,
mEvents[i - 1].mTimeConstant, aTime);
}
// For other event types
return mEvents[i - 1].mValue;
}
previous = next;
@ -335,10 +345,13 @@ public:
return v1 + (v0 - v1) * expf(-(t - t0) / timeConstant);
}
void ConvertEventTimesToTicks(int64_t (*aConvertor)(double aTime, void* aClosure), void* aClosure)
void ConvertEventTimesToTicks(int64_t (*aConvertor)(double aTime, void* aClosure), void* aClosure,
int32_t aSampleRate)
{
for (unsigned i = 0; i < mEvents.Length(); ++i) {
mEvents[i].SetTimeInTicks(aConvertor(mEvents[i].template Time<double>(), aClosure));
mEvents[i].mTimeConstant *= aSampleRate;
mEvents[i].mDuration *= aSampleRate;
}
}

View File

@ -53,7 +53,7 @@ WebAudioUtils::ConvertAudioParamToTicks(AudioParamTimeline& aParam,
ConvertTimeToTickHelper ctth;
ctth.mSourceStream = aSource;
ctth.mDestinationStream = aDest;
aParam.ConvertEventTimesToTicks(ConvertTimeToTickHelper::Convert, &ctth);
aParam.ConvertEventTimesToTicks(ConvertTimeToTickHelper::Convert, &ctth, IdealAudioRate());
}
}

View File

@ -29,6 +29,7 @@ MOCHITEST_FILES := \
test_AudioParam.html \
test_audioParamExponentialRamp.html \
test_audioParamLinearRamp.html \
test_audioParamSetTargetAtTime.html \
test_audioBufferSourceNode.html \
test_audioBufferSourceNodeLazyLoopParam.html \
test_audioBufferSourceNodeLoop.html \

View File

@ -0,0 +1,64 @@
<!DOCTYPE HTML>
<html>
<head>
<title>Test AudioParam.setTargetAtTime</title>
<script type="text/javascript" src="/tests/SimpleTest/SimpleTest.js"></script>
<script type="text/javascript" src="webaudio.js"></script>
<link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css" />
</head>
<body>
<pre id="test">
<script class="testbody" type="text/javascript">
SimpleTest.waitForExplicitFinish();
addLoadEvent(function() {
SpecialPowers.setBoolPref("media.webaudio.enabled", true);
var context = new AudioContext();
var V0 = 0.1;
var V1 = 0.9;
var T0 = 0;
var T1 = 2048 / context.sampleRate;
var TimeConstant = 10;
var sourceBuffer = context.createBuffer(1, 2048, context.sampleRate);
for (var i = 0; i < 2048; ++i) {
sourceBuffer.getChannelData(0)[i] = 1;
}
var expectedBuffer = context.createBuffer(1, 2048, context.sampleRate);
for (var i = 0; i < 2048; ++i) {
var t = i / context.sampleRate;
expectedBuffer.getChannelData(0)[i] = V1 + (V0 - V1) * Math.exp(-(t - T0) / TimeConstant);
}
var destination = context.destination;
var source = context.createBufferSource();
source.buffer = sourceBuffer;
var gain = context.createGain();
gain.gain.value = V0;
gain.gain.setTargetAtTime(V1, T0, TimeConstant);
var sp = context.createScriptProcessor(2048, 1);
source.connect(gain);
gain.connect(sp);
sp.connect(destination);
source.start(0);
sp.onaudioprocess = function(e) {
is(e.inputBuffer.numberOfChannels, 1, "Correct input channel count");
compareBuffers(e.inputBuffer.getChannelData(0), expectedBuffer.getChannelData(0));
sp.onaudioprocess = null;
SpecialPowers.clearUserPref("media.webaudio.enabled");
SimpleTest.finish();
};
});
</script>
</pre>
</body>
</html>