diff --git a/qb.js b/qb.js index 5b29654..eceac3a 100644 --- a/qb.js +++ b/qb.js @@ -2815,8 +2815,11 @@ var QB = new function() { } }; - this.sub_Sound = async function(freq, duration, shape) { - if (shape == undefined) { shape = "square"; } + + this.sub_Sound = async function(freq, duration, shape, decay, gain) { + if (shape == undefined || (typeof shape != 'string')) { shape = "square"; } + if (decay == undefined || (typeof decay != 'number')) { decay = 0.0; } + if (gain == undefined || (typeof gain != 'number')) { gain = 1.0; } if (!(freq == 0 || (freq >= 32 && freq <= 32767))) { throw new Error("Frequency invalid - valid: 0 (delay), 32 to 32767"); } @@ -2829,12 +2832,16 @@ var QB = new function() { } else { var context = new AudioContext(); var oscillator = context.createOscillator(); + var gainNode = context.createGain(); oscillator.type = shape; oscillator.frequency.value = freq; - oscillator.connect(context.destination); + oscillator.connect(gainNode); + gainNode.connect(context.destination) + gainNode.gain.value = gain; oscillator.start(); - setTimeout(await function () { - oscillator.stop(); + setTimeout(await async function () { + gainNode.gain.setTargetAtTime(0, context.currentTime, decay); + oscillator.stop(duration + decay + 1); }, duration); } };