Upstream version 5.34.104.0
[platform/framework/web/crosswalk.git] / src / third_party / WebKit / LayoutTests / webaudio / gain.html
1 <!DOCTYPE html>
2
3 <!--
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).
7 -->
8
9 <html>
10 <head>
11 <script type="text/javascript" src="resources/audio-testing.js"></script>
12
13 </head>
14 <body>
15
16 <script>
17
18 window.onload = init;
19
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;
25
26 var context = 0;
27 var sinWaveBuffer = 0;
28
29 function createSinWaveBuffer(lengthInSeconds, frequency) {
30     var audioBuffer = context.createBuffer(2, lengthInSeconds * sampleRate, sampleRate);
31
32     var n = audioBuffer.length;
33     var channelL = audioBuffer.getChannelData(0);
34     var channelR = audioBuffer.getChannelData(1);
35
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];
39     }
40
41     return audioBuffer;
42 }
43
44 function playNote(time, gain) {
45     var source = context.createBufferSource();
46     source.buffer = sinWaveBuffer;
47
48     var gainNode = context.createGain();
49     gainNode.gain.value = gain;
50
51     source.connect(gainNode);
52     gainNode.connect(context.destination);
53
54     source.start(time);
55 }
56
57 function init() {
58     if (!window.testRunner)
59         return;
60
61     // Create offline audio context.
62     context = new webkitOfflineAudioContext(2, sampleRate * lengthInSeconds, sampleRate);
63
64     // Create a buffer for a short "note".
65     sinWaveBuffer = createSinWaveBuffer(bufferDurationSeconds, 880.0);
66
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);
72         playNote(time, gain);
73     }
74
75     context.oncomplete = finishAudioTest;
76     context.startRendering();
77
78     testRunner.waitUntilDone();
79 }
80
81 </script>
82
83 </body>
84 </html>