Permalink
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
85 lines (61 sloc) 2.14 KB
<!DOCTYPE html>
<!--
Tests that GainNode is properly scaling the gain.
We'll render 11 notes, starting at a gain of 1.0, decreasing in gain by 0.1.
The 11th note will be of gain 0.0, so it should be silent (at the end in the rendered output).
-->
<html>
<head>
<script type="text/javascript" src="resources/audio-testing.js"></script>
</head>
<body>
<script>
window.onload = init;
var sampleRate = 44100.0;
var bufferDurationSeconds = 0.125;
var numberOfNotes = 11;
var noteSpacing = bufferDurationSeconds + 0.020; // leave 20ms of silence between each "note"
var lengthInSeconds = numberOfNotes * noteSpacing;
var context = 0;
var sinWaveBuffer = 0;
function createSinWaveBuffer(lengthInSeconds, frequency) {
var audioBuffer = context.createBuffer(2, lengthInSeconds * sampleRate, sampleRate);
var n = audioBuffer.length;
var channelL = audioBuffer.getChannelData(0);
var channelR = audioBuffer.getChannelData(1);
for (var i = 0; i < n; ++i) {
channelL[i] = Math.sin(frequency * 2.0*Math.PI * i / sampleRate);
channelR[i] = channelL[i];
}
return audioBuffer;
}
function playNote(time, gain) {
var source = context.createBufferSource();
source.buffer = sinWaveBuffer;
var gainNode = context.createGain();
gainNode.gain.value = gain;
source.connect(gainNode);
gainNode.connect(context.destination);
source.start(time);
}
function init() {
if (!window.testRunner)
return;
// Create offline audio context.
context = new webkitOfflineAudioContext(2, sampleRate * lengthInSeconds, sampleRate);
// Create a buffer for a short "note".
sinWaveBuffer = createSinWaveBuffer(bufferDurationSeconds, 880.0);
// Render 11 notes, starting at a gain of 1.0, decreasing in gain by 0.1.
// The last note will be of gain 0.0, so shouldn't be perceptible in the rendered output.
for (var i = 0; i < numberOfNotes; ++i) {
var time = i * noteSpacing;
var gain = 1.0 - i / (numberOfNotes - 1);
playNote(time, gain);
}
context.oncomplete = finishAudioTest;
context.startRendering();
testRunner.waitUntilDone();
}
</script>
</body>
</html>