1 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
4 <script src="resources/audio-testing.js"></script>
5 <script src="../resources/js-test.js"></script>
6 <script src="resources/compatibility.js"></script>
10 <div id="description"></div>
11 <div id="console"></div>
14 description("Test AudioContext activeSourceCount and AudioBufferSourceNode playbackState.");
16 // Create a few sources that start and end playing at various times. After rendering, check that
17 // each source is in the correct state and that the number of active sources is correct.
19 var sampleRate = 44100;
21 // Render for this long.
23 var renderLength = timeToSampleFrame(renderTime, sampleRate);
27 // List of AudioBufferSourceNodes sources.
30 // List of messages that will be printed out on success (or failure). Messages are in the same
31 // order as the sources list above.
32 var sourceMessages = [];
34 // List of the expected playback state for each source. In the same order a sources list above.
35 var sourceExpectedStates = [];
37 // Array mapping the playback state to a string.
38 var playbackStateName = ["UNSCHEDULED_STATE ",
43 function checkResult(event)
47 // For each source, verify that the playback state matches our expected playback state.
48 for (var k = 0; k < sources.length; ++k) {
49 var prefix = playbackStateName[sourceExpectedStates[k]] + sourceMessages[k];
50 if (sources[k].playbackState == sourceExpectedStates[k]) {
53 testFailed(prefix + ": Actual = " + playbackStateName[sources[k].playbackState]);
58 // Figure out how many active sources there should be from the expected states.
59 var playingState = sources[0].PLAYING_STATE
61 var expectedActiveCount = 0;
62 for (k = 0; k < sourceExpectedStates.length; ++k) {
63 if (sourceExpectedStates[k] == playingState) {
64 ++expectedActiveCount;
68 if (context.activeSourceCount == expectedActiveCount) {
69 testPassed(context.activeSourceCount + " are currently playing as expected.");
71 testFailed(context.activeSourceCount + " are currently playing, but expected " + expectedActiveCount + ".");
76 testPassed("activeSourceCount and playbackState tests succeeded.");
78 testFailed("activeSourceCount and playbackState tests did not succeed.");
84 // sourceLength - length of source in seconds
85 // noteFunction - function to turn on source appropriately
86 // expectedState - expected state of the source at the end of rendering
87 // message - message to be displayed if test passes
88 function createTest(sourceLength, noteFunction, expectedState, message)
90 var s = context.createBufferSource();
91 s.buffer = createImpulseBuffer(context, timeToSampleFrame(sourceLength, sampleRate));
92 s.connect(context.destination);
95 sourceMessages.push(message);
96 sourceExpectedStates.push(expectedState);
101 if (window.testRunner) {
102 testRunner.dumpAsText();
103 testRunner.waitUntilDone();
106 window.jsTestIsAsync = true;
108 // Create offline audio context, rendering for renderTime seconds.
109 context = new OfflineAudioContext(2, timeToSampleFrame(renderTime, sampleRate), sampleRate);
111 // This is only used so we can access the playback state constants.
112 var bufferSource = context.createBufferSource();
114 // Dummy message so we know how long we're rendering so we can interpret the pass/fail messages
116 testPassed("Rendering time is " + renderTime + " seconds.");
118 // Test unscheduled state. Create 3 second source, but don't schedule it.
122 bufferSource.UNSCHEDULED_STATE,
123 "Source has been created");
128 function(s) { s.start(renderTime + 1); },
129 bufferSource.SCHEDULED_STATE,
130 "3 second source scheduled to start at time " + (renderTime + 1));
133 function(s) { s.start(1); },
134 bufferSource.PLAYING_STATE,
135 "2 second source starting at time 1");
138 function(s) { s.start(0); },
139 bufferSource.FINISHED_STATE,
140 "1.25 second source starting at time 0");
145 function(s) { s.start(renderTime + 1, 0, 1); },
146 bufferSource.SCHEDULED_STATE,
147 "1 second grain scheduled to start at time " + (renderTime + 1));
150 function(s) { s.start(0.5, 0, 2); },
151 bufferSource.PLAYING_STATE,
152 "2 second grain starting at time 0.5");
155 function(s) { s.start(0.5, 0, 1); },
156 bufferSource.FINISHED_STATE,
157 "1 second grain starting at time 0.5");
159 // Test looping source
162 function(s) { s.loop = true; s.start(renderTime + 1); },
163 bufferSource.SCHEDULED_STATE,
164 "a looping 0.5 second source scheduled at time " + (renderTime + 1));
167 function(s) { s.loop = true; s.start(1.25); },
168 bufferSource.PLAYING_STATE,
169 "a looping 0.5 second source starting at time 1.25");
171 context.oncomplete = checkResult;
172 context.startRendering();
176 successfullyParsed = true;