4 Tests that GainNode is properly scaling the gain.
5 We'll render 11 notes, starting at a gain of 1.0, decreasing in gain by 0.1.
6 The 11th note will be of gain 0.0, so it should be silent (at the end in the rendered output).
11 <script type="text/javascript" src="resources/audio-testing.js"></script>
20 var sampleRate = 44100.0;
21 var bufferDurationSeconds = 0.125;
22 var numberOfNotes = 11;
23 var noteSpacing = bufferDurationSeconds + 0.020; // leave 20ms of silence between each "note"
24 var lengthInSeconds = numberOfNotes * noteSpacing;
27 var sinWaveBuffer = 0;
29 function createSinWaveBuffer(lengthInSeconds, frequency) {
30 var audioBuffer = context.createBuffer(2, lengthInSeconds * sampleRate, sampleRate);
32 var n = audioBuffer.length;
33 var channelL = audioBuffer.getChannelData(0);
34 var channelR = audioBuffer.getChannelData(1);
36 for (var i = 0; i < n; ++i) {
37 channelL[i] = Math.sin(frequency * 2.0*Math.PI * i / sampleRate);
38 channelR[i] = channelL[i];
44 function playNote(time, gain) {
45 var source = context.createBufferSource();
46 source.buffer = sinWaveBuffer;
48 var gainNode = context.createGain();
49 gainNode.gain.value = gain;
51 source.connect(gainNode);
52 gainNode.connect(context.destination);
58 if (!window.testRunner)
61 // Create offline audio context.
62 context = new webkitOfflineAudioContext(2, sampleRate * lengthInSeconds, sampleRate);
64 // Create a buffer for a short "note".
65 sinWaveBuffer = createSinWaveBuffer(bufferDurationSeconds, 880.0);
67 // Render 11 notes, starting at a gain of 1.0, decreasing in gain by 0.1.
68 // The last note will be of gain 0.0, so shouldn't be perceptible in the rendered output.
69 for (var i = 0; i < numberOfNotes; ++i) {
70 var time = i * noteSpacing;
71 var gain = 1.0 - i / (numberOfNotes - 1);
75 context.oncomplete = finishAudioTest;
76 context.startRendering();
78 testRunner.waitUntilDone();