b5d5965bd7830ed4e13d646f97a5cfdd77b0863b
[WebKit-https.git] / LayoutTests / webaudio / note-grain-on-play.html
1 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
2 <html>
3   <head>
4     <script src="resources/audio-testing.js"></script>
5     <script src="resources/note-grain-on-testing.js"></script>
6     <script src="../fast/js/resources/js-test-pre.js"></script>
7   </head>
8
9   <body>
10     <div id="description"></div>
11     <div id="console"></div>
12
13     <script>
14       description("Test noteGrainOn offset rendering.");
15
16       // To test noteGrainOn, a single ramp signal is created.
17       // Various sections of the ramp are rendered by noteGrainOn() at
18       // different times, and we verify that the actual output
19       // consists of the correct section of the ramp at the correct
20       // time.
21       
22       var linearRampBuffer;
23
24       // Array of the grain offset used for each ramp played.
25       var grainOffsetTime = [];
26
27       // Verify the received signal is a ramp from the correct section
28       // of our ramp signal.
29       function verifyGrain(renderedData, startFrame, endFrame, grainIndex) {
30           var grainOffsetFrame = timeToSampleFrame(grainOffsetTime[grainIndex], sampleRate);
31           var grainFrameLength = endFrame - startFrame;
32           var ramp = linearRampBuffer.getChannelData(0);
33           var isCorrect = true;
34
35           var expected;
36           var actual;
37           var frame;
38       
39           for (var k = 0; k < grainFrameLength; ++k) {
40               if (renderedData[startFrame + k] != ramp[grainOffsetFrame + k]) {
41                   expected = ramp[grainOffsetFrame + k];
42                   actual = renderedData[startFrame + k];
43                   frame = startFrame + k;
44                   isCorrect = false;
45                   break;
46               }
47           }
48           return { verified: isCorrect,
49                    expected : expected ,
50                    actual : actual,
51                    frame : frame };
52       }
53       
54       function checkResult(event) {
55           var buffer = event.renderedBuffer;
56           renderedData = buffer.getChannelData(0);
57           var nSamples = renderedData.length;
58
59           var success = true;
60
61           // Number of grains that we found that have incorrect data.
62           var invalidGrainDataCount = 0;
63       
64           var startEndFrames = findStartAndEndSamples(renderedData);
65
66           // Verify the start and stop times.  Not strictly needed for
67           // this test, but it's useful to know that if the ramp data
68           // appears to be incorrect.
69           success = success && verifyStartAndEndFrames(startEndFrames);
70
71           // Loop through each of the rendered grains and check that
72           // each grain contains our expected ramp.
73           for (var k = 0; k < startEndFrames.start.length; ++k) {
74               // Verify that the rendered data matches the expected
75               // section of our ramp signal.
76               var result = verifyGrain(renderedData, startEndFrames.start[k], startEndFrames.end[k], k);
77
78               if (!result.verified) {
79                   testFailed("Grain " + k + " incorrect.  Expected " + result.expected + " but received " + result.actual + " at sample frame " + result.frame);
80                   ++invalidGrainDataCount;
81                   success = false;
82               }
83           }
84           
85           if (!invalidGrainDataCount) {
86               testPassed("All " + numberOfTests + " grains contained the correct data.");
87           } else {
88               testFailed(invalidGrainDataCount + " grains out of " + numberOfTests + " did not contain the expected data.");
89               success = false;
90           }
91       
92           if (success) {
93               testPassed("noteGrainOn offset rendering tests passed.");
94           } else {
95               testFailed("noteGrainOn offset rendering tests failed.");
96           }
97
98           finishJSTest();
99       }
100
101       function runTest() {
102           if (window.testRunner) {
103               testRunner.dumpAsText();
104               testRunner.waitUntilDone();
105           }
106
107           window.jsTestIsAsync = true;
108
109           // Create offline audio context.
110           context = new webkitAudioContext(2, sampleRate * renderTime, sampleRate);
111
112           // Create a linear ramp for testing noteGrainOn.
113           linearRampBuffer = createSignalBuffer(context,
114                                                 function(k) {
115                                                     // Want the ramp to start
116                                                     // with 1, not 0.
117                                                     return k + 1;
118                                                 });    
119
120           var grainInfo = playAllGrains(context, linearRampBuffer, numberOfTests);
121
122           grainOffsetTime = grainInfo.grainOffsetTimes;
123
124           context.oncomplete = checkResult;
125           context.startRendering();
126       }
127       
128       runTest();
129       successfullyParsed = true;
130
131     </script>
132
133     <script src="../fast/js/resources/js-test-post.js"></script>
134   </body>
135 </html>