4 <title>Test WebAudio Output Clipping
</title>
6 var context
= new AudioContext();
7 var gainNode
= context
.createGain();
8 gainNode
.connect(context
.destination
);
9 gainNode
.gain
.value
= 100;
14 function playNormal() {
15 // Play a square wave of amplitude 1 (roughly)
16 var oscillator
= context
.createOscillator();
17 oscillator
.frequency
.value
= frequency
;
18 oscillator
.type
= 'square';
19 oscillator
.connect(context
.destination
);
21 oscillator
.stop(context
.currentTime
+ duration
);
25 // Play a large amplitude sine wave.
26 var oscillator
= context
.createOscillator();
27 oscillator
.frequency
.value
= frequency
;
28 oscillator
.type
= 'sine';
29 oscillator
.connect(gainNode
);
31 oscillator
.stop(context
.currentTime
+ duration
);
35 // Set the text correctly with the actual sine amplitude.
36 document
.getElementById("amplitude").textContent
= gainNode
.gain
.value
;
39 window
.onload
= setText
;
44 This tests that output audio is clamped to
0db maximum. Press each button below in turn. The
45 apparent volume for both should be about equal, even though the second button plays with much
46 higher gain than the first. The resulting waveforms are not equal, however, as the second
47 button audio is clipped to a square wave shape.
50 The first button plays a square wave with nominal amplitude of
1. That actual amplitude is
51 slightly less than this because the square wave is band-limited which causes ringing, so the
52 amplitude is reduced slightly to prevent overflows due to the ringing.
55 The second button plays a very loud sine wave of amplitude
<span id=
"amplitude"></span>. This
56 is clipped to unit amplitude so it is approximately equal to a square wave. There will be
57 some buzzing due to the clipping.
60 <strong>WARNING
</strong>: Full amplitude signals may be loud.
62 <button onclick=
"playNormal()" title=
"Normal square wave with nominal amplitude of 1">
65 <button onclick=
"playLoud()" title=
"Loud sine wave that will be clipped to a square wave">
66 Loud sine wave (clipped)