test_audioContextParams_sampleRate.html (2920B)
1 <!DOCTYPE HTML> 2 <html> 3 <head> 4 <script type="application/javascript" src="mediaStreamPlayback.js"></script> 5 </head> 6 <body> 7 <pre id="test"> 8 9 <script> 10 /* import-globals-from ../../webrtc/tests/mochitests/mediaStreamPlayback.js */ 11 createHTML({ 12 title: "Parallel MTG by setting AudioContextParam sample rate", 13 bug: "1387454", 14 visible: true 15 }); 16 17 runTest(async () => { 18 // Test an AudioContext of specific sample rate. 19 // Verify that the oscillator produces a tone. 20 const rate1 = 500; 21 const ac1 = new AudioContext({sampleRate: 44100}); 22 const dest_ac1 = ac1.createMediaStreamDestination(); 23 const osc_ac1 = ac1.createOscillator(); 24 osc_ac1.frequency.value = rate1; 25 osc_ac1.connect(dest_ac1); 26 osc_ac1.start(0); 27 28 const analyser = new AudioStreamAnalyser(ac1, dest_ac1.stream); 29 analyser.enableDebugCanvas(); 30 await analyser.waitForAnalysisSuccess( array => { 31 const freg_50Hz = array[analyser.binIndexForFrequency(50)]; 32 const freq_rate1 = array[analyser.binIndexForFrequency(rate1)]; 33 const freq_4000Hz = array[analyser.binIndexForFrequency(4000)]; 34 35 info("Analysing audio frequency - low:target1:high = " 36 + freg_50Hz + ':' + freq_rate1 + ':' + freq_4000Hz); 37 return freg_50Hz < 50 && freq_rate1 > 200 && freq_4000Hz < 50; 38 }) 39 osc_ac1.stop(); 40 41 // Same test using a new AudioContext of different sample rate. 42 const rate2 = 1500; 43 const ac2 = new AudioContext({sampleRate: 48000}); 44 const dest_ac2 = ac2.createMediaStreamDestination(); 45 const osc_ac2 = ac2.createOscillator(); 46 osc_ac2.frequency.value = rate2; 47 osc_ac2.connect(dest_ac2); 48 osc_ac2.start(0); 49 50 const analyser2 = new AudioStreamAnalyser(ac2, dest_ac2.stream); 51 analyser2.enableDebugCanvas(); 52 await analyser2.waitForAnalysisSuccess( array => { 53 const freg_50Hz = array[analyser2.binIndexForFrequency(50)]; 54 const freq_rate2 = array[analyser2.binIndexForFrequency(rate2)]; 55 const freq_4000Hz = array[analyser2.binIndexForFrequency(4000)]; 56 57 info("Analysing audio frequency - low:target2:high = " 58 + freg_50Hz + ':' + freq_rate2 + ':' + freq_4000Hz); 59 return freg_50Hz < 50 && freq_rate2 > 200 && freq_4000Hz < 50; 60 }) 61 osc_ac2.stop(); 62 63 // Two AudioContexts with different sample rate can communicate. 64 ac2.createMediaStreamSource(dest_ac1.stream); 65 ok(true, "Connect nodes with the different sample rate is ok"); 66 67 // Two AudioContext with the same sample rate can communicate. 68 const ac3 = new AudioContext({sampleRate: 48000}); 69 const dest_ac3 = ac3.createMediaStreamDestination(); 70 ac2.createMediaStreamSource(dest_ac3.stream); 71 ok(true, "Connect nodes with the same sample rate is ok"); 72 73 mustThrowWith("Invalid zero samplerate", "NotSupportedError", 74 () => new AudioContext({sampleRate: 0})); 75 76 mustThrowWith("Invalid negative samplerate", "NotSupportedError", 77 () => new AudioContext({sampleRate: -1})); 78 }); 79 </script> 80 </pre> 81 </body> 82 </html>