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>
9 <div id="description"></div>
10 <div id="console"></div>
13 description("Test AudioContext activeSourceCount and AudioBufferSourceNode playbackState.");
15 // Create a few sources that start and end playing at various times. After rendering, check that
16 // each source is in the correct state and that the number of active sources is correct.
18 var sampleRate = 44100;
20 // Render for this long.
22 var renderLength = timeToSampleFrame(renderTime, sampleRate);
26 // List of AudioBufferSourceNodes sources.
29 // List of messages that will be printed out on success (or failure). Messages are in the same
30 // order as the sources list above.
31 var sourceMessages = [];
33 // List of the expected playback state for each source. In the same order a sources list above.
34 var sourceExpectedStates = [];
36 // Array mapping the playback state to a string.
37 var playbackStateName = ["UNSCHEDULED_STATE ",
42 function checkResult(event)
46 // For each source, verify that the playback state matches our expected playback state.
47 for (var k = 0; k < sources.length; ++k) {
48 var prefix = playbackStateName[sourceExpectedStates[k]] + sourceMessages[k];
49 if (sources[k].playbackState == sourceExpectedStates[k]) {
52 testFailed(prefix + ": Actual = " + playbackStateName[sources[k].playbackState]);
57 // Figure out how many active sources there should be from the expected states.
58 var playingState = sources[0].PLAYING_STATE
60 var expectedActiveCount = 0;
61 for (k = 0; k < sourceExpectedStates.length; ++k) {
62 if (sourceExpectedStates[k] == playingState) {
63 ++expectedActiveCount;
67 if (context.activeSourceCount == expectedActiveCount) {
68 testPassed(context.activeSourceCount + " are currently playing as expected.");
70 testFailed(context.activeSourceCount + " are currently playing, but expected " + expectedActiveCount + ".");
75 testPassed("activeSourceCount and playbackState tests succeeded.");
77 testFailed("activeSourceCount and playbackState tests did not succeed.");
83 // sourceLength - length of source in seconds
84 // noteFunction - function to turn on source appropriately
85 // expectedState - expected state of the source at the end of rendering
86 // message - message to be displayed if test passes
87 function createTest(sourceLength, noteFunction, expectedState, message)
89 var s = context.createBufferSource();
90 s.buffer = createImpulseBuffer(context, timeToSampleFrame(sourceLength, sampleRate));
91 s.connect(context.destination);
94 sourceMessages.push(message);
95 sourceExpectedStates.push(expectedState);
100 if (window.testRunner) {
101 testRunner.dumpAsText();
102 testRunner.waitUntilDone();
105 window.jsTestIsAsync = true;
107 // Create offline audio context, rendering for renderTime seconds.
108 context = new webkitOfflineAudioContext(2, timeToSampleFrame(renderTime, sampleRate), sampleRate);
110 // This is only used so we can access the playback state constants.
111 var bufferSource = context.createBufferSource();
113 // Dummy message so we know how long we're rendering so we can interpret the pass/fail messages
115 testPassed("Rendering time is " + renderTime + " seconds.");
117 // Test unscheduled state. Create 3 second source, but don't schedule it.
121 bufferSource.UNSCHEDULED_STATE,
122 "Source has been created");
127 function(s) { s.start(renderTime + 1); },
128 bufferSource.SCHEDULED_STATE,
129 "3 second source scheduled to start at time " + (renderTime + 1));
132 function(s) { s.start(1); },
133 bufferSource.PLAYING_STATE,
134 "2 second source starting at time 1");
137 function(s) { s.start(0); },
138 bufferSource.FINISHED_STATE,
139 "1.25 second source starting at time 0");
144 function(s) { s.start(renderTime + 1, 0, 1); },
145 bufferSource.SCHEDULED_STATE,
146 "1 second grain scheduled to start at time " + (renderTime + 1));
149 function(s) { s.start(0.5, 0, 2); },
150 bufferSource.PLAYING_STATE,
151 "2 second grain starting at time 0.5");
154 function(s) { s.start(0.5, 0, 1); },
155 bufferSource.FINISHED_STATE,
156 "1 second grain starting at time 0.5");
158 // Test looping source
161 function(s) { s.loop = true; s.start(renderTime + 1); },
162 bufferSource.SCHEDULED_STATE,
163 "a looping 0.5 second source scheduled at time " + (renderTime + 1));
166 function(s) { s.loop = true; s.start(1.25); },
167 bufferSource.PLAYING_STATE,
168 "a looping 0.5 second source starting at time 1.25");
170 context.oncomplete = checkResult;
171 context.startRendering();
175 successfullyParsed = true;