| <!DOCTYPE html> |
| |
| <html> |
| <head> |
| <script src="resources/audio-testing.js"></script> |
| <script src="resources/audiobuffersource-testing.js"></script> |
| <script src="../resources/js-test.js"></script> |
| </head> |
| |
| <body> |
| |
| <div id="description"></div> |
| <div id="console"></div> |
| |
| <script> |
| description("Tests AudioBufferSourceNode looping with a variety of loop points."); |
| |
| // The following test cases assume an AudioBuffer of length 8 whose PCM data is a linear ramp, 0, 1, 2, 3,... |
| |
| var tests = [ |
| |
| { description: "loop whole buffer by default with loopStart == loopEnd == 0", |
| loopStartFrame: 0, loopEndFrame: 0, renderFrames: 16, playbackRate: 1, expected: [0,1,2,3,4,5,6,7,0,1,2,3,4,5,6,7] }, |
| |
| { description: "loop whole buffer explicitly", |
| loopStartFrame: 0, loopEndFrame: 8, renderFrames: 16, playbackRate: 1, expected: [0,1,2,3,4,5,6,7,0,1,2,3,4,5,6,7] }, |
| |
| { description: "loop from middle to end of buffer", |
| loopStartFrame: 4, loopEndFrame: 8, renderFrames: 16, playbackRate: 1, expected: [0,1,2,3,4,5,6,7,4,5,6,7,4,5,6,7] }, |
| |
| { description: "loop from start to middle of buffer", |
| loopStartFrame: 0, loopEndFrame: 4, renderFrames: 16, playbackRate: 1, expected: [0,1,2,3,0,1,2,3,0,1,2,3,0,1,2,3] }, |
| |
| { description: "loop internally from 4 -> 6", |
| loopStartFrame: 4, loopEndFrame: 6, renderFrames: 16, playbackRate: 1, expected: [0,1,2,3,4,5,4,5,4,5,4,5,4,5,4,5] }, |
| |
| { description: "loop internally from 3 -> 7", |
| loopStartFrame: 3, loopEndFrame: 7, renderFrames: 16, playbackRate: 1, expected: [0,1,2,3,4,5,6,3,4,5,6,3,4,5,6,3] }, |
| |
| { description: "loop internally from 4 -> 6 with playbackRate of 0.5", |
| loopStartFrame: 4, loopEndFrame: 6, renderFrames: 16, playbackRate: 0.5, expected: [0, 0.5, 1, 1.5, 2, 2.5, 3, 3.5, 4, 4.5, 5, 5.5, 4, 4.5, 5, 5.5] }, |
| |
| { description: "loop internally from 4 -> 6 with playbackRate of 1.5", |
| loopStartFrame: 4, loopEndFrame: 6, renderFrames: 16, playbackRate: 1.5, expected: [0, 1.5, 3, 4.5, 4, 5.5, 5, 4.5, 4, 5.5, 5, 4.5, 4, 5.5, 5, 4.5] }, |
| |
| { description: "illegal playbackRate of 47 greater than loop length", |
| loopStartFrame: 4, loopEndFrame: 6, renderFrames: 16, playbackRate: 47, expected: [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0] }, |
| |
| // Try illegal loop-points - they should be ignored and we'll loop the whole buffer. |
| |
| { description: "illegal loop: loopStartFrame > loopEndFrame", |
| loopStartFrame: 7, loopEndFrame: 3, renderFrames: 16, playbackRate: 1, expected: [0,1,2,3,4,5,6,7,0,1,2,3,4,5,6,7] }, |
| |
| { description: "illegal loop: loopStartFrame == loopEndFrame", |
| loopStartFrame: 3, loopEndFrame: 3, renderFrames: 16, playbackRate: 1, expected: [0,1,2,3,4,5,6,7,0,1,2,3,4,5,6,7] }, |
| |
| { description: "illegal loop: loopStartFrame < 0", |
| loopStartFrame: -45, loopEndFrame: 3, renderFrames: 16, playbackRate: 1, expected: [0,1,2,3,4,5,6,7,0,1,2,3,4,5,6,7] }, |
| |
| { description: "illegal loop: loopEndFrame > bufferLength", |
| loopStartFrame: 0, loopEndFrame: 30000, renderFrames: 16, playbackRate: 1, expected: [0,1,2,3,4,5,6,7,0,1,2,3,4,5,6,7] }, |
| |
| ]; |
| |
| var sampleRate = 44100; |
| var buffer; |
| var bufferFrameLength = 8; |
| var testSpacingFrames = 32; |
| var testSpacingSeconds = testSpacingFrames / sampleRate; |
| var totalRenderLengthFrames = tests.length * testSpacingFrames; |
| |
| function runLoopTest(context, testNumber, test) { |
| var source = context.createBufferSource(); |
| |
| source.buffer = buffer; |
| source.playbackRate.value = test.playbackRate; |
| source.loop = true; |
| source.loopStart = test.loopStartFrame / context.sampleRate; |
| source.loopEnd = test.loopEndFrame / context.sampleRate; |
| |
| source.connect(context.destination); |
| |
| // Render each test one after the other, spaced apart by testSpacingSeconds. |
| var startTime = testNumber * testSpacingSeconds; |
| var duration = test.renderFrames / context.sampleRate; |
| source.start(startTime); |
| source.stop(startTime + duration); |
| } |
| |
| function runTest() { |
| window.jsTestIsAsync = true; |
| |
| // Create offline audio context. |
| var context = new webkitOfflineAudioContext(1, totalRenderLengthFrames, sampleRate); |
| buffer = createTestBuffer(context, bufferFrameLength); |
| |
| for (var i = 0; i < tests.length; ++i) |
| runLoopTest(context, i, tests[i]); |
| |
| context.oncomplete = checkAllTests; |
| context.startRendering(); |
| } |
| |
| runTest(); |
| |
| </script> |
| </body> |
| </html> |