1 <!DOCTYPE HTML PUBLIC
"-//W3C//DTD HTML 4.01 Transitional//EN">
4 <script src=
"resources/compatibility.js"></script>
5 <script src=
"resources/audio-testing.js"></script>
6 <script src=
"resources/note-grain-on-testing.js"></script>
7 <script src=
"../resources/js-test.js"></script>
11 <div id=
"description"></div>
12 <div id=
"console"></div>
15 description("Test noteGrainOn offset rendering.");
17 // To test noteGrainOn, a single ramp signal is created.
18 // Various sections of the ramp are rendered by noteGrainOn() at
19 // different times, and we verify that the actual output
20 // consists of the correct section of the ramp at the correct
25 // Array of the grain offset used for each ramp played.
26 var grainOffsetTime
= [];
28 // Verify the received signal is a ramp from the correct section
29 // of our ramp signal.
30 function verifyGrain(renderedData
, startFrame
, endFrame
, grainIndex
) {
31 var grainOffsetFrame
= timeToSampleFrame(grainOffsetTime
[grainIndex
], sampleRate
);
32 var grainFrameLength
= endFrame
- startFrame
;
33 var ramp
= linearRampBuffer
.getChannelData(0);
40 for (var k
= 0; k
< grainFrameLength
; ++k
) {
41 if (renderedData
[startFrame
+ k
] != ramp
[grainOffsetFrame
+ k
]) {
42 expected
= ramp
[grainOffsetFrame
+ k
];
43 actual
= renderedData
[startFrame
+ k
];
44 frame
= startFrame
+ k
;
49 return { verified
: isCorrect
,
55 function checkResult(event
) {
56 var buffer
= event
.renderedBuffer
;
57 renderedData
= buffer
.getChannelData(0);
58 var nSamples
= renderedData
.length
;
62 // Number of grains that we found that have incorrect data.
63 var invalidGrainDataCount
= 0;
65 var startEndFrames
= findStartAndEndSamples(renderedData
);
67 // Verify the start and stop times. Not strictly needed for
68 // this test, but it's useful to know that if the ramp data
69 // appears to be incorrect.
70 success
= success
&& verifyStartAndEndFrames(startEndFrames
);
72 // Loop through each of the rendered grains and check that
73 // each grain contains our expected ramp.
74 for (var k
= 0; k
< startEndFrames
.start
.length
; ++k
) {
75 // Verify that the rendered data matches the expected
76 // section of our ramp signal.
77 var result
= verifyGrain(renderedData
, startEndFrames
.start
[k
], startEndFrames
.end
[k
], k
);
79 if (!result
.verified
) {
80 testFailed("Grain " + k
+ " incorrect. Expected " + result
.expected
+ " but received " + result
.actual
+ " at sample frame " + result
.frame
);
81 ++invalidGrainDataCount
;
86 if (!invalidGrainDataCount
) {
87 testPassed("All " + numberOfTests
+ " grains contained the correct data.");
89 testFailed(invalidGrainDataCount
+ " grains out of " + numberOfTests
+ " did not contain the expected data.");
94 testPassed("noteGrainOn offset rendering tests passed.");
96 testFailed("noteGrainOn offset rendering tests failed.");
103 if (window
.testRunner
) {
104 testRunner
.dumpAsText();
105 testRunner
.waitUntilDone();
108 window
.jsTestIsAsync
= true;
110 // Create offline audio context.
111 context
= new OfflineAudioContext(2, sampleRate
* renderTime
, sampleRate
);
113 // Create a linear ramp for testing noteGrainOn.
114 linearRampBuffer
= createSignalBuffer(context
,
116 // Want the ramp to start
121 var grainInfo
= playAllGrains(context
, linearRampBuffer
, numberOfTests
);
123 grainOffsetTime
= grainInfo
.grainOffsetTimes
;
125 context
.oncomplete
= checkResult
;
126 context
.startRendering();
130 successfullyParsed
= true;