5 <script src="resources/compatibility.js"></script>
6 <script src="resources/audio-testing.js"></script>
7 <script src="resources/audiobuffersource-testing.js"></script>
8 <script src="../resources/js-test.js"></script>
13 <div id="description"></div>
14 <div id="console"></div>
17 description("Tests AudioBufferSourceNode start() with a variety of offsets and durations.");
19 // The following test cases assume an AudioBuffer of length 8 whose PCM data is a linear ramp, 0, 1, 2, 3,...
23 { description: "start(when): implicitly play whole buffer from beginning to end",
24 offsetFrame: "none", durationFrames: "none", renderFrames: 16, playbackRate: 1, expected: [0,1,2,3,4,5,6,7,0,0,0,0,0,0,0,0] },
26 { description: "start(when, 0): play whole buffer from beginning to end explicitly giving offset of 0",
27 offsetFrame: 0, durationFrames: "none", renderFrames: 16, playbackRate: 1, expected: [0,1,2,3,4,5,6,7,0,0,0,0,0,0,0,0] },
29 { description: "start(when, 0, 8_frames): play whole buffer from beginning to end explicitly giving offset of 0 and duration of 8 frames",
30 offsetFrame: 0, durationFrames: 8, renderFrames: 16, playbackRate: 1, expected: [0,1,2,3,4,5,6,7,0,0,0,0,0,0,0,0] },
32 { description: "start(when, 4_frames): play with explicit non-zero offset",
33 offsetFrame: 4, durationFrames: "none", renderFrames: 16, playbackRate: 1, expected: [4,5,6,7,0,0,0,0,0,0,0,0,0,0,0,0] },
35 { description: "start(when, 4_frames, 4_frames): play with explicit non-zero offset and duration",
36 offsetFrame: 4, durationFrames: 4, renderFrames: 16, playbackRate: 1, expected: [4,5,6,7,0,0,0,0,0,0,0,0,0,0,0,0] },
38 { description: "start(when, 7_frames): play with explicit non-zero offset near end of buffer",
39 offsetFrame: 7, durationFrames: 1, renderFrames: 16, playbackRate: 1, expected: [7,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0] },
41 { description: "start(when, 8_frames): play with explicit offset at end of buffer",
42 offsetFrame: 8, durationFrames: 0, renderFrames: 16, playbackRate: 1, expected: [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0] },
44 { description: "start(when, 9_frames): play with explicit offset past end of buffer",
45 offsetFrame: 8, durationFrames: 0, renderFrames: 16, playbackRate: 1, expected: [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0] },
47 // Enable test when AudioBufferSourceNode hack is fixed: https://bugs.webkit.org/show_bug.cgi?id=77224
48 // { description: "start(when, 3_frames, 3_frames): play a middle section with explicit offset and duration",
49 // offsetFrame: 3, durationFrames: 3, renderFrames: 16, playbackRate: 1, expected: [4,5,6,7,0,0,0,0,0,0,0,0,0,0,0,0] },
53 var sampleRate = 44100;
55 var bufferFrameLength = 8;
56 var testSpacingFrames = 32;
57 var testSpacingSeconds = testSpacingFrames / sampleRate;
58 var totalRenderLengthFrames = tests.length * testSpacingFrames;
60 function runLoopTest(context, testNumber, test) {
61 var source = context.createBufferSource();
63 source.buffer = buffer;
64 source.playbackRate.value = test.playbackRate;
66 source.connect(context.destination);
68 // Render each test one after the other, spaced apart by testSpacingSeconds.
69 var startTime = testNumber * testSpacingSeconds;
71 if (test.offsetFrame == "none" && test.durationFrames == "none") {
72 source.start(startTime);
73 } else if (test.durationFrames == "none") {
74 var offset = test.offsetFrame / context.sampleRate;
75 source.start(startTime, offset);
77 var offset = test.offsetFrame / context.sampleRate;
78 var duration = test.durationFrames / context.sampleRate;
79 source.start(startTime, offset, duration);
84 if (window.testRunner) {
85 testRunner.dumpAsText();
86 testRunner.waitUntilDone();
89 window.jsTestIsAsync = true;
91 // Create offline audio context.
92 var context = new OfflineAudioContext(1, totalRenderLengthFrames, sampleRate);
93 buffer = createTestBuffer(context, bufferFrameLength);
95 for (var i = 0; i < tests.length; ++i)
96 runLoopTest(context, i, tests[i]);
98 context.oncomplete = checkAllTests;
99 context.startRendering();
103 successfullyParsed = true;