test_dynamicsCompressorNodeWithGain.html (1587B)
1 <!DOCTYPE HTML> 2 <html> 3 <head> 4 <meta charset="utf-8"> 5 <title>Test DynamicsCompressor with Gain</title> 6 <script src="/tests/SimpleTest/SimpleTest.js"></script> 7 <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css" /> 8 </head> 9 <body> 10 <script class="testbody" type="text/javascript"> 11 SimpleTest.waitForExplicitFinish(); 12 13 addLoadEvent(function() { 14 var samplerate = 44100; 15 var context = new OfflineAudioContext(1, samplerate/100, samplerate); 16 17 var osc = context.createOscillator(); 18 osc.frequency.value = 2400; 19 20 var gain = context.createGain(); 21 gain.gain.value = 1.5; 22 23 // These numbers are borrowed from the example code on MDN 24 // https://developer.mozilla.org/en-US/docs/Web/API/DynamicsCompressorNode 25 var compressor = context.createDynamicsCompressor(); 26 compressor.threshold.value = -50; 27 compressor.knee.value = 40; 28 compressor.ratio.value = 12; 29 compressor.reduction.value = -20; 30 compressor.attack.value = 0; 31 compressor.release.value = 0.25; 32 33 osc.connect(gain); 34 gain.connect(compressor); 35 compressor.connect(context.destination); 36 osc.start(); 37 38 context.startRendering().then(buffer => { 39 var peak = Math.max(...buffer.getChannelData(0)); 40 console.log(peak); 41 // These values are experimentally determined. Without dynamics compression 42 // the peak should be just under 1.5. We also check for a minimum value 43 // to make sure we are not getting all zeros. 44 ok(peak >= 0.2 && peak < 1.0, "Peak value should be greater than 0.25 and less than 1.0"); 45 SimpleTest.finish(); 46 }); 47 }); 48 </script> 49 <pre> 50 </pre> 51 </body>