audiobuffersource-channels.html (2522B)
1 <!DOCTYPE html> 2 <html> 3 <head> 4 <title>audiobuffersource-channels.html</title> 5 <script src="/resources/testharness.js"></script> 6 <script src="/resources/testharnessreport.js"></script> 7 <script src="/webaudio/resources/audit-util.js"></script> 8 </head> 9 <body> 10 <script> 11 test(() => { 12 const context = new AudioContext(); 13 const source = new AudioBufferSourceNode(context); 14 15 // Make sure we can't set to something which isn't an AudioBuffer. 16 assert_throws_js(TypeError, () => { 17 source.buffer = 57; 18 }, 'source.buffer = 57'); 19 20 // It's ok to set the buffer to null. 21 source.buffer = null; 22 23 // Set the buffer to a valid AudioBuffer 24 const buffer = new AudioBuffer({ 25 length: 128, 26 sampleRate: context.sampleRate}); 27 28 source.buffer = buffer; 29 30 // The buffer has been set; we can't set it again. 31 assert_throws_dom('InvalidStateError', () => { 32 source.buffer = new AudioBuffer({ 33 length: 128, 34 sampleRate: context.sampleRate}); 35 }, 'source.buffer = new buffer'); 36 37 // The buffer has been set; it's ok to set it to null. 38 source.buffer = null; 39 40 // The buffer was already set (and set to null). Can't set it again. 41 assert_throws_dom('InvalidStateError', () => { 42 source.buffer = buffer; 43 }, 'source.buffer = buffer again'); 44 45 // But setting to null is ok. 46 source.buffer = null; 47 48 // Check that mono buffer can be set. 49 { 50 const monoBuffer = new AudioBuffer({ 51 numberOfChannels: 1, 52 length: 1024, 53 sampleRate: context.sampleRate}); 54 const testSource = new AudioBufferSourceNode(context); 55 testSource.buffer = monoBuffer; 56 } 57 58 // Check that stereo buffer can be set. 59 { 60 const stereoBuffer = new AudioBuffer({ 61 numberOfChannels: 2, 62 length: 1024, 63 sampleRate: context.sampleRate}); 64 const testSource = new AudioBufferSourceNode(context); 65 testSource.buffer = stereoBuffer; 66 } 67 68 // Check buffers with more than two channels. 69 for (let i = 3; i < 10; ++i) { 70 const buffer = new AudioBuffer({ 71 numberOfChannels: i, 72 length: 1024, 73 sampleRate: context.sampleRate}); 74 const testSource = new AudioBufferSourceNode(context); 75 testSource.buffer = buffer; 76 } 77 }, 'validate .buffer: Validation of AudioBuffer in ' + 78 '.buffer attribute setter'); 79 </script> 80 </body> 81 </html>