Bug 1153783 - Implement the detune AudioParam for the AudioBufferSourceNode. r=ehsan

This commit is contained in:
Paul Adenot 2015-04-14 17:03:50 +02:00
parent 0a319b6e65
commit 9ac2974974
6 changed files with 128 additions and 8 deletions

View File

@ -14,6 +14,7 @@
#include "AudioDestinationNode.h"
#include "AudioParamTimeline.h"
#include <limits>
#include <algorithm>
namespace mozilla {
namespace dom {
@ -23,6 +24,7 @@ NS_IMPL_CYCLE_COLLECTION_CLASS(AudioBufferSourceNode)
NS_IMPL_CYCLE_COLLECTION_UNLINK_BEGIN(AudioBufferSourceNode)
NS_IMPL_CYCLE_COLLECTION_UNLINK(mBuffer)
NS_IMPL_CYCLE_COLLECTION_UNLINK(mPlaybackRate)
NS_IMPL_CYCLE_COLLECTION_UNLINK(mDetune)
if (tmp->Context()) {
// AudioNode's Unlink implementation disconnects us from the graph
// too, but we need to do this right here to make sure that
@ -36,6 +38,7 @@ NS_IMPL_CYCLE_COLLECTION_UNLINK_END_INHERITED(AudioNode)
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_BEGIN_INHERITED(AudioBufferSourceNode, AudioNode)
NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mBuffer)
NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mPlaybackRate)
NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mDetune)
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_END
NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION_INHERITED(AudioBufferSourceNode)
@ -64,7 +67,9 @@ public:
mBufferSampleRate(0), mBufferPosition(0), mChannels(0),
mDopplerShift(1.0f),
mDestination(static_cast<AudioNodeStream*>(aDestination->Stream())),
mPlaybackRateTimeline(1.0f), mLoop(false)
mPlaybackRateTimeline(1.0f),
mDetuneTimeline(0.0f),
mLoop(false)
{}
~AudioBufferSourceNodeEngine()
@ -88,6 +93,10 @@ public:
mPlaybackRateTimeline = aValue;
WebAudioUtils::ConvertAudioParamToTicks(mPlaybackRateTimeline, mSource, mDestination);
break;
case AudioBufferSourceNode::DETUNE:
mDetuneTimeline = aValue;
WebAudioUtils::ConvertAudioParamToTicks(mDetuneTimeline, mSource, mDestination);
break;
default:
NS_ERROR("Bad AudioBufferSourceNodeEngine TimelineParameter");
}
@ -226,7 +235,7 @@ public:
}
// Resamples input data to an output buffer, according to |mBufferSampleRate| and
// the playbackRate.
// the playbackRate/detune.
// The number of frames consumed/produced depends on the amount of space
// remaining in both the input and output buffer, and the playback rate (that
// is, the ratio between the output samplerate and the input samplerate).
@ -397,30 +406,39 @@ public:
}
}
int32_t ComputeFinalOutSampleRate(float aPlaybackRate)
int32_t ComputeFinalOutSampleRate(float aPlaybackRate, float aDetune)
{
float computedPlaybackRate = aPlaybackRate * pow(2, aDetune / 1200.f);
// Make sure the playback rate and the doppler shift are something
// our resampler can work with.
int32_t rate = WebAudioUtils::
TruncateFloatToInt<int32_t>(mSource->SampleRate() /
(aPlaybackRate * mDopplerShift));
(computedPlaybackRate * mDopplerShift));
return rate ? rate : mBufferSampleRate;
}
void UpdateSampleRateIfNeeded(uint32_t aChannels)
{
float playbackRate;
float detune;
if (mPlaybackRateTimeline.HasSimpleValue()) {
playbackRate = mPlaybackRateTimeline.GetValue();
} else {
playbackRate = mPlaybackRateTimeline.GetValueAtTime(mSource->GetCurrentPosition());
}
if (mDetuneTimeline.HasSimpleValue()) {
detune = mDetuneTimeline.GetValue();
} else {
detune = mDetuneTimeline.GetValueAtTime(mSource->GetCurrentPosition());
}
if (playbackRate <= 0 || mozilla::IsNaN(playbackRate)) {
playbackRate = 1.0f;
}
int32_t outRate = ComputeFinalOutSampleRate(playbackRate);
detune = std::min(std::max(-1200.f, detune), 1200.f);
int32_t outRate = ComputeFinalOutSampleRate(playbackRate, detune);
UpdateResampler(outRate, aChannels);
}
@ -440,9 +458,6 @@ public:
return;
}
// WebKit treats the playbackRate as a k-rate parameter in their code,
// despite the spec saying that it should be an a-rate parameter. We treat
// it as k-rate. Spec bug: https://www.w3.org/Bugs/Public/show_bug.cgi?id=21592
UpdateSampleRateIfNeeded(channels);
uint32_t written = 0;
@ -488,6 +503,7 @@ public:
// Not owned:
// - mBuffer - shared w/ AudioNode
// - mPlaybackRateTimeline - shared w/ AudioNode
// - mDetuneTimeline - shared w/ AudioNode
size_t amount = AudioNodeEngine::SizeOfExcludingThis(aMallocSizeOf);
@ -530,6 +546,7 @@ public:
AudioNodeStream* mDestination;
AudioNodeStream* mSource;
AudioParamTimeline mPlaybackRateTimeline;
AudioParamTimeline mDetuneTimeline;
bool mLoop;
};
@ -542,6 +559,7 @@ AudioBufferSourceNode::AudioBufferSourceNode(AudioContext* aContext)
, mLoopEnd(0.0)
// mOffset and mDuration are initialized in Start().
, mPlaybackRate(new AudioParam(this, SendPlaybackRateToStream, 1.0f))
, mDetune(new AudioParam(this, SendDetuneToStream, 0.0f))
, mLoop(false)
, mStartCalled(false)
, mStopped(false)
@ -568,6 +586,7 @@ AudioBufferSourceNode::SizeOfExcludingThis(MallocSizeOf aMallocSizeOf) const
}
amount += mPlaybackRate->SizeOfIncludingThis(aMallocSizeOf);
amount += mDetune->SizeOfIncludingThis(aMallocSizeOf);
return amount;
}
@ -732,6 +751,13 @@ AudioBufferSourceNode::SendPlaybackRateToStream(AudioNode* aNode)
SendTimelineParameterToStream(This, PLAYBACKRATE, *This->mPlaybackRate);
}
void
AudioBufferSourceNode::SendDetuneToStream(AudioNode* aNode)
{
AudioBufferSourceNode* This = static_cast<AudioBufferSourceNode*>(aNode);
SendTimelineParameterToStream(This, DETUNE, *This->mDetune);
}
void
AudioBufferSourceNode::SendDopplerShiftToStream(double aDopplerShift)
{

View File

@ -59,6 +59,10 @@ public:
{
return mPlaybackRate;
}
AudioParam* Detune() const
{
return mDetune;
}
bool Loop() const
{
return mLoop;
@ -123,6 +127,7 @@ private:
LOOPSTART,
LOOPEND,
PLAYBACKRATE,
DETUNE,
DOPPLERSHIFT
};
@ -130,6 +135,7 @@ private:
void SendBufferParameterToStream(JSContext* aCx);
void SendOffsetAndDurationParametersToStream(AudioNodeStream* aStream);
static void SendPlaybackRateToStream(AudioNode* aNode);
static void SendDetuneToStream(AudioNode* aNode);
private:
double mLoopStart;
@ -138,6 +144,7 @@ private:
double mDuration;
nsRefPtr<AudioBuffer> mBuffer;
nsRefPtr<AudioParam> mPlaybackRate;
nsRefPtr<AudioParam> mDetune;
bool mLoop;
bool mStartCalled;
bool mStopped;

View File

@ -43,6 +43,7 @@ skip-if = (toolkit == 'android' && (processor == 'x86' || debug)) || os == 'win'
[test_audioBufferSourceNodeOffset.html]
skip-if = (toolkit == 'gonk') || (toolkit == 'android') || debug #bug 906752
[test_audioBufferSourceNodePassThrough.html]
[test_audioBufferSourceNodeRate.html]
[test_AudioContext.html]
[test_audioDestinationNode.html]
[test_AudioListener.html]

View File

@ -0,0 +1,66 @@
<!DOCTYPE HTML>
<html>
<head>
<title>Test AudioBufferSourceNode</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();
var rate = 44100;
var off = new OfflineAudioContext(1, rate, rate);
var off2 = new OfflineAudioContext(1, rate, rate);
var source = off.createBufferSource();
var source2 = off2.createBufferSource();
// a buffer of a 440Hz at half the length. If we detune by -1200 or set the
// playbackRate to 0.5, we should get 44100 samples back with a sine at 220Hz.
var buf = off.createBuffer(1, rate / 2, rate);
var bufarray = buf.getChannelData(0);
for (var i = 0; i < bufarray.length; i++) {
bufarray[i] = Math.sin(i * 440 * 2 * Math.PI / rate);
}
source.buffer = buf;
source.playbackRate.value = 0.5; // 50% slowdown
source.connect(off.destination);
source.start(0);
source2.buffer = buf;
source2.detune.value = -1200; // one octave -> 50% slowdown
source2.connect(off2.destination);
source2.start(0);
var finished = 0;
function finish() {
finished++;
if (finished == 2) {
SimpleTest.finish();
}
}
off.startRendering().then((renderedPlaybackRate) => {
// we don't care about comparing the value here, we just want to know whether
// the second part is noisy.
var rmsValue = rms(renderedPlaybackRate, 0, 22050);
ok(rmsValue != 0, "Resampling happened (rms of the second part " + rmsValue + ")");
off2.startRendering().then((renderedDetune) => {
var rmsValue = rms(renderedDetune, 0, 22050);
ok(rmsValue != 0, "Resampling happened (rms of the second part " + rmsValue + ")");
// The two buffers should be the same: detune of -1200 is a 50% slowdown
compareBuffers(renderedPlaybackRate, renderedDetune);
SimpleTest.finish();
});
});
</script>
</pre>
</body>
</html>

View File

@ -93,6 +93,25 @@ function compareBuffers(got, expected) {
}
}
/**
* Compute the root mean square (RMS,
* <http://en.wikipedia.org/wiki/Root_mean_square>) of a channel of a slice
* (defined by `start` and `end`) of an AudioBuffer.
*
* This is useful to detect that a buffer is noisy or silent.
*/
function rms(audiobuffer, channel = 0, start = 0, end = audiobuffer.length) {
var buffer= audiobuffer.getChannelData(channel);
var rms = 0;
for (var i = start; i < end; i++) {
rms += buffer[i] * buffer[i];
}
rms /= buffer.length;
rms = Math.sqrt(rms);
return rms;
}
function getEmptyBuffer(context, length) {
return context.createBuffer(gTest.numberOfChannels, length, context.sampleRate);
}

View File

@ -15,6 +15,7 @@ interface AudioBufferSourceNode : AudioNode {
attribute AudioBuffer? buffer;
readonly attribute AudioParam playbackRate;
readonly attribute AudioParam detune;
attribute boolean loop;
attribute double loopStart;