| <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> |
| <html> |
| <head> |
| <script src="resources/audio-testing.js"></script> |
| <script src="../resources/js-test.js"></script> |
| </head> |
| |
| <body> |
| <div id="description"></div> |
| <div id="console"></div> |
| |
| <script> |
| description("Test AudioContext activeSourceCount and AudioBufferSourceNode playbackState."); |
| |
| // Create a few sources that start and end playing at various times. After rendering, check that |
| // each source is in the correct state and that the number of active sources is correct. |
| |
| var sampleRate = 44100; |
| |
| // Render for this long. |
| var renderTime = 2; |
| var renderLength = timeToSampleFrame(renderTime, sampleRate); |
| |
| var context; |
| |
| // List of AudioBufferSourceNodes sources. |
| var sources = []; |
| |
| // List of messages that will be printed out on success (or failure). Messages are in the same |
| // order as the sources list above. |
| var sourceMessages = []; |
| |
| // List of the expected playback state for each source. In the same order a sources list above. |
| var sourceExpectedStates = []; |
| |
| // Array mapping the playback state to a string. |
| var playbackStateName = ["UNSCHEDULED_STATE ", |
| "SCHEDULED_STATE ", |
| "PLAYING_STATE ", |
| "FINISHED_STATE "]; |
| |
| function checkResult(event) |
| { |
| var success = true; |
| |
| // For each source, verify that the playback state matches our expected playback state. |
| for (var k = 0; k < sources.length; ++k) { |
| var prefix = playbackStateName[sourceExpectedStates[k]] + sourceMessages[k]; |
| if (sources[k].playbackState == sourceExpectedStates[k]) { |
| testPassed(prefix); |
| } else { |
| testFailed(prefix + ": Actual = " + playbackStateName[sources[k].playbackState]); |
| success = false; |
| } |
| } |
| |
| // Figure out how many active sources there should be from the expected states. |
| var playingState = sources[0].PLAYING_STATE |
| |
| var expectedActiveCount = 0; |
| for (k = 0; k < sourceExpectedStates.length; ++k) { |
| if (sourceExpectedStates[k] == playingState) { |
| ++expectedActiveCount; |
| } |
| } |
| |
| if (context.activeSourceCount == expectedActiveCount) { |
| testPassed(context.activeSourceCount + " are currently playing as expected."); |
| } else { |
| testFailed(context.activeSourceCount + " are currently playing, but expected " + expectedActiveCount + "."); |
| success = false; |
| } |
| |
| if (success) { |
| testPassed("activeSourceCount and playbackState tests succeeded."); |
| } else { |
| testFailed("activeSourceCount and playbackState tests did not succeed."); |
| } |
| |
| finishJSTest(); |
| } |
| |
| // sourceLength - length of source in seconds |
| // noteFunction - function to turn on source appropriately |
| // expectedState - expected state of the source at the end of rendering |
| // message - message to be displayed if test passes |
| function createTest(sourceLength, noteFunction, expectedState, message) |
| { |
| var s = context.createBufferSource(); |
| s.buffer = createImpulseBuffer(context, timeToSampleFrame(sourceLength, sampleRate)); |
| s.connect(context.destination); |
| noteFunction(s); |
| sources.push(s); |
| sourceMessages.push(message); |
| sourceExpectedStates.push(expectedState); |
| } |
| |
| function runTest() |
| { |
| window.jsTestIsAsync = true; |
| |
| // Create offline audio context, rendering for renderTime seconds. |
| context = new webkitOfflineAudioContext(2, timeToSampleFrame(renderTime, sampleRate), sampleRate); |
| |
| // This is only used so we can access the playback state constants. |
| var bufferSource = context.createBufferSource(); |
| |
| // Dummy message so we know how long we're rendering so we can interpret the pass/fail messages |
| // correctly. |
| testPassed("Rendering time is " + renderTime + " seconds."); |
| |
| // Test unscheduled state. Create 3 second source, but don't schedule it. |
| |
| createTest(3, |
| function(s) { }, |
| bufferSource.UNSCHEDULED_STATE, |
| "Source has been created"); |
| |
| // Test start. |
| |
| createTest(3, |
| function(s) { s.start(renderTime + 1); }, |
| bufferSource.SCHEDULED_STATE, |
| "3 second source scheduled to start at time " + (renderTime + 1)); |
| |
| createTest(2, |
| function(s) { s.start(1); }, |
| bufferSource.PLAYING_STATE, |
| "2 second source starting at time 1"); |
| |
| createTest(1.25, |
| function(s) { s.start(0); }, |
| bufferSource.FINISHED_STATE, |
| "1.25 second source starting at time 0"); |
| |
| // Test looping source |
| |
| createTest(0.5, |
| function(s) { s.loop = true; s.start(renderTime + 1); }, |
| bufferSource.SCHEDULED_STATE, |
| "a looping 0.5 second source scheduled at time " + (renderTime + 1)); |
| |
| createTest(0.5, |
| function(s) { s.loop = true; s.start(1.25); }, |
| bufferSource.PLAYING_STATE, |
| "a looping 0.5 second source starting at time 1.25"); |
| |
| context.oncomplete = checkResult; |
| context.startRendering(); |
| } |
| |
| runTest(); |
| |
| </script> |
| </body> |
| </html> |