audiosource-onended.html (3948B)
1 <!DOCTYPE html> 2 <html> 3 <head> 4 <title> 5 Test Onended Event Listener for AudioBufferSourceNode and OscillatorNode 6 </title> 7 <script src="/resources/testharness.js"></script> 8 <script src="/resources/testharnessreport.js"></script> 9 <script src="/webaudio/resources/audit-util.js"></script> 10 </head> 11 <body> 12 <script> 13 const sampleRate = 44100; 14 const renderLengthSeconds = 1; 15 const renderLengthFrames = renderLengthSeconds * sampleRate; 16 17 // Length of the source buffer. Anything less than the render length is 18 // fine. 19 const sourceBufferLengthFrames = renderLengthFrames / 8; 20 // When to stop the oscillator. Anything less than the render time is 21 // fine. 22 const stopTime = renderLengthSeconds / 8; 23 24 promise_test(async (t) => { 25 // Test that the onended event for an AudioBufferSourceNode is fired 26 // when it is set directly. 27 const context = new OfflineAudioContext( 28 1, renderLengthFrames, sampleRate); 29 const buffer = new AudioBuffer({length: sourceBufferLengthFrames, 30 numberOfChannels: 1, sampleRate: context.sampleRate}); 31 const source = new AudioBufferSourceNode(context, {buffer: buffer}); 32 source.connect(context.destination); 33 source.onended = (e) => { 34 assert_true( 35 true, 'AudioBufferSource.onended called when ended set directly'); 36 }; 37 source.start(); 38 await context.startRendering(); 39 }, 'absn-set-onended: onended event fires for AudioBufferSourceNode ' + 40 'when set directly'); 41 42 promise_test(async (t) => { 43 // Test that the onended event for an AudioBufferSourceNode is fired 44 // when addEventListener is used to set the handler. 45 const context = new OfflineAudioContext( 46 1, renderLengthFrames, sampleRate); 47 const buffer = new AudioBuffer({length: sourceBufferLengthFrames, 48 numberOfChannels: 1, sampleRate: context.sampleRate}); 49 const source = new AudioBufferSourceNode(context, {buffer: buffer}); 50 source.connect(context.destination); 51 source.addEventListener('ended', (e) => { 52 assert_true( 53 true, 54 'AudioBufferSource.onended called when using addEventListener'); 55 }); 56 source.start(); 57 await context.startRendering(); 58 }, 'absn-add-listener: onended event fires for AudioBufferSourceNode ' + 59 'when using addEventListener'); 60 61 promise_test(async (t) => { 62 // Test that the onended event for an OscillatorNode is fired when it is 63 // set directly. 64 const context = new OfflineAudioContext( 65 1, renderLengthFrames, sampleRate); 66 const source = new OscillatorNode(context); 67 source.connect(context.destination); 68 source.onended = (e) => { 69 assert_true( 70 true, 'Oscillator.onended called when ended set directly'); 71 }; 72 source.start(); 73 source.stop(stopTime); 74 await context.startRendering(); 75 }, 'osc-set-onended: onended event fires for OscillatorNode ' + 76 'when set directly'); 77 78 promise_test(async (t) => { 79 // Test that the onended event for an OscillatorNode is fired when 80 // addEventListener is used to set the handler. 81 const context = new OfflineAudioContext( 82 1, renderLengthFrames, sampleRate); 83 const source = new OscillatorNode(context); 84 source.connect(context.destination); 85 source.addEventListener('ended', (e) => { 86 assert_true( 87 true, 'Oscillator.onended called when using addEventListener'); 88 }); 89 source.start(); 90 source.stop(stopTime); 91 await context.startRendering(); 92 }, 'osc-add-listener: onended event fires for OscillatorNode ' + 93 'when using addEventListener'); 94 </script> 95 </body> 96 </html>