Upstream version 5.34.104.0
[platform/framework/web/crosswalk.git] / src / third_party / WebKit / LayoutTests / webaudio / audiobuffersource-playbackrate.html
1 <!DOCTYPE html>
2
3 <!--
4 Tests that AudioBufferSourceNode can playback at different rates properly.
5 Render 72 notes over a 6 octave range.
6 -->
7
8 <html>
9 <head>
10 <script type="text/javascript" src="resources/audio-testing.js"></script>
11 <script type="text/javascript" src="resources/buffer-loader.js"></script>
12
13 </head>
14 <body>
15
16 <script>
17
18 window.onload = init;
19
20 var sampleRate = 44100.0;
21 var numberOfNotes = 72; // play over a 6 octave range
22 var noteDuration = 0.050;
23 var noteSpacing = noteDuration + 0.005; // leave 5ms of silence between each "note"
24 var lengthInSeconds = numberOfNotes * noteSpacing;
25
26 var context = 0;
27 var sinWaveBuffer = 0;
28
29 function createOneCycleSinWaveBuffer(frequency, sampleRate) {
30     var duration = 1 / frequency;
31     var sampleFrameLength = duration * sampleRate;
32     
33     var audioBuffer = context.createBuffer(2, sampleFrameLength, sampleRate);
34
35     var n = audioBuffer.length;
36     var channelL = audioBuffer.getChannelData(0);
37     var channelR = audioBuffer.getChannelData(1);
38
39     for (var i = 0; i < n; ++i) {
40         channelL[i] = Math.sin(frequency * 2.0*Math.PI * i / sampleRate);
41         channelR[i] = channelL[i];
42     }
43
44     return audioBuffer;
45 }
46
47 function playNote(time, duration, playbackRate) {
48     var source = context.createBufferSource();
49     source.buffer = sinWaveBuffer;
50     source.playbackRate.value = playbackRate;
51     
52     var gainNode = context.createGain();
53     source.connect(gainNode);
54     gainNode.connect(context.destination);
55
56     // Loop and play for the given duration.
57     source.loop = true;
58     source.start(time);
59     source.stop(time + duration);
60     
61     // Apply quick fade-in and fade-out to avoid clicks.
62     gainNode.gain.value = 0;
63     gainNode.gain.setValueAtTime(0, time);
64     gainNode.gain.linearRampToValueAtTime(1, time + 0.005);
65     gainNode.gain.setValueAtTime(1, time + duration - 0.005);
66     gainNode.gain.linearRampToValueAtTime(0, time + duration);
67 }
68
69 function init() {
70     if (!window.testRunner)
71         return;
72
73     // Create offline audio context.
74     context = new webkitOfflineAudioContext(2, sampleRate * lengthInSeconds, sampleRate);
75
76     // Create a single cycle of a sine wave.
77     // We'll loop this to play notes of a given duration, at a given playback rate.
78     sinWaveBuffer = createOneCycleSinWaveBuffer(440.0, sampleRate);
79
80     // Play 48 notes over a 4 octave range.
81     for (var i = 0; i < numberOfNotes; ++i) {
82         var time = i * noteSpacing;
83
84         var semitone = i - numberOfNotes/2; // start three octaves down
85         
86         // Convert from semitone to rate.
87         var playbackRate = Math.pow(2, semitone / 12);
88
89         playNote(time, noteDuration, playbackRate);
90     }
91
92     context.oncomplete = finishAudioTest;
93     context.startRendering();
94
95     testRunner.waitUntilDone();
96 }
97
98 </script>
99
100 </body>
101 </html>