Refactoring: Pull all fullscreen code out of Document and into its own helper class
[WebKit-https.git] / LayoutTests / webaudio / audioprocessingevent.html
1 <!DOCTYPE html>
2 <html>
3 <head>
4 <script src="../resources/js-test.js"></script>
5 <script type="text/javascript" src="resources/audio-testing.js"></script>
6 </head>
7
8 <body>
9
10 <div id="description"></div>
11 <div id="console"></div>
12
13 <script>
14 description("Tests AudioProcessingEvent attributes in IDL.");
15
16 var sampleRate = 44100.0;
17
18 // For the current implementation of ScriptProcessorNode, when it works with OfflineAudioContext (which runs much faster
19 // than real-time) the event.inputBuffer might be overwrite again before onaudioprocess ever get chance to be called.
20 // We carefully arrange the renderLengthInFrames and bufferSize to have exactly the same value to avoid this issue.
21 var renderLengthInFrames = 512;
22 var bufferSize = 512;
23 var duration = 5;
24 var frequency = 100;
25 var context;
26
27 function processAudioData(event) {
28     shouldBeDefined("event.playbackTime");
29     shouldBeDefined("event.inputBuffer");
30     shouldBeDefined("event.outputBuffer");
31
32     var expectedTime = context.currentTime + (bufferSize / context.sampleRate);
33     var allowedTimeGap = 0.0000001;
34
35     // There may be a little time gap which is from different thread operation
36     // between currentTime when main thread fires onaudioprocess() and currenTime when read in JS
37     // since currentTime is continuously increasing on audio thread.
38     shouldBeCloseTo("event.playbackTime", expectedTime, allowedTimeGap, true);
39 }
40
41 function runTest() {
42     window.jsTestIsAsync = true;
43
44     // Create offline audio context.
45     context = new webkitOfflineAudioContext(2, renderLengthInFrames, sampleRate);
46
47     var oscillator = context.createOscillator();
48
49     oscillator.frequency.value = frequency;
50     oscillator.type = 0;
51
52     var scriptProcessorNode = context.createScriptProcessor(bufferSize, 1, 1);
53
54     oscillator.connect(scriptProcessorNode);
55     scriptProcessorNode.onaudioprocess = processAudioData;
56
57     var gainNode = context.createGain();
58
59     scriptProcessorNode.connect(gainNode);
60     gainNode.connect(context.destination);
61     gainNode.gain.value = 2;
62
63     var startTime = context.currentTime;
64
65     oscillator.start(startTime);
66     oscillator.stop(startTime + duration);
67
68     context.oncomplete = finishJSTest;
69     context.startRendering();
70 }
71
72 runTest();
73
74 </script>
75 </body>
76 </html>