Skip to content

Commit

Permalink
Bug 916285 - Make OscillatorNode handle negative frequencies. r=karlt
Browse files Browse the repository at this point in the history
--HG--
extra : rebase_source : 8db864efa7d7fcfc02d6c66b8b0f050180968ac9
  • Loading branch information
padenot committed Dec 2, 2014
1 parent dc3fa97 commit 9d479b3
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 5 deletions.
10 changes: 5 additions & 5 deletions dom/media/webaudio/OscillatorNode.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -142,8 +142,10 @@ class OscillatorNodeEngine : public AudioNodeEngine
void IncrementPhase()
{
mPhase += mPhaseIncrement;
if (mPhase > mPhaseWrap) {
mPhase -= mPhaseWrap;
if (mPhase > 2 * M_PI) {
mPhase -= 2 * M_PI;
} else if (mPhase < -2 * M_PI) {
mPhase += 2 * M_PI;
}
}

Expand Down Expand Up @@ -171,11 +173,10 @@ class OscillatorNodeEngine : public AudioNodeEngine
detune = mDetune.GetValueAtTime(ticks, count);
}

float signalPeriod = mSource->SampleRate() / mFinalFrequency;
mFinalFrequency = frequency * pow(2., detune / 1200.);
float signalPeriod = mSource->SampleRate() / mFinalFrequency;
mRecomputeParameters = false;

mPhaseWrap = 2 * M_PI;
mPhaseIncrement = 2 * M_PI / signalPeriod;
}

Expand Down Expand Up @@ -367,7 +368,6 @@ class OscillatorNodeEngine : public AudioNodeEngine
float mPhase;
float mFinalFrequency;
float mPhaseIncrement;
float mPhaseWrap;
bool mRecomputeParameters;
nsRefPtr<ThreadSharedFloatArrayBufferList> mCustom;
uint32_t mCustomLength;
Expand Down
1 change: 1 addition & 0 deletions dom/media/webaudio/test/mochitest.ini
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,7 @@ skip-if = (toolkit == 'gonk' && !debug)
[test_offlineDestinationChannelCountMore.html]
[test_oscillatorNode.html]
[test_oscillatorNode2.html]
[test_oscillatorNodeNegativeFrequency.html]
[test_oscillatorNodePassThrough.html]
[test_oscillatorNodeStart.html]
[test_oscillatorTypeChange.html]
Expand Down
50 changes: 50 additions & 0 deletions dom/media/webaudio/test/test_oscillatorNodeNegativeFrequency.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
<!DOCTYPE HTML>
<html>
<head>
<title>Test the OscillatorNode when the frequency is negative</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() {

var types = ["sine",
"square",
"sawtooth",
"triangle"];

var finished = 0;
function finish() {
if (++finished == types.length) {
SimpleTest.finish();
}
}

types.forEach(function(t) {
var context = new OfflineAudioContext(1, 256, 44100);
var osc = context.createOscillator();

osc.frequency.value = -440;
osc.type = t;

osc.connect(context.destination);
osc.start();
context.startRendering().then(function(buffer) {
var samples = buffer.getChannelData(0);
// This samples the wave form in the middle of the first period, the value
// should be negative.
ok(samples[Math.floor(44100 / 440 / 4)] < 0., "Phase should be inverted when using a " + t + " waveform");
finish();
});
});
});

</script>
</pre>
</body>
</html>

0 comments on commit 9d479b3

Please sign in to comment.